-
Notifications
You must be signed in to change notification settings - Fork 273
all: add examples of customizing tool schemas, and clarify documentation #421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| // Copyright 2025 The Go MCP SDK Authors. All rights reserved. | ||
| // Use of this source code is governed by an MIT-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| // The toolschemas example demonstrates how to create tools using both the | ||
| // low-level [ToolHandler] and high level [ToolHandlerFor], as well as how to | ||
| // customize schemas in both cases. | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "log" | ||
|
|
||
| "github.com/google/jsonschema-go/jsonschema" | ||
| "github.com/modelcontextprotocol/go-sdk/mcp" | ||
| ) | ||
|
|
||
| // Input is the input into all the tools handlers below. | ||
| type Input struct { | ||
| Name string `json:"name" jsonschema:"the person to greet"` | ||
| } | ||
|
|
||
| // Output is the structured output of the tool. | ||
| // | ||
| // Not every tool needs to have structured output. | ||
| type Output struct { | ||
| Greeting string `json:"greeting" jsonschema:"the greeting to send to the user"` | ||
| } | ||
|
|
||
| // simpleGreeting is an [mcp.ToolHandlerFor] that only cares about input and output. | ||
| func simpleGreeting(_ context.Context, _ *mcp.CallToolRequest, input Input) (*mcp.CallToolResult, Output, error) { | ||
| return nil, Output{"Hi " + input.Name}, nil | ||
| } | ||
|
|
||
| // manualGreeter handles the parsing and validation of input and output manually. | ||
| // | ||
| // Therfore, it needs to close over its resolved schemas, to use them in | ||
| // validation. | ||
| type manualGreeter struct { | ||
| inputSchema *jsonschema.Resolved | ||
| outputSchema *jsonschema.Resolved | ||
| } | ||
|
|
||
| func (t *manualGreeter) greet(_ context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
| // errf produces a 'tool error', embedding the error in a CallToolResult. | ||
| errf := func(format string, args ...any) *mcp.CallToolResult { | ||
| return &mcp.CallToolResult{ | ||
| Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf(format, args...)}}, | ||
| IsError: true, | ||
| } | ||
| } | ||
| // Handle the parsing and validation of input and output. | ||
| // | ||
| // Note that errors here are treated as tool errors, not protocol errors. | ||
| var input Input | ||
| if err := json.Unmarshal(req.Params.Arguments, &input); err != nil { | ||
| return errf("failed to unmarshal arguments: %v", err), nil | ||
| } | ||
| if err := t.inputSchema.Validate(input); err != nil { | ||
| return errf("invalid input: %v", err), nil | ||
| } | ||
| output := Output{Greeting: "Hi " + input.Name} | ||
| if err := t.outputSchema.Validate(output); err != nil { | ||
| return errf("tool produced invalid output: %v", err), nil | ||
| } | ||
| outputJSON, err := json.Marshal(output) | ||
| if err != nil { | ||
| return errf("output failed to marshal: %v", err), nil | ||
| } | ||
| return &mcp.CallToolResult{ | ||
| Content: []mcp.Content{&mcp.TextContent{Text: string(outputJSON)}}, | ||
| StructuredContent: output, | ||
findleyr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, nil | ||
| } | ||
|
|
||
| func main() { | ||
| server := mcp.NewServer(&mcp.Implementation{Name: "greeter"}, nil) | ||
|
|
||
| // 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: | ||
| 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. | ||
jba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var ( | ||
| manual manualGreeter | ||
| err error | ||
| ) | ||
| inputSchema := &jsonschema.Schema{ | ||
| Type: "object", | ||
| Properties: map[string]*jsonschema.Schema{ | ||
| "name": {Type: "string", MaxLength: jsonschema.Ptr(10)}, | ||
| }, | ||
| } | ||
| manual.inputSchema, err = inputSchema.Resolve(nil) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| outputSchema := &jsonschema.Schema{ | ||
| Type: "object", | ||
| Properties: map[string]*jsonschema.Schema{ | ||
| "greeting": {Type: "string"}, | ||
| }, | ||
| } | ||
| manual.outputSchema, err = outputSchema.Resolve(nil) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| server.AddTool(&mcp.Tool{ | ||
| Name: "manual greeting", | ||
| InputSchema: inputSchema, | ||
| OutputSchema: outputSchema, | ||
| }, manual.greet) | ||
|
|
||
| // 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. | ||
jba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| mcp.AddTool(server, &mcp.Tool{ | ||
| Name: "customized greeting 1", | ||
| InputSchema: inputSchema, | ||
| // OutputSchema will still be derived from Output. | ||
| }, simpleGreeting) | ||
|
|
||
| customSchema, err := jsonschema.For[Input](nil) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| customSchema.Properties["name"].MaxLength = jsonschema.Ptr(10) | ||
| mcp.AddTool(server, &mcp.Tool{ | ||
| Name: "customized greeting 2", | ||
| InputSchema: customSchema, | ||
| }, simpleGreeting) | ||
|
|
||
| // Now run the server. | ||
| if err := server.Run(context.Background(), &mcp.StdioTransport{}); err != nil { | ||
| log.Printf("Server failed: %v", err) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.