|
25 | 25 | ---@param has_function_calling boolean |
26 | 26 | ---@param opts MCPHub.Extensions.CodeCompanionConfig |
27 | 27 | local function create_static_handler(action_name, has_function_calling, opts) |
28 | | - ---@param agent CodeCompanion.Agent The Editor tool |
29 | | - ---@param args MCPHub.ToolCallArgs | MCPHub.ResourceAccessArgs The arguments from the LLM's tool call |
30 | | - ---@param output_handler function Callback for asynchronous calls |
| 28 | + ---@param self CodeCompanion.Tools The tools coordinator |
| 29 | + ---@param action MCPHub.ToolCallArgs | MCPHub.ResourceAccessArgs The arguments from the LLM's tool call |
| 30 | + ---@param cmd_opts { input?: any, output_cb: function } Options including the output callback |
31 | 31 | ---@return nil|{ status: "success"|"error", data: string } |
32 | | - return function(agent, args, _, output_handler) |
| 32 | + return function(self, action, cmd_opts) |
33 | 33 | local context = { |
34 | 34 | tool_display_name = action_name, |
35 | 35 | is_individual_tool = false, |
36 | 36 | action = action_name, |
37 | 37 | } |
38 | | - core.execute_mcp_tool(args, agent, output_handler, context) |
| 38 | + core.execute_mcp_tool(action, self, cmd_opts.output_cb, context) |
39 | 39 | end |
40 | 40 | end |
41 | 41 |
|
|
50 | 50 | ---@param namespaced_name string Namespaced tool name (safe_server_name__safe_tool_name) |
51 | 51 | ---@return function |
52 | 52 | local function create_individual_tool_handler(server_name, tool_name, namespaced_name) |
53 | | - ---@param agent CodeCompanion.Agent The Editor tool |
54 | | - ---@param args MCPHub.ToolCallArgs |
55 | | - ---@param output_handler function Callback for asynchronous calls |
56 | | - return function(agent, args, _, output_handler) |
| 53 | + ---@param self CodeCompanion.Tools The tools coordinator |
| 54 | + ---@param action MCPHub.ToolCallArgs The arguments from the LLM's tool call |
| 55 | + ---@param cmd_opts { input?: any, output_cb: function } Options including the output callback |
| 56 | + return function(self, action, cmd_opts) |
57 | 57 | local params = { |
58 | 58 | server_name = server_name, |
59 | 59 | tool_name = tool_name, |
60 | | - tool_input = args, |
| 60 | + tool_input = action, |
61 | 61 | } |
62 | 62 | ---@type MCPHub.ToolCallContext |
63 | 63 | local context = { |
64 | 64 | tool_display_name = namespaced_name, |
65 | 65 | is_individual_tool = true, |
66 | 66 | action = "use_mcp_tool", |
67 | 67 | } |
68 | | - core.execute_mcp_tool(params, agent, output_handler, context) |
| 68 | + core.execute_mcp_tool(params, self, cmd_opts.output_cb, context) |
69 | 69 | end |
70 | 70 | end |
71 | 71 |
|
@@ -137,7 +137,7 @@ function M.create_static_tools(opts) |
137 | 137 | id = "mcp_static:mcp", |
138 | 138 | description = " Call tools and resources from MCP servers with:\n\n - `use_mcp_tool`\n - `access_mcp_resource`\n", |
139 | 139 | hide_in_help_window = false, |
140 | | - system_prompt = function(_) |
| 140 | + system_prompt = function(group_config, ctx) |
141 | 141 | local hub = require("mcphub").get_hub_instance() |
142 | 142 | if not hub then |
143 | 143 | vim.notify("MCP Hub is not initialized", vim.log.levels.WARN) |
@@ -170,15 +170,21 @@ function M.create_static_tools(opts) |
170 | 170 | hide_in_help_window = true, |
171 | 171 | visible = false, |
172 | 172 | ---@class MCPHub.Extensions.CodeCompanionTool: CodeCompanion.Agent.Tool |
173 | | - callback = { |
174 | | - name = action_name, |
175 | | - cmds = { create_static_handler(action_name, has_function_calling, opts) }, |
176 | | - system_prompt = function() |
177 | | - return string.format("You can use the %s tool to %s\n", action_name, schema["function"].description) |
178 | | - end, |
179 | | - output = core.create_output_handlers(action_name, has_function_calling, opts), |
180 | | - schema = schema, |
181 | | - }, |
| 173 | + callback = function() |
| 174 | + return { |
| 175 | + name = action_name, |
| 176 | + cmds = { create_static_handler(action_name, has_function_calling, opts) }, |
| 177 | + system_prompt = function(group_config, ctx) |
| 178 | + return string.format( |
| 179 | + "You can use the %s tool to %s\n", |
| 180 | + action_name, |
| 181 | + schema["function"].description |
| 182 | + ) |
| 183 | + end, |
| 184 | + output = core.create_output_handlers(action_name, has_function_calling, opts), |
| 185 | + schema = schema, |
| 186 | + } |
| 187 | + end, |
182 | 188 | } |
183 | 189 | table.insert(tools.groups.mcp.tools, action_name) |
184 | 190 | end |
@@ -277,19 +283,23 @@ function M.register(opts) |
277 | 283 | description = tool.description, |
278 | 284 | hide_in_help_window = true, |
279 | 285 | visible = opts.show_server_tools_in_chat == true, |
280 | | - callback = { |
281 | | - name = namespaced_tool_name, |
282 | | - cmds = { create_individual_tool_handler(server.name, tool_name, namespaced_tool_name) }, |
283 | | - output = core.create_output_handlers(namespaced_tool_name, true, opts), |
284 | | - schema = { |
285 | | - type = "function", |
286 | | - ["function"] = { |
287 | | - name = namespaced_tool_name, |
288 | | - description = tool.description, |
289 | | - parameters = tool.inputSchema, |
| 286 | + callback = function() |
| 287 | + return { |
| 288 | + name = namespaced_tool_name, |
| 289 | + cmds = { |
| 290 | + create_individual_tool_handler(server.name, tool_name, namespaced_tool_name), |
| 291 | + }, |
| 292 | + output = core.create_output_handlers(namespaced_tool_name, true, opts), |
| 293 | + schema = { |
| 294 | + type = "function", |
| 295 | + ["function"] = { |
| 296 | + name = namespaced_tool_name, |
| 297 | + description = tool.description, |
| 298 | + parameters = tool.inputSchema, |
| 299 | + }, |
290 | 300 | }, |
291 | | - }, |
292 | | - }, |
| 301 | + } |
| 302 | + end, |
293 | 303 | } |
294 | 304 | end |
295 | 305 | end |
@@ -328,7 +338,7 @@ function M.register(opts) |
328 | 338 | ) |
329 | 339 | ), |
330 | 340 | tools = tool_names, |
331 | | - system_prompt = function(self) |
| 341 | + system_prompt = function(group_config, ctx) |
332 | 342 | if custom_instructions and custom_instructions ~= "" then |
333 | 343 | return custom_instructions |
334 | 344 | end |
|
0 commit comments