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:
Robert Findley 2025-09-26 11:55:54 -04:00 • committed by GitHub
commit 964574b818
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -126,15 +126,13 @@ func main() {
// Add the 'greeting' tool in a few different ways.
// First, we can just use [mcp.AddTool], and get the out-of-the-box handling
// it provides:
// it provides for schema inference, validation, parsing, and packing the
// result.
mcp.AddTool(server, &mcp.Tool{Name: "simple greeting"}, simpleGreeting)
// Next, we can create our schemas entirely manually, and add them using
// [mcp.Server.AddTool]. Since we're working manually, we can add some
// constraints on the length of the name.
//
// We don't need to do all this work: below, we use jsonschema.For to start
// from the default schema.
// Alternatively, we can create our schemas entirely manually, and add them
// using [mcp.Server.AddTool]. Since we're using the 'raw' API, we have to do
// the parsing and validation ourselves
manual, err := newManualGreeter()
if err != nil {
log.Fatal(err)
@ -145,6 +143,22 @@ func main() {
OutputSchema: outputSchema,
}, manual.greet)
// We can even use raw schema values. In this case, note that we're not
// validating the input at all.
server.AddTool(&mcp.Tool{
Name: "unvalidated greeting",
InputSchema: json.RawMessage(`{"type":"object","properties":{"user":{"type":"string"}}}`),
}, func(_ context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) {
// Note: no validation!
var args struct{ User string }
if err := json.Unmarshal(req.Params.Arguments, &args); err != nil {
return nil, err
}
return &mcp.CallToolResult{
Content: []mcp.Content{&mcp.TextContent{Text: "Hi " + args.User}},
}, nil
})
// Finally, note that we can also use custom schemas with a ToolHandlerFor.
// We can do this in two ways: by using one of the schema values constructed
// above, or by using jsonschema.For and adjusting the resulting schema.