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
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Reference in a new issue