forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-client.ts
More file actions
118 lines (104 loc) · 3.44 KB
/
Copy pathtest-client.ts
File metadata and controls
118 lines (104 loc) · 3.44 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
/**
* Test client for elizaOS MCP Server
*
* Connects to the MCP server and tests the chat and get_agent_info tools.
*/
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
function childEnv(extra: Record<string, string>): Record<string, string> {
return {
...Object.fromEntries(
Object.entries(process.env).filter(
(entry): entry is [string, string] => typeof entry[1] === "string",
),
),
...extra,
};
}
async function main(): Promise<void> {
console.log("🧪 Testing elizaOS MCP Server\n");
const dataDir = await mkdtemp(join(tmpdir(), "eliza-mcp-example-"));
// Create client transport
const transport = new StdioClientTransport({
command: "bun",
args: ["run", "server.ts"],
cwd: import.meta.dirname,
env: childEnv({ PGLITE_DATA_DIR: dataDir }),
});
const client = new Client(
{ name: "test-client", version: "1.0.0" },
{ capabilities: {} },
);
try {
await client.connect(transport);
console.log("✅ Connected to MCP server\n");
// Test 1: List tools
console.log("📋 Listing available tools...");
const toolsResult = await client.listTools();
console.log(` Found ${toolsResult.tools.length} tools:`);
for (const tool of toolsResult.tools) {
console.log(` - ${tool.name}: ${tool.description}`);
}
console.log();
// Test 2: Get agent info
console.log("ℹ️ Getting agent info...");
const infoResult = await client.callTool({
name: "get_agent_info",
arguments: {},
});
const infoContent = infoResult.content as Array<{
type: string;
text?: string;
}>;
if (infoContent[0]?.type === "text" && infoContent[0].text) {
const info = JSON.parse(infoContent[0].text);
console.log(` Name: ${info.name}`);
console.log(` Bio: ${info.bio}`);
console.log(` Capabilities: ${info.capabilities.join(", ")}`);
}
console.log();
// Test 3: Chat with agent (live model path, opt-in for local/manual runs)
if (process.env.ELIZA_EXAMPLE_MCP_LIVE_CHAT !== "1") {
console.log(
"💬 Skipping live chat test (set ELIZA_EXAMPLE_MCP_LIVE_CHAT=1 to run)",
);
console.log("\n✅ All tests passed!");
return;
}
console.log("💬 Testing chat...");
const testMessages = [
"Hello! What's your name?",
"What can you help me with?",
];
for (const message of testMessages) {
console.log(` User: ${message}`);
const chatResult = await client.callTool({
name: "chat",
arguments: { message },
});
const chatContent = chatResult.content as Array<{
type: string;
text?: string;
}>;
const responseText = chatContent[0]?.text ?? "";
if (chatResult.isError || responseText.startsWith("Error:")) {
throw new Error(responseText || "MCP chat tool returned an error");
}
if (chatContent[0]?.type === "text" && responseText) {
console.log(` Agent: ${responseText}`);
}
console.log();
}
console.log("✅ All tests passed!");
} catch (error) {
console.error("❌ Test failed:", error);
process.exit(1);
} finally {
await transport.close();
await rm(dataDir, { recursive: true, force: true });
}
}
main();