@@ -36,7 +36,6 @@ function M.get()
3636 url = " " ,
3737 headers = {},
3838 env = {},
39- alwaysAllow = true ,
4039 }
4140end
4241
@@ -89,99 +88,122 @@ function M.get_all_servers()
8988 return formatted_servers
9089end
9190
92- --- Call a tool via hub.
91+ --- Send result back to proxy.js via notification
92+ --- @param request_id string The request ID to respond to
93+ --- @param result table The result to send
94+ local function send_result (request_id , result )
95+ vim .rpcnotify (0 , " mcphub_proxy_result" , request_id , result )
96+ end
97+
98+ --- Async version of call_tool for proxy (uses nice UI)
99+ --- @param request_id string Request ID for async response
93100--- @param server_name string Name of the server
94101--- @param tool_name string Name of the tool
95102--- @param params table Parameters including arguments and caller context
96- --- @return table Result or error
97- function M .call_tool (server_name , tool_name , params )
98- local mcphub = require (" mcphub" )
99- local hub = mcphub .get_hub_instance ()
100-
101- if not hub or not hub :is_ready () then
102- return { error = " Hub is not ready" }
103- end
104-
105- local arguments = params .arguments or {}
106- local caller = params .caller or { type = " proxy" }
107-
108- -- Handle approval flow (synchronous version for RPC context)
109- local parsed_params = shared .parse_params ({
110- server_name = server_name ,
111- tool_name = tool_name ,
112- tool_input = arguments ,
113- }, " use_mcp_tool" )
114-
115- if # parsed_params .errors > 0 then
116- return { error = table.concat (parsed_params .errors , " \n " ) }
117- end
118-
119- local approval = shared .handle_auto_approval_decision_sync (parsed_params )
120- if approval .error then
121- return { error = approval .error }
122- end
123-
124- local result , err = hub :call_tool (server_name , tool_name , arguments , {
125- parse_response = false ,
126- caller = vim .tbl_extend (" force" , caller , { auto_approve = approval .approve }),
127- })
128-
129- if err then
130- return { error = err }
131- elseif result and result .result then
132- return result .result
133- elseif result then
134- return result
135- end
103+ function M .call_tool (request_id , server_name , tool_name , params )
104+ local async = require (" plenary.async" )
105+
106+ async .run (function ()
107+ local mcphub = require (" mcphub" )
108+ local hub = mcphub .get_hub_instance ()
109+
110+ if not hub or not hub :is_ready () then
111+ send_result (request_id , { error = " Hub is not ready" })
112+ return
113+ end
114+
115+ local arguments = params .arguments or {}
116+ local caller = params .caller or { type = " proxy" }
117+
118+ -- Parse and validate params
119+ local parsed_params = shared .parse_params ({
120+ server_name = server_name ,
121+ tool_name = tool_name ,
122+ tool_input = arguments ,
123+ }, " use_mcp_tool" )
124+
125+ if # parsed_params .errors > 0 then
126+ send_result (request_id , { error = table.concat (parsed_params .errors , " \n " ) })
127+ return
128+ end
129+
130+ -- Use the async approval with nice UI!
131+ local approval = shared .handle_auto_approval_decision (parsed_params )
132+ if approval .error then
133+ send_result (request_id , { error = approval .error })
134+ return
135+ end
136+
137+ local result , err = hub :call_tool (server_name , tool_name , arguments , {
138+ parse_response = false ,
139+ caller = vim .tbl_extend (" force" , caller , { auto_approve = approval .approve }),
140+ })
136141
137- return { error = " No result returned from hub" }
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
151+ end )
138152end
139153
140- --- Access a resource via hub.
154+ --- Async version of access_resource for proxy (uses nice UI)
155+ --- @param request_id string Request ID for async response
141156--- @param server_name string Name of the server
142157--- @param uri string Resource URI
143158--- @param params ? table Optional parameters including caller context
144- --- @return table Result or error
145- function M .access_resource (server_name , uri , params )
146- params = params or {}
147- local mcphub = require (" mcphub" )
148- local hub = mcphub .get_hub_instance ()
149-
150- if not hub or not hub :is_ready () then
151- return { error = " Hub is not ready" }
152- end
153-
154- local caller = params .caller or { type = " proxy" }
155-
156- -- Handle approval flow (same as codecompanion/avante)
157- local parsed_params = shared .parse_params ({
158- server_name = server_name ,
159- uri = uri ,
160- }, " access_mcp_resource" )
161-
162- if # parsed_params .errors > 0 then
163- return { error = table.concat (parsed_params .errors , " \n " ) }
164- end
165-
166- local approval = shared .handle_auto_approval_decision_sync (parsed_params )
167- if approval .error then
168- return { error = approval .error }
169- end
170-
171- local result , err = hub :access_resource (server_name , uri , {
172- parse_response = false ,
173- caller = vim .tbl_extend (" force" , caller , { auto_approve = approval .approve }),
174- })
175-
176- if err then
177- return { error = err }
178- elseif result and result .result then
179- return result .result
180- elseif result then
181- return result
182- end
159+ function M .access_resource (request_id , server_name , uri , params )
160+ local async = require (" plenary.async" )
161+
162+ async .run (function ()
163+ params = params or {}
164+ local mcphub = require (" mcphub" )
165+ local hub = mcphub .get_hub_instance ()
166+
167+ if not hub or not hub :is_ready () then
168+ send_result (request_id , { error = " Hub is not ready" })
169+ return
170+ end
171+
172+ local caller = params .caller or { type = " proxy" }
173+
174+ -- Parse and validate params
175+ local parsed_params = shared .parse_params ({
176+ server_name = server_name ,
177+ uri = uri ,
178+ }, " access_mcp_resource" )
179+
180+ if # parsed_params .errors > 0 then
181+ send_result (request_id , { error = table.concat (parsed_params .errors , " \n " ) })
182+ return
183+ end
184+
185+ -- Use the async approval with nice UI!
186+ local approval = shared .handle_auto_approval_decision (parsed_params )
187+ if approval .error then
188+ send_result (request_id , { error = approval .error })
189+ return
190+ end
191+
192+ local result , err = hub :access_resource (server_name , uri , {
193+ parse_response = false ,
194+ caller = vim .tbl_extend (" force" , caller , { auto_approve = approval .approve }),
195+ })
183196
184- return { error = " No result returned from hub" }
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
206+ end )
185207end
186208
187209--- Get a prompt via hub.
0 commit comments