Skip to content

Commit 47946fa

Browse files
committed
fix(mcp): fix some issues found in CR
1 parent 163c909 commit 47946fa

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

lua/codecompanion/interactions/chat/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ function Chat.new(args)
523523

524524
self:update_metadata()
525525

526-
require("codecompanion.mcp").start_all_if_not_started()
526+
require("codecompanion.mcp").start_all()
527527

528528
-- Likely this hasn't been set by the time the user opens the chat buffer
529529
if not _G.codecompanion_current_context then

lua/codecompanion/mcp/client.lua

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,11 @@ Client.__index = Client
204204

205205
Client.static = {}
206206
Client.static.methods = {
207-
new_transport = { default = function(name, cfg, methods)
208-
return StdioTransport:new(name, cfg, methods)
209-
end },
207+
new_transport = {
208+
default = function(name, cfg, methods)
209+
return StdioTransport:new(name, cfg, methods)
210+
end,
211+
},
210212
json_decode = { default = vim.json.decode },
211213
json_encode = { default = vim.json.encode },
212214
schedule_wrap = { default = vim.schedule_wrap },
@@ -234,7 +236,6 @@ function Client:new(name, cfg, methods)
234236
}, self)
235237
end
236238

237-
238239
---Start the client.
239240
function Client:start()
240241
if self.transport:started() then
@@ -272,7 +273,6 @@ function Client:_start_initialization()
272273
}, function(resp)
273274
if resp.error then
274275
log:error("[MCP.%s] initialization failed: %s", self.name, resp)
275-
276276
return
277277
end
278278
log:info("[MCP.%s] initialized successfully.", self.name)
@@ -302,6 +302,14 @@ function Client:_on_transport_close(err)
302302
else
303303
log:warn("[MCP.%s] exited with error: %s", self.name, err)
304304
end
305+
for id, handler in pairs(self.resp_handlers) do
306+
-- Notify all pending requests of the transport closure
307+
pcall(handler, {
308+
jsonrpc = "2.0",
309+
id = id,
310+
error = { code = JsonRpc.ERROR_INTERNAL, message = "MCP server connection closed" },
311+
})
312+
end
305313
utils.fire("MCPServerExit", { name = self.name, err = err })
306314
end
307315

@@ -510,7 +518,7 @@ end
510518

511519
---Call a tool on the MCP server
512520
---@param name string The name of the tool to call
513-
---@param args table<string, any> The arguments to pass to the tool
521+
---@param args? table<string, any> The arguments to pass to the tool
514522
---@param callback fun(ok: boolean, result_or_error: MCP.CallToolResult | string) Callback function that receives (ok, result_or_error)
515523
---@param opts? table { timeout_ms? integer }
516524
---@return integer req_id
@@ -555,8 +563,8 @@ function Client:refresh_tools()
555563

556564
-- pagination handling
557565
local next_cursor = resp.result and resp.result.nextCursor
558-
if next_cursor and #tools >= MAX_TOOLS_PER_SERVER then
559-
log:warn("[MCP.%s] returned too many tools (%d), stop further loading", self.name, #tools)
566+
if next_cursor and #all_tools >= MAX_TOOLS_PER_SERVER then
567+
log:warn("[MCP.%s] returned too many tools (%d), stop further loading", self.name, #all_tools)
560568
elseif next_cursor then
561569
log:info("[MCP.%s] loading more tools with cursor: %s", self.name, next_cursor)
562570
return load_tools(next_cursor)

lua/codecompanion/mcp/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ local M = {}
2525
local clients = {}
2626

2727
---Start all configured MCP servers if not already started
28-
function M.start_all_if_not_started()
28+
function M.start_all()
2929
local mcp_cfg = require("codecompanion.config").interactions.chat.mcp
3030
for name, cfg in pairs(mcp_cfg.servers or {}) do
3131
if not clients[name] then

lua/codecompanion/types.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
---@alias MCP.ContentBlock MCP.TextContent|any
5656

5757
---@class MCP.CallToolResult
58-
---@field isError boolean
58+
---@field isError? boolean
5959
---@field content MCP.ContentBlock[]
6060

6161
---@meta Tree-sitter

tests/stubs/mcp/tools.jsonl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{"name":"echo","description":"Echoes back the input","inputSchema":{"type":"object","properties":{"value":{"type":"string","description":"A string value to echo back"}},"required":["value"]}}
2-
{"name":"say_hi","description":"Say Hi to you","inputSchema":{"type":"object","properties":{"name":{"type":"string","description":"Who are you?"}},"required":[""]}}
2+
{"name":"say_hi","description":"Say Hi to you","inputSchema":{"type":"object","properties":{"name":{"type":"string","description":"Who are you?"}},"required":[]}}
33
{"name":"make_list","description":"Creates a list of items","inputSchema":{"type":"object","properties":{"count":{"type":"number","description":"Number of items"},"item":{"oneOf":[{"type":"string"},{"type":"number"}],"description":"The item to repeat in the list"}},"required":["count","item"]}}
44
{"name":"math_add","title":"Math/Add","description":"Adds two numbers","inputSchema":{"type":"object","properties":{"a":{"type":"number","description":"The first number to add"},"b":{"type":"number","description":"The second number to add"}},"required":["a","b"]}}
55
{"name":"math_mul","title":"Math/Mul","description":"Multiply two numbers","inputSchema":{"type":"object","properties":{"a":{"type":"number","description":"The first number to multiply"},"b":{"type":"number","description":"The second number to multiply"}},"required":["a","b"]}}

0 commit comments

Comments
 (0)