NewTool creates a ServerTool that retrieves ToolDependencies from context at call time. This avoids creating closures at registration time, which is important for performance in servers that create a new server instance per request (like the remote server). The handler function receives deps extrac
( toolset inventory.ToolsetMetadata, tool mcp.Tool, requiredScopes []scopes.Scope, handler func(ctx context.Context, deps ToolDependencies, req *mcp.CallToolRequest, args In) (*mcp.CallToolResult, Out, error), )
| 225 | // AcceptedScopes are automatically derived using the scope hierarchy (e.g., if |
| 226 | // public_repo is required, repo is also accepted since repo grants public_repo). |
| 227 | func NewTool[In, Out any]( |
| 228 | toolset inventory.ToolsetMetadata, |
| 229 | tool mcp.Tool, |
| 230 | requiredScopes []scopes.Scope, |
| 231 | handler func(ctx context.Context, deps ToolDependencies, req *mcp.CallToolRequest, args In) (*mcp.CallToolResult, Out, error), |
| 232 | ) inventory.ServerTool { |
| 233 | st := inventory.NewServerToolWithContextHandler(tool, toolset, func(ctx context.Context, req *mcp.CallToolRequest, args In) (*mcp.CallToolResult, Out, error) { |
| 234 | deps := MustDepsFromContext(ctx) |
| 235 | return handler(ctx, deps, req, args) |
| 236 | }) |
| 237 | st.RequiredScopes = scopes.ToStringSlice(requiredScopes...) |
| 238 | st.AcceptedScopes = scopes.ExpandScopes(requiredScopes...) |
| 239 | return st |
| 240 | } |
| 241 | |
| 242 | // NewToolFromHandler creates a ServerTool that retrieves ToolDependencies from context at call time. |
| 243 | // Use this when you have a handler that conforms to mcp.ToolHandler directly. |