@@ -12,8 +12,8 @@ import (
12
12
// Note: The jsonschema_description tag is added to the JSON schema as description
13
13
// Ideally use better descriptions, this is just an example
14
14
type WeatherRequest struct {
15
- Location string `json:"location" jsonschema_description:"City or location"`
16
- Units string `json:"units,omitempty" jsonschema_description:"celsius or fahrenheit"`
15
+ Location string `json:"location,required " jsonschema_description:"City or location"`
16
+ Units string `json:"units,omitempty" jsonschema_description:"celsius or fahrenheit" jsonschema:"enum=celsius,enum=fahrenheit" `
17
17
}
18
18
19
19
type WeatherResponse struct {
@@ -32,7 +32,7 @@ type UserProfile struct {
32
32
}
33
33
34
34
type UserRequest struct {
35
- UserID string `json:"userId" jsonschema_description:"User ID"`
35
+ UserID string `json:"userId,required " jsonschema_description:"User ID"`
36
36
}
37
37
38
38
type Asset struct {
@@ -43,46 +43,45 @@ type Asset struct {
43
43
}
44
44
45
45
type AssetListRequest struct {
46
- Limit int `json:"limit,omitempty" jsonschema_description:"Number of assets to return"`
46
+ Limit int `json:"limit,omitempty" jsonschema_description:"Number of assets to return" jsonschema:"minimum=1,maximum=100,default=10" `
47
47
}
48
48
49
49
func main () {
50
50
s := server .NewMCPServer (
51
- "Structured Output Example" ,
51
+ "Structured Input/ Output Example" ,
52
52
"1.0.0" ,
53
53
server .WithToolCapabilities (false ),
54
54
)
55
55
56
56
// Example 1: Auto-generated schema from struct
57
57
weatherTool := mcp .NewTool ("get_weather" ,
58
58
mcp .WithDescription ("Get weather with structured output" ),
59
+ mcp .WithInputSchema [WeatherRequest ](),
59
60
mcp .WithOutputSchema [WeatherResponse ](),
60
- mcp .WithString ("location" , mcp .Required ()),
61
- mcp .WithString ("units" , mcp .Enum ("celsius" , "fahrenheit" ), mcp .DefaultString ("celsius" )),
62
61
)
63
62
s .AddTool (weatherTool , mcp .NewStructuredToolHandler (getWeatherHandler ))
64
63
65
64
// Example 2: Nested struct schema
66
65
userTool := mcp .NewTool ("get_user_profile" ,
67
66
mcp .WithDescription ("Get user profile" ),
67
+ mcp .WithInputSchema [UserRequest ](),
68
68
mcp .WithOutputSchema [UserProfile ](),
69
- mcp .WithString ("userId" , mcp .Required ()),
70
69
)
71
70
s .AddTool (userTool , mcp .NewStructuredToolHandler (getUserProfileHandler ))
72
71
73
72
// Example 3: Array output - direct array of objects
74
73
assetsTool := mcp .NewTool ("get_assets" ,
75
74
mcp .WithDescription ("Get list of assets as array" ),
75
+ mcp .WithInputSchema [AssetListRequest ](),
76
76
mcp .WithOutputSchema [[]Asset ](),
77
- mcp .WithNumber ("limit" , mcp .Min (1 ), mcp .Max (100 ), mcp .DefaultNumber (10 )),
78
77
)
79
78
s .AddTool (assetsTool , mcp .NewStructuredToolHandler (getAssetsHandler ))
80
79
81
80
// Example 4: Manual result creation
82
81
manualTool := mcp .NewTool ("manual_structured" ,
83
82
mcp .WithDescription ("Manual structured result" ),
83
+ mcp .WithInputSchema [WeatherRequest ](),
84
84
mcp .WithOutputSchema [WeatherResponse ](),
85
- mcp .WithString ("location" , mcp .Required ()),
86
85
)
87
86
s .AddTool (manualTool , mcp .NewTypedToolHandler (manualWeatherHandler ))
88
87
0 commit comments