|
12 | 12 |
|
13 | 13 | [](http://www.youtube.com/watch?v=qoaeYMrXJH0 "Tutorial") |
14 | 14 |
|
| 15 | +<br> |
| 16 | + |
| 17 | +Discuss the SDK on [Discord](https://discord.gg/RqSS2NQVsY) |
| 18 | + |
15 | 19 | </div> |
16 | 20 |
|
17 | 21 | ```go |
@@ -122,6 +126,7 @@ func main() { |
122 | 126 | "1.0.0", |
123 | 127 | server.WithResourceCapabilities(true, true), |
124 | 128 | server.WithLogging(), |
| 129 | + server.WithRecovery(), |
125 | 130 | ) |
126 | 131 |
|
127 | 132 | // Add a calculator tool |
@@ -158,7 +163,7 @@ func main() { |
158 | 163 | result = x * y |
159 | 164 | case "divide": |
160 | 165 | if y == 0 { |
161 | | - return nil, errors.New("Cannot divide by zero") |
| 166 | + return mcp.NewToolResultError("cannot divide by zero"), nil |
162 | 167 | } |
163 | 168 | result = x / y |
164 | 169 | } |
@@ -320,7 +325,7 @@ s.AddTool(calculatorTool, func(ctx context.Context, request mcp.CallToolRequest) |
320 | 325 | result = x * y |
321 | 326 | case "divide": |
322 | 327 | if y == 0 { |
323 | | - return nil, errors.New("Division by zero is not allowed") |
| 328 | + return mcp.NewToolResultError("cannot divide by zero"), nil |
324 | 329 | } |
325 | 330 | result = x / y |
326 | 331 | } |
@@ -365,20 +370,20 @@ s.AddTool(httpTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp |
365 | 370 | req, err = http.NewRequest(method, url, nil) |
366 | 371 | } |
367 | 372 | if err != nil { |
368 | | - return nil, fmt.Errorf("Failed to create request: %v", err) |
| 373 | + return mcp.NewToolResultErrorFromErr("unable to create request", err), nil |
369 | 374 | } |
370 | 375 |
|
371 | 376 | client := &http.Client{} |
372 | 377 | resp, err := client.Do(req) |
373 | 378 | if err != nil { |
374 | | - return nil, fmt.Errorf("Request failed: %v", err) |
| 379 | + return mcp.NewToolResultErrorFromErr("unable to execute request", err), nil |
375 | 380 | } |
376 | 381 | defer resp.Body.Close() |
377 | 382 |
|
378 | 383 | // Return response |
379 | 384 | respBody, err := io.ReadAll(resp.Body) |
380 | 385 | if err != nil { |
381 | | - return nil, fmt.Errorf("Failed to read response: %v", err) |
| 386 | + return mcp.NewToolResultErrorFromErr("unable to read request response", err), nil |
382 | 387 | } |
383 | 388 |
|
384 | 389 | return mcp.NewToolResultText(fmt.Sprintf("Status: %d\nBody: %s", resp.StatusCode, string(respBody))), nil |
@@ -522,6 +527,12 @@ initialization. |
522 | 527 | Add the `Hooks` to the server at the time of creation using the |
523 | 528 | `server.WithHooks` option. |
524 | 529 |
|
| 530 | +### Tool Handler Middleware |
| 531 | + |
| 532 | +Add middleware to tool call handlers using the `server.WithToolHandlerMiddleware` option. Middlewares can be registered on server creation and are applied on every tool call. |
| 533 | + |
| 534 | +A recovery middleware option is available to recover from panics in a tool call and can be added to the server with the `server.WithRecovery` option. |
| 535 | + |
525 | 536 | ## Contributing |
526 | 537 |
|
527 | 538 | <details> |
|
0 commit comments