Skip to content

Commit bd6ffe3

Browse files
committed
mcp: add an example customizing a nested type schema
Add an example that demonstrates how to customize a nested type schema, which is a recurring problem for our users (see #467). Fixes #467 Updates #368
1 parent b3fb83f commit bd6ffe3

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

mcp/tool_example_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
package mcp_test
6+
7+
import (
8+
"context"
9+
"encoding/json"
10+
"fmt"
11+
"log"
12+
"time"
13+
14+
"github.com/google/jsonschema-go/jsonschema"
15+
"github.com/modelcontextprotocol/go-sdk/mcp"
16+
)
17+
18+
func ExampleAddTool_customTypeSchema() {
19+
// Sometimes when you want to customize the input or output schema for a
20+
// tool, you need to customize the schema of a single helper type that's used
21+
// in several places.
22+
//
23+
// For example, suppose you had a type that marshals/unmarshals like a
24+
// time.Time, and that type was used multiple times in your tool input.
25+
type MyDate struct {
26+
time.Time
27+
}
28+
type Input struct {
29+
Query string `json:"query,omitempty"`
30+
Start MyDate `json:"start,omitempty"`
31+
End MyDate `json:"end,omitempty"`
32+
}
33+
34+
// In this case, you can use jsonschema.For along with jsonschema.ForOptions
35+
// to customize the schema inference for your custom type.
36+
inputSchema, err := jsonschema.For[Input](&jsonschema.ForOptions{
37+
TypeSchemas: map[any]*jsonschema.Schema{
38+
MyDate{}: {Type: "string"},
39+
},
40+
})
41+
if err != nil {
42+
log.Fatal(err)
43+
}
44+
45+
server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil)
46+
toolHandler := func(context.Context, *mcp.CallToolRequest, Input) (*mcp.CallToolResult, any, error) {
47+
panic("not implemented")
48+
}
49+
mcp.AddTool(server, &mcp.Tool{Name: "my_tool", InputSchema: inputSchema}, toolHandler)
50+
51+
ctx := context.Background()
52+
session, err := connect(ctx, server) // create an in-memory connection
53+
if err != nil {
54+
log.Fatal(err)
55+
}
56+
defer session.Close()
57+
58+
for t, err := range session.Tools(ctx, nil) {
59+
if err != nil {
60+
log.Fatal(err)
61+
}
62+
schemaJSON, err := json.MarshalIndent(t.InputSchema, "", "\t")
63+
if err != nil {
64+
log.Fatal(err)
65+
}
66+
fmt.Println(t.Name, string(schemaJSON))
67+
}
68+
// Output:
69+
// my_tool {
70+
// "type": "object",
71+
// "properties": {
72+
// "end": {
73+
// "type": "string"
74+
// },
75+
// "query": {
76+
// "type": "string"
77+
// },
78+
// "start": {
79+
// "type": "string"
80+
// }
81+
// },
82+
// "additionalProperties": false
83+
// }
84+
}
85+
86+
func connect(ctx context.Context, server *mcp.Server) (*mcp.ClientSession, error) {
87+
t1, t2 := mcp.NewInMemoryTransports()
88+
if _, err := server.Connect(ctx, t1, nil); err != nil {
89+
return nil, err
90+
}
91+
client := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, nil)
92+
return client.Connect(ctx, t2, nil)
93+
}

0 commit comments

Comments
 (0)