go-sdk icon indicating copy to clipboard operation
go-sdk copied to clipboard

Server panics on `id: null` request with per-request protocol `_meta`

Open cclabadmin opened this issue 5 months ago • 1 comments

Describe the bug

When an inbound JSON-RPC frame includes "id": null and per-request protocol _meta (io.modelcontextprotocol/protocolVersion: "2026-07-28"), the server panics with a nil pointer dereference and the process exits with SIGSEGV.

In MCP draft, request IDs MUST be strings or integers and MUST NOT be null. Therefore, this request-shaped input is malformed and should be rejected with a JSON-RPC error, but it should not crash the server process.

From the stack trace and code path, this appears to happen when the per-request _meta validation takes the non-call path for id: null, leaving initializeParams nil, and ServerSession.handle later dereferences it during protocol-version validation.

This code path does not exist in the v1.6.0 stable release (f5f2015), where handle() dispatches directly to method handlers without calling validateRequestMeta. The crash is a regression introduced with the per-request protocol detection (SEP-2575).

Environment tested:

  • main snapshot from 2026-06-28: 65ece5964304528e7568507e2b17351824ef73fc
  • Server used: conformance/everything-server
  • Transport: stdio

To Reproduce

Steps to reproduce the behavior:

  1. Start the Go SDK stdio server.
  2. Send a message with "id": null and per-request _meta:
{"jsonrpc":"2.0","id":null,"method":"completion/complete","params":{"_meta":{"io.modelcontextprotocol/protocolVersion":"2026-07-28","io.modelcontextprotocol/clientInfo":{"name":"repro","version":"0.1.0"},"io.modelcontextprotocol/clientCapabilities":{}},"ref":{"type":"ref/prompt","name":"test"},"argument":{"name":"arg","value":"x"}}}
  1. The server process terminates with a panic.

Expected behavior

The server should reject the malformed input with a JSON-RPC error, likely -32600 Invalid Request. At minimum, the server should not crash.

Observed behavior

The server process exits with:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x70b501]

goroutine 13 [running]:
github.com/modelcontextprotocol/go-sdk/mcp.(*ServerSession).handle(0x..., {0x8abf18, 0x...}, 0x...)
	.../mcp/server.go:1816 +0x101
github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2.HandlerFunc.Handle(...)
	.../internal/jsonrpc2/jsonrpc2.go:60 +0x29
github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2.(*Connection).handleAsync.func3()
	.../internal/jsonrpc2/conn.go:685 +0x76

The crash reproduced in repeated isolated runs with the same stack trace.

Additional context

This appears to be specific to the stdio path: HTTP calls checkRequest() before dispatching the message to the session handler, so the same malformed input is rejected before it reaches ServerSession.handle.

The root cause spans two functions:

In mcp/shared.go, validateRequestMeta explicitly returns initializeParams: nil for non-call messages:

// validateRequestMeta (mcp/shared.go)
func validateRequestMeta(req *jsonrpc.Request) (*validatedMeta, error) {
    // ...
    // Notifications do not carry full client identity.
    if !req.IsCall() {
        return &validatedMeta{usesNewProtocol: true, initializeParams: nil}, nil
    }
    // ...
}

In mcp/server.go, handle dereferences initializeParams without checking for nil:

// handle (mcp/server.go)
if validatedMeta.usesNewProtocol &&
    !slices.Contains(supportedProtocolVersions, validatedMeta.initializeParams.ProtocolVersion) {
    //                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    //                                          nil when IsCall() returned false

A minimal panic-avoidance guard would be:

if validatedMeta.usesNewProtocol && validatedMeta.initializeParams != nil &&
    !slices.Contains(supportedProtocolVersions, validatedMeta.initializeParams.ProtocolVersion) {

A fuller fix may be to reject id: null before using req.IsCall() to choose the call vs. notification path, so the malformed frame is handled as an invalid request rather than as a notification.

cclabadmin avatar Jul 01 '26 05:07 cclabadmin

@cclabadmin thank you for raising this concern, the issue should be now fixed by PR #1049

guglielmo-san avatar Jul 06 '26 15:07 guglielmo-san