Skip to content

mcp: reuse server selection for new streamable requests - #1414

Merged
guglielmo-san merged 3 commits into
modelcontextprotocol:mainfrom
Adebayo120:fix-streamable-get-server-double-call
Oct 20, 2026
Merged

guglielmo-san merged 3 commits into
modelcontextprotocol:mainfrom
Adebayo120:fix-streamable-get-server-double-call

Conversation

@Adebayo120

Copy link
Copy Markdown
Contributor

Reuse the server selected for protocol-version validation when creating a new stateful or stateless session. This prevents duplicate getServer callback invocations for a single POST.

Regression coverage verifies one callback invocation in both modes, while preserving request-body handling.

Fixes #1413

Validation:

  • go test ./... — passed.
  • git diff --check — passed.

Reuse the server selected for protocol-version validation when creating a new stateful or stateless session. This prevents duplicate getServer callback invocations for a single POST.

Fixes modelcontextprotocol#1413

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread mcp/streamable.go Outdated
Comment on lines 414 to 422
@@ -419,12 +421,11 @@ func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
http.Error(w, fmt.Sprintf("Bad Request: Unsupported protocol version (supported versions: %s)", strings.Join(supportedVersions, ",")), http.StatusBadRequest)
return

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this check can be moved to a helper function, and avoid passing server to serveStateless and serveStateful
Instead we could call h.getServer directly from serveStatefulPOST and serveStateless, in case of stateful request, the server can be retrieved from sessInfo in
sessInfo, ok := h.lookupSession(w, req, sessionID)

What do you thihk?

@Adebayo120 Adebayo120 Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, this is a good suggestion. I moved protocol-version validation into a helper, resolve the server in the stateless/new-stateful POST paths, and use the server on the existing session for subsequent stateful requests.

Validate protocol versions using the server selected for new POSTs, or the server associated with an existing session. Avoid invoking getServer for requests routed to established sessions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@guglielmo-san
guglielmo-san merged commit 75b83b6 into modelcontextprotocol:main Oct 20, 2026
9 checks passed
@guglielmo-san

Copy link
Copy Markdown
Contributor

@Adebayo120 thank you for the contribution!

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.

mcp: StreamableHTTPHandler calls getServer twice for one new POST

2 participants