mcp-adder-simple 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -0
- package/mcp-adder.js +100 -0
- package/package.json +24 -0
package/README.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# MCP Adder
|
|
2
|
+
|
|
3
|
+
A simple MCP (Model Context Protocol) server for adding two numbers.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npx @xiaosheng/mcp-adder
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Add this server to your MCP client configuration:
|
|
14
|
+
|
|
15
|
+
```json
|
|
16
|
+
{
|
|
17
|
+
"mcpServers": {
|
|
18
|
+
"adder": {
|
|
19
|
+
"command": "npx",
|
|
20
|
+
"args": ["@xiaosheng/mcp-adder"]
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Tools
|
|
27
|
+
|
|
28
|
+
- `add_numbers`: Adds two numbers together
|
|
29
|
+
- `a` (number): The first number
|
|
30
|
+
- `b` (number): The second number
|
|
31
|
+
|
|
32
|
+
## License
|
|
33
|
+
|
|
34
|
+
MIT
|
package/mcp-adder.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const readline = require('readline');
|
|
4
|
+
|
|
5
|
+
// 创建 readline 接口
|
|
6
|
+
const rl = readline.createInterface({
|
|
7
|
+
input: process.stdin,
|
|
8
|
+
output: process.stdout,
|
|
9
|
+
terminal: false
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
// 强制启用 stdin,避免延迟
|
|
13
|
+
process.stdin.setEncoding('utf8');
|
|
14
|
+
process.stdin.resume(); // 关键!防止 Node.js 缓冲输入
|
|
15
|
+
|
|
16
|
+
console.error('[MCP] ✅ 服务启动,等待 initialize...');
|
|
17
|
+
|
|
18
|
+
rl.on('line', (line) => {
|
|
19
|
+
try {
|
|
20
|
+
const trimmed = line.trim();
|
|
21
|
+
if (!trimmed) return; // 跳过空行
|
|
22
|
+
const msg = JSON.parse(trimmed);
|
|
23
|
+
|
|
24
|
+
if (!msg.id) return;
|
|
25
|
+
|
|
26
|
+
let result = null;
|
|
27
|
+
let error = null;
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
switch (msg.method) {
|
|
31
|
+
case 'initialize':
|
|
32
|
+
result = { protocolVersion: '2024-10-07' };
|
|
33
|
+
console.error('[MCP] ← initialize 成功响应');
|
|
34
|
+
break;
|
|
35
|
+
|
|
36
|
+
case 'list_tools':
|
|
37
|
+
result = {
|
|
38
|
+
tools: [
|
|
39
|
+
{
|
|
40
|
+
name: 'add_numbers',
|
|
41
|
+
description: '将两个数字相加',
|
|
42
|
+
inputSchema: {
|
|
43
|
+
type: 'object',
|
|
44
|
+
properties: {
|
|
45
|
+
a: { type: 'number', description: '第一个数' },
|
|
46
|
+
b: { type: 'number', description: '第二个数' }
|
|
47
|
+
},
|
|
48
|
+
required: ['a', 'b']
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
};
|
|
53
|
+
console.error('[MCP] ← list_tools 成功响应');
|
|
54
|
+
break;
|
|
55
|
+
|
|
56
|
+
case 'call_tool':
|
|
57
|
+
const { name, arguments: args } = msg.params;
|
|
58
|
+
if (name === 'add_numbers') {
|
|
59
|
+
const { a, b } = args;
|
|
60
|
+
if (typeof a !== 'number' || typeof b !== 'number') {
|
|
61
|
+
throw new Error('参数必须是数字');
|
|
62
|
+
}
|
|
63
|
+
result = {
|
|
64
|
+
content: [{ type: 'text', text: `${a} + ${b} = ${a + b}` }]
|
|
65
|
+
};
|
|
66
|
+
} else {
|
|
67
|
+
throw new Error(`未知工具: ${name}`);
|
|
68
|
+
}
|
|
69
|
+
break;
|
|
70
|
+
|
|
71
|
+
default:
|
|
72
|
+
throw new Error(`不支持的方法: ${msg.method}`);
|
|
73
|
+
}
|
|
74
|
+
} catch (err) {
|
|
75
|
+
error = { code: -32602, message: err.message };
|
|
76
|
+
console.error('[MCP] ❌ 错误:', err.message);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const response = { jsonrpc: '2.0', id: msg.id };
|
|
80
|
+
if (error) {
|
|
81
|
+
response.error = error;
|
|
82
|
+
} else {
|
|
83
|
+
response.result = result;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
console.log(JSON.stringify(response));
|
|
87
|
+
} catch (e) {
|
|
88
|
+
console.error('[MCP] ❌ JSON 解析失败:', line);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
rl.on('close', () => {
|
|
93
|
+
console.error('[MCP] 连接关闭,退出服务');
|
|
94
|
+
process.exit(0);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// ⚠️ 关键:防止 Node.js 阻塞
|
|
98
|
+
process.stdin.on('end', () => {
|
|
99
|
+
process.exit(0);
|
|
100
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mcp-adder-simple",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "mcp-adder.js",
|
|
5
|
+
"bin": {
|
|
6
|
+
"mcp-adder": "./mcp-adder.js"
|
|
7
|
+
},
|
|
8
|
+
"scripts": {
|
|
9
|
+
"start": "node mcp-adder.js"
|
|
10
|
+
},
|
|
11
|
+
"keywords": ["mcp", "model-context-protocol", "calculator", "add"],
|
|
12
|
+
"author": "xiaosheng_2021",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"description": "A simple MCP server for adding numbers",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/xiaosheng-2021/mcp-adder.git"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"mcp-adder.js",
|
|
21
|
+
"package.json",
|
|
22
|
+
"README.md"
|
|
23
|
+
]
|
|
24
|
+
}
|