Skip to content

Commit fe97109

Browse files
committed
fix: seperate the installation script
fix: dont format fix: dont block revert: .
1 parent ab89f6c commit fe97109

4 files changed

Lines changed: 88 additions & 90 deletions

File tree

bundled_build.lua

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,5 @@ else
7878
error("Failed to install mcp-hub: " .. npm_install_result.stderr)
7979
end
8080

81-
status("Installing RPC proxy dependencies...", vim.log.levels.INFO)
82-
local proxy_install_result = vim.system({
83-
"npm",
84-
"install",
85-
}, {
86-
cwd = root .. "/scripts",
87-
stdout = on_stdout,
88-
stderr = on_stderr,
89-
}):wait()
90-
if proxy_install_result.code ~= 0 then
91-
status(
92-
"Warning: Failed to install RPC proxy dependencies: " .. proxy_install_result.stderr,
93-
vim.log.levels.WARN
94-
)
95-
else
96-
status("RPC proxy dependencies installed successfully", vim.log.levels.INFO)
97-
end
98-
9981
status("Build complete!", vim.log.levels.INFO)
10082
end

bundled_scripts.lua

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
-- Get plugin root directory
2+
---@return string
3+
local function get_root()
4+
return vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":p:h")
5+
end
6+
7+
---@param msg string
8+
---@param level? number default: TRACE
9+
local function status(msg, level)
10+
vim.schedule(function()
11+
print(msg)
12+
--INFO: This is not working as expected
13+
-- coroutine.yield({
14+
-- msg = msg,
15+
-- level = level or vim.log.levels.TRACE,
16+
-- })
17+
end)
18+
end
19+
20+
local root = get_root()
21+
22+
local function on_stdout(err, data)
23+
if data then
24+
status(data, vim.log.levels.INFO)
25+
end
26+
if err then
27+
status(err, vim.log.levels.ERROR)
28+
end
29+
end
30+
31+
local function on_stderr(err, data)
32+
if data then
33+
status(data, vim.log.levels.ERROR)
34+
end
35+
if err then
36+
status(err, vim.log.levels.ERROR)
37+
end
38+
end
39+
40+
status("Installing bundled script dependencies...", vim.log.levels.INFO)
41+
local result = vim.system({
42+
"npm",
43+
"install",
44+
}, {
45+
cwd = root .. "/scripts",
46+
stdout = on_stdout,
47+
stderr = on_stderr,
48+
}):wait()
49+
if result.code ~= 0 then
50+
status("Warning: Failed to install RPC proxy dependencies: " .. result.stderr, vim.log.levels.WARN)
51+
else
52+
status("RPC proxy dependencies installed successfully", vim.log.levels.INFO)
53+
end
54+
55+
status("Build complete!", vim.log.levels.INFO)

lua/mcphub/extensions/proxy/init.lua

Lines changed: 28 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -49,43 +49,7 @@ function M.get_all_servers()
4949
return {}
5050
end
5151

52-
local servers = hub:get_servers()
53-
54-
local formatted_servers = {}
55-
for _, server in ipairs(servers) do
56-
table.insert(formatted_servers, {
57-
name = server.name,
58-
displayName = server.displayName,
59-
description = server.description,
60-
status = server.status,
61-
capabilities = {
62-
tools = vim.tbl_map(function(t)
63-
return {
64-
name = t.name,
65-
description = t.description,
66-
inputSchema = t.inputSchema,
67-
}
68-
end, server.capabilities.tools or {}),
69-
resources = vim.tbl_map(function(r)
70-
return {
71-
uri = r.uri,
72-
name = r.name,
73-
description = r.description,
74-
mimeType = r.mimeType,
75-
}
76-
end, server.capabilities.resources or {}),
77-
prompts = vim.tbl_map(function(p)
78-
return {
79-
name = p.name,
80-
description = p.description,
81-
arguments = p.arguments,
82-
}
83-
end, server.capabilities.prompts or {}),
84-
},
85-
})
86-
end
87-
88-
return formatted_servers
52+
return hub:get_servers()
8953
end
9054

9155
--- Send result back to proxy.js via notification
@@ -134,20 +98,23 @@ function M.call_tool(request_id, server_name, tool_name, params)
13498
return
13599
end
136100

137-
local result, err = hub:call_tool(server_name, tool_name, arguments, {
101+
-- Use callback to avoid blocking vim.wait() in NativeServer:call_tool
102+
-- This allows interactive tools like edit_file to work properly
103+
hub:call_tool(server_name, tool_name, arguments, {
138104
parse_response = false,
139105
caller = vim.tbl_extend("force", caller, { auto_approve = approval.approve }),
106+
callback = function(result, err)
107+
if err then
108+
send_result(request_id, { error = err })
109+
elseif result and result.result then
110+
send_result(request_id, result.result)
111+
elseif result then
112+
send_result(request_id, result)
113+
else
114+
send_result(request_id, { error = "No result returned from hub" })
115+
end
116+
end,
140117
})
141-
142-
if err then
143-
send_result(request_id, { error = err })
144-
elseif result and result.result then
145-
send_result(request_id, result.result)
146-
elseif result then
147-
send_result(request_id, result)
148-
else
149-
send_result(request_id, { error = "No result returned from hub" })
150-
end
151118
end)
152119
end
153120

@@ -189,20 +156,22 @@ function M.access_resource(request_id, server_name, uri, params)
189156
return
190157
end
191158

192-
local result, err = hub:access_resource(server_name, uri, {
159+
-- Use callback to avoid blocking vim.wait() in NativeServer:access_resource
160+
hub:access_resource(server_name, uri, {
193161
parse_response = false,
194162
caller = vim.tbl_extend("force", caller, { auto_approve = approval.approve }),
163+
callback = function(result, err)
164+
if err then
165+
send_result(request_id, { error = err })
166+
elseif result and result.result then
167+
send_result(request_id, result.result)
168+
elseif result then
169+
send_result(request_id, result)
170+
else
171+
send_result(request_id, { error = "No result returned from hub" })
172+
end
173+
end,
195174
})
196-
197-
if err then
198-
send_result(request_id, { error = err })
199-
elseif result and result.result then
200-
send_result(request_id, result.result)
201-
elseif result then
202-
send_result(request_id, result)
203-
else
204-
send_result(request_id, { error = "No result returned from hub" })
205-
end
206175
end)
207176
end
208177

lua/mcphub/extensions/shared.lua

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -292,19 +292,20 @@ function M.show_mcp_tool_prompt(params)
292292
return confirmed, cancelled
293293
end
294294

295-
--- Check auto-approval status without showing any dialog
296295
---@param parsed_params MCPHub.ParsedParams
297296
---@return {error?:string, approve:boolean}
298-
function M.check_auto_approval(parsed_params)
297+
function M.handle_auto_approval_decision(parsed_params)
299298
local auto_approve = State.config.auto_approve or false
300299
local status = { approve = false, error = nil }
301-
302-
-- Check global auto_approve config (can be boolean or function)
300+
--- If user has a custom function that decides whether to auto-approve
301+
--- call that with params + saved autoApprove state as is_auto_approved_in_server field
303302
if type(auto_approve) == "function" then
304303
local ok, res = pcall(auto_approve, parsed_params)
305304
if not ok or type(res) == "string" then
305+
--- If auto_approve function throws an error, or returns a string, treat it as an error
306306
status = { approve = false, error = res }
307307
elseif type(res) == "boolean" then
308+
--- If auto_approve function returns a boolean, use that as the decision
308309
status = { approve = res, error = nil }
309310
end
310311
elseif type(auto_approve) == "boolean" then
@@ -316,14 +317,6 @@ function M.check_auto_approval(parsed_params)
316317
status = { approve = true, error = nil }
317318
end
318319

319-
return status
320-
end
321-
322-
---@param parsed_params MCPHub.ParsedParams
323-
---@return {error?:string, approve:boolean}
324-
function M.handle_auto_approval_decision(parsed_params)
325-
local status = M.check_auto_approval(parsed_params)
326-
327320
if status.error then
328321
return { error = status.error or "Something went wrong with auto-approval", approve = false }
329322
end
@@ -332,7 +325,6 @@ function M.handle_auto_approval_decision(parsed_params)
332325
local confirmed, _ = M.show_mcp_tool_prompt(parsed_params)
333326
return { error = not confirmed and "User cancelled the operation", approve = confirmed }
334327
end
335-
336328
return status
337329
end
338330

0 commit comments

Comments
 (0)