-
Notifications
You must be signed in to change notification settings - Fork 711
Closed
Description
Description
The MCP streamable_http session is not reused properly in the MCP server. Ephemeral sessions are always created.
The fix needed is in the session reuse logic for non-initialize requests in the handlePost method. Here are the specific changes:
(I am trying to get a PR ready with these changes)
Code Sample
streamable_http.go code changes to handlePost:
// For non-initialize requests, try to reuse existing registered session
if !isInitializeRequest {
if sessionValue, ok := s.server.sessions.Load(sessionID); ok {
if existingSession, ok := sessionValue.(*streamableHttpSession); ok {
session = existingSession
}
}
}
// Register session after successful initialization
if isInitializeRequest && sessionID != "" {
// Register the session with the MCPServer for notification support
if err := s.server.RegisterSession(ctx, session); err != nil {
s.logger.Errorf("Failed to register POST session: %v", err)
// Don't fail the request, just log the error
}
}
Logs or Error Messages
Environment
- mcp-go version: Latest code
Additional Context
Found when testing notifications sent from server and noticing that they are not on the same session that was registered with the client.
Possible Solution
See code above
Metadata
Metadata
Assignees
Labels
No labels