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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
import { describe, expect, it } from "vitest";
import { McpClient } from "./client.js";
import type { Connection } from "./transport.js";
function makeMockConnection(): Connection & {
responses: Map<string, unknown>;
feedResponse: (method: string, result: unknown) => void;
notifications: Array<{ method: string; params: unknown }>;
} {
const responses = new Map<string, unknown>();
const pendingRequests = new Map<number, { method: string; resolve: (v: unknown) => void }>();
let nextId = 1;
const notifications: Array<{ method: string; params: unknown }> = [];
const notificationHandlers = new Map<string, (params: unknown) => void>();
return {
responses,
notifications,
feedResponse: (_method: string, _result: unknown) => {},
send: (method: string, _params?: unknown) => {
const id = nextId++;
return new Promise((resolve) => {
pendingRequests.set(id, { method, resolve });
// Auto-respond for initialize
if (method === "initialize") {
resolve({
protocolVersion: "2025-11-25",
capabilities: { tools: { listChanged: true } },
serverInfo: { name: "test-server", version: "1.0.0" },
});
} else if (method === "tools/list") {
resolve({
tools: [
{
name: "test_tool",
description: "A test tool",
inputSchema: { type: "object", properties: { input: { type: "string" } } },
},
],
});
} else if (method === "tools/call") {
resolve({
content: [{ type: "text", text: "result from tool" }],
isError: false,
});
}
});
},
notify: (method: string, params?: unknown) => {
notifications.push({ method, params });
},
onNotification: (method: string, handler: (params: unknown) => void) => {
notificationHandlers.set(method, handler);
},
close: () => {},
pid: 999,
};
}
describe("McpClient", () => {
it("initialize sends correct protocolVersion + capabilities", async () => {
const conn = makeMockConnection();
const client = new McpClient({ connection: conn });
const result = await client.initialize();
expect(result.protocolVersion).toBe("2025-11-25");
expect(result.capabilities.tools?.listChanged).toBe(true);
expect(result.serverInfo.name).toBe("test-server");
expect(client.getState()).toBe("connected");
// Should have sent notifications/initialized
expect(conn.notifications.length).toBe(1);
expect(conn.notifications[0].method).toBe("notifications/initialized");
});
it("listTools returns parsed tools", async () => {
const conn = makeMockConnection();
const client = new McpClient({ connection: conn });
await client.initialize();
const tools = await client.listTools();
expect(tools.length).toBe(1);
expect(tools[0].name).toBe("test_tool");
expect(tools[0].description).toBe("A test tool");
});
it("callTool sends name + arguments", async () => {
const conn = makeMockConnection();
let callParams: unknown = null;
const origSend = conn.send.bind(conn);
conn.send = (method: string, params?: unknown) => {
if (method === "tools/call") callParams = params;
return origSend(method, params);
};
const client = new McpClient({ connection: conn });
await client.initialize();
const result = await client.callTool("test_tool", { input: "hello" });
expect(callParams).toEqual({ name: "test_tool", arguments: { input: "hello" } });
expect(result.content).toEqual([{ type: "text", text: "result from tool" }]);
expect(result.isError).toBe(false);
});
it("list_changed triggers re-list", async () => {
const conn = makeMockConnection();
const notificationHandlers = new Map<string, (params: unknown) => void>();
conn.onNotification = (method: string, handler: (params: unknown) => void) => {
notificationHandlers.set(method, handler);
};
const client = new McpClient({ connection: conn });
let toolsChangedFired = false;
client.onToolsChanged(() => {
toolsChangedFired = true;
});
await client.initialize();
// Simulate list_changed notification
const handler = notificationHandlers.get("notifications/tools/list_changed");
expect(handler).toBeDefined();
handler?.(undefined);
expect(toolsChangedFired).toBe(true);
});
it("handles server error on initialize", async () => {
const conn = makeMockConnection();
conn.send = (method: string) => {
if (method === "initialize") {
return Promise.reject(new Error("Server startup failed"));
}
return Promise.resolve({});
};
const client = new McpClient({ connection: conn });
await expect(client.initialize()).rejects.toThrow("Server startup failed");
expect(client.getState()).toBe("error");
});
it("callTool rejects when not connected", async () => {
const conn = makeMockConnection();
const client = new McpClient({ connection: conn });
await expect(client.callTool("test", {})).rejects.toThrow("Client not connected");
});
it("listTools rejects when not connected", async () => {
const conn = makeMockConnection();
const client = new McpClient({ connection: conn });
await expect(client.listTools()).rejects.toThrow("Client not connected");
});
it("close sets state to disconnected", async () => {
const conn = makeMockConnection();
const client = new McpClient({ connection: conn });
await client.initialize();
expect(client.getState()).toBe("connected");
client.close();
expect(client.getState()).toBe("disconnected");
});
it("callTool with abort signal", async () => {
const conn = makeMockConnection();
let resolveRequest: ((v: unknown) => void) | null = null;
conn.send = (method: string) => {
if (method === "tools/call") {
return new Promise((resolve) => {
resolveRequest = resolve;
});
}
if (method === "initialize") {
return Promise.resolve({
protocolVersion: "2025-11-25",
capabilities: {},
serverInfo: { name: "test", version: "1.0.0" },
});
}
return Promise.resolve({});
};
const client = new McpClient({ connection: conn });
await client.initialize();
const controller = new AbortController();
const callPromise = client.callTool("test", {}, controller.signal);
controller.abort();
await expect(callPromise).rejects.toThrow("Aborted");
// Clean up
resolveRequest?.({
content: [{ type: "text", text: "too late" }],
});
});
});
|