Repository navigation
Expand file tree
/
Copy pathrpc_mcp_lifecycle.e2e.test.ts
More file actions
151 lines (134 loc) · 5.65 KB
/
Copy pathrpc_mcp_lifecycle.e2e.test.ts
File metadata and controls
151 lines (134 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { randomUUID } from "node:crypto";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
import type { CopilotSession, MCPServerConfig, MCPStdioServerConfig } from "../../src/index.js";
import { approveAll } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext.js";
import { formatError, waitForCondition } from "./harness/sdkTestHelper.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const TEST_MCP_SERVER = resolve(__dirname, "../../../test/harness/test-mcp-server.mjs");
const TEST_HARNESS_DIR = dirname(TEST_MCP_SERVER);
describe("Session-scoped MCP lifecycle RPC", async () => {
const { copilotClient: client } = await createSdkTestContext();
function createTestMcpServers(...serverNames: string[]): Record<string, MCPServerConfig> {
return Object.fromEntries(
serverNames.map((name) => [
name,
{
type: "local",
command: "node",
args: [TEST_MCP_SERVER],
workingDirectory: TEST_HARNESS_DIR,
tools: ["*"],
} as MCPStdioServerConfig,
])
);
}
async function createSessionWithMcp(serverName: string): Promise<CopilotSession> {
return client.createSession({
onPermissionRequest: approveAll,
mcpServers: createTestMcpServers(serverName),
});
}
async function waitForMcpServerStatus(
session: CopilotSession,
serverName: string,
expectedStatus = "connected"
): Promise<void> {
let lastStatus = "<not listed>";
await waitForCondition(
async () => {
const result = await session.rpc.mcp.list();
const server = result.servers.find((entry) => entry.name === serverName);
lastStatus = server?.status ?? "<not listed>";
return server?.status === expectedStatus;
},
{
timeoutMs: 60_000,
intervalMs: 200,
timeoutMessage: `${serverName} did not reach ${expectedStatus}; last status was ${lastStatus}`,
}
);
}
async function waitForMcpRunning(
session: CopilotSession,
serverName: string,
expectedRunning: boolean
): Promise<void> {
await waitForCondition(
async () =>
(await session.rpc.mcp.isServerRunning({ serverName })).running === expectedRunning,
{
timeoutMs: 60_000,
intervalMs: 200,
timeoutMessage: `${serverName} running=${expectedRunning}`,
}
);
}
function missingName(prefix: string): string {
return `${prefix}-${randomUUID().replace(/-/g, "")}`;
}
function assertNotUnhandledMethod(message: string): void {
expect(message.toLowerCase()).not.toContain("unhandled method");
}
it(
"should list tools and report running status for connected server",
{ timeout: 120_000 },
async () => {
const serverName = "rpc-lifecycle-list-server";
const session = await createSessionWithMcp(serverName);
try {
await waitForMcpServerStatus(session, serverName);
const tools = await session.rpc.mcp.listTools({ serverName });
expect(tools.tools.length).toBeGreaterThan(0);
for (const tool of tools.tools) {
expect(tool.name).toBeTruthy();
}
expect((await session.rpc.mcp.isServerRunning({ serverName })).running).toBe(true);
expect(
(
await session.rpc.mcp.isServerRunning({
serverName: missingName("missing"),
})
).running
).toBe(false);
} finally {
await session.disconnect();
}
}
);
it("should throw when listing tools for unconnected server", { timeout: 120_000 }, async () => {
const serverName = "rpc-lifecycle-unconnected-host";
const session = await createSessionWithMcp(serverName);
try {
await waitForMcpServerStatus(session, serverName);
await expect(
session.rpc.mcp.listTools({ serverName: missingName("missing") })
).rejects.toSatisfy((error: unknown) => {
const message = formatError(error);
assertNotUnhandledMethod(message);
expect(message.toLowerCase()).toContain("not connected");
return true;
});
} finally {
await session.disconnect();
}
});
it("should stop running mcp server", { timeout: 180_000 }, async () => {
const serverName = "rpc-lifecycle-stop-server";
const session = await createSessionWithMcp(serverName);
try {
await waitForMcpServerStatus(session, serverName);
expect((await session.rpc.mcp.isServerRunning({ serverName })).running).toBe(true);
await session.rpc.mcp.stopServer({ serverName });
await waitForMcpRunning(session, serverName, false);
} finally {
await session.disconnect();
}
});
});