Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions mcp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ type ServerOptions struct {
// If true, advertises the tools capability during initialization,
// even if no tools have been registered.
HasTools bool

// GetSessionID provides the next session ID to use for an incoming request.
// If nil, a default randomly generated ID will be used.
//
// Session IDs should be globally unique across the scope of the server,
// which may span multiple processes in the case of distributed servers.
//
// As a special case, if GetSessionID returns the empty string, the
// Mcp-Session-Id header will not be set.
GetSessionID func() string
}

// NewServer creates a new MCP server. The resulting server has no features:
Expand Down Expand Up @@ -114,6 +124,11 @@ func NewServer(impl *Implementation, options *ServerOptions) *Server {
if opts.UnsubscribeHandler != nil && opts.SubscribeHandler == nil {
panic("UnsubscribeHandler requires SubscribeHandler")
}

if opts.GetSessionID == nil {
opts.GetSessionID = randText
}

return &Server{
impl: impl,
opts: opts,
Expand Down
15 changes: 1 addition & 14 deletions mcp/streamable.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,6 @@ type StreamableHTTPHandler struct {

// StreamableHTTPOptions configures the StreamableHTTPHandler.
type StreamableHTTPOptions struct {
// GetSessionID provides the next session ID to use for an incoming request.
// If nil, a default randomly generated ID will be used.
//
// Session IDs should be globally unique across the scope of the server,
// which may span multiple processes in the case of distributed servers.
//
// As a special case, if GetSessionID returns the empty string, the
// Mcp-Session-Id header will not be set.
GetSessionID func() string

// Stateless controls whether the session is 'stateless'.
//
// A stateless server does not validate the Mcp-Session-Id header, and uses a
Expand Down Expand Up @@ -92,9 +82,6 @@ func NewStreamableHTTPHandler(getServer func(*http.Request) *Server, opts *Strea
if opts != nil {
h.opts = *opts
}
if h.opts.GetSessionID == nil {
h.opts.GetSessionID = randText
}
return h
}

Expand Down Expand Up @@ -233,7 +220,7 @@ func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
if sessionID == "" {
// In stateless mode, sessionID may be nonempty even if there's no
// existing transport.
sessionID = h.opts.GetSessionID()
sessionID = server.opts.GetSessionID()
}
transport = &StreamableServerTransport{
SessionID: sessionID,
Expand Down
27 changes: 18 additions & 9 deletions mcp/streamable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,15 +409,14 @@ func testClientReplay(t *testing.T, test clientReplayTest) {
}

func TestServerTransportCleanup(t *testing.T) {
server := NewServer(testImpl, &ServerOptions{KeepAlive: 10 * time.Millisecond})

nClient := 3

var mu sync.Mutex
var id int = -1 // session id starting from "0", "1", "2"...
chans := make(map[string]chan struct{}, nClient)

handler := NewStreamableHTTPHandler(func(*http.Request) *Server { return server }, &StreamableHTTPOptions{
server := NewServer(testImpl, &ServerOptions{
KeepAlive: 10 * time.Millisecond,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is KeepAlive being set here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you already resolved this but just for future reference: this is an artifact of moving code around (i.e., the keep alive was there already).

I was wondering about this separately when debugging leaking goroutines and flakyness and it took me some time to realize that the keep alive here is used as a way to make the server close the connection with the client (because it would fail the keep alive after 10ms). This is necessary in this test because of the bug that transport cleanup does not happen even when the client closes the connection itself (which does happen in this test).

When we merge #480 we can actually improve this test and remove the keepalive.

GetSessionID: func() string {
mu.Lock()
defer mu.Unlock()
Expand All @@ -430,6 +429,7 @@ func TestServerTransportCleanup(t *testing.T) {
},
})

handler := NewStreamableHTTPHandler(func(*http.Request) *Server { return server }, nil)
handler.onTransportDeletion = func(sessionID string) {
chans[sessionID] <- struct{}{}
}
Expand Down Expand Up @@ -1201,8 +1201,6 @@ func TestStreamableStateless(t *testing.T) {
}
return &CallToolResult{Content: []Content{&TextContent{Text: "hi " + args.Name}}}, nil, nil
}
server := NewServer(testImpl, nil)
AddTool(server, &Tool{Name: "greet", Description: "say hi"}, sayHi)

requests := []streamableRequest{
{
Expand Down Expand Up @@ -1265,9 +1263,15 @@ func TestStreamableStateless(t *testing.T) {
}
}

sessionlessHandler := NewStreamableHTTPHandler(func(*http.Request) *Server { return server }, &StreamableHTTPOptions{
GetSessionID: func() string { return "" },
Stateless: true,
sessionlessHandler := NewStreamableHTTPHandler(func(*http.Request) *Server {
// Return a stateless server which never assigns a session ID.
server := NewServer(testImpl, &ServerOptions{
GetSessionID: func() string { return "" },
})
AddTool(server, &Tool{Name: "greet", Description: "say hi"}, sayHi)
return server
}, &StreamableHTTPOptions{
Stateless: true,
})

// First, test the "sessionless" stateless mode, where there is no session ID.
Expand All @@ -1281,7 +1285,12 @@ func TestStreamableStateless(t *testing.T) {
// This can be used by tools to look up application state preserved across
// subsequent requests.
requests[0].wantSessionID = true // now expect a session ID for initialize
statelessHandler := NewStreamableHTTPHandler(func(*http.Request) *Server { return server }, &StreamableHTTPOptions{
statelessHandler := NewStreamableHTTPHandler(func(*http.Request) *Server {
// Return a server with default options which should assign a random session ID.
server := NewServer(testImpl, nil)
AddTool(server, &Tool{Name: "greet", Description: "say hi"}, sayHi)
return server
}, &StreamableHTTPOptions{
Stateless: true,
})
t.Run("stateless", func(t *testing.T) {
Expand Down