|
| 1 | +package toolsets |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + |
| 7 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 8 | +) |
| 9 | + |
| 10 | +// HandlerFunc is a function that takes dependencies and returns an MCP tool handler. |
| 11 | +// This allows tools to be defined statically while their handlers are generated |
| 12 | +// on-demand with the appropriate dependencies. |
| 13 | +type HandlerFunc func(deps ToolDependencies) mcp.ToolHandler |
| 14 | + |
| 15 | +// ToolDependencies contains all dependencies that tool handlers might need. |
| 16 | +// Fields are pointers/interfaces so they can be nil when not needed by a specific tool. |
| 17 | +type ToolDependencies struct { |
| 18 | + // GetClient returns a GitHub REST API client |
| 19 | + GetClient any // func(context.Context) (*github.Client, error) |
| 20 | + |
| 21 | + // GetGQLClient returns a GitHub GraphQL client |
| 22 | + GetGQLClient any // func(context.Context) (*githubv4.Client, error) |
| 23 | + |
| 24 | + // GetRawClient returns a raw HTTP client for GitHub |
| 25 | + GetRawClient any // raw.GetRawClientFn |
| 26 | + |
| 27 | + // RepoAccessCache is the lockdown mode repo access cache |
| 28 | + RepoAccessCache any // *lockdown.RepoAccessCache |
| 29 | + |
| 30 | + // T is the translation helper function |
| 31 | + T any // translations.TranslationHelperFunc |
| 32 | + |
| 33 | + // Flags are feature flags |
| 34 | + Flags any // FeatureFlags |
| 35 | + |
| 36 | + // ContentWindowSize is the size of the content window for log truncation |
| 37 | + ContentWindowSize int |
| 38 | +} |
| 39 | + |
| 40 | +// ServerTool represents an MCP tool with a handler generator function. |
| 41 | +// The tool definition is static, while the handler is generated on-demand |
| 42 | +// when the tool is registered with a server. |
| 43 | +type ServerTool struct { |
| 44 | + // Tool is the MCP tool definition containing name, description, schema, etc. |
| 45 | + Tool mcp.Tool |
| 46 | + |
| 47 | + // HandlerFunc generates the handler when given dependencies. |
| 48 | + // This allows tools to be passed around without handlers being set up, |
| 49 | + // and handlers are only created when needed. |
| 50 | + HandlerFunc HandlerFunc |
| 51 | +} |
| 52 | + |
| 53 | +// Handler returns a tool handler by calling HandlerFunc with the given dependencies. |
| 54 | +func (st *ServerTool) Handler(deps ToolDependencies) mcp.ToolHandler { |
| 55 | + if st.HandlerFunc == nil { |
| 56 | + return nil |
| 57 | + } |
| 58 | + return st.HandlerFunc(deps) |
| 59 | +} |
| 60 | + |
| 61 | +// RegisterFunc registers the tool with the server using the provided dependencies. |
| 62 | +func (st *ServerTool) RegisterFunc(s *mcp.Server, deps ToolDependencies) { |
| 63 | + handler := st.Handler(deps) |
| 64 | + s.AddTool(&st.Tool, handler) |
| 65 | +} |
| 66 | + |
| 67 | +// NewServerTool creates a ServerTool from a tool definition and a typed handler function. |
| 68 | +// The handler function takes dependencies and returns a typed handler. |
| 69 | +func NewServerTool[In any, Out any](tool mcp.Tool, handlerFn func(deps ToolDependencies) mcp.ToolHandlerFor[In, Out]) ServerTool { |
| 70 | + return ServerTool{ |
| 71 | + Tool: tool, |
| 72 | + HandlerFunc: func(deps ToolDependencies) mcp.ToolHandler { |
| 73 | + typedHandler := handlerFn(deps) |
| 74 | + return func(ctx context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 75 | + var arguments In |
| 76 | + if err := json.Unmarshal(req.Params.Arguments, &arguments); err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + resp, _, err := typedHandler(ctx, req, arguments) |
| 80 | + return resp, err |
| 81 | + } |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// NewServerToolFromHandler creates a ServerTool from a tool definition and a raw handler function. |
| 87 | +// Use this when you have a handler that already conforms to mcp.ToolHandler. |
| 88 | +func NewServerToolFromHandler(tool mcp.Tool, handlerFn func(deps ToolDependencies) mcp.ToolHandler) ServerTool { |
| 89 | + return ServerTool{Tool: tool, HandlerFunc: handlerFn} |
| 90 | +} |
| 91 | + |
| 92 | +// NewServerToolLegacy creates a ServerTool from a tool definition and an already-bound typed handler. |
| 93 | +// This is for backward compatibility during the refactor - the handler doesn't use ToolDependencies. |
| 94 | +// Deprecated: Use NewServerTool instead for new code. |
| 95 | +func NewServerToolLegacy[In any, Out any](tool mcp.Tool, handler mcp.ToolHandlerFor[In, Out]) ServerTool { |
| 96 | + return ServerTool{ |
| 97 | + Tool: tool, |
| 98 | + HandlerFunc: func(_ ToolDependencies) mcp.ToolHandler { |
| 99 | + return func(ctx context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 100 | + var arguments In |
| 101 | + if err := json.Unmarshal(req.Params.Arguments, &arguments); err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + resp, _, err := handler(ctx, req, arguments) |
| 105 | + return resp, err |
| 106 | + } |
| 107 | + }, |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// NewServerToolFromHandlerLegacy creates a ServerTool from a tool definition and an already-bound raw handler. |
| 112 | +// This is for backward compatibility during the refactor - the handler doesn't use ToolDependencies. |
| 113 | +// Deprecated: Use NewServerToolFromHandler instead for new code. |
| 114 | +func NewServerToolFromHandlerLegacy(tool mcp.Tool, handler mcp.ToolHandler) ServerTool { |
| 115 | + return ServerTool{ |
| 116 | + Tool: tool, |
| 117 | + HandlerFunc: func(_ ToolDependencies) mcp.ToolHandler { |
| 118 | + return handler |
| 119 | + }, |
| 120 | + } |
| 121 | +} |
0 commit comments