Skip to content

mcp: ClientSession methods mutate caller-owned request params and Meta #1472

Closed

Description

@ar8327

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:

  1. injectRequestMeta writes io.modelcontextprotocol/protocolVersion, io.modelcontextprotocol/clientInfo, and io.modelcontextprotocol/clientCapabilities directly into params.GetMeta() (when _, ok := m[key]; !ok) and calls params.SetMeta(m) on the caller's struct.
  2. ClientSession.CallTool sets params.Arguments = map[string]any{} directly on the caller's CallToolParams when Arguments == 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 to sessionA and then sessionB on protocol 2026-07-28, sessionA populates params.Meta with its own clientInfo and clientCapabilities. Because injectRequestMeta only populates missing keys, sessionB sees those keys already set and sends sessionA's clientInfo and capabilities to the server.
  • Data races on concurrent calls: If concurrent goroutines share a *CallToolParams (even on the same session, or on 2025-11-25 when Arguments is nil), CallTool and injectRequestMeta write to params.Arguments and params.Meta while other calls read and marshal them, triggering DATA RACEs under go 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

  1. guglielmo-san commented on Oct 29, 2026

    @guglielmo-san
    Contributor

    @ar8327 thank you for reporting this, the issue is assigned to you so you can send the PR now!

  2. added a commit that references this issue on Oct 29, 2026
    51e4f98
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions