|
| 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 | +type WeatherInput struct { |
| 19 | + Location Location `json:"location" jsonschema:"user location"` |
| 20 | + Days int `json:"days" jsonschema:"number of days to forecast"` |
| 21 | +} |
| 22 | + |
| 23 | +type Location struct { |
| 24 | + Name string `json:"name"` |
| 25 | + Latitude *float64 `json:"latitude,omitempty"` |
| 26 | + Longitude *float64 `json:"longitude,omitempty"` |
| 27 | +} |
| 28 | + |
| 29 | +type Forecast struct { |
| 30 | + Forecast string `json:"forecast" jsonschema:"description of the day's weather"` |
| 31 | + Type WeatherType `json:"type" jsonschema:"type of weather"` |
| 32 | + Rain float64 `json:"rain" jsonschema:"probability of rain, between 0 and 1"` |
| 33 | + High float64 `json:"high" jsonschema:"high temperature"` |
| 34 | + Low float64 `json:"low" jsonschema:"low temperature"` |
| 35 | +} |
| 36 | + |
| 37 | +type WeatherType string |
| 38 | + |
| 39 | +const ( |
| 40 | + Sunny WeatherType = "sun" |
| 41 | + PartlyCloudy WeatherType = "partly_cloudy" |
| 42 | + Cloudy WeatherType = "clouds" |
| 43 | + Rainy WeatherType = "rain" |
| 44 | + Snowy WeatherType = "snow" |
| 45 | +) |
| 46 | + |
| 47 | +type Probability float64 |
| 48 | + |
| 49 | +type WeatherOutput struct { |
| 50 | + Summary string `json:"summary" jsonschema:"a summary of the weather forecast"` |
| 51 | + Confidence Probability `json:"confidence" jsonschema:"confidence, between 0 and 1"` |
| 52 | + AsOf time.Time `json:"asOf" jsonschema:"the time the weather was computed"` |
| 53 | + DailyForecast []Forecast `json:"dailyForecast" jsonschema:"the daily forecast"` |
| 54 | + Source string `json:"source,omitempty" jsonschema:"the organization providing the weather forecast"` |
| 55 | +} |
| 56 | + |
| 57 | +func WeatherTool(ctx context.Context, req *mcp.CallToolRequest, in WeatherInput) (*mcp.CallToolResult, WeatherOutput, error) { |
| 58 | + perfectWeather := WeatherOutput{ |
| 59 | + Summary: "perfect", |
| 60 | + Confidence: 1.0, |
| 61 | + AsOf: time.Now(), |
| 62 | + } |
| 63 | + for range in.Days { |
| 64 | + perfectWeather.DailyForecast = append(perfectWeather.DailyForecast, Forecast{ |
| 65 | + Forecast: "another perfect day", |
| 66 | + Type: Sunny, |
| 67 | + Rain: 0.0, |
| 68 | + High: 72.0, |
| 69 | + Low: 72.0, |
| 70 | + }) |
| 71 | + } |
| 72 | + return nil, perfectWeather, nil |
| 73 | +} |
| 74 | + |
| 75 | +func ExampleAddTool_complexSchema() { |
| 76 | + customSchemas := map[any]*jsonschema.Schema{ |
| 77 | + Probability(0): {Type: "number", Minimum: jsonschema.Ptr(0.0), Maximum: jsonschema.Ptr(1.0)}, |
| 78 | + WeatherType(""): {Type: "string", Enum: []any{Sunny, PartlyCloudy, Cloudy, Rainy, Snowy}}, |
| 79 | + } |
| 80 | + opts := &jsonschema.ForOptions{TypeSchemas: customSchemas} |
| 81 | + in, err := jsonschema.For[WeatherInput](opts) |
| 82 | + if err != nil { |
| 83 | + log.Fatal(err) |
| 84 | + } |
| 85 | + out, err := jsonschema.For[WeatherOutput](opts) |
| 86 | + if err != nil { |
| 87 | + log.Fatal(err) |
| 88 | + } |
| 89 | + |
| 90 | + server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil) |
| 91 | + mcp.AddTool(server, &mcp.Tool{ |
| 92 | + Name: "weather", |
| 93 | + InputSchema: in, |
| 94 | + OutputSchema: out, |
| 95 | + }, WeatherTool) |
| 96 | + |
| 97 | + ctx := context.Background() |
| 98 | + session, err := connect(ctx, server) // create an in-memory connection |
| 99 | + if err != nil { |
| 100 | + log.Fatal(err) |
| 101 | + } |
| 102 | + defer session.Close() |
| 103 | + |
| 104 | + for t, err := range session.Tools(ctx, nil) { |
| 105 | + if err != nil { |
| 106 | + log.Fatal(err) |
| 107 | + } |
| 108 | + schemaJSON, err := json.MarshalIndent(t.OutputSchema, "", "\t") |
| 109 | + if err != nil { |
| 110 | + log.Fatal(err) |
| 111 | + } |
| 112 | + fmt.Println(t.Name, string(schemaJSON)) |
| 113 | + } |
| 114 | + // Output: |
| 115 | + // nothing |
| 116 | +} |
| 117 | + |
| 118 | +func connect(ctx context.Context, server *mcp.Server) (*mcp.ClientSession, error) { |
| 119 | + t1, t2 := mcp.NewInMemoryTransports() |
| 120 | + if _, err := server.Connect(ctx, t1, nil); err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + client := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, nil) |
| 124 | + return client.Connect(ctx, t2, nil) |
| 125 | +} |
0 commit comments