From 53aaf2416249fae1bb2df99e4fe25ca97379074a Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 14 Aug 2025 18:53:06 -0700 Subject: [PATCH] fix: further mcp standardization --- src/services/mcp.test.ts | 28 ++++++--------------- src/services/mcp.ts | 53 ++++++---------------------------------- src/services/types.ts | 2 -- 3 files changed, 16 insertions(+), 67 deletions(-) diff --git a/src/services/mcp.test.ts b/src/services/mcp.test.ts index 5f1b898ce..765c59ea4 100644 --- a/src/services/mcp.test.ts +++ b/src/services/mcp.test.ts @@ -43,13 +43,13 @@ describe("MCPService", () => { afterEach(async () => { vi.clearAllMocks(); - await mcpService.shutdown() + await mcpService.cleanup() }); describe("initialization", () => { it("should initialize with empty state", () => { const state = mcpService.getState(); - expect(state.isReady).toBe(false); + expect(mcpService.isReady()).toBe(false); expect(state.connections).toEqual([]); expect(state.toolCount).toBe(0); expect(state.promptCount).toBe(0); @@ -62,7 +62,7 @@ describe("MCPService", () => { } as AssistantConfig; const state = await mcpService.initialize(emptyAssistant); - expect(state.isReady).toBe(true); + expect(mcpService.isReady()).toBe(true); expect(state.connections).toEqual([]); expect(state.toolCount).toBe(0); expect(state.promptCount).toBe(0); @@ -72,7 +72,7 @@ describe("MCPService", () => { const state = await mcpService.initialize(mockAssistant); // Should return immediately with ready state - expect(state.isReady).toBe(true); + expect(mcpService.isReady()).toBe(true); expect(state.mcpService).toBe(mcpService); }); }); @@ -89,23 +89,12 @@ describe("MCPService", () => { expect(info).toHaveProperty("connectionCount"); }); - it("should handle service updates", async () => { - const newAssistant = { - name: "updated-assistant", - version: "1.0.0", - mcpServers: [], - } as AssistantConfig; - - const state = await mcpService.update(newAssistant); - expect(state.isReady).toBe(true); - }); - it("should get overall status", async () => { console.log(mcpService.getConnectionInfo()) const firstStatus = mcpService.getOverallStatus(); expect(firstStatus.status).toBe("connected"); expect(firstStatus.hasWarnings).toBe(false); - await mcpService.shutdown() + await mcpService.cleanup() const secondStatus = mcpService.getOverallStatus(); expect(secondStatus.status).toBe("idle"); }); @@ -163,17 +152,16 @@ describe("MCPService", () => { describe("shutdown", () => { it("should handle shutdown gracefully", async () => { await mcpService.initialize(mockAssistant); - await mcpService.shutdown() + await mcpService.cleanup() const state = mcpService.getState(); - expect(state.isReady).toBe(false); expect(state.mcpService?.getState().connections.filter(c => c.status === "connected")).toHaveLength(0) }); it("should handle multiple shutdown calls", async () => { await mcpService.initialize(mockAssistant); - await mcpService.shutdown(); - await expect(mcpService.shutdown()).resolves.not.toThrow(); + await mcpService.cleanup(); + await expect(mcpService.cleanup()).resolves.not.toThrow(); }); }); }); diff --git a/src/services/mcp.ts b/src/services/mcp.ts index 10cd27bab..8d2e5c808 100644 --- a/src/services/mcp.ts +++ b/src/services/mcp.ts @@ -6,7 +6,6 @@ import { getErrorString } from "../util/error.js"; import { logger } from "../util/logger.js"; import { BaseService, ServiceWithDependencies } from "./BaseService.js"; -import { serviceContainer } from "./ServiceContainer.js"; import { MCPServiceState, SERVICE_NAMES } from "./types.js"; export type MCPServerStatus = "idle" | "connecting" | "connected" | "error"; @@ -51,13 +50,12 @@ export class MCPService connections: [], toolCount: 0, promptCount: 0, - isReady: false, }); // Register shutdown handler - process.on("exit", () => this.shutdown()); - process.on("SIGINT", () => this.shutdown()); - process.on("SIGTERM", () => this.shutdown()); + process.on("exit", () => this.cleanup()); + process.on("SIGINT", () => this.cleanup()); + process.on("SIGTERM", () => this.cleanup()); } /** @@ -72,7 +70,6 @@ export class MCPService serverCount: assistant.mcpServers?.length || 0, }); - console.log("MCP INITIALIZATION: SHUTTING DOWN CONNS"); await this.shutdownConnections(); this.assistant = assistant; @@ -89,7 +86,6 @@ export class MCPService } }); - console.log("MCP INITIALIZATION: CONNECTING"); const connectionInit = Promise.all(connectionPromises ?? []).then( (connections) => { logger.debug("MCP connections established", { @@ -98,13 +94,9 @@ export class MCPService }, ); if (waitForConnections) { - console.log("MCP INITIALIZATION: WAITING FOR CONNS"); await connectionInit; } - this.currentState.isReady = true; - console.log("MCP INITIALIZATION: UPDATING STATE"); this.updateState(); - console.log("MCP INITIALIZATION: DONE"); return this.currentState; } @@ -118,7 +110,7 @@ export class MCPService (c) => c.status === "connected", ); - this.currentState = { + this.setState({ mcpService: this, connections: this.getConnectionInfo(), toolCount: connectedConnections.reduce( @@ -129,15 +121,7 @@ export class MCPService (sum, c) => sum + c.prompts.length, 0, ), - isReady: this.currentState.isReady, - }; - - // Propagate state changes to the service container so the UI updates - try { - serviceContainer.set(SERVICE_NAMES.MCP, this.currentState); - } catch { - // In early boot, container may not yet be registered; ignore - } + }); } /** @@ -147,22 +131,6 @@ export class MCPService return { ...this.currentState }; } - /** - * Update the MCP service with a new assistant config - */ - async update(assistant: AssistantConfig): Promise { - logger.debug("Updating MCPService config"); - // Shutdown existing connections - return await this.initialize(assistant); - } - - /** - * Check if the MCP service is ready - */ - isReady(): boolean { - return this.currentState.isReady; - } - /** * Get connection information for display */ @@ -478,18 +446,13 @@ export class MCPService } } - /** - * Shutdown the entire service - */ - public async shutdown(): Promise { + public async cleanup(): Promise { if (this.isShuttingDown) return; - this.isShuttingDown = true; + + this.removeAllListeners(); logger.debug("Shutting down MCPService"); - await this.shutdownConnections(); - - this.currentState.isReady = false; this.updateState(); } } diff --git a/src/services/types.ts b/src/services/types.ts index a3ae0c678..8510b16fb 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -56,8 +56,6 @@ export interface MCPServiceState { connections: Array; toolCount: number; promptCount: number; - isReady: boolean; - error?: Error; } export interface ApiClientServiceState {