|
| 1 | +// Copyright 2025 The Go MCP SDK Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by an MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// The toolschemas example demonstrates how to create tools using both the |
| 6 | +// low-level [ToolHandler] and high level [ToolHandlerFor], as well as how to |
| 7 | +// customize schemas in both cases. |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "encoding/json" |
| 13 | + "fmt" |
| 14 | + "log" |
| 15 | + |
| 16 | + "github.com/google/jsonschema-go/jsonschema" |
| 17 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 18 | +) |
| 19 | + |
| 20 | +// Input is the input into all the tools handlers below. |
| 21 | +type Input struct { |
| 22 | + Name string `json:"name" jsonschema:"the person to greet"` |
| 23 | +} |
| 24 | + |
| 25 | +// Output is the structured output of the tool. |
| 26 | +// |
| 27 | +// Not every tool needs to have structured output. |
| 28 | +type Output struct { |
| 29 | + Greeting string `json:"greeting" jsonschema:"the greeting to send to the user"` |
| 30 | +} |
| 31 | + |
| 32 | +// simpleGreeting is an [mcp.ToolHandlerFor] that only cares about input and output. |
| 33 | +func simpleGreeting(_ context.Context, _ *mcp.CallToolRequest, input Input) (*mcp.CallToolResult, Output, error) { |
| 34 | + return nil, Output{"Hi " + input.Name}, nil |
| 35 | +} |
| 36 | + |
| 37 | +// manualGreeter handles the parsing and validation of input and output manually. |
| 38 | +// |
| 39 | +// Therfore, it needs to close over its resolved schemas, to use them in |
| 40 | +// validation. |
| 41 | +type manualGreeter struct { |
| 42 | + inputSchema *jsonschema.Resolved |
| 43 | + outputSchema *jsonschema.Resolved |
| 44 | +} |
| 45 | + |
| 46 | +func (t *manualGreeter) greet(_ context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 47 | + // errf produces a 'tool error', embedding the error in a CallToolResult. |
| 48 | + errf := func(format string, args ...any) *mcp.CallToolResult { |
| 49 | + return &mcp.CallToolResult{ |
| 50 | + Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf(format, args...)}}, |
| 51 | + IsError: true, |
| 52 | + } |
| 53 | + } |
| 54 | + // Handle the parsing and validation of input and output. |
| 55 | + // |
| 56 | + // Note that errors here are treated as tool errors, not protocol errors. |
| 57 | + var input Input |
| 58 | + if err := json.Unmarshal(req.Params.Arguments, &input); err != nil { |
| 59 | + return errf("failed to unmarshal arguments: %v", err), nil |
| 60 | + } |
| 61 | + if err := t.inputSchema.Validate(input); err != nil { |
| 62 | + return errf("invalid input: %v", err), nil |
| 63 | + } |
| 64 | + output := Output{Greeting: "Hi " + input.Name} |
| 65 | + if err := t.outputSchema.Validate(output); err != nil { |
| 66 | + return errf("tool produced invalid output: %v", err), nil |
| 67 | + } |
| 68 | + outputJSON, err := json.Marshal(output) |
| 69 | + if err != nil { |
| 70 | + return errf("output failed to marshal: %v", err), nil |
| 71 | + } |
| 72 | + return &mcp.CallToolResult{ |
| 73 | + Content: []mcp.Content{&mcp.TextContent{Text: string(outputJSON)}}, |
| 74 | + StructuredContent: output, |
| 75 | + }, nil |
| 76 | +} |
| 77 | + |
| 78 | +func main() { |
| 79 | + server := mcp.NewServer(&mcp.Implementation{Name: "greeter"}, nil) |
| 80 | + |
| 81 | + // Add the 'greeting' tool in a few different ways. |
| 82 | + |
| 83 | + // First, we can just use [mcp.AddTool], and get the out-of-the-box handling |
| 84 | + // it provides: |
| 85 | + mcp.AddTool(server, &mcp.Tool{Name: "simple greeting"}, simpleGreeting) |
| 86 | + |
| 87 | + // Next, we can create our schemas entirely manually, and add them using |
| 88 | + // [mcp.Server.AddTool]. Since we're working manually, we can add some |
| 89 | + // constraints on the length of the name. |
| 90 | + // |
| 91 | + // We don't need to do all this work: below, we use jsonschema.For to start |
| 92 | + // from the default schema. |
| 93 | + var ( |
| 94 | + manual manualGreeter |
| 95 | + err error |
| 96 | + ) |
| 97 | + inputSchema := &jsonschema.Schema{ |
| 98 | + Type: "object", |
| 99 | + Properties: map[string]*jsonschema.Schema{ |
| 100 | + "name": {Type: "string", MaxLength: jsonschema.Ptr(10)}, |
| 101 | + }, |
| 102 | + } |
| 103 | + manual.inputSchema, err = inputSchema.Resolve(nil) |
| 104 | + if err != nil { |
| 105 | + log.Fatal(err) |
| 106 | + } |
| 107 | + outputSchema := &jsonschema.Schema{ |
| 108 | + Type: "object", |
| 109 | + Properties: map[string]*jsonschema.Schema{ |
| 110 | + "greeting": {Type: "string"}, |
| 111 | + }, |
| 112 | + } |
| 113 | + manual.outputSchema, err = outputSchema.Resolve(nil) |
| 114 | + if err != nil { |
| 115 | + log.Fatal(err) |
| 116 | + } |
| 117 | + server.AddTool(&mcp.Tool{ |
| 118 | + Name: "manual greeting", |
| 119 | + InputSchema: inputSchema, |
| 120 | + OutputSchema: outputSchema, |
| 121 | + }, manual.greet) |
| 122 | + |
| 123 | + // Finally, note that we can also use custom schemas with a ToolHandlerFor. |
| 124 | + // We can do this in two ways: by using one of the schema values constructed |
| 125 | + // above, or by using jsonschema.For and adjusting the resulting schema. |
| 126 | + mcp.AddTool(server, &mcp.Tool{ |
| 127 | + Name: "customized greeting 1", |
| 128 | + InputSchema: inputSchema, |
| 129 | + // OutputSchema will still be derived from Output. |
| 130 | + }, simpleGreeting) |
| 131 | + |
| 132 | + customSchema, err := jsonschema.For[Input](nil) |
| 133 | + if err != nil { |
| 134 | + log.Fatal(err) |
| 135 | + } |
| 136 | + customSchema.Properties["name"].MaxLength = jsonschema.Ptr(10) |
| 137 | + mcp.AddTool(server, &mcp.Tool{ |
| 138 | + Name: "customized greeting 2", |
| 139 | + InputSchema: customSchema, |
| 140 | + }, simpleGreeting) |
| 141 | + |
| 142 | + // Now run the server. |
| 143 | + if err := server.Run(context.Background(), &mcp.StdioTransport{}); err != nil { |
| 144 | + log.Printf("Server failed: %v", err) |
| 145 | + } |
| 146 | +} |
0 commit comments