Skip to content

Commit fc34a1c

Browse files
committed
Replace http.Server by a generic ListenAndServer
Signed-off-by: Alvaro Saurin <[email protected]>
1 parent 781b732 commit fc34a1c

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

server/sse.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ func (s *sseSession) Initialized() bool {
5151

5252
var _ ClientSession = (*sseSession)(nil)
5353

54+
// Server is a type that implements ListenAndServe and Shutdown
55+
type Server interface {
56+
ListenAndServe() error
57+
Shutdown(ctx context.Context) error
58+
}
59+
5460
// SSEServer implements a Server-Sent Events (SSE) based MCP server.
5561
// It provides real-time communication capabilities over HTTP using the SSE protocol.
5662
type SSEServer struct {
@@ -61,7 +67,7 @@ type SSEServer struct {
6167
messageEndpoint string
6268
sseEndpoint string
6369
sessions sync.Map
64-
srv *http.Server
70+
srv Server
6571
contextFunc SSEContextFunc
6672

6773
keepAlive bool
@@ -131,7 +137,7 @@ func WithSSEEndpoint(endpoint string) SSEOption {
131137
}
132138

133139
// WithHTTPServer sets the HTTP server instance
134-
func WithHTTPServer(srv *http.Server) SSEOption {
140+
func WithHTTPServer(srv Server) SSEOption {
135141
return func(s *SSEServer) {
136142
s.srv = srv
137143
}
@@ -165,6 +171,7 @@ func NewSSEServer(server *MCPServer, opts ...SSEOption) *SSEServer {
165171
sseEndpoint: "/sse",
166172
messageEndpoint: "/message",
167173
useFullURLForMessageEndpoint: true,
174+
srv: nil, // will be set by Start
168175
keepAlive: false,
169176
keepAliveInterval: 10 * time.Second,
170177
}
@@ -190,9 +197,11 @@ func NewTestServer(server *MCPServer, opts ...SSEOption) *httptest.Server {
190197
// It sets up HTTP handlers for SSE and message endpoints.
191198
func (s *SSEServer) Start(addr string) error {
192199
s.mu.Lock()
193-
s.srv = &http.Server{
194-
Addr: addr,
195-
Handler: s,
200+
if s.srv == nil {
201+
s.srv = &http.Server{
202+
Addr: addr,
203+
Handler: s,
204+
}
196205
}
197206
s.mu.Unlock()
198207

0 commit comments

Comments
 (0)