mcp: add param check for CompleteResult (#1080)

Fixes #1071
This commit is contained in:
Guglielmo Colombo 2026-07-13 14:14:21 +02:00 • committed by GitHub
commit c79a30ae0a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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
}