Skip to content

mcp: ClientSession.CallTool omits Mcp-Param-* headers unless the tool is in the ListTools cache, so a call without a prior tools/list is rejected #1481

Closed

Description

@huynle-dev

On 2026-07-28, ClientSession.CallTool gets the tool definition for SEP-2243 x-mcp-header headers only from lookupTool, which searches toolsCache (mcp/client.go:599, used at :1292 in v1.8.0; same on main). If the session never called ListTools, no Mcp-Param-* header is sent, and a go-sdk server rejects the call in validateParamHeaders with 400. There is no exported way to supply the definition: toolContextKey is unexported and CallToolParams has no slot for it.

Repro (v1.8.0)

srv := mcp.NewServer(&mcp.Implementation{Name: "s", Version: "v1"}, nil)
srv.AddTool(&mcp.Tool{
	Name: "get_weather",
	InputSchema: map[string]any{"type": "object", "properties": map[string]any{
		"region": map[string]any{"type": "string", "x-mcp-header": "Region"},
	}},
}, func(context.Context, *mcp.CallToolRequest) (*mcp.CallToolResult, error) {
	return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: "ok"}}}, nil
})
ts := httptest.NewServer(mcp.NewStreamableHTTPHandler(
	func(*http.Request) *mcp.Server { return srv }, &mcp.StreamableHTTPOptions{Stateless: true}))
cs, _ := mcp.NewClient(&mcp.Implementation{Name: "c", Version: "v1"}, nil).
	Connect(ctx, &mcp.StreamableClientTransport{Endpoint: ts.URL}, nil)
_, err := cs.CallTool(ctx, &mcp.CallToolParams{Name: "get_weather", Arguments: map[string]any{"region": "us-west1"}})
fmt.Println(err)
// calling "tools/call": sending "tools/call": header mismatch: missing Mcp-Param-Region header for parameter "region": rejected by transport: Bad Request

Calling cs.ListTools first makes it succeed (Mcp-Param-Region: us-west1).

Why it matters

Gateways and proxies cache upstream catalogs centrally and open a fresh ClientSession per proxied call (per-user upstream credentials, multiple replicas), so a tools/list before every call doubles the round trips. Clients that persist catalogs across sessions, and short-lived stateless 2026-07-28 sessions in general, hit the same thing.

Proposal (in order of preference)

  1. func WithTool(ctx context.Context, tool *Tool) context.Context — exposes the mechanism that already exists; takes precedence over the cache.
  2. func (cs *ClientSession) CallToolWithTool(ctx context.Context, tool *Tool, params *CallToolParams) (*CallToolResult, error)
  3. CallToolParams.Tool *Tool with json:"-" — least preferred, it mixes local options into a wire type (cf. mcp: ClientSession methods mutate caller-owned request params and Meta #1472).

Also worth documenting on CallTool that header generation depends on a prior ListTools. Happy to send a PR for 1 or 2.

Activity

  1. sundarshahi commented on Oct 31, 2026

    @sundarshahi

    I've put up a fix in #1483, which the bot closed until this issue is assigned. On a cache miss, CallTool pages through tools/list to find the tool, only for 2026-07-28 sessions over the streamable transport, and the repro above is its regression test. If you'd rather callers pass the definition themselves, I can switch it to that. Could you assign this to me so the PR can reopen?

    AI-assisted: I used an AI coding assistant and reviewed and tested every change.

  2. guglielmo-san commented on Nov 2, 2026

    @guglielmo-san
    Contributor

    Rather than eagerly calling tools/list before tools/call on every cache miss (as in #1483), we should implement the reactive recovery prescribed by the 2026-07-28 Streamable HTTP spec (which is also what the TypeScript and Python SDKs do):

    If the server rejects a request with a HeaderMismatch (-32020) error because required Mcp-Param-* headers are missing or do not match the body, the client SHOULD call tools/list to check for changes to the tool's inputSchema, then retry the original request with the appropriate headers.

    Concretely, in ClientSession.CallTool:

    1. Send tools/call immediately using cs.lookupTool(params.Name) as today.
    2. If that call fails with a *jsonrpc.Error whose Code == CodeHeaderMismatch (-32020) on a 2026-07-28 Streamable HTTP session:
      • Invalidate cs.toolsCache.
      • Page through ListTools and stop as soon as params.Name is found.
      • If the tool is found, retry tools/call. If listing fails or the tool isn't listed, return the original -32020 error.

    Retrying once on -32020 is safe because servers reject HeaderMismatch during HTTP header validation before invoking the tool handler, and this covers both cold-cache calls and mid-session schema changes without expanding the public API.

  3. added a commit that references this issue on Nov 6, 2026
    e747987
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