Skip to content

Commit 8c42b69

Browse files
SSE: Add support for SSE handler options (#508)
mcp/sse: add support to provide for options for SSE transport - Add SSEOptions struct to define SSE handler options. - Update Signature of NewSSEHandler to accept SSEOptions [braking change] - Update unit test - Update example test Fixes #507, #503
1 parent 4e9a387 commit 8c42b69

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

examples/server/sse/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ func main() {
6565
default:
6666
return nil
6767
}
68-
})
68+
}, nil)
6969
log.Fatal(http.ListenAndServe(addr, handler))
7070
}

mcp/sse.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,18 @@ import (
4343
// [2024-11-05 version]: https://modelcontextprotocol.io/specification/2024-11-05/basic/transports
4444
type SSEHandler struct {
4545
getServer func(request *http.Request) *Server
46+
opts SSEOptions
4647
onConnection func(*ServerSession) // for testing; must not block
4748

4849
mu sync.Mutex
4950
sessions map[string]*SSEServerTransport
5051
}
5152

53+
// SSEOptions specifies options for an [SSEHandler].
54+
// for now, it is empty, but may be extended in future.
55+
// https://github.com/modelcontextprotocol/go-sdk/issues/507
56+
type SSEOptions struct{}
57+
5258
// NewSSEHandler returns a new [SSEHandler] that creates and manages MCP
5359
// sessions created via incoming HTTP requests.
5460
//
@@ -62,13 +68,17 @@ type SSEHandler struct {
6268
// The getServer function may return a distinct [Server] for each new
6369
// request, or reuse an existing server. If it returns nil, the handler
6470
// will return a 400 Bad Request.
65-
//
66-
// TODO(rfindley): add options.
67-
func NewSSEHandler(getServer func(request *http.Request) *Server) *SSEHandler {
68-
return &SSEHandler{
71+
func NewSSEHandler(getServer func(request *http.Request) *Server, opts *SSEOptions) *SSEHandler {
72+
s := &SSEHandler{
6973
getServer: getServer,
7074
sessions: make(map[string]*SSEServerTransport),
7175
}
76+
77+
if opts != nil {
78+
s.opts = *opts
79+
}
80+
81+
return s
7282
}
7383

7484
// A SSEServerTransport is a logical SSE session created through a hanging GET

mcp/sse_example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func ExampleSSEHandler() {
3131
server := mcp.NewServer(&mcp.Implementation{Name: "adder", Version: "v0.0.1"}, nil)
3232
mcp.AddTool(server, &mcp.Tool{Name: "add", Description: "add two numbers"}, Add)
3333

34-
handler := mcp.NewSSEHandler(func(*http.Request) *mcp.Server { return server })
34+
handler := mcp.NewSSEHandler(func(*http.Request) *mcp.Server { return server }, nil)
3535
httpServer := httptest.NewServer(handler)
3636
defer httpServer.Close()
3737

mcp/sse_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestSSEServer(t *testing.T) {
2424
server := NewServer(testImpl, nil)
2525
AddTool(server, &Tool{Name: "greet"}, sayHi)
2626

27-
sseHandler := NewSSEHandler(func(*http.Request) *Server { return server })
27+
sseHandler := NewSSEHandler(func(*http.Request) *Server { return server }, nil)
2828

2929
serverSessions := make(chan *ServerSession, 1)
3030
sseHandler.onConnection = func(ss *ServerSession) {

0 commit comments

Comments
 (0)