mcp: allow any value for schemas (#517)
As discussed at length in https://github.com/modelcontextprotocol/go-sdk/issues/518, coupling our schema fields to jsonschema.Schema limits our user's ability to use a different schema package, and makes it impossible for our client to talk to some backends. This CL relaxes the type of `*jsonschema.Schema` fields to be of type `any`, which is a breaking change but is unlikely to affect most server code. (It *is* a nontrivial change for clients). Fixes https://github.com/modelcontextprotocol/go-sdk/issues/518
This commit is contained in:
parent
5f05f8a43a
commit
964574b818
1 changed files with 201 additions and 47 deletions
|
|
@ -295,7 +295,8 @@ func (c *Client) elicit(ctx context.Context, req *ElicitRequest) (*ElicitResult,
|
|||
}
|
||||
|
||||
// Validate that the requested schema only contains top-level properties without nesting
|
||||
if err := validateElicitSchema(req.Params.RequestedSchema); err != nil {
|
||||
schema, err := validateElicitSchema(req.Params.RequestedSchema)
|
||||
if err != nil {
|
||||
return nil, jsonrpc2.NewError(CodeInvalidParams, err.Error())
|
||||
}
|
||||
|
||||
|
|
@ -305,11 +306,11 @@ func (c *Client) elicit(ctx context.Context, req *ElicitRequest) (*ElicitResult,
|
|||
}
|
||||
|
||||
// Validate elicitation result content against requested schema
|
||||
if req.Params.RequestedSchema != nil && res.Content != nil {
|
||||
if schema != nil && res.Content != nil {
|
||||
// TODO: is this the correct behavior if validation fails?
|
||||
// It isn't the *server's* params that are invalid, so why would we return
|
||||
// this code to the server?
|
||||
resolved, err := req.Params.RequestedSchema.Resolve(nil)
|
||||
resolved, err := schema.Resolve(nil)
|
||||
if err != nil {
|
||||
return nil, jsonrpc2.NewError(CodeInvalidParams, fmt.Sprintf("failed to resolve requested schema: %v", err))
|
||||
}
|
||||
|
|
@ -324,14 +325,19 @@ func (c *Client) elicit(ctx context.Context, req *ElicitRequest) (*ElicitResult,
|
|||
|
||||
// validateElicitSchema validates that the schema conforms to MCP elicitation schema requirements.
|
||||
// Per the MCP specification, elicitation schemas are limited to flat objects with primitive properties only.
|
||||
func validateElicitSchema(schema *jsonschema.Schema) error {
|
||||
if schema == nil {
|
||||
return nil // nil schema is allowed
|
||||
func validateElicitSchema(wireSchema any) (*jsonschema.Schema, error) {
|
||||
if wireSchema == nil {
|
||||
return nil, nil // nil schema is allowed
|
||||
}
|
||||
|
||||
var schema *jsonschema.Schema
|
||||
if err := remarshal(wireSchema, &schema); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// The root schema must be of type "object" if specified
|
||||
if schema.Type != "" && schema.Type != "object" {
|
||||
return fmt.Errorf("elicit schema must be of type 'object', got %q", schema.Type)
|
||||
return nil, fmt.Errorf("elicit schema must be of type 'object', got %q", schema.Type)
|
||||
}
|
||||
|
||||
// Check if the schema has properties
|
||||
|
|
@ -342,12 +348,12 @@ func validateElicitSchema(schema *jsonschema.Schema) error {
|
|||
}
|
||||
|
||||
if err := validateElicitProperty(propName, propSchema); err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return schema, nil
|
||||
}
|
||||
|
||||
// validateElicitProperty validates a single property in an elicitation schema.
|
||||
|
|
@ -383,7 +389,7 @@ func validateElicitStringProperty(propName string, propSchema *jsonschema.Schema
|
|||
if propSchema.Extra != nil {
|
||||
if enumNamesRaw, exists := propSchema.Extra["enumNames"]; exists {
|
||||
// Type check enumNames - should be a slice
|
||||
if enumNamesSlice, ok := enumNamesRaw.([]interface{}); ok {
|
||||
if enumNamesSlice, ok := enumNamesRaw.([]any); ok {
|
||||
if len(enumNamesSlice) != len(propSchema.Enum) {
|
||||
return fmt.Errorf("elicit schema property %q has %d enum values but %d enumNames, they must match", propName, len(propSchema.Enum), len(enumNamesSlice))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue