Repository navigation
Description
Checklist
- I searched the existing issues, and this has not been reported before.
- This is a real problem that I ran into myself, using the latest release of the SDK.
- I ran into it in a production system, not in a toy example, a benchmark, or an automated or AI-driven scan of the code.
- I (a human) examined the relevant SDK code, and wrote or reviewed every line of this report. It is not unreviewed AI output.
- This report is brief and concise, and sticks to the facts needed to understand the problem.
What happened?
Calling ClientSession request methods (such as CallTool, GetPrompt, ReadResource, etc.) mutates the caller's params struct and its Meta map in place:
injectRequestMetawritesio.modelcontextprotocol/protocolVersion,io.modelcontextprotocol/clientInfo, andio.modelcontextprotocol/clientCapabilitiesdirectly intoparams.GetMeta()(when_, ok := m[key]; !ok) and callsparams.SetMeta(m)on the caller's struct.ClientSession.CallToolsetsparams.Arguments = map[string]any{}directly on the caller'sCallToolParamswhenArguments == nil.
This causes two issues when callers reuse a request struct across calls:
- Stale client metadata across sessions: If the same
*CallToolParams(or*GetPromptParams, etc.) is passed tosessionAand thensessionBon protocol2026-07-28,sessionApopulatesparams.Metawith its ownclientInfoandclientCapabilities. BecauseinjectRequestMetaonly populates missing keys,sessionBsees those keys already set and sendssessionA'sclientInfoand capabilities to the server. - Data races on concurrent calls: If concurrent goroutines share a
*CallToolParams(even on the same session, or on2025-11-25whenArgumentsisnil),CallToolandinjectRequestMetawrite toparams.Argumentsandparams.Metawhile other calls read and marshal them, triggeringDATA RACEs undergo test -race.
Expected behavior:
ClientSession methods should not mutate caller-owned request structs or Meta maps. Shallow-copying params before setting default Arguments or Meta and cloning params.GetMeta() with maps.Clone in injectRequestMeta avoids both the cross-session metadata leak and the data race while preserving explicit caller overrides.
I have a small fix and test ready and would be happy to send a PR if you'd like one. (Used an AI assistant to help draft the reproduction test; I reviewed the SDK code and verified the output.)
How to reproduce
package mcp
import (
"context"
"sync"
"testing"
)
func TestCallToolSharedParams(t *testing.T) {
ctx := t.Context()
s := NewServer(testImpl, nil)
s.AddTool(&Tool{Name: "ping", InputSchema: map[string]any{"type": "object"}},
func(_ context.Context, req *CallToolRequest) (*CallToolResult, error) {
var name string
if info, ok := req.Params.Meta[MetaKeyClientInfo].(map[string]any); ok {
name, _ = info["name"].(string)
}
return &CallToolResult{Content: []Content{&TextContent{Text: name}}}, nil
})
connect := func(name string) *ClientSession {
ct, st := NewInMemoryTransports()
ss, err := s.Connect(ctx, st, nil)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = ss.Close() })
cs, err := NewClient(&Implementation{Name: name, Version: "1"}, nil).Connect(ctx, ct,
&ClientSessionOptions{ProtocolVersion: protocolVersion20260728})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = cs.Close() })
return cs
}
csA := connect("client-a")
csB := connect("client-b")
// 1. Sequential reuse across two sessions sends client-a's clientInfo from client-b.
shared := &CallToolParams{Name: "ping"}
if _, err := csA.CallTool(ctx, shared); err != nil {
t.Fatal(err)
}
res, err := csB.CallTool(ctx, shared)
if err != nil {
t.Fatal(err)
}
if got := res.Content[0].(*TextContent).Text; got != "client-b" {
t.Errorf("server received clientInfo.name = %q from client-b, want %q", got, "client-b")
}
// 2. Concurrent reuse races on params.Arguments and params.Meta.
concurrentParams := &CallToolParams{Name: "ping"}
var wg sync.WaitGroup
for range 4 {
wg.Go(func() { _, _ = csA.CallTool(ctx, concurrentParams) })
wg.Go(func() { _, _ = csB.CallTool(ctx, concurrentParams) })
}
wg.Wait()
}Where did you hit this?
No response
Go MCP SDK version
main (c15e86b)
Go version
go version go1.27.2 darwin/arm64
Logs
--- FAIL: TestCallToolSharedParams (0.01s)
client_params_test.go:47: server received clientInfo.name = "client-a" from client-b, want "client-b"
==================
WARNING: DATA RACE
Read at 0x00c000339918 by goroutine 42:
github.com/modelcontextprotocol/go-sdk/mcp.(*ClientSession).CallTool()
mcp/client.go:1381 +0x58
Previous write at 0x00c000339918 by goroutine 35:
github.com/modelcontextprotocol/go-sdk/mcp.(*ClientSession).CallTool()
mcp/client.go:1383 +0x74
==================
WARNING: DATA RACE
Read at 0x00c0004ce140 by goroutine 106:
github.com/modelcontextprotocol/go-sdk/mcp.(*CallToolParams).GetMeta()
<autogenerated>:1 +0x2c
github.com/modelcontextprotocol/go-sdk/mcp.injectRequestMeta[...]()
mcp/client.go:553 +0x78
github.com/modelcontextprotocol/go-sdk/mcp.(*ClientSession).CallTool()
mcp/client.go:1389 +0x170
Previous write at 0x00c0004ce140 by goroutine 105:
github.com/modelcontextprotocol/go-sdk/mcp.(*Meta).SetMeta()
mcp/shared.go:553 +0x30
github.com/modelcontextprotocol/go-sdk/mcp.injectRequestMeta[...]()
mcp/client.go:566 +0x3ac
github.com/modelcontextprotocol/go-sdk/mcp.(*ClientSession).CallTool()
mcp/client.go:1389 +0x170
==================
Activity
guglielmo-san commentedon Oct 29, 2026
@ar8327 thank you for reporting this, the issue is assigned to you so you can send the PR now!
mcp: avoid mutating client request parameters (modelcontextprotocol#1473