Skip to content

mcp: report an error when a sampling or elicitation handler returns nil - #1304

Open
po-et wants to merge 5 commits into
modelcontextprotocol:mainfrom
po-et:fix/client-nil-handler-results
Open

po-et wants to merge 5 commits into
modelcontextprotocol:mainfrom
po-et:fix/client-nil-handler-results

Conversation

@po-et

@po-et po-et commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

A sampling or elicitation handler returning (nil, nil) could panic in the client or send a null result back to the server. These handler paths report -32603 by wrapping jsonrpc2.ErrInternal, while preserving errors returned by the handler.

Tests cover both sampling handlers and form/URL elicitation. Four direct tests check the error code and sentinel; eight multi-round-trip cases check propagation on 2026-07-28 and 2025-11-25 sessions.

Validation after integrating current main: go test -race ./..., go vet ./..., CI-pinned staticcheck v0.6.1, formatting, and diff checks pass locally on Go 1.25.6. The latest public commit also passed all nine GitHub checks, including Go 1.25/1.26 tests, race, client/server conformance, lint, and CodeQL.

po-et and others added 2 commits September 27, 2026 08:28
A CreateMessageHandler, or an ElicitationHandler for a form elicitation,
that returned nil, nil made the client dereference the nil result and
crash. CreateMessageWithToolsHandler and URL elicitation sent the nil to
the server instead. Report an error naming the handler in all four
cases, as readResource does for a resource handler.
Comment thread mcp/client.go Outdated

This branch has not been deployed

No deployments
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.

2 participants