@xiaozhi-client/calculator-mcp 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/LICENSE +21 -0
- package/README.md +45 -0
- package/dist/index.js +73 -0
- package/dist/index.js.map +1 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 shenjingnan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# @xiaozhi-client/calculator-mcp
|
|
2
|
+
|
|
3
|
+
小智计算器 MCP 服务,提供数学计算功能。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g @xiaozhi-client/calculator-mcp
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 使用
|
|
12
|
+
|
|
13
|
+
### 在 xiaozhi.config.json 中配置
|
|
14
|
+
|
|
15
|
+
```json
|
|
16
|
+
{
|
|
17
|
+
"mcpServers": {
|
|
18
|
+
"calculator": {
|
|
19
|
+
"command": "npx",
|
|
20
|
+
"args": ["-y", "@xiaozhi-client/calculator-mcp"]
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### 提供的工具
|
|
27
|
+
|
|
28
|
+
#### calculator
|
|
29
|
+
计算数学表达式
|
|
30
|
+
|
|
31
|
+
**参数**:
|
|
32
|
+
- `expression` (string): 要计算的数学表达式
|
|
33
|
+
|
|
34
|
+
**返回**:
|
|
35
|
+
计算结果
|
|
36
|
+
|
|
37
|
+
## 开发
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
# 构建
|
|
41
|
+
pnpm --filter @xiaozhi-client/calculator-mcp build
|
|
42
|
+
|
|
43
|
+
# 本地运行
|
|
44
|
+
node dist/index.js
|
|
45
|
+
```
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
#!/usr/bin/env node
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
6
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
var logger = {
|
|
9
|
+
info: (message) => {
|
|
10
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
11
|
+
console.error(`${timestamp} - Calculator - INFO - ${message}`);
|
|
12
|
+
},
|
|
13
|
+
error: (message) => {
|
|
14
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
15
|
+
console.error(`${timestamp} - Calculator - ERROR - ${message}`);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var server = new McpServer({
|
|
19
|
+
name: "@xiaozhi-client/calculator-mcp",
|
|
20
|
+
version: "1.0.0"
|
|
21
|
+
});
|
|
22
|
+
server.tool(
|
|
23
|
+
"calculator",
|
|
24
|
+
"\u7528\u4E8E\u6570\u5B66\u8BA1\u7B97\uFF0C\u8BA1\u7B97 JavaScript \u8868\u8FBE\u5F0F\u7684\u7ED3\u679C",
|
|
25
|
+
{
|
|
26
|
+
expression: z.string().describe("\u8981\u8BA1\u7B97\u7684\u6570\u5B66\u8868\u8FBE\u5F0F")
|
|
27
|
+
},
|
|
28
|
+
async ({ expression }) => {
|
|
29
|
+
try {
|
|
30
|
+
const result = eval(expression);
|
|
31
|
+
logger.info(`\u8BA1\u7B97\u8868\u8FBE\u5F0F: ${expression}, \u7ED3\u679C: ${result}`);
|
|
32
|
+
return {
|
|
33
|
+
content: [
|
|
34
|
+
{
|
|
35
|
+
type: "text",
|
|
36
|
+
text: JSON.stringify({
|
|
37
|
+
success: true,
|
|
38
|
+
result
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
};
|
|
43
|
+
} catch (error) {
|
|
44
|
+
logger.error(`\u8BA1\u7B97\u9519\u8BEF: ${error.message}`);
|
|
45
|
+
return {
|
|
46
|
+
content: [
|
|
47
|
+
{
|
|
48
|
+
type: "text",
|
|
49
|
+
text: JSON.stringify({
|
|
50
|
+
success: false,
|
|
51
|
+
error: error.message
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
],
|
|
55
|
+
isError: true
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
async function main() {
|
|
61
|
+
const transport = new StdioServerTransport();
|
|
62
|
+
await server.connect(transport);
|
|
63
|
+
logger.info("Calculator MCP \u670D\u52A1\u5DF2\u542F\u52A8");
|
|
64
|
+
}
|
|
65
|
+
main().catch((error) => {
|
|
66
|
+
logger.error(`\u542F\u52A8\u670D\u52A1\u5931\u8D25: ${error.message}`);
|
|
67
|
+
process.exit(1);
|
|
68
|
+
});
|
|
69
|
+
var src_default = server;
|
|
70
|
+
export {
|
|
71
|
+
src_default as default
|
|
72
|
+
};
|
|
73
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["#!/usr/bin/env node\n\n/**\n * MCP Calculator Server\n * 提供数学计算功能的 MCP 服务\n */\n\nimport { McpServer } from \"@modelcontextprotocol/sdk/server/mcp.js\";\nimport { StdioServerTransport } from \"@modelcontextprotocol/sdk/server/stdio.js\";\nimport { z } from \"zod\";\n\n// 日志工具\nconst logger = {\n info: (message: string) => {\n const timestamp = new Date().toISOString();\n console.error(`${timestamp} - Calculator - INFO - ${message}`);\n },\n error: (message: string) => {\n const timestamp = new Date().toISOString();\n console.error(`${timestamp} - Calculator - ERROR - ${message}`);\n },\n};\n\n// 创建 MCP 服务器实例\nconst server = new McpServer({\n name: \"@xiaozhi-client/calculator-mcp\",\n version: \"1.0.0\",\n});\n\n// 注册计算器工具\nserver.tool(\n \"calculator\",\n \"用于数学计算,计算 JavaScript 表达式的结果\",\n {\n expression: z.string().describe(\"要计算的数学表达式\"),\n },\n async ({ expression }) => {\n try {\n // 计算表达式\n // 注意:生产环境可能需要更安全的计算方式\n const result = eval(expression);\n logger.info(`计算表达式: ${expression}, 结果: ${result}`);\n\n return {\n content: [\n {\n type: \"text\",\n text: JSON.stringify({\n success: true,\n result,\n }),\n },\n ],\n };\n } catch (error) {\n logger.error(`计算错误: ${error.message}`);\n return {\n content: [\n {\n type: \"text\",\n text: JSON.stringify({\n success: false,\n error: error.message,\n }),\n },\n ],\n isError: true,\n };\n }\n }\n);\n\n// 启动服务器\nasync function main() {\n const transport = new StdioServerTransport();\n await server.connect(transport);\n logger.info(\"Calculator MCP 服务已启动\");\n}\n\nmain().catch((error) => {\n logger.error(`启动服务失败: ${error.message}`);\n process.exit(1);\n});\n\nexport default server;\n"],"mappings":";;;;AAOA,SAAS,iBAAiB;AAC1B,SAAS,4BAA4B;AACrC,SAAS,SAAS;AAGlB,IAAM,SAAS;AAAA,EACb,MAAM,CAAC,YAAoB;AACzB,UAAM,aAAY,oBAAI,KAAK,GAAE,YAAY;AACzC,YAAQ,MAAM,GAAG,SAAS,0BAA0B,OAAO,EAAE;AAAA,EAC/D;AAAA,EACA,OAAO,CAAC,YAAoB;AAC1B,UAAM,aAAY,oBAAI,KAAK,GAAE,YAAY;AACzC,YAAQ,MAAM,GAAG,SAAS,2BAA2B,OAAO,EAAE;AAAA,EAChE;AACF;AAGA,IAAM,SAAS,IAAI,UAAU;AAAA,EAC3B,MAAM;AAAA,EACN,SAAS;AACX,CAAC;AAGD,OAAO;AAAA,EACL;AAAA,EACA;AAAA,EACA;AAAA,IACE,YAAY,EAAE,OAAO,EAAE,SAAS,wDAAW;AAAA,EAC7C;AAAA,EACA,OAAO,EAAE,WAAW,MAAM;AACxB,QAAI;AAGF,YAAM,SAAS,KAAK,UAAU;AAC9B,aAAO,KAAK,mCAAU,UAAU,mBAAS,MAAM,EAAE;AAEjD,aAAO;AAAA,QACL,SAAS;AAAA,UACP;AAAA,YACE,MAAM;AAAA,YACN,MAAM,KAAK,UAAU;AAAA,cACnB,SAAS;AAAA,cACT;AAAA,YACF,CAAC;AAAA,UACH;AAAA,QACF;AAAA,MACF;AAAA,IACF,SAAS,OAAO;AACd,aAAO,MAAM,6BAAS,MAAM,OAAO,EAAE;AACrC,aAAO;AAAA,QACL,SAAS;AAAA,UACP;AAAA,YACE,MAAM;AAAA,YACN,MAAM,KAAK,UAAU;AAAA,cACnB,SAAS;AAAA,cACT,OAAO,MAAM;AAAA,YACf,CAAC;AAAA,UACH;AAAA,QACF;AAAA,QACA,SAAS;AAAA,MACX;AAAA,IACF;AAAA,EACF;AACF;AAGA,eAAe,OAAO;AACpB,QAAM,YAAY,IAAI,qBAAqB;AAC3C,QAAM,OAAO,QAAQ,SAAS;AAC9B,SAAO,KAAK,+CAAsB;AACpC;AAEA,KAAK,EAAE,MAAM,CAAC,UAAU;AACtB,SAAO,MAAM,yCAAW,MAAM,OAAO,EAAE;AACvC,UAAQ,KAAK,CAAC;AAChB,CAAC;AAED,IAAO,cAAQ;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xiaozhi-client/calculator-mcp",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "小智计算器 MCP 服务,提供数学计算功能",
|
|
6
|
+
"bin": {
|
|
7
|
+
"xiaozhi-calculator-mcp": "dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@modelcontextprotocol/sdk": "^1.0.4",
|
|
18
|
+
"zod": "^3.22.4"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@biomejs/biome": "^1.5.0",
|
|
22
|
+
"tsup": "^8.0.0",
|
|
23
|
+
"typescript": "^5.3.0"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsup",
|
|
27
|
+
"dev": "tsup --watch",
|
|
28
|
+
"type-check": "tsc --noEmit",
|
|
29
|
+
"lint": "biome check src/",
|
|
30
|
+
"lint:fix": "biome check --write src/"
|
|
31
|
+
}
|
|
32
|
+
}
|