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
3 changed files with 29 additions and 0 deletions

View file

@ -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 restoring the previous behavior. The default behavior was changed so that
the client always attempts to surface the underlying JSON-RPC error. 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 ### 1.6.1
Options listed below were added and will be removed in the 1.8.0 version of the SDK. Options listed below were added and will be removed in the 1.8.0 version of the SDK.

View file

@ -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 restoring the previous behavior. The default behavior was changed so that
the client always attempts to surface the underlying JSON-RPC error. 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 ### 1.6.1
Options listed below were added and will be removed in the 1.8.0 version of the SDK. Options listed below were added and will be removed in the 1.8.0 version of the SDK.

View file

@ -27,6 +27,7 @@ import (
"github.com/google/jsonschema-go/jsonschema" "github.com/google/jsonschema-go/jsonschema"
internaljson "github.com/modelcontextprotocol/go-sdk/internal/json" internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2" "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/internal/util"
"github.com/modelcontextprotocol/go-sdk/jsonrpc" "github.com/modelcontextprotocol/go-sdk/jsonrpc"
"github.com/yosida95/uritemplate/v3" "github.com/yosida95/uritemplate/v3"
@ -661,7 +662,23 @@ func (s *Server) capabilities() *ServerCapabilities {
return caps 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) { 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 { if s.opts.CompletionHandler == nil {
return nil, jsonrpc2.ErrMethodNotFound return nil, jsonrpc2.ErrMethodNotFound
} }