mcpFromSDK adapts a toolsdk.Tool to go-mcp's server.ServerTool. It assumes that the tool responds with a valid JSON object.
(sdkTool toolsdk.GenericTool, tb toolsdk.Deps)
| 986 | // mcpFromSDK adapts a toolsdk.Tool to go-mcp's server.ServerTool. |
| 987 | // It assumes that the tool responds with a valid JSON object. |
| 988 | func mcpFromSDK(sdkTool toolsdk.GenericTool, tb toolsdk.Deps) server.ServerTool { |
| 989 | // NOTE: some clients will silently refuse to use tools if there is an issue |
| 990 | // with the tool's schema or configuration. |
| 991 | if sdkTool.Schema.Properties == nil { |
| 992 | panic("developer error: schema properties cannot be nil") |
| 993 | } |
| 994 | return server.ServerTool{ |
| 995 | Tool: mcp.Tool{ |
| 996 | Name: sdkTool.Tool.Name, |
| 997 | Description: sdkTool.Description, |
| 998 | InputSchema: mcp.ToolInputSchema{ |
| 999 | Type: "object", // Default of mcp.NewTool() |
| 1000 | Properties: sdkTool.Schema.Properties, |
| 1001 | Required: sdkTool.Schema.Required, |
| 1002 | }, |
| 1003 | Annotations: mcp.ToolAnnotation{ |
| 1004 | ReadOnlyHint: mcp.ToBoolPtr(sdkTool.MCPAnnotations.ReadOnlyHint), |
| 1005 | DestructiveHint: mcp.ToBoolPtr(sdkTool.MCPAnnotations.DestructiveHint), |
| 1006 | IdempotentHint: mcp.ToBoolPtr(sdkTool.MCPAnnotations.IdempotentHint), |
| 1007 | OpenWorldHint: mcp.ToBoolPtr(sdkTool.MCPAnnotations.OpenWorldHint), |
| 1008 | }, |
| 1009 | }, |
| 1010 | Handler: func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 1011 | var buf bytes.Buffer |
| 1012 | if err := json.NewEncoder(&buf).Encode(request.Params.Arguments); err != nil { |
| 1013 | return nil, xerrors.Errorf("failed to encode request arguments: %w", err) |
| 1014 | } |
| 1015 | result, err := sdkTool.Handler(ctx, tb, buf.Bytes()) |
| 1016 | if err != nil { |
| 1017 | return nil, err |
| 1018 | } |
| 1019 | return &mcp.CallToolResult{ |
| 1020 | Content: []mcp.Content{ |
| 1021 | mcp.NewTextContent(string(result)), |
| 1022 | }, |
| 1023 | }, nil |
| 1024 | }, |
| 1025 | } |
| 1026 | } |
no test coverage detected