@@ -35,6 +35,17 @@ type UserRequest struct {
35
35
UserID string `json:"userId" jsonschema_description:"User ID"`
36
36
}
37
37
38
+ type Asset struct {
39
+ ID string `json:"id" jsonschema_description:"Asset identifier"`
40
+ Name string `json:"name" jsonschema_description:"Asset name"`
41
+ Value float64 `json:"value" jsonschema_description:"Current value"`
42
+ Currency string `json:"currency" jsonschema_description:"Currency code"`
43
+ }
44
+
45
+ type AssetListRequest struct {
46
+ Limit int `json:"limit,omitempty" jsonschema_description:"Number of assets to return"`
47
+ }
48
+
38
49
func main () {
39
50
s := server .NewMCPServer (
40
51
"Structured Output Example" ,
@@ -59,7 +70,15 @@ func main() {
59
70
)
60
71
s .AddTool (userTool , mcp .NewStructuredToolHandler (getUserProfileHandler ))
61
72
62
- // Example 3: Manual result creation
73
+ // Example 3: Array output - direct array of objects
74
+ assetsTool := mcp .NewTool ("get_assets" ,
75
+ mcp .WithDescription ("Get list of assets as array" ),
76
+ mcp .WithOutputSchema [[]Asset ](),
77
+ mcp .WithNumber ("limit" , mcp .Min (1 ), mcp .Max (100 ), mcp .DefaultNumber (10 )),
78
+ )
79
+ s .AddTool (assetsTool , mcp .NewStructuredToolHandler (getAssetsHandler ))
80
+
81
+ // Example 4: Manual result creation
63
82
manualTool := mcp .NewTool ("manual_structured" ,
64
83
mcp .WithDescription ("Manual structured result" ),
65
84
mcp .WithOutputSchema [WeatherResponse ](),
@@ -96,6 +115,27 @@ func getUserProfileHandler(ctx context.Context, request mcp.CallToolRequest, arg
96
115
}, nil
97
116
}
98
117
118
+ func getAssetsHandler (ctx context.Context , request mcp.CallToolRequest , args AssetListRequest ) ([]Asset , error ) {
119
+ limit := args .Limit
120
+ if limit <= 0 {
121
+ limit = 10
122
+ }
123
+
124
+ assets := []Asset {
125
+ {ID : "btc" , Name : "Bitcoin" , Value : 45000.50 , Currency : "USD" },
126
+ {ID : "eth" , Name : "Ethereum" , Value : 3200.75 , Currency : "USD" },
127
+ {ID : "ada" , Name : "Cardano" , Value : 0.85 , Currency : "USD" },
128
+ {ID : "sol" , Name : "Solana" , Value : 125.30 , Currency : "USD" },
129
+ {ID : "dot" , Name : "Pottedot" , Value : 18.45 , Currency : "USD" },
130
+ }
131
+
132
+ if limit > len (assets ) {
133
+ limit = len (assets )
134
+ }
135
+
136
+ return assets [:limit ], nil
137
+ }
138
+
99
139
func manualWeatherHandler (ctx context.Context , request mcp.CallToolRequest , args WeatherRequest ) (* mcp.CallToolResult , error ) {
100
140
response := WeatherResponse {
101
141
Location : args .Location ,
0 commit comments