MCPcopy
hub / github.com/github/github-mcp-server / NewServerToolWithContextHandler

Function NewServerToolWithContextHandler

pkg/inventory/server_tool.go:128–149  ·  view source on GitHub ↗

NewServerToolWithContextHandler creates a ServerTool with a handler that receives deps via context. This is the preferred approach for tools because it doesn't create closures at registration time, which is critical for performance in servers that create a new instance per request. The handler func

(tool mcp.Tool, toolset ToolsetMetadata, handler mcp.ToolHandlerFor[In, Out])

Source from the content-addressed store, hash-verified

126// The handler function is stored directly without wrapping in a deps closure.
127// Dependencies should be injected into context before calling tool handlers.
128func NewServerToolWithContextHandler[In any, Out any](tool mcp.Tool, toolset ToolsetMetadata, handler mcp.ToolHandlerFor[In, Out]) ServerTool {
129 return ServerTool{
130 Tool: tool,
131 Toolset: toolset,
132 // HandlerFunc ignores deps - deps are retrieved from context at call time
133 HandlerFunc: func(_ any) mcp.ToolHandler {
134 return func(ctx context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) {
135 var arguments In
136 if err := json.Unmarshal(req.Params.Arguments, &arguments); err != nil {
137 return &mcp.CallToolResult{
138 Content: []mcp.Content{
139 &mcp.TextContent{Text: fmt.Sprintf("invalid arguments: %s", err)},
140 },
141 IsError: true,
142 }, nil
143 }
144 resp, _, err := handler(ctx, req, arguments)
145 return resp, err
146 }
147 },
148 }
149}
150
151// NewServerTool creates a ServerTool with a raw handler that receives deps via context.
152// This is the preferred constructor for tools that use mcp.ToolHandler directly because

Calls

no outgoing calls