Skip to content

internal/jsonrpc2: contain handler panics and reply with an internal error - #1180

Open
ez-lbz wants to merge 1 commit into
modelcontextprotocol:mainfrom
ez-lbz:fix/jsonrpc2-recover-handler-panics
Open

internal/jsonrpc2: contain handler panics and reply with an internal error#1180
ez-lbz wants to merge 1 commit into
modelcontextprotocol:mainfrom
ez-lbz:fix/jsonrpc2-recover-handler-panics

Conversation

@ez-lbz

@ez-lbz ez-lbz commented Aug 16, 2026

Copy link
Copy Markdown

Summary

A panicking request handler currently escapes the per-request goroutine in handleAsync and terminates the entire server process, taking every session down with it. Handlers are user code operating on attacker-controlled input (e.g. tools/call arguments), so a single malformed request can crash the server remotely.

This change recovers panics in the handler goroutine and replies with an internal error (-32603) instead, matching the isolation behavior of the other official SDKs:

  • New ErrPanic sentinel distinguishes recovered panics from ordinary handler errors.
  • The recovered panic is converted to a JSON-RPC internal error via processResult, so the client receives code -32603 and the connection stays usable.

Tests

  • Added TestServerToolPanic in mcp/error_test.go: a panicking tool handler yields a -32603 error to the client, and a subsequent tools/call on the same connection succeeds.

…error

A panicking request handler currently escapes the per-request goroutine
in handleAsync and terminates the entire server process, taking every
session down with it. Handlers are user code operating on
attacker-controlled input, so a single malformed request can crash the
server.

Recover panics in the handler goroutine and reply with an internal error
(code -32603) instead, matching the isolation behavior of the other
official SDKs. A new ErrPanic sentinel distinguishes recovered panics
from ordinary handler errors.
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.

1 participant