You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Refactor: Extract MCP Client CLI Logic into Dedicated Controller Class
23
+
24
+
The current `ai/mcp/client/mcp-stdio.mjs` script is overloaded with logic (CLI parsing, tool execution, output formatting) that violates the "thin runner" pattern established by the server implementation.
25
+
26
+
To fix this and maintain architectural consistency:
27
+
1.**Create `Neo.ai.mcp.client.Cli`:** A new class in `ai/mcp/client/Cli.mjs` responsible for handling the CLI interaction flow. It will:
* Instantiate and manage the `Neo.ai.mcp.client.Client`.
30
+
* Execute the requested operation.
31
+
* Handle output formatting and logging.
32
+
2.**Refactor `ai/mcp/client/mcp-stdio.mjs`:** Reduce this file to a minimal runner that simply parses command-line arguments using `commander` and instantiates `Neo.ai.mcp.client.Cli` to do the work.
33
+
34
+
This separation ensures `Client.mjs` remains a clean transport wrapper for use by Agents, while the CLI logic is encapsulated in its own class, and the runner remains purely a bootstrapper.
***Lifecycle:** Move connection logic from `connect()` to `initAsync()`. The client should self-connect during initialization. `connect()` should be removed or made private.
30
+
***Configuration:** Add `configFile` config. Inside `initAsync`, call `ClientConfig.load(this.configFile)` if provided, before resolving server details.
31
+
***Proxies:** Ensure dynamic tool proxies (`this.tools`) are created during `initAsync`.
32
+
33
+
2.**Rename and Refactor CLI Runner:**
34
+
***Rename:** Move `ai/mcp/client/mcp-stdio.mjs` to `ai/mcp/client/mcp-cli.mjs`.
35
+
***Logic:** Update the script to use the new Client lifecycle (`await Neo.create(Client, ...).ready()`).
36
+
***Implementation:** Keep the CLI logic (argument parsing, executing `client.listTools()` or `client.tools.x()`, logging results) inside this runner script.
37
+
***Cleanup:** Ensure `client.close()` is called before exit.
38
+
39
+
3.**Update `package.json`:**
40
+
* Update `ai:mcp-client` script to point to `ai/mcp/client/mcp-cli.mjs`.
41
+
42
+
4.**Update `Neo.ai.Agent` (`ai/Agent.mjs`):**
43
+
* Update `connect()` to use `await Neo.create(Client).ready()` instead of `client.connect()`.
***Lifecycle:** Move connection logic from `connect()` to `initAsync()`. The client should self-connect during initialization. `connect()` should be removed or made private.
30
+
***Configuration:** Add `configFile` config. Inside `initAsync`, call `ClientConfig.load(this.configFile)` if provided, before resolving server details.
31
+
***Proxies:** Ensure dynamic tool proxies (`this.tools`) are created during `initAsync`.
32
+
33
+
2.**Rename and Refactor CLI Runner:**
34
+
***Rename:** Move `ai/mcp/client/mcp-stdio.mjs` to `ai/mcp/client/mcp-cli.mjs`.
35
+
***Logic:** Update the script to use the new Client lifecycle (`await Neo.create(Client, ...).ready()`).
36
+
***Implementation:** Keep the CLI logic (argument parsing, executing `client.listTools()` or `client.tools.x()`, logging results) inside this runner script.
37
+
***Cleanup:** Ensure `client.close()` is called before exit.
38
+
39
+
3.**Update `package.json`:**
40
+
* Update `ai:mcp-client` script to point to `ai/mcp/client/mcp-cli.mjs`.
41
+
42
+
4.**Refactor `Neo.ai.Agent` (`ai/Agent.mjs`):**
43
+
***Remove the `connect()` method.**
44
+
***Move client creation, `client.ready()` calls, and tool aggregation logic into `Agent.initAsync()`**.
45
+
* The `Agent` itself will then be ready when its `initAsync` completes.
46
+
47
+
5.**Update `ai/agents/mcp-demo-agent.mjs`:**
48
+
* Refactor to use `await agent.ready()` instead of `await agent.connect()`.
0 commit comments