eagle-rag 1.0.1

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 (3) hide show
  1. package/README.md +116 -0
  2. package/package.json +25 -0
  3. package/src/index.js +139 -0
package/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # Coze RAG MCP Server
2
+
3
+ 通过 npx 运行的 MCP Server,对接 Coze 工作流,实现 RAG 问答功能。
4
+
5
+ ## 功能特性
6
+
7
+ - **身份识别**: 自动获取机器唯一 ID,作为用户标识
8
+ - **RAG 问答**: 将用户问题传递给 Coze 工作流,获取知识库相关答案
9
+ - **MCP 协议**: 完全兼容 Model Context Protocol
10
+
11
+ ## 快速开始
12
+
13
+ ### 1. 安装依赖
14
+
15
+ ```bash
16
+ npm install
17
+ ```
18
+
19
+ ### 2. 配置环境变量(可选)
20
+
21
+ 可以通过环境变量覆盖默认配置:
22
+
23
+ ```bash
24
+ # 设置 Coze API Token
25
+ export COZE_API_TOKEN="your_api_token"
26
+
27
+ # 设置 Coze 工作流 ID
28
+ export COZE_WORKFLOW_ID="your_workflow_id"
29
+ ```
30
+
31
+ ### 3. 运行 MCP Server
32
+
33
+ ```bash
34
+ # 使用 npx 运行
35
+ npx coze-rag-mcp
36
+
37
+ # 或者使用 npm 脚本
38
+ npm start
39
+ ```
40
+
41
+ ## MCP 工具
42
+
43
+ ### ask
44
+
45
+ 向 Coze RAG 工作流提问,获取知识库相关答案。
46
+
47
+ **参数:**
48
+ - `question` (string): 用户问题
49
+
50
+ **返回值:**
51
+ - 知识库检索结果
52
+
53
+ **示例:**
54
+ ```json
55
+ {
56
+ "question": "如何创建无边框窗口?"
57
+ }
58
+ ```
59
+
60
+ ### get_machine_id
61
+
62
+ 获取当前机器的唯一标识 ID。
63
+
64
+ **参数:**
65
+ - 无
66
+
67
+ **返回值:**
68
+ - 机器唯一 ID
69
+
70
+ ## 配置说明
71
+
72
+ | 环境变量 | 默认值 | 说明 |
73
+ |---------|--------|------|
74
+ | `COZE_API_TOKEN` | `sat_pqqZVXMBiDTas0khz2x3vBqrCcicmfOt9XlNi4KrbtUsRtbroa8i1z5wfGrcXWdn` | Coze API Token |
75
+ | `COZE_WORKFLOW_ID` | `7610685653369798719` | Coze 工作流 ID |
76
+
77
+ ## 在 Cursor 中使用
78
+
79
+ 在 Cursor 的 `mcp.json` 配置文件中添加:
80
+
81
+ ```json
82
+ {
83
+ "mcpServers": {
84
+ "coze-rag": {
85
+ "command": "npx",
86
+ "args": ["coze-rag-mcp"],
87
+ "env": {
88
+ "COZE_API_TOKEN": "your_api_token"
89
+ }
90
+ }
91
+ }
92
+ }
93
+ ```
94
+
95
+ ## API 请求格式
96
+
97
+ ```javascript
98
+ POST https://api.coze.cn/v1/workflow/run
99
+ Authorization: Bearer <token>
100
+ Content-Type: application/json
101
+
102
+ {
103
+ "workflow_id": "7610685653369798719",
104
+ "parameters": {
105
+ "user_id": "<machine_id>",
106
+ "question": "<user_question>"
107
+ }
108
+ }
109
+ ```
110
+
111
+ ## 依赖
112
+
113
+ - `@modelcontextprotocol/sdk` - MCP Server SDK
114
+ - `axios` - HTTP 请求库
115
+ - `node-machine-id` - 机器唯一 ID 获取
116
+ - `zod` - 参数验证
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "eagle-rag",
3
+ "version": "1.0.1",
4
+ "description": "MCP Server对接Coze工作流,实现RAG问答",
5
+ "type": "module",
6
+ "main": "src/index.js",
7
+ "bin": {
8
+ "eagle-rag": "./src/index.js"
9
+ },
10
+ "scripts": {
11
+ "start": "node src/index.js"
12
+ },
13
+ "keywords": ["mcp", "coze", "rag", "ai", "cursor"],
14
+ "author": "",
15
+ "license": "MIT",
16
+ "dependencies": {
17
+ "@modelcontextprotocol/sdk": "^1.0.0",
18
+ "axios": "^1.6.0",
19
+ "node-machine-id": "^1.1.12",
20
+ "zod": "^3.22.0"
21
+ },
22
+ "engines": {
23
+ "node": ">=18.0.0"
24
+ }
25
+ }
package/src/index.js ADDED
@@ -0,0 +1,139 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Coze RAG MCP Server
5
+ * 通过 npx 运行的 MCP Server,对接 Coze 工作流
6
+ */
7
+
8
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
9
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
10
+ import { z } from "zod";
11
+ import axios from "axios";
12
+ import nodeMachineId from "node-machine-id";
13
+
14
+ const { machineIdSync } = nodeMachineId;
15
+
16
+ // ==================== 配置 ====================
17
+ // Coze API 配置
18
+ const COZE_API_URL = "https://api.coze.cn/v1/workflow/run";
19
+ // Coze API Token (写死)
20
+ const COZE_API_TOKEN = "sat_pqqZVXMBiDTas0khz2x3vBqrCcicmfOt9XlNi4KrbtUsRtbroa8i1z5wfGrcXWdn";
21
+ // Coze 工作流 ID
22
+ const WORKFLOW_ID = "7610685653369798719";
23
+
24
+ // ==================== 获取机器唯一 ID ====================
25
+ let cachedMachineId = null;
26
+
27
+ function getMachineId() {
28
+ if (!cachedMachineId) {
29
+ try {
30
+ // 获取机器唯一 ID
31
+ cachedMachineId = machineIdSync({ original: true });
32
+ console.error(`[Coze RAG MCP] Machine ID: ${cachedMachineId}`);
33
+ } catch (error) {
34
+ console.error(`[Coze RAG MCP] Failed to get machine ID: ${error.message}`);
35
+ // 使用默认 ID
36
+ cachedMachineId = "unknown";
37
+ }
38
+ }
39
+ return cachedMachineId;
40
+ }
41
+
42
+ // ==================== Coze API 调用 ====================
43
+ async function callCozeWorkflow(userId, question) {
44
+ try {
45
+ const response = await axios.post(
46
+ COZE_API_URL,
47
+ {
48
+ workflow_id: WORKFLOW_ID,
49
+ parameters: {
50
+ user_id: userId,
51
+ question: question
52
+ }
53
+ },
54
+ {
55
+ headers: {
56
+ "Authorization": `Bearer ${COZE_API_TOKEN}`,
57
+ "Content-Type": "application/json"
58
+ },
59
+ timeout: 60000 // 60秒超时
60
+ }
61
+ );
62
+
63
+ return response.data;
64
+ } catch (error) {
65
+ console.error(`[Coze RAG MCP] API Error: ${error.message}`);
66
+ if (error.response) {
67
+ return {
68
+ code: error.response.status,
69
+ msg: error.message,
70
+ data: JSON.stringify({ error: error.response.data })
71
+ };
72
+ }
73
+ return {
74
+ code: -1,
75
+ msg: error.message,
76
+ data: JSON.stringify({ error: "Network error" })
77
+ };
78
+ }
79
+ }
80
+
81
+ // ==================== MCP Server 工具 ====================
82
+ const server = new McpServer({
83
+ name: "coze-rag",
84
+ version: "1.0.0",
85
+ capabilities: {
86
+ resources: {},
87
+ tools: {},
88
+ },
89
+ });
90
+
91
+ // 注册问答工具
92
+ server.tool(
93
+ "ask",
94
+ "向 Coze RAG 工作流提问,获取知识库相关答案",
95
+ {
96
+ question: z.string().describe("用户问题")
97
+ },
98
+ async ({ question }) => {
99
+ const userId = getMachineId();
100
+ console.error(`[Coze RAG MCP] Processing question: "${question}" from user: ${userId}`);
101
+
102
+ const result = await callCozeWorkflow(userId, question);
103
+
104
+ // 解析返回结果
105
+ let responseText = "";
106
+ if (result.code === 0) {
107
+ try {
108
+ // data 字段是 JSON 字符串,需要解析
109
+ const data = JSON.parse(result.data);
110
+ responseText = data.result || JSON.stringify(data);
111
+ } catch {
112
+ responseText = result.data;
113
+ }
114
+ } else {
115
+ responseText = `错误: ${result.msg}`;
116
+ }
117
+
118
+ return {
119
+ content: [
120
+ {
121
+ type: "text",
122
+ text: responseText
123
+ }
124
+ ]
125
+ };
126
+ }
127
+ );
128
+
129
+ // ==================== 启动 Server ====================
130
+ async function main() {
131
+ const transport = new StdioServerTransport();
132
+ await server.connect(transport);
133
+ console.error("[Coze RAG MCP] Server started successfully");
134
+ }
135
+
136
+ main().catch((error) => {
137
+ console.error("[Coze RAG MCP] Server error:", error);
138
+ process.exit(1);
139
+ });