Repository navigation
Description
The JSON-RPC 2.0 base protocol MCP builds on makes the params member optional, and the SDK agrees. serverMethodInfos in mcp/server.go sets missingParamsOK for server/discover, ping, the four list methods, and the cancelled, initialized and roots/list_changed notifications, and checkRequest in mcp/shared.go accepts those with no params member at all. {"jsonrpc":"2.0","id":1,"method":"tools/list"} is therefore a complete, valid request, and clients really send it.
What a receiving middleware is handed for one of those is not what the obvious check expects. newMethodInfo.unmarshalParams in mcp/shared.go returns a nil Params when the member is absent, and newServerMethodInfo.newRequest then leaves ServerRequest.Params at the zero value of P, which is a typed nil pointer. Request.GetParams returns that boxed in a Params interface. An interface holding a nil pointer is not a nil interface, so req.GetParams() != nil is true, and the first method called on the result dereferences the pointer: GetMeta is promoted from the embedded Meta, so it panics in (*ListToolsParams).GetMeta.
The SDK knows the case exists. Params declares isNil next to GetMeta, SetMeta and isParams, for exactly this. It is unexported, so the one accessor named after the question is the one thing a middleware outside the package cannot reach.
Nothing points a middleware author at it, either. The doc comments on Server.AddReceivingMiddleware and Client.AddReceivingMiddleware say what receiving middleware is useful for and do not mention that params may be absent, and the SDK's own middleware example, examples/server/middleware/main.go, logs "has_params", req.GetParams() != nil, which reports true for a request that carried none.
Minimal reproduction, against v1.7.0:
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
func main() {
server := mcp.NewServer(&mcp.Implementation{Name: "repro", Version: "1"}, nil)
server.AddReceivingMiddleware(func(next mcp.MethodHandler) mcp.MethodHandler {
return func(ctx context.Context, method string, req mcp.Request) (mcp.Result, error) {
if params := req.GetParams(); params != nil {
fmt.Printf("%s: params != nil, reading _meta\n", method)
_ = params.GetMeta()
}
return next(ctx, method, req)
}
})
ts := httptest.NewServer(mcp.NewStreamableHTTPHandler(
func(*http.Request) *mcp.Server { return server }, nil))
defer ts.Close()
session := ""
post := func(body string) {
req, _ := http.NewRequest("POST", ts.URL, strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json, text/event-stream")
if session != "" {
req.Header.Set("Mcp-Session-Id", session)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("post:", err)
return
}
defer resp.Body.Close()
if id := resp.Header.Get("Mcp-Session-Id"); id != "" {
session = id
}
}
post(`{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"c","version":"1"}}}`)
post(`{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}`)
post(`{"jsonrpc":"2.0","id":1,"method":"tools/list"}`)
}initialize: params != nil, reading _meta
notifications/initialized: params != nil, reading _meta
tools/list: params != nil, reading _meta
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7a5dc0]
goroutine 24 [running]:
github.com/modelcontextprotocol/go-sdk/mcp.(*ListToolsParams).GetMeta(0xc6e798?)
<autogenerated>:1
main.main.func1.1(...)
/tmp/repro/main.go:19 +0xc6
github.com/modelcontextprotocol/go-sdk/mcp.handleReceive[...](...)
.../[email protected]/mcp/shared.go:200 +0x267
github.com/modelcontextprotocol/go-sdk/mcp.(*ServerSession).handle(...)
.../[email protected]/mcp/server.go:1925 +0x7ce
github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2.(*Connection).handleAsync.func3()
.../[email protected]/internal/jsonrpc2/conn.go:685 +0x76
Dropping the "params":{} from the notifications/initialized line panics one message earlier in exactly the same way, in (*InitializedParams).GetMeta.
There is no recover anywhere outside the tests in this repository, and ServerSession.handle runs on the goroutine jsonrpc2.(*Connection).handleAsync starts, so the panic takes the process down unless the application installed a recovery middleware of its own. Mine had one, which is why the symptom I saw was a log line rather than a crash.
The same value reaches the SDK's own per-request accessors. getRequestMeta guards with isNil, which is right for the params types the SDK defines, and is not right for a params type declared the documented way for a custom method, as struct{ mcp.ParamsBase }. There the promoted isNil reaches the embedded field through the nil outer pointer, so it panics before the method body runs. AddReceivingCustomMethod registers with missingParamsOK, so a client that omits params for a custom method makes ServerRequest.ProtocolVersion, ClientInfo and ClientCapabilities panic.
What this costs a server built on the SDK: no middleware can look at params safely using the API the SDK exports. Reading _meta is the ordinary reason to want to, and from the 2026-07-28 revision that is where the per-request protocol version, client info and client capabilities live, so a trace-context carrier, a request logger and a metric keyed on a params field all end up there. The choices left are to reflect on the value in the caller, or to type switch over every params type the SDK defines and keep that list current as the SDK grows. I took the first.
How I found it: auditing my MCP server against the specification, after a hosted deployment logged a recovered panic about a hundred times in a day on tools/list, prompts/list, resources/list and notifications/initialized, every stack ending in a promoted GetMeta. The comment in my own carrier had asserted that the wire could not produce such a request.
Observed on v1.7.0. The code is unchanged on main at 5bc078a.
I have a change ready for this and will offer it as a pull request.
Part of #1257.
Activity
mcp: add HasParams so middleware can tell whether params are present (m…