Skip to content

Commit c79a30a

Browse files
mcp: add param check for CompleteResult (#1080)
Fixes #1071
1 parent dd79ca3 commit c79a30a

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

docs/mcpgodebug.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ Options listed below were added and will be removed in the 1.9.0 version of the
5050
restoring the previous behavior. The default behavior was changed so that
5151
the client always attempts to surface the underlying JSON-RPC error.
5252

53+
- `disablecompleteparamsvalidation` added. If set to `1`, `Server.complete`
54+
will not validate that the required `ref` and `argument.name` fields on
55+
`CompleteParams` are present, restoring the previous behavior of dispatching
56+
the request to the completion handler unconditionally. The default behavior
57+
was changed to reject malformed requests with `-32602` (Invalid Params).
58+
5359
### 1.6.1
5460

5561
Options listed below were added and will be removed in the 1.8.0 version of the SDK.

internal/docs/mcpgodebug.src.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ Options listed below were added and will be removed in the 1.9.0 version of the
4949
restoring the previous behavior. The default behavior was changed so that
5050
the client always attempts to surface the underlying JSON-RPC error.
5151

52+
- `disablecompleteparamsvalidation` added. If set to `1`, `Server.complete`
53+
will not validate that the required `ref` and `argument.name` fields on
54+
`CompleteParams` are present, restoring the previous behavior of dispatching
55+
the request to the completion handler unconditionally. The default behavior
56+
was changed to reject malformed requests with `-32602` (Invalid Params).
57+
5258
### 1.6.1
5359

5460
Options listed below were added and will be removed in the 1.8.0 version of the SDK.

mcp/server.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/google/jsonschema-go/jsonschema"
2828
internaljson "github.com/modelcontextprotocol/go-sdk/internal/json"
2929
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
30+
"github.com/modelcontextprotocol/go-sdk/internal/mcpgodebug"
3031
"github.com/modelcontextprotocol/go-sdk/internal/util"
3132
"github.com/modelcontextprotocol/go-sdk/jsonrpc"
3233
"github.com/yosida95/uritemplate/v3"
@@ -661,7 +662,23 @@ func (s *Server) capabilities() *ServerCapabilities {
661662
return caps
662663
}
663664

665+
// disablecompleteparamsvalidation is a compatibility parameter that restores
666+
// the previous behavior of [Server.complete], where required fields on
667+
// [CompleteParams] were not validated before dispatching to the completion
668+
// handler. See the documentation for the mcpgodebug package for instructions
669+
// how to enable it.
670+
// The option will be removed in a future version of the SDK.
671+
var disablecompleteparamsvalidation = mcpgodebug.Value("disablecompleteparamsvalidation")
672+
664673
func (s *Server) complete(ctx context.Context, req *CompleteRequest) (*CompleteResult, error) {
674+
if disablecompleteparamsvalidation != "1" {
675+
if req.Params.Ref == nil {
676+
return nil, fmt.Errorf("%w: missing required 'ref' field", jsonrpc2.ErrInvalidParams)
677+
}
678+
if req.Params.Argument.Name == "" {
679+
return nil, fmt.Errorf("%w: missing required 'argument.name' field", jsonrpc2.ErrInvalidParams)
680+
}
681+
}
665682
if s.opts.CompletionHandler == nil {
666683
return nil, jsonrpc2.ErrMethodNotFound
667684
}

0 commit comments

Comments
 (0)