@@ -65,18 +65,21 @@ StdioTransport.static.methods = {
6565 schedule_wrap = { default = vim .schedule_wrap },
6666}
6767
68+ --- @class CodeCompanion.MCP.StdioTransportArgs
69+ --- @field name string
70+ --- @field cfg CodeCompanion.MCP.ServerConfig
71+ --- @field methods ? table<string , function> Optional method overrides for testing
72+
6873--- Create a new StdioTransport for the given server configuration.
69- --- @param name string
70- --- @param cfg CodeCompanion.MCP.ServerConfig
71- --- @param methods ? table<string , function> Optional method overrides for testing
74+ --- @param args CodeCompanion.MCP.StdioTransportArgs
7275--- @return CodeCompanion.MCP.StdioTransport
73- function StdioTransport : new (name , cfg , methods )
76+ function StdioTransport . new (args )
7477 return setmetatable ({
75- name = name ,
76- cmd = cfg .cmd ,
77- env = cfg .env ,
78- methods = transform_static_methods (StdioTransport , methods ),
79- }, self )
78+ name = args . name ,
79+ cmd = args . cfg .cmd ,
80+ env = args . cfg .env ,
81+ methods = transform_static_methods (StdioTransport , args . methods ),
82+ }, StdioTransport )
8083end
8184
8285--- Start the underlying process and attach stdout/stderr callbacks.
@@ -245,8 +248,8 @@ Client.__index = Client
245248Client .static = {}
246249Client .static .methods = {
247250 new_transport = {
248- default = function (name , cfg , methods )
249- return StdioTransport : new (name , cfg , methods )
251+ default = function (args )
252+ return StdioTransport . new (args )
250253 end ,
251254 },
252255 json_decode = { default = vim .json .decode },
@@ -255,25 +258,36 @@ Client.static.methods = {
255258 defer_fn = { default = vim .defer_fn },
256259}
257260
261+ --- @class CodeCompanion.MCP.ClientArgs
262+ --- @field name string
263+ --- @field cfg CodeCompanion.MCP.ServerConfig
264+ --- @field methods ? table<string , function> Optional method overrides for testing
265+
258266--- Create a new MCP client instance bound to the provided server configuration.
259- --- @param name string
260- --- @param cfg CodeCompanion.MCP.ServerConfig
261- --- @param methods ? table<string , function> Optional method overrides for testing
267+ --- @param args CodeCompanion.MCP.ClientArgs
262268--- @return CodeCompanion.MCP.Client
263- function Client : new (name , cfg , methods )
264- local static_methods = transform_static_methods (Client , methods )
265- return setmetatable ({
266- name = name ,
267- cfg = cfg ,
269+ function Client . new (args )
270+ local static_methods = transform_static_methods (Client , args . methods )
271+ local self = setmetatable ({
272+ name = args . name ,
273+ cfg = args . cfg ,
268274 ready = false ,
269- transport = static_methods .new_transport (name , cfg , methods ),
275+ transport = static_methods .new_transport ({ name = args . name , cfg = args . cfg , methods = args . methods } ),
270276 resp_handlers = {},
271- server_request_handlers = {
272- [" ping" ] = self ._handle_server_ping ,
273- [" roots/list" ] = self ._handler_server_roots_list ,
274- },
277+ server_request_handlers = {},
275278 methods = static_methods ,
276- }, self )
279+ }, Client )
280+
281+ self .server_request_handlers = {
282+ [" ping" ] = function ()
283+ return self :_handle_server_ping ()
284+ end ,
285+ [" roots/list" ] = function ()
286+ return self :_handle_server_roots_list ()
287+ end ,
288+ }
289+
290+ return self
277291end
278292
279293--- Start the client.
531545
532546--- Handler for 'roots/list' server requests.
533547--- @return " result" | " error" , table
534- function Client :_handler_server_roots_list ()
548+ function Client :_handle_server_roots_list ()
535549 if not self .cfg .roots then
536550 return " error" , { code = CONSTANTS .JSONRPC .ERROR_METHOD_NOT_FOUND , message = " roots capability not enabled" }
537551 end
0 commit comments