Skip to content

Conversation

bhosmer-ant
Copy link
Contributor

@bhosmer-ant bhosmer-ant commented Aug 20, 2025

Protocol-level session support implementation

Implements SEP-1359: Protocol-Level Sessions for MCP

Current state

Protocol-level session support has been implemented with the following components:

Core implementation

  • Protocol layer: Session state management, validation, and lifecycle methods in Protocol base class
  • Server integration: Session options configuration, synchronous handlers for HTTP transport
  • Client handling: Session creation from server-assigned IDs, automatic session ID inclusion in requests
  • Transport updates: Session state propagation, synchronous initialization/termination for proper HTTP error codes

Key features

  • Session ID generation and validation
  • Session timeout and expiry checking
  • Activity tracking for keep-alive
  • Backward compatibility with HTTP transport session headers
  • Session termination support (protocol message and HTTP DELETE)

Testing

  • Comprehensive unit tests for session lifecycle
  • Integration tests for stateful/stateless modes
  • Transport switching tests for initialize requests (verifies correct routing and prevents re-initialization)
  • All 721 tests passing (including 15 new tests)

Remaining tasks

  • Thorough final review for code quality and test coverage of protocol-level session functionality
  • Update documentation for session support

Breaking changes

None - maintains backward compatibility with existing transport-level sessions.

- Add SessionId type and schema
- Add session interfaces (SessionState, SessionOptions)
- Add session state to Protocol class
- Add session validation and lifecycle methods
- Include sessionId in all outgoing JSON-RPC messages
- Add InvalidSession error code and handling
- Add comprehensive tests for session validation and lifecycle
- Fix session validation to handle all session/sessionless combinations
- Update capturedTransport pattern to capture session state
- Include sessionId in all response messages using captured state
- Update RequestHandlerExtra sessionId type to support SessionId
- Remove unused imports from tests

Phase 1 complete: Protocol class has full session support
- Simplify server session tests to avoid complex internal mocking
- Test session configuration and transport access functionality
- Verify session terminate handler registration without method not found error
- All session tests now pass (13/13)
- Change SessionId type to string-only for simplicity
- Add header-to-protocol sessionId injection with mismatch validation
- Remove all session generation and validation logic from transport
- Convert HTTP DELETE to session/terminate protocol message
- Store legacy session callbacks for server delegation
- Transport is now pure HTTP-to-protocol bridge
- Update tests for string-only SessionId

Phase 3 transport migration complete: all session logic moved to server
- Add getLegacySessionOptions and setSessionState to Transport interface
- Implement legacy session options delegation in Protocol connect
- Add session state support to InMemoryTransport and StreamableHTTPClientTransport
- Client now handles sessionId from InitializeResult and notifies transport
- Server delegation system working (debug output shows session creation flow)

Note: Some integration tests still failing - require full Phase 4 client implementation
- Don't override existing sessionId in requests/notifications
- Add sessionId property support to StreamableHTTPClientTransport
- Client session delegation working (confirmed by debug output)
- Skip problematic test with timeout issues for later investigation

Core client-server session flow now working correctly
- Fix client-side session validation to only validate incoming requests
- Don't reset request ID counter when creating session
- Move session options from Protocol to Server class
- Fix previously skipped sessionId test
- All 719 tests now pass
- Test initialize request handling when transport switches mid-flight
- Test session validation when new transport attempts re-initialization
- Both tests verify correct request routing and session state management
- Restore console.log statements that were incorrectly removed
- Add similar logging to new initialize tests for consistency
- Fixes lint errors from unused parameters
…r backward compatibility

- Constructor now accepts optional options parameter
- Preserves backward compatibility for existing code that creates transport without options
- Legacy session callbacks are only stored when options are provided
- Added regression test to ensure transport can be created without options
- Increased notification interval from 10ms to 50ms for more reliable timing
- Increased wait time from 20ms to 75ms to ensure notifications are received
- Increased disconnect delay from 10ms to 50ms for cleaner disconnect
- Test now passes consistently without flakiness
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant