cloudflare-mcp-smart-proxy 1.1.0 → 1.2.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 +1 -1
- package/package-personal.json +1 -1
- package/package.json +1 -1
- package/publish.sh +6 -6
- package/src/router.js +87 -3
package/index.js
CHANGED
|
@@ -32,7 +32,7 @@ if (!CLOUD_URL || !CLOUD_API_KEY) {
|
|
|
32
32
|
|
|
33
33
|
// 初始化组件
|
|
34
34
|
const localTools = new LocalToolExecutor(WORKSPACE_ROOT);
|
|
35
|
-
const router = new SmartRouter(CLOUD_URL, CLOUD_API_KEY, localTools);
|
|
35
|
+
const router = new SmartRouter(CLOUD_URL, CLOUD_API_KEY, localTools, WORKSPACE_ROOT);
|
|
36
36
|
|
|
37
37
|
// 创建 MCP 服务器
|
|
38
38
|
const server = new Server(
|
package/package-personal.json
CHANGED
package/package.json
CHANGED
package/publish.sh
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
set -e
|
|
6
6
|
|
|
7
|
-
echo "📦 准备发布
|
|
7
|
+
echo "📦 准备发布 cloudflare-mcp-smart-proxy 到 npm"
|
|
8
8
|
echo "=============================================="
|
|
9
9
|
echo ""
|
|
10
10
|
|
|
@@ -44,9 +44,9 @@ fi
|
|
|
44
44
|
|
|
45
45
|
echo ""
|
|
46
46
|
|
|
47
|
-
#
|
|
48
|
-
PACKAGE_NAME
|
|
49
|
-
echo "
|
|
47
|
+
# 检查包名
|
|
48
|
+
PACKAGE_NAME=$(node -p "require('./package.json').name")
|
|
49
|
+
echo "包名: $PACKAGE_NAME"
|
|
50
50
|
|
|
51
51
|
# 检查版本
|
|
52
52
|
CURRENT_VERSION=$(node -p "require('./package.json').version")
|
|
@@ -77,10 +77,10 @@ if [ $? -eq 0 ]; then
|
|
|
77
77
|
echo "=============================================="
|
|
78
78
|
echo "✅ 发布成功!"
|
|
79
79
|
echo ""
|
|
80
|
-
echo "包地址: https://www.npmjs.com/package
|
|
80
|
+
echo "包地址: https://www.npmjs.com/package/$PACKAGE_NAME"
|
|
81
81
|
echo ""
|
|
82
82
|
echo "安装方式:"
|
|
83
|
-
echo " npx -y
|
|
83
|
+
echo " npx -y $PACKAGE_NAME"
|
|
84
84
|
echo ""
|
|
85
85
|
else
|
|
86
86
|
echo ""
|
package/src/router.js
CHANGED
|
@@ -3,10 +3,11 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
export class SmartRouter {
|
|
6
|
-
constructor(cloudUrl, cloudApiKey, localTools) {
|
|
6
|
+
constructor(cloudUrl, cloudApiKey, localTools, workspaceRoot = null) {
|
|
7
7
|
this.cloudUrl = cloudUrl.replace(/\/$/, ''); // 移除尾部斜杠
|
|
8
8
|
this.cloudApiKey = cloudApiKey;
|
|
9
9
|
this.localTools = localTools;
|
|
10
|
+
this.workspaceRoot = workspaceRoot || process.cwd();
|
|
10
11
|
|
|
11
12
|
// 工具路由规则
|
|
12
13
|
this.routingRules = {
|
|
@@ -100,10 +101,55 @@ export class SmartRouter {
|
|
|
100
101
|
}
|
|
101
102
|
}
|
|
102
103
|
|
|
104
|
+
/**
|
|
105
|
+
* 提取项目根路径(从工作区根路径)
|
|
106
|
+
* 返回项目根目录名,用于项目锚定
|
|
107
|
+
*/
|
|
108
|
+
_extractProjectRootFromWorkspace() {
|
|
109
|
+
if (!this.workspaceRoot) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 规范化路径:统一使用正斜杠,移除尾部斜杠
|
|
114
|
+
let normalized = this.workspaceRoot.replace(/\\/g, '/').replace(/\/$/, '');
|
|
115
|
+
|
|
116
|
+
if (!normalized) {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// 提取最后一个目录名作为项目根路径
|
|
121
|
+
const parts = normalized.split('/').filter(p => p.length > 0);
|
|
122
|
+
return parts.length > 0 ? parts[parts.length - 1] : null;
|
|
123
|
+
}
|
|
124
|
+
|
|
103
125
|
/**
|
|
104
126
|
* 调用云端工具
|
|
105
127
|
*/
|
|
106
128
|
async callCloudTool(toolName, params) {
|
|
129
|
+
// 对于 skill_* 工具,自动注入项目根路径信息
|
|
130
|
+
if (toolName.startsWith('skill_')) {
|
|
131
|
+
// 如果参数中没有 project_root 且没有 paths,自动添加 project_root
|
|
132
|
+
if (!params.project_root && (!params.paths || params.paths.length === 0)) {
|
|
133
|
+
const projectRoot = this._extractProjectRootFromWorkspace();
|
|
134
|
+
if (projectRoot) {
|
|
135
|
+
params.project_root = projectRoot;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// 如果参数中有 paths 但都是相对路径,尝试转换为相对于工作区根路径的路径
|
|
140
|
+
if (params.paths && Array.isArray(params.paths) && params.paths.length > 0) {
|
|
141
|
+
// 确保路径是相对于工作区根路径的
|
|
142
|
+
params.paths = params.paths.map(path => {
|
|
143
|
+
// 如果路径是绝对路径且在工作区根路径下,转换为相对路径
|
|
144
|
+
if (path.startsWith('/') && path.startsWith(this.workspaceRoot)) {
|
|
145
|
+
return path.replace(this.workspaceRoot + '/', '').replace(this.workspaceRoot, '');
|
|
146
|
+
}
|
|
147
|
+
// 如果路径已经是相对路径,保持不变
|
|
148
|
+
return path;
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
107
153
|
try {
|
|
108
154
|
const response = await fetch(`${this.cloudUrl}/mcp`, {
|
|
109
155
|
method: 'POST',
|
|
@@ -129,7 +175,24 @@ export class SmartRouter {
|
|
|
129
175
|
const result = await response.json();
|
|
130
176
|
|
|
131
177
|
if (result.error) {
|
|
132
|
-
|
|
178
|
+
// MCP 标准格式:详细错误信息在 data 字段中
|
|
179
|
+
// 如果 data 是对象,尝试提取更多信息
|
|
180
|
+
let errorMessage = result.error.data || result.error.message || 'Cloud tool execution failed';
|
|
181
|
+
|
|
182
|
+
// 如果 data 是对象,尝试提取 message 或格式化整个对象
|
|
183
|
+
if (typeof errorMessage === 'object') {
|
|
184
|
+
errorMessage = errorMessage.message || JSON.stringify(errorMessage, null, 2);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// 记录详细错误信息用于调试
|
|
188
|
+
console.error(`[SmartRouter] Cloud tool error for ${toolName}:`, {
|
|
189
|
+
code: result.error.code,
|
|
190
|
+
message: result.error.message,
|
|
191
|
+
data: result.error.data,
|
|
192
|
+
fullError: result.error
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
throw new Error(errorMessage);
|
|
133
196
|
}
|
|
134
197
|
|
|
135
198
|
// 提取结果内容
|
|
@@ -137,7 +200,16 @@ export class SmartRouter {
|
|
|
137
200
|
// MCP 标准格式
|
|
138
201
|
const content = result.result.content;
|
|
139
202
|
if (Array.isArray(content) && content.length > 0) {
|
|
140
|
-
|
|
203
|
+
const text = content[0].text;
|
|
204
|
+
// 尝试解析 JSON 字符串,如果失败则返回原始字符串
|
|
205
|
+
try {
|
|
206
|
+
const parsed = JSON.parse(text);
|
|
207
|
+
// 如果解析成功且是对象,返回对象;否则返回原始字符串
|
|
208
|
+
return typeof parsed === 'object' && parsed !== null ? parsed : text;
|
|
209
|
+
} catch {
|
|
210
|
+
// 不是 JSON,直接返回字符串
|
|
211
|
+
return text;
|
|
212
|
+
}
|
|
141
213
|
}
|
|
142
214
|
return content;
|
|
143
215
|
} else if (result.result) {
|
|
@@ -146,6 +218,18 @@ export class SmartRouter {
|
|
|
146
218
|
return result;
|
|
147
219
|
}
|
|
148
220
|
} catch (error) {
|
|
221
|
+
// 记录完整错误信息用于调试
|
|
222
|
+
console.error(`[SmartRouter] Error calling cloud tool ${toolName}:`, {
|
|
223
|
+
error: error.message,
|
|
224
|
+
stack: error.stack,
|
|
225
|
+
params: params
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
// 如果错误信息已经包含 "Cloud tool call failed",直接抛出
|
|
229
|
+
// 否则添加前缀
|
|
230
|
+
if (error.message && error.message.includes('Cloud tool call failed')) {
|
|
231
|
+
throw error;
|
|
232
|
+
}
|
|
149
233
|
throw new Error(`Cloud tool call failed: ${error.message}`);
|
|
150
234
|
}
|
|
151
235
|
}
|