-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho_server.js
More file actions
87 lines (78 loc) · 1.95 KB
/
Copy pathecho_server.js
File metadata and controls
87 lines (78 loc) · 1.95 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
#!/usr/bin/env node
const readline = require("node:readline");
const TOOLS = [
{
name: "echo",
description: "Echo back the input text",
inputSchema: {
type: "object",
properties: {
text: { type: "string", description: "Text to echo" },
},
required: ["text"],
},
},
{
name: "add",
description: "Add two numbers",
inputSchema: {
type: "object",
properties: {
a: { type: "number", description: "First number" },
b: { type: "number", description: "Second number" },
},
required: ["a", "b"],
},
},
];
function handleRequest(req) {
const reqId = req.id;
const method = req.method;
const params = req.params || {};
if (method === "tools/list") {
return { jsonrpc: "2.0", id: reqId, result: { tools: TOOLS } };
}
if (method === "tools/call") {
const name = params.name || "";
const args = params.arguments || {};
if (name === "echo") {
const text = args.text || "";
return {
jsonrpc: "2.0",
id: reqId,
result: { content: [{ type: "text", text: `Echo: ${text}` }] },
};
}
if (name === "add") {
const a = args.a || 0;
const b = args.b || 0;
return {
jsonrpc: "2.0",
id: reqId,
result: { content: [{ type: "text", text: String(a + b) }] },
};
}
return {
jsonrpc: "2.0",
id: reqId,
error: { code: -32601, message: `Tool not found: ${name}` },
};
}
return {
jsonrpc: "2.0",
id: reqId,
error: { code: -32601, message: `Method not found: ${method}` },
};
}
const rl = readline.createInterface({ input: process.stdin });
rl.on("line", (line) => {
line = line.trim();
if (!line) return;
try {
const req = JSON.parse(line);
const resp = handleRequest(req);
process.stdout.write(JSON.stringify(resp) + "\n");
} catch (err) {
process.stderr.write(`JSON parse error: ${err.message}\n`);
}
});