Skip to content

Commit 5b022ba

Browse files
refactor: simplify NewServerTool naming
- Rename NewServerToolFromHandler -> NewServerTool (the simpler, preferred name) - Rename deprecated generic NewServerTool[In, Out] -> NewServerToolWithDeps to free up the simpler name and make its closure-based nature explicit - Remove unused NewServerToolWithRawContextHandler (wrapper that was never adopted by callers; raw context handlers go through NewServerTool directly) - Update all call sites in dependencies.go, dynamic_tools.go, and registry_test.go Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f21dcd3 commit 5b022ba

4 files changed

Lines changed: 13 additions & 31 deletions

File tree

pkg/github/dependencies.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,11 @@ func NewToolFromHandler(
253253
requiredScopes []scopes.Scope,
254254
handler func(ctx context.Context, deps ToolDependencies, req *mcp.CallToolRequest) (*mcp.CallToolResult, error),
255255
) inventory.ServerTool {
256-
st := inventory.NewServerToolWithRawContextHandler(tool, toolset, func(ctx context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) {
257-
deps := MustDepsFromContext(ctx)
258-
return handler(ctx, deps, req)
256+
st := inventory.NewServerTool(tool, toolset, func(_ any) mcp.ToolHandler {
257+
return func(ctx context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) {
258+
deps := MustDepsFromContext(ctx)
259+
return handler(ctx, deps, req)
260+
}
259261
})
260262
st.RequiredScopes = scopes.ToStringSlice(requiredScopes...)
261263
st.AcceptedScopes = scopes.ExpandScopes(requiredScopes...)

pkg/github/dynamic_tools.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type DynamicToolDependencies struct {
3131
// tools (ToolDependencies), so they intentionally use the closure pattern.
3232
func NewDynamicTool(toolset inventory.ToolsetMetadata, tool mcp.Tool, handler func(deps DynamicToolDependencies) mcp.ToolHandlerFor[map[string]any, any]) inventory.ServerTool {
3333
//nolint:staticcheck // SA1019: Dynamic tools use a different deps structure, closure pattern is intentional
34-
return inventory.NewServerTool(tool, toolset, func(d any) mcp.ToolHandlerFor[map[string]any, any] {
34+
return inventory.NewServerToolWithDeps(tool, toolset, func(d any) mcp.ToolHandlerFor[map[string]any, any] {
3535
return handler(d.(DynamicToolDependencies))
3636
})
3737
}

pkg/inventory/registry_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func testToolsetMetadataWithDefault(id string, isDefault bool) ToolsetMetadata {
3838

3939
// mockToolWithDefault creates a mock tool with a default toolset flag
4040
func mockToolWithDefault(name string, toolsetID string, readOnly bool, isDefault bool) ServerTool {
41-
return NewServerToolFromHandler(
41+
return NewServerTool(
4242
mcp.Tool{
4343
Name: name,
4444
Annotations: &mcp.ToolAnnotations{
@@ -57,7 +57,7 @@ func mockToolWithDefault(name string, toolsetID string, readOnly bool, isDefault
5757

5858
// mockTool creates a minimal ServerTool for testing
5959
func mockTool(name string, toolsetID string, readOnly bool) ServerTool {
60-
return NewServerToolFromHandler(
60+
return NewServerTool(
6161
mcp.Tool{
6262
Name: name,
6363
Annotations: &mcp.ToolAnnotations{
@@ -1839,7 +1839,7 @@ func TestWithTools_DeprecatedAliasAndFeatureFlag(t *testing.T) {
18391839

18401840
// mockToolWithMeta creates a ServerTool with Meta for testing insiders mode
18411841
func mockToolWithMeta(name string, toolsetID string, meta map[string]any) ServerTool {
1842-
return NewServerToolFromHandler(
1842+
return NewServerTool(
18431843
mcp.Tool{
18441844
Name: name,
18451845
Annotations: &mcp.ToolAnnotations{

pkg/inventory/server_tool.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ func (st *ServerTool) RegisterFunc(s *mcp.Server, deps any) {
118118
s.AddTool(&toolCopy, handler)
119119
}
120120

121-
// NewServerTool creates a ServerTool from a tool definition, toolset metadata, and a typed handler function.
121+
// NewServerToolWithDeps creates a ServerTool from a tool definition, toolset metadata, and a typed handler function.
122122
// The handler function takes dependencies (as any) and returns a typed handler.
123123
// Callers should type-assert deps to their typed dependencies struct.
124124
//
125125
// Deprecated: This creates closures at registration time. For better performance in
126126
// per-request server scenarios, use NewServerToolWithContextHandler instead.
127-
func NewServerTool[In any, Out any](tool mcp.Tool, toolset ToolsetMetadata, handlerFn func(deps any) mcp.ToolHandlerFor[In, Out]) ServerTool {
127+
func NewServerToolWithDeps[In any, Out any](tool mcp.Tool, toolset ToolsetMetadata, handlerFn func(deps any) mcp.ToolHandlerFor[In, Out]) ServerTool {
128128
return ServerTool{
129129
Tool: tool,
130130
Toolset: toolset,
@@ -166,28 +166,8 @@ func NewServerToolWithContextHandler[In any, Out any](tool mcp.Tool, toolset Too
166166
}
167167
}
168168

169-
// NewServerToolFromHandler creates a ServerTool from a tool definition, toolset metadata, and a raw handler function.
169+
// NewServerTool creates a ServerTool from a tool definition, toolset metadata, and a raw handler function.
170170
// Use this when you have a handler that already conforms to mcp.ToolHandler.
171-
//
172-
// Deprecated: This creates closures at registration time. For better performance in
173-
// per-request server scenarios, use NewServerToolWithRawContextHandler instead.
174-
func NewServerToolFromHandler(tool mcp.Tool, toolset ToolsetMetadata, handlerFn func(deps any) mcp.ToolHandler) ServerTool {
171+
func NewServerTool(tool mcp.Tool, toolset ToolsetMetadata, handlerFn func(deps any) mcp.ToolHandler) ServerTool {
175172
return ServerTool{Tool: tool, Toolset: toolset, HandlerFunc: handlerFn}
176173
}
177-
178-
// NewServerToolWithRawContextHandler creates a ServerTool with a raw handler that receives deps via context.
179-
// This is the preferred approach for tools that use mcp.ToolHandler directly because it doesn't
180-
// create closures at registration time.
181-
//
182-
// The handler function is stored directly without wrapping in a deps closure.
183-
// Dependencies should be injected into context before calling tool handlers.
184-
func NewServerToolWithRawContextHandler(tool mcp.Tool, toolset ToolsetMetadata, handler mcp.ToolHandler) ServerTool {
185-
return ServerTool{
186-
Tool: tool,
187-
Toolset: toolset,
188-
// HandlerFunc ignores deps - deps are retrieved from context at call time
189-
HandlerFunc: func(_ any) mcp.ToolHandler {
190-
return handler
191-
},
192-
}
193-
}

0 commit comments

Comments
 (0)