Skip to content

Fix #275: Reject uninitialized session requests with INVALID_REQUEST#1069

Open
tanujkapoor-cmd wants to merge 1 commit into
modelcontextprotocol:mainfrom
tanujkapoor-cmd:fix/issue-275
Open

Fix #275: Reject uninitialized session requests with INVALID_REQUEST#1069
tanujkapoor-cmd wants to merge 1 commit into
modelcontextprotocol:mainfrom
tanujkapoor-cmd:fix/issue-275

Conversation

@tanujkapoor-cmd

Copy link
Copy Markdown

Resolves #275 by immediately rejecting requests sent before the session is fully initialized.

Motivation and Context

Right now, if a client accidentally sends a request (or notification) before completing the notifications/initialized handshake, the server just silently hangs. This happens because McpServerSession routes it to exchangeSink.asMono(), which waits forever since the sink only emits when the init notification arrives.

This PR fixes that silent failure by returning a proper JSON-RPC -32600 (INVALID_REQUEST) error instead. This gives client devs immediate feedback if they mess up the handshake order. I also added a quick logger.warn so server admins can see these rejections.

How Has This Been Tested?

I tested this locally by running the build and verifying the state machine logic in McpServerSession. Since this just intercepts the request before it hits the sink, it's a very straightforward guard clause.

Breaking Changes

No breaking changes. It just replaces an indefinite hang with a fast-fail error response.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

I made sure to append the method name to the error message string to make it slightly easier to debug on the client side. Also double-checked the MCP spec to ensure rejecting pre-init messages with INVALID_REQUEST is fully compliant.

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.

Non initialized McpServerSession hangs and timesout instead of providing helpful error message

1 participant