mcp-multi-bridge 2.1.2 → 2.1.3
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/lib/higress.js +38 -7
- package/package.json +1 -1
package/lib/higress.js
CHANGED
|
@@ -31,19 +31,50 @@ export async function rpc(cfg, org, method, params) {
|
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
33
|
* 从 MCP 工具返回里取出业务 JSON
|
|
34
|
-
*
|
|
34
|
+
*
|
|
35
|
+
* 网关会在正文前拼一段说明文字(responseTemplate.prependBody),业务 JSON 在最后。
|
|
36
|
+
* 不能简单取第一个 `{`:说明文字里可能带花括号,比如描述字段结构时写的
|
|
37
|
+
* 「data 为 [{value, label}] 数组」,那样会把提示文字当成 JSON 解析,失败后
|
|
38
|
+
* 返回 code=-1,把一个正常的业务响应报成异常。
|
|
39
|
+
*
|
|
40
|
+
* 所以从后往前找:JSON 一定在末尾,而提示文字在前。优先试整段,
|
|
41
|
+
* 再按 `{` 位置倒序尝试,命中即止。最多试 MAX_TRIES 次,避免长文本下反复解析。
|
|
35
42
|
*/
|
|
43
|
+
const MAX_TRIES = 12;
|
|
44
|
+
|
|
36
45
|
export function extractPayload(rpcResult) {
|
|
37
46
|
const content = rpcResult?.result?.content;
|
|
38
47
|
if (!Array.isArray(content) || !content.length) {
|
|
39
48
|
return rpcResult?.result?.isError ? { code: -1, message: '调用失败' } : null;
|
|
40
49
|
}
|
|
41
50
|
const text = content[0].text || '';
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
51
|
+
|
|
52
|
+
// 常见情况:整段就是 JSON
|
|
53
|
+
const whole = text.trim();
|
|
54
|
+
if (whole.startsWith('{')) {
|
|
55
|
+
try {
|
|
56
|
+
return JSON.parse(whole);
|
|
57
|
+
} catch (e) {
|
|
58
|
+
/* 继续往下找 */
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// 从后往前,逐个 `{` 尝试
|
|
63
|
+
let idx = text.length;
|
|
64
|
+
for (let n = 0; n < MAX_TRIES; n++) {
|
|
65
|
+
const start = text.lastIndexOf('{', idx - 1);
|
|
66
|
+
if (start === -1) break;
|
|
67
|
+
try {
|
|
68
|
+
const parsed = JSON.parse(text.slice(start));
|
|
69
|
+
if (parsed && typeof parsed === 'object') return parsed;
|
|
70
|
+
} catch (e) {
|
|
71
|
+
/* 这个位置不是 JSON 起点,继续往前 */
|
|
72
|
+
}
|
|
73
|
+
idx = start;
|
|
48
74
|
}
|
|
75
|
+
|
|
76
|
+
// 兜底:仍然取第一个 `{`,保持与旧行为一致的错误信息
|
|
77
|
+
const first = text.indexOf('{');
|
|
78
|
+
if (first === -1) return { code: -1, message: text.trim() || '响应无 JSON 内容' };
|
|
79
|
+
return { code: -1, message: text.slice(first, first + 200) };
|
|
49
80
|
}
|