Skip to content

Commit 424e8c9

Browse files
committed
fix: schedule MCP calls in CopilotChat extension to avoid fast event context errors
Wraps hub:call_tool() and hub:access_resource() calls with vim.schedule() to prevent E5560 errors when accessing neovim resources through CopilotChat. Fixes #223
1 parent 163b3ad commit 424e8c9

1 file changed

Lines changed: 24 additions & 19 deletions

File tree

lua/mcphub/extensions/copilotchat/functions.lua

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,29 +76,34 @@ function M.register(opts)
7676
-- Cleanup existing mcphub functions
7777
cleanup_mcphub_functions(chat)
7878

79-
-- Create async wrappers
79+
-- Create async wrappers with proper event loop scheduling
8080
local call_tool = async.wrap(function(server, tool, input, callback)
81-
hub:call_tool(server, tool, input, {
82-
caller = {
83-
type = "copilotchat",
84-
copilotchat = chat,
85-
},
86-
callback = function(res, err)
87-
callback(res, err)
88-
end,
89-
})
81+
-- Schedule the MCP call to run in the main event loop to avoid fast event context issues
82+
vim.schedule(function()
83+
hub:call_tool(server, tool, input, {
84+
caller = {
85+
type = "copilotchat",
86+
copilotchat = chat,
87+
},
88+
callback = function(res, err)
89+
callback(res, err)
90+
end,
91+
})
92+
end)
9093
end, 4)
9194

9295
local access_resource = async.wrap(function(server, uri, callback)
93-
hub:access_resource(server, uri, {
94-
caller = {
95-
type = "copilotchat",
96-
copilotchat = chat,
97-
},
98-
callback = function(res, err)
99-
callback(res, err)
100-
end,
101-
})
96+
vim.schedule(function()
97+
hub:access_resource(server, uri, {
98+
caller = {
99+
type = "copilotchat",
100+
copilotchat = chat,
101+
},
102+
callback = function(res, err)
103+
callback(res, err)
104+
end,
105+
})
106+
end)
102107
end, 3)
103108

104109
-- Get servers and process them to avoid name conflicts

0 commit comments

Comments
 (0)