Skip to content

mcp: avoid mutating client request parameters - #1473

Merged
guglielmo-san merged 2 commits into
modelcontextprotocol:mainfrom
ar8327:fix/client-request-ownership
Oct 29, 2026
Merged

guglielmo-san merged 2 commits into
modelcontextprotocol:mainfrom
ar8327:fix/client-request-ownership

Conversation

@ar8327

@ar8327 ar8327 commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Reusing CallToolParams (or other request params) across ClientSession calls can send the first client's _meta fields on subsequent calls, because injectRequestMeta only populates missing keys. Concurrent calls sharing a request struct also race when CallTool initializes a nil Arguments map or injectRequestMeta writes to Meta.

Shallow-copy the request struct before setting default Arguments or Meta, and clone params.GetMeta() with maps.Clone before injecting per-request metadata. Explicit caller metadata overrides are preserved.

Tests in mcp/client_params_test.go cover injectRequestMeta ownership and sequential/concurrent CallTool reuse across sessions on both 2025-11-25 and 2026-07-28.

  • go test -race -count=1 ./... passes
  • go vet ./... passes

Used an AI coding assistant to help write the reproduction test and fix; reviewed and verified locally.

Fixes #1472

Reusing request params across ClientSession calls could send the first
session's metadata on subsequent calls, and concurrent calls sharing
params raced when CallTool set default Arguments or injectRequestMeta
updated Meta.

Shallow-copy the request struct before setting default Arguments or
Meta, and clone params.GetMeta() with maps.Clone before injecting
per-request metadata. Explicit caller metadata overrides are preserved.

Fixes modelcontextprotocol#1472

Signed-off-by: ar8327 <ar8327@outlook.com>
Comment thread mcp/client_params_test.go Outdated
Comment on lines +1 to +5
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
// Use of this source code is governed by the license
// that can be found in the LICENSE file.

package mcp

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.

lets not create a separate file for this, can you add the test to @client_test ?

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.

Done — moved the tests to mcp/client_test.go and removed mcp/client_params_test.go in 1e119a5.

Signed-off-by: ar8327 <ar8327@outlook.com>
@guglielmo-san
guglielmo-san merged commit 51e4f98 into modelcontextprotocol:main Oct 29, 2026
9 checks passed
@guglielmo-san

Copy link
Copy Markdown
Contributor

@ar8327 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: ClientSession methods mutate caller-owned request params and Meta

2 participants