-
Notifications
You must be signed in to change notification settings - Fork 673
/
Copy pathstdio.test.ts
91 lines (77 loc) · 2.1 KB
/
stdio.test.ts
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
import { JSONRPCMessage } from "../types.js";
import { StdioClientTransport, StdioServerParameters } from "./stdio.js";
const serverParameters: StdioServerParameters = {
command: "/usr/bin/tee",
};
test("should start then close cleanly", async () => {
const client = new StdioClientTransport(serverParameters);
client.onerror = (error) => {
throw error;
};
let didClose = false;
client.onclose = () => {
didClose = true;
};
await client.start();
expect(didClose).toBeFalsy();
await client.close();
expect(didClose).toBeTruthy();
});
test("should read messages", async () => {
const client = new StdioClientTransport(serverParameters);
client.onerror = (error) => {
throw error;
};
const messages: JSONRPCMessage[] = [
{
jsonrpc: "2.0",
id: 1,
method: "ping",
},
{
jsonrpc: "2.0",
method: "notifications/initialized",
},
];
const readMessages: JSONRPCMessage[] = [];
const finished = new Promise<void>((resolve) => {
client.onmessage = (message) => {
readMessages.push(message);
if (JSON.stringify(message) === JSON.stringify(messages[1])) {
resolve();
}
};
});
await client.start();
await client.send(messages[0]);
await client.send(messages[1]);
await finished;
expect(readMessages).toEqual(messages);
await client.close();
});
test("should work with actual node mcp server", async () => {
const client = new StdioClientTransport({
command: "npx",
args: ["-y", "@wrtnlabs/calculator-mcp"],
});
await client.start();
await client.close();
});
test("should work with actual node mcp server and empty env", async () => {
const client = new StdioClientTransport({
command: "npx",
args: ["-y", "@wrtnlabs/calculator-mcp"],
env: {},
});
await client.start();
await client.close();
});
test("should work with actual node mcp server and custom env", async () => {
const client = new StdioClientTransport({
command: "npx",
args: ["-y", "@wrtnlabs/calculator-mcp"],
env: {TEST_VAR: "test-value"},
});
await client.start();
await client.close();
});