diff --git a/docs/mcpgodebug.md b/docs/mcpgodebug.md index 7082518..847e2e7 100644 --- a/docs/mcpgodebug.md +++ b/docs/mcpgodebug.md @@ -50,6 +50,12 @@ Options listed below were added and will be removed in the 1.9.0 version of the restoring the previous behavior. The default behavior was changed so that the client always attempts to surface the underlying JSON-RPC error. +- `disablecompleteparamsvalidation` added. If set to `1`, `Server.complete` + will not validate that the required `ref` and `argument.name` fields on + `CompleteParams` are present, restoring the previous behavior of dispatching + the request to the completion handler unconditionally. The default behavior + was changed to reject malformed requests with `-32602` (Invalid Params). + ### 1.6.1 Options listed below were added and will be removed in the 1.8.0 version of the SDK. diff --git a/internal/docs/mcpgodebug.src.md b/internal/docs/mcpgodebug.src.md index 1f15072..5c9c9d9 100644 --- a/internal/docs/mcpgodebug.src.md +++ b/internal/docs/mcpgodebug.src.md @@ -49,6 +49,12 @@ Options listed below were added and will be removed in the 1.9.0 version of the restoring the previous behavior. The default behavior was changed so that the client always attempts to surface the underlying JSON-RPC error. +- `disablecompleteparamsvalidation` added. If set to `1`, `Server.complete` + will not validate that the required `ref` and `argument.name` fields on + `CompleteParams` are present, restoring the previous behavior of dispatching + the request to the completion handler unconditionally. The default behavior + was changed to reject malformed requests with `-32602` (Invalid Params). + ### 1.6.1 Options listed below were added and will be removed in the 1.8.0 version of the SDK. diff --git a/mcp/server.go b/mcp/server.go index 58e0bb1..14adfad 100644 --- a/mcp/server.go +++ b/mcp/server.go @@ -27,6 +27,7 @@ import ( "github.com/google/jsonschema-go/jsonschema" internaljson "github.com/modelcontextprotocol/go-sdk/internal/json" "github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2" + "github.com/modelcontextprotocol/go-sdk/internal/mcpgodebug" "github.com/modelcontextprotocol/go-sdk/internal/util" "github.com/modelcontextprotocol/go-sdk/jsonrpc" "github.com/yosida95/uritemplate/v3" @@ -661,7 +662,23 @@ func (s *Server) capabilities() *ServerCapabilities { return caps } +// disablecompleteparamsvalidation is a compatibility parameter that restores +// the previous behavior of [Server.complete], where required fields on +// [CompleteParams] were not validated before dispatching to the completion +// handler. See the documentation for the mcpgodebug package for instructions +// how to enable it. +// The option will be removed in a future version of the SDK. +var disablecompleteparamsvalidation = mcpgodebug.Value("disablecompleteparamsvalidation") + func (s *Server) complete(ctx context.Context, req *CompleteRequest) (*CompleteResult, error) { + if disablecompleteparamsvalidation != "1" { + if req.Params.Ref == nil { + return nil, fmt.Errorf("%w: missing required 'ref' field", jsonrpc2.ErrInvalidParams) + } + if req.Params.Argument.Name == "" { + return nil, fmt.Errorf("%w: missing required 'argument.name' field", jsonrpc2.ErrInvalidParams) + } + } if s.opts.CompletionHandler == nil { return nil, jsonrpc2.ErrMethodNotFound }