@@ -26,9 +26,21 @@ import (
2626 "github.com/modelcontextprotocol/go-sdk/auth"
2727 internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
2828 "github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
29+ "github.com/modelcontextprotocol/go-sdk/internal/mcpgodebug"
2930 "github.com/modelcontextprotocol/go-sdk/jsonrpc"
3031)
3132
33+ // nowrapinvalidparams is a compatibility parameter that restores the previous
34+ // behavior of [methodInfo.unmarshalParams]. When unset (the default), a
35+ // params-decoding failure is wrapped with [jsonrpc2.ErrInvalidParams] so the
36+ // wire response carries error code -32602 ("invalid params") rather than the
37+ // zero-value code 0. See:
38+ // https://github.com/modelcontextprotocol/go-sdk/issues/976#issuecomment-4829124838.
39+ //
40+ // See the documentation for the mcpgodebug package for instructions how to enable it.
41+ // The option will be removed in a future version of the SDK.
42+ var nowrapinvalidparams = mcpgodebug .Value ("nowrapinvalidparams" )
43+
3244const (
3345 // latestProtocolVersion is the latest protocol version that this version of
3446 // the SDK supports.
@@ -304,7 +316,17 @@ func newMethodInfo[P paramsPtr[T], R Result, T any](flags methodFlags) methodInf
304316 var p P
305317 if m != nil {
306318 if err := internaljson .Unmarshal (m , & p ); err != nil {
307- return nil , fmt .Errorf ("unmarshaling %q into a %T: %w" , m , p , err )
319+ // Legacy behavior: pre-fix versions surfaced this as a
320+ // plain wrapped error, which caused the wire response to
321+ // carry code 0 instead of -32602. Restore via
322+ // MCPGODEBUG=nowrapinvalidparams=1.
323+ if nowrapinvalidparams == "1" {
324+ return nil , fmt .Errorf ("unmarshaling %q into a %T: %w" , m , p , err )
325+ }
326+ // Wrap jsonrpc2.ErrInvalidParams so toWireError surfaces
327+ // code -32602 ("invalid params") while preserving the
328+ // descriptive message.
329+ return nil , fmt .Errorf ("%w: unmarshaling %q into a %T: %w" , jsonrpc2 .ErrInvalidParams , m , p , err )
308330 }
309331 }
310332 // We must check missingParamsOK here, in addition to checkRequest, to
0 commit comments