Skip to content

bug: Sessions are not reused correctly in the streamable_http transport #614

@bcain99

Description

@bcain99

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions