Skip to content

Commit af618a0

Browse files
committed
Merge remote-tracking branch 'upstream/main'
2 parents b14de59 + 33c98f1 commit af618a0

39 files changed

+4789
-1421
lines changed

README.md

+16-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
[![Tutorial](http://img.youtube.com/vi/qoaeYMrXJH0/0.jpg)](http://www.youtube.com/watch?v=qoaeYMrXJH0 "Tutorial")
1414

15+
<br>
16+
17+
Discuss the SDK on [Discord](https://discord.gg/RqSS2NQVsY)
18+
1519
</div>
1620

1721
```go
@@ -122,6 +126,7 @@ func main() {
122126
"1.0.0",
123127
server.WithResourceCapabilities(true, true),
124128
server.WithLogging(),
129+
server.WithRecovery(),
125130
)
126131

127132
// Add a calculator tool
@@ -158,7 +163,7 @@ func main() {
158163
result = x * y
159164
case "divide":
160165
if y == 0 {
161-
return nil, errors.New("Cannot divide by zero")
166+
return mcp.NewToolResultError("cannot divide by zero"), nil
162167
}
163168
result = x / y
164169
}
@@ -320,7 +325,7 @@ s.AddTool(calculatorTool, func(ctx context.Context, request mcp.CallToolRequest)
320325
result = x * y
321326
case "divide":
322327
if y == 0 {
323-
return nil, errors.New("Division by zero is not allowed")
328+
return mcp.NewToolResultError("cannot divide by zero"), nil
324329
}
325330
result = x / y
326331
}
@@ -365,20 +370,20 @@ s.AddTool(httpTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp
365370
req, err = http.NewRequest(method, url, nil)
366371
}
367372
if err != nil {
368-
return nil, fmt.Errorf("Failed to create request: %v", err)
373+
return mcp.NewToolResultErrorFromErr("unable to create request", err), nil
369374
}
370375

371376
client := &http.Client{}
372377
resp, err := client.Do(req)
373378
if err != nil {
374-
return nil, fmt.Errorf("Request failed: %v", err)
379+
return mcp.NewToolResultErrorFromErr("unable to execute request", err), nil
375380
}
376381
defer resp.Body.Close()
377382

378383
// Return response
379384
respBody, err := io.ReadAll(resp.Body)
380385
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
382387
}
383388

384389
return mcp.NewToolResultText(fmt.Sprintf("Status: %d\nBody: %s", resp.StatusCode, string(respBody))), nil
@@ -522,6 +527,12 @@ initialization.
522527
Add the `Hooks` to the server at the time of creation using the
523528
`server.WithHooks` option.
524529

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+
525536
## Contributing
526537

527538
<details>

0 commit comments

Comments
 (0)