| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package mcp_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"encoding/json" |
| 10 |
"fmt" |
| 11 |
"log" |
| 12 |
"reflect" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/google/jsonschema-go/jsonschema" |
| 16 |
"github.com/modelcontextprotocol/go-sdk/mcp" |
| 17 |
) |
| 18 |
|
| 19 |
func ExampleServer_AddTool_rawSchema() { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil) |
| 28 |
server.AddTool(&mcp.Tool{ |
| 29 |
Name: "greet", |
| 30 |
InputSchema: json.RawMessage(`{"type":"object","properties":{"user":{"type":"string"}}}`), |
| 31 |
}, func(_ context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 32 |
|
| 33 |
var args struct{ User string } |
| 34 |
if err := json.Unmarshal(req.Params.Arguments, &args); err != nil { |
| 35 |
|
| 36 |
|
| 37 |
return nil, err |
| 38 |
} |
| 39 |
return &mcp.CallToolResult{ |
| 40 |
Content: []mcp.Content{&mcp.TextContent{Text: "Hi " + args.User}}, |
| 41 |
}, nil |
| 42 |
}) |
| 43 |
|
| 44 |
ctx := context.Background() |
| 45 |
session, err := connect(ctx, server) |
| 46 |
if err != nil { |
| 47 |
log.Fatal(err) |
| 48 |
} |
| 49 |
defer session.Close() |
| 50 |
|
| 51 |
res, err := session.CallTool(ctx, &mcp.CallToolParams{ |
| 52 |
Name: "greet", |
| 53 |
Arguments: map[string]any{"user": "you"}, |
| 54 |
}) |
| 55 |
if err != nil { |
| 56 |
log.Fatal(err) |
| 57 |
} |
| 58 |
fmt.Println(res.Content[0].(*mcp.TextContent).Text) |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
func ExampleAddTool_customMarshalling() { |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
type MyDate struct { |
| 70 |
time.Time |
| 71 |
} |
| 72 |
type Input struct { |
| 73 |
Query string `json:"query,omitempty"` |
| 74 |
Start MyDate `json:"start,omitempty"` |
| 75 |
End MyDate `json:"end,omitempty"` |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
inputSchema, err := jsonschema.For[Input](&jsonschema.ForOptions{ |
| 81 |
TypeSchemas: map[reflect.Type]*jsonschema.Schema{ |
| 82 |
reflect.TypeFor[MyDate](): {Type: "string"}, |
| 83 |
}, |
| 84 |
}) |
| 85 |
if err != nil { |
| 86 |
log.Fatal(err) |
| 87 |
} |
| 88 |
|
| 89 |
server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil) |
| 90 |
toolHandler := func(context.Context, *mcp.CallToolRequest, Input) (*mcp.CallToolResult, any, error) { |
| 91 |
panic("not implemented") |
| 92 |
} |
| 93 |
mcp.AddTool(server, &mcp.Tool{Name: "my_tool", InputSchema: inputSchema}, toolHandler) |
| 94 |
|
| 95 |
ctx := context.Background() |
| 96 |
session, err := connect(ctx, server) |
| 97 |
if err != nil { |
| 98 |
log.Fatal(err) |
| 99 |
} |
| 100 |
defer session.Close() |
| 101 |
|
| 102 |
for t, err := range session.Tools(ctx, nil) { |
| 103 |
if err != nil { |
| 104 |
log.Fatal(err) |
| 105 |
} |
| 106 |
schemaJSON, err := json.MarshalIndent(t.InputSchema, "", "\t") |
| 107 |
if err != nil { |
| 108 |
log.Fatal(err) |
| 109 |
} |
| 110 |
fmt.Println(t.Name, string(schemaJSON)) |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
type Location struct { |
| 131 |
Name string `json:"name"` |
| 132 |
Latitude *float64 `json:"latitude,omitempty"` |
| 133 |
Longitude *float64 `json:"longitude,omitempty"` |
| 134 |
} |
| 135 |
|
| 136 |
type Forecast struct { |
| 137 |
Forecast string `json:"forecast" jsonschema:"description of the day's weather"` |
| 138 |
Type WeatherType `json:"type" jsonschema:"type of weather"` |
| 139 |
Rain float64 `json:"rain" jsonschema:"probability of rain, between 0 and 1"` |
| 140 |
High float64 `json:"high" jsonschema:"high temperature"` |
| 141 |
Low float64 `json:"low" jsonschema:"low temperature"` |
| 142 |
} |
| 143 |
|
| 144 |
type WeatherType string |
| 145 |
|
| 146 |
const ( |
| 147 |
Sunny WeatherType = "sun" |
| 148 |
PartlyCloudy WeatherType = "partly_cloudy" |
| 149 |
Cloudy WeatherType = "clouds" |
| 150 |
Rainy WeatherType = "rain" |
| 151 |
Snowy WeatherType = "snow" |
| 152 |
) |
| 153 |
|
| 154 |
type Probability float64 |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
type WeatherInput struct { |
| 159 |
Location Location `json:"location" jsonschema:"user location"` |
| 160 |
Days int `json:"days" jsonschema:"number of days to forecast"` |
| 161 |
} |
| 162 |
|
| 163 |
type WeatherOutput struct { |
| 164 |
Summary string `json:"summary" jsonschema:"a summary of the weather forecast"` |
| 165 |
Confidence Probability `json:"confidence" jsonschema:"confidence, between 0 and 1"` |
| 166 |
AsOf time.Time `json:"asOf" jsonschema:"the time the weather was computed"` |
| 167 |
DailyForecast []Forecast `json:"dailyForecast" jsonschema:"the daily forecast"` |
| 168 |
Source string `json:"source,omitempty" jsonschema:"the organization providing the weather forecast"` |
| 169 |
} |
| 170 |
|
| 171 |
func WeatherTool(ctx context.Context, req *mcp.CallToolRequest, in WeatherInput) (*mcp.CallToolResult, WeatherOutput, error) { |
| 172 |
perfectWeather := WeatherOutput{ |
| 173 |
Summary: "perfect", |
| 174 |
Confidence: 1.0, |
| 175 |
AsOf: time.Now(), |
| 176 |
} |
| 177 |
for range in.Days { |
| 178 |
perfectWeather.DailyForecast = append(perfectWeather.DailyForecast, Forecast{ |
| 179 |
Forecast: "another perfect day", |
| 180 |
Type: Sunny, |
| 181 |
Rain: 0.0, |
| 182 |
High: 72.0, |
| 183 |
Low: 72.0, |
| 184 |
}) |
| 185 |
} |
| 186 |
return nil, perfectWeather, nil |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
func ExampleAddTool_complexSchema() { |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
customSchemas := map[reflect.Type]*jsonschema.Schema{ |
| 200 |
reflect.TypeFor[Probability](): {Type: "number", Minimum: jsonschema.Ptr(0.0), Maximum: jsonschema.Ptr(1.0)}, |
| 201 |
reflect.TypeFor[WeatherType](): {Type: "string", Enum: []any{Sunny, PartlyCloudy, Cloudy, Rainy, Snowy}}, |
| 202 |
} |
| 203 |
opts := &jsonschema.ForOptions{TypeSchemas: customSchemas} |
| 204 |
in, err := jsonschema.For[WeatherInput](opts) |
| 205 |
if err != nil { |
| 206 |
log.Fatal(err) |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
daysSchema := in.Properties["days"] |
| 212 |
daysSchema.Minimum = jsonschema.Ptr(0.0) |
| 213 |
daysSchema.Maximum = jsonschema.Ptr(10.0) |
| 214 |
|
| 215 |
|
| 216 |
out, err := jsonschema.For[WeatherOutput](opts) |
| 217 |
if err != nil { |
| 218 |
log.Fatal(err) |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil) |
| 224 |
mcp.AddTool(server, &mcp.Tool{ |
| 225 |
Name: "weather", |
| 226 |
InputSchema: in, |
| 227 |
OutputSchema: out, |
| 228 |
}, WeatherTool) |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
ctx := context.Background() |
| 233 |
session, err := connect(ctx, server) |
| 234 |
if err != nil { |
| 235 |
log.Fatal(err) |
| 236 |
} |
| 237 |
defer session.Close() |
| 238 |
|
| 239 |
|
| 240 |
for t, err := range session.Tools(ctx, nil) { |
| 241 |
if err != nil { |
| 242 |
log.Fatal(err) |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
fmt.Println("max days:", jsonPath(t.InputSchema, "properties", "days", "maximum")) |
| 247 |
fmt.Println("max confidence:", jsonPath(t.OutputSchema, "properties", "confidence", "maximum")) |
| 248 |
fmt.Println("weather types:", jsonPath(t.OutputSchema, "properties", "dailyForecast", "items", "properties", "type", "enum")) |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
func connect(ctx context.Context, server *mcp.Server) (*mcp.ClientSession, error) { |
| 257 |
t1, t2 := mcp.NewInMemoryTransports() |
| 258 |
if _, err := server.Connect(ctx, t1, nil); err != nil { |
| 259 |
return nil, err |
| 260 |
} |
| 261 |
client := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, nil) |
| 262 |
return client.Connect(ctx, t2, nil) |
| 263 |
} |
| 264 |
|
| 265 |
func jsonPath(s any, path ...string) any { |
| 266 |
if len(path) == 0 { |
| 267 |
return s |
| 268 |
} |
| 269 |
return jsonPath(s.(map[string]any)[path[0]], path[1:]...) |
| 270 |
} |