mcp-adder-simple 1.0.7 → 1.0.9

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.
Files changed (2) hide show
  1. package/mcp-adder.js +104 -7
  2. package/package.json +4 -3
package/mcp-adder.js CHANGED
@@ -1,10 +1,10 @@
1
1
  /**
2
- * MCP Adder Server
3
- * 一个简单的 MCP (Model Context Protocol) 服务器,提供数字相加功能
2
+ * MCP Calculator Server
3
+ * 一个简单的 MCP (Model Context Protocol) 服务器,提供基本的数学计算功能
4
4
  */
5
5
 
6
6
  const readline = require('readline');
7
-
7
+ const https = require('https');
8
8
  // ========== 配置和初始化 ==========
9
9
 
10
10
  // 创建 readline 接口用于处理 stdin/stdout 通信
@@ -53,7 +53,7 @@ rl.on('line', (line) => {
53
53
  * 处理 MCP 请求
54
54
  * @param {Object} msg - JSON-RPC 请求对象
55
55
  */
56
- function handleRequest(msg) {
56
+ async function handleRequest(msg) {
57
57
  let result = null;
58
58
  let error = null;
59
59
 
@@ -69,7 +69,7 @@ function handleRequest(msg) {
69
69
  break;
70
70
 
71
71
  case 'tools/call':
72
- result = handleToolCall(msg.params);
72
+ result = await handleToolCall(msg.params);
73
73
  break;
74
74
 
75
75
  default:
@@ -107,7 +107,7 @@ function handleInitialize() {
107
107
  return {
108
108
  protocolVersion: '2024-10-07',
109
109
  serverInfo: {
110
- name: 'mcp-adder-server',
110
+ name: 'mcp-calculator-server',
111
111
  version: '1.0.0'
112
112
  },
113
113
  capabilities: {
@@ -140,6 +140,42 @@ function handleToolsList() {
140
140
  },
141
141
  required: ['a', 'b']
142
142
  }
143
+ },
144
+ {
145
+ name: 'multiply_numbers',
146
+ description: '将两个数字相乘',
147
+ inputSchema: {
148
+ type: 'object',
149
+ properties: {
150
+ a: {
151
+ type: 'number',
152
+ description: '第一个数'
153
+ },
154
+ b: {
155
+ type: 'number',
156
+ description: '第二个数'
157
+ }
158
+ },
159
+ required: ['a', 'b']
160
+ }
161
+ },
162
+ {
163
+ name: 'sup8_decode',
164
+ description: '解码',
165
+ inputSchema: {
166
+ type: 'object',
167
+ properties: {
168
+ code: {
169
+ type: 'string',
170
+ description: '数码'
171
+ },
172
+ enterpriseNo: {
173
+ type: 'string',
174
+ description: '企业号'
175
+ }
176
+ },
177
+ required: ['code', 'enterpriseNo']
178
+ }
143
179
  }
144
180
  ]
145
181
  };
@@ -152,7 +188,7 @@ function handleToolsList() {
152
188
  * @param {Object} params.arguments - 工具参数
153
189
  * @returns {Object} 工具执行结果
154
190
  */
155
- function handleToolCall(params) {
191
+ async function handleToolCall(params) {
156
192
  const { name, arguments: args } = params;
157
193
 
158
194
  if (name === 'add_numbers') {
@@ -173,6 +209,67 @@ function handleToolCall(params) {
173
209
  ]
174
210
  };
175
211
 
212
+ } else if (name === 'multiply_numbers') {
213
+ const { a, b } = args;
214
+
215
+ // 参数类型验证
216
+ if (typeof a !== 'number' || typeof b !== 'number') {
217
+ throw new Error('参数必须是数字');
218
+ }
219
+
220
+ // 计算并返回结果
221
+ return {
222
+ content: [
223
+ {
224
+ type: 'text',
225
+ text: `${a} × ${b} = ${a * b}`
226
+ }
227
+ ]
228
+ };
229
+
230
+ } else if (name === 'sup8_decode') {
231
+ const { code, enterpriseNo } = args;
232
+
233
+ // 参数类型验证
234
+ if (typeof code !== 'string' || typeof enterpriseNo !== 'string') {
235
+ throw new Error('参数必须是字符串');
236
+ }
237
+
238
+ // 构建请求 URL
239
+ const url = `https://pts.mama100.cn/restapi/digit/v2/digit/get-digit-info?code=${encodeURIComponent(code)}&enterpriseNo=${encodeURIComponent(enterpriseNo)}`;
240
+
241
+ console.error(`[HTTP] 调用外部API: ${url}`);
242
+
243
+ // 调用外部 API 并返回结果
244
+ const result = await new Promise((resolve, reject) => {
245
+ https.get(url, (res) => {
246
+ let data = '';
247
+ res.on('data', (chunk) => {
248
+ data += chunk;
249
+ });
250
+ res.on('end', () => {
251
+ try {
252
+ const jsonData = JSON.parse(data);
253
+ console.error(`[HTTP] API响应:`, JSON.stringify(jsonData, null, 2));
254
+ resolve(jsonData);
255
+ } catch (e) {
256
+ reject(new Error(`JSON解析失败: ${e.message}`));
257
+ }
258
+ });
259
+ }).on('error', (err) => {
260
+ reject(new Error(`HTTP请求失败: ${err.message}`));
261
+ });
262
+ });
263
+ // 计算并返回结果
264
+ return {
265
+ content: [
266
+ {
267
+ type: 'text',
268
+ text: JSON.stringify(result, null, 2)
269
+ }
270
+ ]
271
+ };
272
+
176
273
  } else {
177
274
  throw new Error(`未知工具: ${name}`);
178
275
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcp-adder-simple",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "main": "mcp-adder.js",
5
5
  "bin": {
6
6
  "mcp-adder-simple": "./bin/mcp-adder-simple.cmd"
@@ -12,11 +12,12 @@
12
12
  "mcp",
13
13
  "model-context-protocol",
14
14
  "calculator",
15
- "add"
15
+ "add",
16
+ "multiply"
16
17
  ],
17
18
  "author": "xiaosheng_2021",
18
19
  "license": "MIT",
19
- "description": "A simple MCP server for adding numbers",
20
+ "description": "A simple MCP server for basic math calculations",
20
21
  "repository": {
21
22
  "type": "git",
22
23
  "url": "https://github.com/xiaosheng-2021/mcp-adder.git"