@@ -53,15 +53,15 @@ var _ ClientSession = (*sseSession)(nil)
53
53
// SSEServer implements a Server-Sent Events (SSE) based MCP server.
54
54
// It provides real-time communication capabilities over HTTP using the SSE protocol.
55
55
type SSEServer struct {
56
- server * MCPServer
57
- baseURL string
58
- basePath string
59
- useFullURLForMessageEndpoint bool
60
- messageEndpoint string
61
- sseEndpoint string
62
- sessions sync.Map
63
- srv * http.Server
64
- contextFunc SSEContextFunc
56
+ server * MCPServer
57
+ baseURL string
58
+ basePath string
59
+ useFullURLForMessageEndpoint bool
60
+ messageEndpoint string
61
+ sseEndpoint string
62
+ sessions sync.Map
63
+ srv * http.Server
64
+ contextFunc SSEContextFunc
65
65
66
66
keepAlive bool
67
67
keepAliveInterval time.Duration
@@ -158,12 +158,12 @@ func WithSSEContextFunc(fn SSEContextFunc) SSEOption {
158
158
// NewSSEServer creates a new SSE server instance with the given MCP server and options.
159
159
func NewSSEServer (server * MCPServer , opts ... SSEOption ) * SSEServer {
160
160
s := & SSEServer {
161
- server : server ,
162
- sseEndpoint : "/sse" ,
163
- messageEndpoint : "/message" ,
164
- useFullURLForMessageEndpoint : true ,
165
- keepAlive : false ,
166
- keepAliveInterval : 10 * time .Second ,
161
+ server : server ,
162
+ sseEndpoint : "/sse" ,
163
+ messageEndpoint : "/message" ,
164
+ useFullURLForMessageEndpoint : true ,
165
+ keepAlive : false ,
166
+ keepAliveInterval : 10 * time .Second ,
167
167
}
168
168
169
169
// Apply all options
@@ -293,7 +293,6 @@ func (s *SSEServer) handleSSE(w http.ResponseWriter, r *http.Request) {
293
293
}()
294
294
}
295
295
296
-
297
296
// Send the initial endpoint event
298
297
fmt .Fprintf (w , "event: endpoint\n data: %s\r \n \r \n " , s .GetMessageEndpointForClient (sessionID ))
299
298
flusher .Flush ()
@@ -356,31 +355,28 @@ func (s *SSEServer) handleMessage(w http.ResponseWriter, r *http.Request) {
356
355
return
357
356
}
358
357
359
- // Process message through MCPServer
360
- response := s . server . HandleMessage ( ctx , rawMessage )
358
+ // quick return request, send 202 Accepted with no body, then deal the message and sent response via SSE
359
+ w . WriteHeader ( http . StatusAccepted )
361
360
362
- // Only send response if there is one (not for notifications)
363
- if response != nil {
364
- eventData , _ := json . Marshal ( response )
361
+ go func () {
362
+ // Process message through MCPServer
363
+ response := s . server . HandleMessage ( ctx , rawMessage )
365
364
366
- // Queue the event for sending via SSE
367
- select {
368
- case session .eventQueue <- fmt .Sprintf ("event: message\n data: %s\n \n " , eventData ):
369
- // Event queued successfully
370
- case <- session .done :
371
- // Session is closed, don't try to queue
372
- default :
373
- // Queue is full, could log this
374
- }
365
+ // Only send response if there is one (not for notifications)
366
+ if response != nil {
367
+ eventData , _ := json .Marshal (response )
375
368
376
- // Send HTTP response
377
- w .Header ().Set ("Content-Type" , "application/json" )
378
- w .WriteHeader (http .StatusAccepted )
379
- json .NewEncoder (w ).Encode (response )
380
- } else {
381
- // For notifications, just send 202 Accepted with no body
382
- w .WriteHeader (http .StatusAccepted )
383
- }
369
+ // Queue the event for sending via SSE
370
+ select {
371
+ case session .eventQueue <- fmt .Sprintf ("event: message\n data: %s\n \n " , eventData ):
372
+ // Event queued successfully
373
+ case <- session .done :
374
+ // Session is closed, don't try to queue
375
+ default :
376
+ // Queue is full, could log this
377
+ }
378
+ }
379
+ }()
384
380
}
385
381
386
382
// writeJSONRPCError writes a JSON-RPC error response with the given error details.
0 commit comments