@@ -51,6 +51,12 @@ func (s *sseSession) Initialized() bool {
51
51
52
52
var _ ClientSession = (* sseSession )(nil )
53
53
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
+
54
60
// SSEServer implements a Server-Sent Events (SSE) based MCP server.
55
61
// It provides real-time communication capabilities over HTTP using the SSE protocol.
56
62
type SSEServer struct {
@@ -61,7 +67,7 @@ type SSEServer struct {
61
67
messageEndpoint string
62
68
sseEndpoint string
63
69
sessions sync.Map
64
- srv * http. Server
70
+ srv Server
65
71
contextFunc SSEContextFunc
66
72
67
73
keepAlive bool
@@ -131,7 +137,7 @@ func WithSSEEndpoint(endpoint string) SSEOption {
131
137
}
132
138
133
139
// WithHTTPServer sets the HTTP server instance
134
- func WithHTTPServer (srv * http. Server ) SSEOption {
140
+ func WithHTTPServer (srv Server ) SSEOption {
135
141
return func (s * SSEServer ) {
136
142
s .srv = srv
137
143
}
@@ -165,6 +171,7 @@ func NewSSEServer(server *MCPServer, opts ...SSEOption) *SSEServer {
165
171
sseEndpoint : "/sse" ,
166
172
messageEndpoint : "/message" ,
167
173
useFullURLForMessageEndpoint : true ,
174
+ srv : nil , // will be set by Start
168
175
keepAlive : false ,
169
176
keepAliveInterval : 10 * time .Second ,
170
177
}
@@ -190,9 +197,11 @@ func NewTestServer(server *MCPServer, opts ...SSEOption) *httptest.Server {
190
197
// It sets up HTTP handlers for SSE and message endpoints.
191
198
func (s * SSEServer ) Start (addr string ) error {
192
199
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
+ }
196
205
}
197
206
s .mu .Unlock ()
198
207
0 commit comments