|
3 | 3 |
|
4 | 4 | package adapter |
5 | 5 |
|
6 | | -import ( |
7 | | - "context" |
8 | | - "encoding/json" |
9 | | - "fmt" |
10 | | - |
11 | | - "github.com/mark3labs/mcp-go/mcp" |
12 | | - "github.com/mark3labs/mcp-go/server" |
13 | | - |
14 | | - "github.com/stacklok/toolhive/pkg/vmcp/optimizer" |
15 | | - "github.com/stacklok/toolhive/pkg/vmcp/schema" |
16 | | -) |
17 | | - |
18 | | -// OptimizerToolNames defines the tool names exposed when optimizer is enabled. |
| 6 | +// OptimizerToolNames defines the tool names exposed when optimizer mode is enabled. |
| 7 | +// These constants are kept here for backwards compatibility with existing tests and |
| 8 | +// callers. The actual tool implementation lives in the optimizerdec session decorator. |
19 | 9 | const ( |
20 | 10 | FindToolName = "find_tool" |
21 | 11 | CallToolName = "call_tool" |
22 | 12 | ) |
23 | | - |
24 | | -// Pre-generated schemas for optimizer tools. |
25 | | -// Generated at package init time so any schema errors panic at startup. |
26 | | -var ( |
27 | | - findToolInputSchema = mustGenerateSchema[optimizer.FindToolInput]() |
28 | | - callToolInputSchema = mustGenerateSchema[optimizer.CallToolInput]() |
29 | | -) |
30 | | - |
31 | | -// CreateOptimizerTools creates the SDK tools for optimizer mode. |
32 | | -// When optimizer is enabled, only these two tools are exposed to clients |
33 | | -// instead of all backend tools. |
34 | | -func CreateOptimizerTools(opt optimizer.Optimizer) []server.ServerTool { |
35 | | - return []server.ServerTool{ |
36 | | - { |
37 | | - Tool: mcp.Tool{ |
38 | | - Name: FindToolName, |
39 | | - Description: "Find and return tools that can help accomplish the user's request. " + |
40 | | - "This searches available MCP server tools using semantic and keyword-based matching. " + |
41 | | - "Use this function when you need to: " + |
42 | | - "(1) discover what tools are available for a specific task, " + |
43 | | - "(2) find the right tool(s) before attempting to solve a problem, " + |
44 | | - "(3) check if required functionality exists in the current environment. " + |
45 | | - "Returns matching tools ranked by relevance including their names, descriptions, " + |
46 | | - "required parameters and schemas, plus token efficiency metrics showing " + |
47 | | - "baseline_tokens, returned_tokens, and savings_percent. " + |
48 | | - "Example: for 'Find good restaurants in San Jose', call with " + |
49 | | - "tool_description='search the web' and tool_keywords='web search restaurants'. " + |
50 | | - "Always call this before call_tool to discover the correct tool name and parameter schema.", |
51 | | - RawInputSchema: findToolInputSchema, |
52 | | - }, |
53 | | - Handler: createFindToolHandler(opt), |
54 | | - }, |
55 | | - { |
56 | | - Tool: mcp.Tool{ |
57 | | - Name: CallToolName, |
58 | | - Description: "Execute a specific tool with the provided parameters. " + |
59 | | - "Use this function to: " + |
60 | | - "(1) run a tool after identifying it with find_tool, " + |
61 | | - "(2) execute operations that require specific MCP server functionality, " + |
62 | | - "(3) perform actions that go beyond your built-in capabilities. " + |
63 | | - "Important: always use find_tool first to get the correct tool_name " + |
64 | | - "and parameter schema before calling this function. " + |
65 | | - "The parameters must match the tool's input schema as returned by find_tool. " + |
66 | | - "Returns the tool's execution result which may include success/failure status, " + |
67 | | - "result data or content, and error messages if execution failed.", |
68 | | - RawInputSchema: callToolInputSchema, |
69 | | - }, |
70 | | - Handler: createCallToolHandler(opt), |
71 | | - }, |
72 | | - } |
73 | | -} |
74 | | - |
75 | | -// createFindToolHandler creates a handler for the find_tool optimizer operation. |
76 | | -func createFindToolHandler(opt optimizer.Optimizer) func(context.Context, mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
77 | | - return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
78 | | - input, err := schema.Translate[optimizer.FindToolInput](request.Params.Arguments) |
79 | | - if err != nil { |
80 | | - return mcp.NewToolResultError(fmt.Sprintf("invalid arguments: %v", err)), nil |
81 | | - } |
82 | | - |
83 | | - output, err := opt.FindTool(ctx, input) |
84 | | - if err != nil { |
85 | | - return mcp.NewToolResultError(fmt.Sprintf("find_tool failed: %v", err)), nil |
86 | | - } |
87 | | - |
88 | | - return mcp.NewToolResultStructuredOnly(output), nil |
89 | | - } |
90 | | -} |
91 | | - |
92 | | -// createCallToolHandler creates a handler for the call_tool optimizer operation. |
93 | | -func createCallToolHandler(opt optimizer.Optimizer) func(context.Context, mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
94 | | - return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
95 | | - input, err := schema.Translate[optimizer.CallToolInput](request.Params.Arguments) |
96 | | - if err != nil { |
97 | | - return mcp.NewToolResultError(fmt.Sprintf("invalid arguments: %v", err)), nil |
98 | | - } |
99 | | - |
100 | | - result, err := opt.CallTool(ctx, input) |
101 | | - if err != nil { |
102 | | - // Exposing the error to the MCP client is important if you want it to correct its behavior. |
103 | | - // Without information on the failure, the model is pretty much hopeless in figuring out the problem. |
104 | | - return mcp.NewToolResultError(fmt.Sprintf("call_tool failed: %v", err)), nil |
105 | | - } |
106 | | - |
107 | | - return result, nil |
108 | | - } |
109 | | -} |
110 | | - |
111 | | -// mustMarshalSchema marshals a schema to JSON, panicking on error. |
112 | | -// This is safe because schemas are generated from known types at startup. |
113 | | -// This should NOT be called by runtime code. |
114 | | -func mustGenerateSchema[T any]() json.RawMessage { |
115 | | - s, err := schema.GenerateSchema[T]() |
116 | | - if err != nil { |
117 | | - panic(fmt.Sprintf("failed to generate schema: %v", err)) |
118 | | - } |
119 | | - |
120 | | - data, err := json.Marshal(s) |
121 | | - if err != nil { |
122 | | - panic(fmt.Sprintf("failed to marshal schema: %v", err)) |
123 | | - } |
124 | | - |
125 | | - return data |
126 | | -} |
0 commit comments