fix: further mcp standardization
This commit is contained in:
parent
34efb8f0a0
commit
53aaf24162
3 changed files with 16 additions and 67 deletions
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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<MCPServiceState> {
|
||||
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<void> {
|
||||
public async cleanup(): Promise<void> {
|
||||
if (this.isShuttingDown) return;
|
||||
|
||||
this.isShuttingDown = true;
|
||||
|
||||
this.removeAllListeners();
|
||||
logger.debug("Shutting down MCPService");
|
||||
|
||||
await this.shutdownConnections();
|
||||
|
||||
this.currentState.isReady = false;
|
||||
this.updateState();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,8 +56,6 @@ export interface MCPServiceState {
|
|||
connections: Array<MCPConnectionInfo>;
|
||||
toolCount: number;
|
||||
promptCount: number;
|
||||
isReady: boolean;
|
||||
error?: Error;
|
||||
}
|
||||
|
||||
export interface ApiClientServiceState {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue