mcp_math_server 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/index.js +317 -0
- package/package.json +17 -0
package/index.js
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
4
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
5
|
+
import {
|
|
6
|
+
CallToolRequestSchema,
|
|
7
|
+
ListToolsRequestSchema,
|
|
8
|
+
} from '@modelcontextprotocol/sdk/types.js';
|
|
9
|
+
|
|
10
|
+
// 求和函数
|
|
11
|
+
function sum(...numbers) {
|
|
12
|
+
return numbers.reduce((total, num) => total + num, 0);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// 求积函数
|
|
16
|
+
function multiply(...numbers) {
|
|
17
|
+
return numbers.reduce((total, num) => total * num, 1);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// 减法函数
|
|
21
|
+
function subtract(a, b) {
|
|
22
|
+
return a - b;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// 除法函数
|
|
26
|
+
function divide(a, b) {
|
|
27
|
+
if (b === 0) {
|
|
28
|
+
throw new Error('除数不能为零');
|
|
29
|
+
}
|
|
30
|
+
return a / b;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 创建 MCP 服务器
|
|
34
|
+
const server = new Server(
|
|
35
|
+
{
|
|
36
|
+
name: 'sum-mcp-server',
|
|
37
|
+
version: '1.0.0',
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
capabilities: {
|
|
41
|
+
tools: {},
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
// 注册工具列表
|
|
47
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
48
|
+
return {
|
|
49
|
+
tools: [
|
|
50
|
+
{
|
|
51
|
+
name: 'local_sum',
|
|
52
|
+
description: '计算多个数字的和',
|
|
53
|
+
inputSchema: {
|
|
54
|
+
type: 'object',
|
|
55
|
+
properties: {
|
|
56
|
+
numbers: {
|
|
57
|
+
type: 'array',
|
|
58
|
+
items: {
|
|
59
|
+
type: 'number',
|
|
60
|
+
},
|
|
61
|
+
description: '要相加的数字数组',
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
required: ['numbers'],
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
name: 'local_multiply',
|
|
69
|
+
description: '计算多个数字的积',
|
|
70
|
+
inputSchema: {
|
|
71
|
+
type: 'object',
|
|
72
|
+
properties: {
|
|
73
|
+
numbers: {
|
|
74
|
+
type: 'array',
|
|
75
|
+
items: {
|
|
76
|
+
type: 'number',
|
|
77
|
+
},
|
|
78
|
+
description: '要相乘的数字数组',
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
required: ['numbers'],
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
name: 'local_subtract',
|
|
86
|
+
description: '计算两个数字的差',
|
|
87
|
+
inputSchema: {
|
|
88
|
+
type: 'object',
|
|
89
|
+
properties: {
|
|
90
|
+
a: {
|
|
91
|
+
type: 'number',
|
|
92
|
+
description: '被减数',
|
|
93
|
+
},
|
|
94
|
+
b: {
|
|
95
|
+
type: 'number',
|
|
96
|
+
description: '减数',
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
required: ['a', 'b'],
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
name: 'local_divide',
|
|
104
|
+
description: '计算两个数字的商',
|
|
105
|
+
inputSchema: {
|
|
106
|
+
type: 'object',
|
|
107
|
+
properties: {
|
|
108
|
+
a: {
|
|
109
|
+
type: 'number',
|
|
110
|
+
description: '被除数',
|
|
111
|
+
},
|
|
112
|
+
b: {
|
|
113
|
+
type: 'number',
|
|
114
|
+
description: '除数',
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
required: ['a', 'b'],
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
};
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// 处理工具调用
|
|
125
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
126
|
+
const { name, arguments: args } = request.params;
|
|
127
|
+
|
|
128
|
+
if (name === 'local_sum') {
|
|
129
|
+
const { numbers } = args;
|
|
130
|
+
|
|
131
|
+
if (!Array.isArray(numbers)) {
|
|
132
|
+
return {
|
|
133
|
+
content: [
|
|
134
|
+
{
|
|
135
|
+
type: 'text',
|
|
136
|
+
text: '错误: 参数 numbers 必须是数组',
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
isError: true,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (numbers.length === 0) {
|
|
144
|
+
return {
|
|
145
|
+
content: [
|
|
146
|
+
{
|
|
147
|
+
type: 'text',
|
|
148
|
+
text: '错误: 至少需要提供一个数字',
|
|
149
|
+
},
|
|
150
|
+
],
|
|
151
|
+
isError: true,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (!numbers.every(num => typeof num === 'number')) {
|
|
156
|
+
return {
|
|
157
|
+
content: [
|
|
158
|
+
{
|
|
159
|
+
type: 'text',
|
|
160
|
+
text: '错误: 数组中的所有元素必须是数字',
|
|
161
|
+
},
|
|
162
|
+
],
|
|
163
|
+
isError: true,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const result = sum(...numbers);
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
content: [
|
|
171
|
+
{
|
|
172
|
+
type: 'text',
|
|
173
|
+
text: `结果: ${result}`,
|
|
174
|
+
},
|
|
175
|
+
],
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (name === 'local_multiply') {
|
|
180
|
+
const { numbers } = args;
|
|
181
|
+
|
|
182
|
+
if (!Array.isArray(numbers)) {
|
|
183
|
+
return {
|
|
184
|
+
content: [
|
|
185
|
+
{
|
|
186
|
+
type: 'text',
|
|
187
|
+
text: '错误: 参数 numbers 必须是数组',
|
|
188
|
+
},
|
|
189
|
+
],
|
|
190
|
+
isError: true,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (numbers.length === 0) {
|
|
195
|
+
return {
|
|
196
|
+
content: [
|
|
197
|
+
{
|
|
198
|
+
type: 'text',
|
|
199
|
+
text: '错误: 至少需要提供一个数字',
|
|
200
|
+
},
|
|
201
|
+
],
|
|
202
|
+
isError: true,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (!numbers.every(num => typeof num === 'number')) {
|
|
207
|
+
return {
|
|
208
|
+
content: [
|
|
209
|
+
{
|
|
210
|
+
type: 'text',
|
|
211
|
+
text: '错误: 数组中的所有元素必须是数字',
|
|
212
|
+
},
|
|
213
|
+
],
|
|
214
|
+
isError: true,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const result = multiply(...numbers);
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
content: [
|
|
222
|
+
{
|
|
223
|
+
type: 'text',
|
|
224
|
+
text: `结果: ${result}`,
|
|
225
|
+
},
|
|
226
|
+
],
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (name === 'local_subtract') {
|
|
231
|
+
const { a, b } = args;
|
|
232
|
+
|
|
233
|
+
if (typeof a !== 'number' || typeof b !== 'number') {
|
|
234
|
+
return {
|
|
235
|
+
content: [
|
|
236
|
+
{
|
|
237
|
+
type: 'text',
|
|
238
|
+
text: '错误: 参数 a 和 b 必须是数字',
|
|
239
|
+
},
|
|
240
|
+
],
|
|
241
|
+
isError: true,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const result = subtract(a, b);
|
|
246
|
+
|
|
247
|
+
return {
|
|
248
|
+
content: [
|
|
249
|
+
{
|
|
250
|
+
type: 'text',
|
|
251
|
+
text: `结果: ${result}`,
|
|
252
|
+
},
|
|
253
|
+
],
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (name === 'local_divide') {
|
|
258
|
+
const { a, b } = args;
|
|
259
|
+
|
|
260
|
+
if (typeof a !== 'number' || typeof b !== 'number') {
|
|
261
|
+
return {
|
|
262
|
+
content: [
|
|
263
|
+
{
|
|
264
|
+
type: 'text',
|
|
265
|
+
text: '错误: 参数 a 和 b 必须是数字',
|
|
266
|
+
},
|
|
267
|
+
],
|
|
268
|
+
isError: true,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
try {
|
|
273
|
+
const result = divide(a, b);
|
|
274
|
+
|
|
275
|
+
return {
|
|
276
|
+
content: [
|
|
277
|
+
{
|
|
278
|
+
type: 'text',
|
|
279
|
+
text: `结果: ${result}`,
|
|
280
|
+
},
|
|
281
|
+
],
|
|
282
|
+
};
|
|
283
|
+
} catch (error) {
|
|
284
|
+
return {
|
|
285
|
+
content: [
|
|
286
|
+
{
|
|
287
|
+
type: 'text',
|
|
288
|
+
text: `错误: ${error.message}`,
|
|
289
|
+
},
|
|
290
|
+
],
|
|
291
|
+
isError: true,
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return {
|
|
297
|
+
content: [
|
|
298
|
+
{
|
|
299
|
+
type: 'text',
|
|
300
|
+
text: `未知的工具: ${name}`,
|
|
301
|
+
},
|
|
302
|
+
],
|
|
303
|
+
isError: true,
|
|
304
|
+
};
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
// 启动服务器
|
|
308
|
+
async function main() {
|
|
309
|
+
const transport = new StdioServerTransport();
|
|
310
|
+
await server.connect(transport);
|
|
311
|
+
console.error('Sum MCP 服务器已启动');
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
main().catch((error) => {
|
|
315
|
+
console.error('服务器启动失败:', error);
|
|
316
|
+
process.exit(1);
|
|
317
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mcp_math_server",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "MCP 服务 - 计算函数",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"start": "node index.js",
|
|
9
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
10
|
+
},
|
|
11
|
+
"keywords": ["mcp", "model-context-protocol", "math", "calculator", "server"],
|
|
12
|
+
"author": "",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@modelcontextprotocol/sdk": "^1.25.1"
|
|
16
|
+
}
|
|
17
|
+
}
|