Skip to content

Commit 8678fab

Browse files
committed
Add tool handler shim to use server.AddTool instead of mcp.AddTool
1 parent fa2d802 commit 8678fab

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

pkg/toolsets/toolsets.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package toolsets
22

33
import (
4+
"context"
5+
"encoding/json"
46
"fmt"
57
"os"
68
"strings"
@@ -35,9 +37,23 @@ type ServerTool struct {
3537
RegisterFunc func(s *mcp.Server)
3638
}
3739

38-
func NewServerTool[In, Out any](tool mcp.Tool, handler mcp.ToolHandlerFor[In, Out]) ServerTool {
40+
func NewServerTool[In any, Out any](tool mcp.Tool, handler mcp.ToolHandlerFor[In, Out]) ServerTool {
3941
return ServerTool{Tool: tool, RegisterFunc: func(s *mcp.Server) {
40-
mcp.AddTool(s, &tool, handler)
42+
th := func(ctx context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) {
43+
var arguments In
44+
if err := json.Unmarshal(req.Params.Arguments, &arguments); err != nil {
45+
return nil, err
46+
}
47+
48+
resp, _, err := handler(ctx, req, arguments)
49+
if err != nil {
50+
return nil, err
51+
}
52+
53+
return resp, err
54+
}
55+
56+
s.AddTool(&tool, th)
4157
}}
4258
}
4359

0 commit comments

Comments
 (0)