Skip to content

mcp: reject null sampling messages instead of panicking - #1567

Merged
guglielmo-san merged 1 commit into
modelcontextprotocol:mainfrom
rene-founder:fix/1563-null-sampling-message
Dec 4, 2026
Merged

guglielmo-san merged 1 commit into
modelcontextprotocol:mainfrom
rene-founder:fix/1563-null-sampling-message

Conversation

@rene-founder

Copy link
Copy Markdown
Contributor

Summary

A sampling/createMessage request containing a null element in messages crashes a client that only configures CreateMessageHandler: toBase dereferences the nil message inside an SDK-managed goroutine, terminating the process before the handler runs.

Changes

  • mcp/protocol.go (toBase): Return an invalid params error when a messages element is nil, so the client responds with JSON-RPC -32602 instead of panicking.
  • mcp/mrtr.go (createMessageParamsToWithTools): Preserve nil message elements as nil when converting a tool's *CreateMessageParams InputRequests (2025-11-25 multi-round-trip path), so the request reaches the client and returns the same error instead of panicking the server.
  • mcp/sampling_test.go (TestCreateMessage_NilMessage): Verify the -32602 error path directly and through tool InputRequests for both CreateMessageWithToolsParams and CreateMessageParams across protocol versions 2026-07-28 and 2025-11-25.

Verification

  • Confirmed red before each guard: removing the toBase guard panics in toBase; removing the createMessageParamsToWithTools guard panics in createMessageParamsToWithTools.
  • Ran gofmt, go vet ./..., staticcheck v0.8.2, go test ./... (Go 1.26.11 and 1.27.4), and go test -race ./... (Go 1.27.4) locally on linux/amd64 (WSL2) and windows/amd64; all passed. (Not run on darwin or arm64.)

Scope & Notes

Fixes #1563


AI tools assisted me with the investigation, the patch, and the test runs; I reviewed and verified the changes locally.

René Atencio Roa
Founder, Alekore Systems Inc.

A sampling/createMessage request whose messages contain null made the
client panic in toBase when only a CreateMessageHandler is set. The
panic happens on a goroutine started by the SDK, so the client process
exits before the handler runs.

The same null in a tool's InputRequests, given as *CreateMessageParams,
also panicked the server in the 2025-11-25 multi-round-trip shim, in
createMessageParamsToWithTools.

Return an invalid params error from toBase for a nil message, and keep
the nil in createMessageParamsToWithTools so that it reaches the client
and is rejected there.

Fixes modelcontextprotocol#1563
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-04T15:37:27.407801Z 78f075f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@guglielmo-san
guglielmo-san merged commit e3114cb into modelcontextprotocol:main Dec 4, 2026
11 of 12 checks passed
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: client panics when a server sends a null message in sampling/createMessage

2 participants