Skip to content

Commit 89ea80c

Browse files
sync
2 parents b312c37 + c509bf9 commit 89ea80c

File tree

9 files changed

+544
-151
lines changed

9 files changed

+544
-151
lines changed

README.md

Lines changed: 11 additions & 0 deletions
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
@@ -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>

client/client.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package client
33

44
import (
55
"context"
6+
"encoding/json"
7+
"fmt"
68

79
"github.com/mark3labs/mcp-go/mcp"
810
)
@@ -18,12 +20,25 @@ type MCPClient interface {
1820
// Ping checks if the server is alive
1921
Ping(ctx context.Context) error
2022

23+
// ListResourcesByPage manually list resources by page.
24+
ListResourcesByPage(
25+
ctx context.Context,
26+
request mcp.ListResourcesRequest,
27+
) (*mcp.ListResourcesResult, error)
28+
2129
// ListResources requests a list of available resources from the server
2230
ListResources(
2331
ctx context.Context,
2432
request mcp.ListResourcesRequest,
2533
) (*mcp.ListResourcesResult, error)
2634

35+
// ListResourceTemplatesByPage manually list resource templates by page.
36+
ListResourceTemplatesByPage(
37+
ctx context.Context,
38+
request mcp.ListResourceTemplatesRequest,
39+
) (*mcp.ListResourceTemplatesResult,
40+
error)
41+
2742
// ListResourceTemplates requests a list of available resource templates from the server
2843
ListResourceTemplates(
2944
ctx context.Context,
@@ -43,6 +58,12 @@ type MCPClient interface {
4358
// Unsubscribe cancels notifications for a specific resource
4459
Unsubscribe(ctx context.Context, request mcp.UnsubscribeRequest) error
4560

61+
// ListPromptsByPage manually list prompts by page.
62+
ListPromptsByPage(
63+
ctx context.Context,
64+
request mcp.ListPromptsRequest,
65+
) (*mcp.ListPromptsResult, error)
66+
4667
// ListPrompts requests a list of available prompts from the server
4768
ListPrompts(
4869
ctx context.Context,
@@ -55,6 +76,12 @@ type MCPClient interface {
5576
request mcp.GetPromptRequest,
5677
) (*mcp.GetPromptResult, error)
5778

79+
// ListToolsByPage manually list tools by page.
80+
ListToolsByPage(
81+
ctx context.Context,
82+
request mcp.ListToolsRequest,
83+
) (*mcp.ListToolsResult, error)
84+
5885
// ListTools requests a list of available tools from the server
5986
ListTools(
6087
ctx context.Context,
@@ -82,3 +109,26 @@ type MCPClient interface {
82109
// OnNotification registers a handler for notifications
83110
OnNotification(handler func(notification mcp.JSONRPCNotification))
84111
}
112+
113+
type mcpClient interface {
114+
MCPClient
115+
116+
sendRequest(ctx context.Context, method string, params interface{}) (*json.RawMessage, error)
117+
}
118+
119+
func listByPage[T any](
120+
ctx context.Context,
121+
client mcpClient,
122+
request mcp.PaginatedRequest,
123+
method string,
124+
) (*T, error) {
125+
response, err := client.sendRequest(ctx, method, request.Params)
126+
if err != nil {
127+
return nil, err
128+
}
129+
var result T
130+
if err := json.Unmarshal(*response, &result); err != nil {
131+
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
132+
}
133+
return &result, nil
134+
}

0 commit comments

Comments
 (0)