@wecom/wecom-openclaw-plugin 2026.3.30 → 2026.3.31-beta.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.
- package/README.md +251 -4
- package/dist/index.d.ts +2 -4
- package/dist/index.js +113 -0
- package/dist/src/accounts.d.ts +57 -0
- package/dist/src/accounts.js +222 -0
- package/dist/src/agent/api-client.d.ts +94 -0
- package/dist/src/agent/api-client.js +292 -0
- package/dist/src/agent/handler.d.ts +63 -0
- package/dist/src/agent/handler.js +562 -0
- package/dist/src/agent/index.d.ts +5 -0
- package/dist/src/agent/index.js +5 -0
- package/dist/src/agent/webhook.d.ts +23 -0
- package/dist/src/agent/webhook.js +226 -0
- package/dist/src/agent/xml.d.ts +21 -0
- package/dist/src/agent/xml.js +41 -0
- package/dist/src/channel.js +564 -0
- package/dist/src/chat-queue.d.ts +31 -0
- package/dist/src/chat-queue.js +45 -0
- package/dist/src/const.d.ts +46 -0
- package/dist/src/const.js +157 -0
- package/dist/src/dm-policy.js +84 -0
- package/dist/src/dynamic-agent.d.ts +37 -0
- package/dist/src/dynamic-agent.js +60 -0
- package/dist/src/dynamic-routing.d.ts +75 -0
- package/dist/src/dynamic-routing.js +124 -0
- package/dist/src/group-policy.js +126 -0
- package/dist/src/http.d.ts +26 -0
- package/dist/src/http.js +93 -0
- package/dist/src/interface.d.ts +0 -2
- package/dist/src/interface.js +4 -0
- package/dist/src/mcp/index.js +6 -0
- package/dist/src/mcp/interceptors/biz-error.js +64 -0
- package/dist/src/mcp/interceptors/index.js +66 -0
- package/dist/src/mcp/interceptors/msg-media.js +133 -0
- package/dist/src/mcp/interceptors/smartpage-create.js +132 -0
- package/dist/src/mcp/interceptors/smartpage-export.js +87 -0
- package/dist/src/mcp/interceptors/types.js +7 -0
- package/dist/src/mcp/schema.js +99 -0
- package/dist/src/mcp/tool.js +202 -0
- package/dist/src/mcp/transport.js +410 -0
- package/dist/src/media-handler.js +122 -0
- package/dist/src/media-uploader.js +333 -0
- package/dist/src/message-parser.js +129 -0
- package/dist/src/message-sender.d.ts +18 -0
- package/dist/src/message-sender.js +120 -0
- package/dist/src/monitor.js +875 -0
- package/dist/src/onboarding.js +175 -0
- package/dist/src/openclaw-compat.d.ts +47 -0
- package/dist/src/openclaw-compat.js +477 -0
- package/dist/src/reqid-store.js +83 -0
- package/dist/src/routing.d.ts +24 -0
- package/dist/src/routing.js +34 -0
- package/dist/src/runtime.js +10 -0
- package/dist/src/shared/command-auth.d.ts +23 -0
- package/dist/src/shared/command-auth.js +73 -0
- package/dist/src/shared/xml-parser.d.ts +46 -0
- package/dist/src/shared/xml-parser.js +210 -0
- package/dist/src/state-dir-resolve.d.ts +2 -0
- package/dist/src/state-dir-resolve.js +9 -0
- package/dist/src/state-manager.js +216 -0
- package/dist/src/target.d.ts +35 -0
- package/dist/src/target.js +67 -0
- package/dist/src/template-card-manager.d.ts +55 -0
- package/dist/src/template-card-manager.js +205 -0
- package/dist/src/template-card-parser.js +647 -0
- package/dist/src/timeout.js +36 -0
- package/dist/src/types/account.d.ts +29 -0
- package/dist/src/types/account.js +4 -0
- package/dist/src/types/config.d.ts +100 -0
- package/dist/src/types/config.js +7 -0
- package/dist/src/types/constants.d.ts +42 -0
- package/dist/src/types/constants.js +42 -0
- package/dist/src/types/index.d.ts +7 -0
- package/dist/src/types/index.js +5 -0
- package/dist/src/types/message.d.ts +238 -0
- package/dist/src/types/message.js +5 -0
- package/dist/src/utils.d.ts +39 -0
- package/dist/src/utils.js +70 -0
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.js +24 -0
- package/dist/src/webhook/command-auth.d.ts +47 -0
- package/dist/src/webhook/command-auth.js +96 -0
- package/dist/src/webhook/gateway.d.ts +36 -0
- package/dist/src/webhook/gateway.js +227 -0
- package/dist/src/webhook/handler.d.ts +18 -0
- package/dist/src/webhook/handler.js +420 -0
- package/dist/src/webhook/helpers.d.ts +156 -0
- package/dist/src/webhook/helpers.js +833 -0
- package/dist/src/webhook/http.d.ts +26 -0
- package/dist/src/webhook/http.js +94 -0
- package/dist/src/webhook/index.d.ts +11 -0
- package/dist/src/webhook/index.js +14 -0
- package/dist/src/webhook/media.d.ts +29 -0
- package/dist/src/webhook/media.js +88 -0
- package/dist/src/webhook/monitor.d.ts +59 -0
- package/dist/src/webhook/monitor.js +1185 -0
- package/dist/src/webhook/state.d.ts +220 -0
- package/dist/src/webhook/state.js +491 -0
- package/dist/src/webhook/target.d.ts +41 -0
- package/dist/src/webhook/target.js +132 -0
- package/dist/src/webhook/types.d.ts +347 -0
- package/dist/src/webhook/types.js +33 -0
- package/package.json +15 -7
- package/dist/index.cjs.js +0 -4953
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.esm.js +0 -4926
- package/dist/index.esm.js.map +0 -1
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* smartpage_get_export_result 响应拦截器
|
|
3
|
+
*
|
|
4
|
+
* 核心逻辑:
|
|
5
|
+
* MCP Server 返回的 smartpage_get_export_result 响应中,当 task_done=true 时
|
|
6
|
+
* 会包含 content 字段(markdown 文本内容)。该内容可能很大,直接返回给 LLM
|
|
7
|
+
* 会消耗大量 token。
|
|
8
|
+
*
|
|
9
|
+
* 本拦截器在 afterCall 阶段:
|
|
10
|
+
* 1. 检测 task_done=true 且存在 content 字段
|
|
11
|
+
* 2. 将 content 保存到本地文件(使用与 msg-media 一致的媒体目录)
|
|
12
|
+
* 3. 用 content_path(文件路径)替换 content 字段
|
|
13
|
+
*
|
|
14
|
+
* 这样 LLM 只看到轻量的文件路径信息,Skill 可通过文件路径读取完整内容。
|
|
15
|
+
*/
|
|
16
|
+
import { getWeComRuntime } from "../../runtime.js";
|
|
17
|
+
// ============================================================================
|
|
18
|
+
// 拦截器实现
|
|
19
|
+
// ============================================================================
|
|
20
|
+
export const smartpageExportInterceptor = {
|
|
21
|
+
name: "smartpage-export",
|
|
22
|
+
/** 仅对 doc 品类的 smartpage_get_export_result 方法生效 */
|
|
23
|
+
match: (ctx) => ctx.category === "doc" && ctx.method === "smartpage_get_export_result",
|
|
24
|
+
/** 拦截响应:将 markdown content 保存为本地文件 */
|
|
25
|
+
async afterCall(_ctx, result) {
|
|
26
|
+
return interceptExportResponse(result);
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
// ============================================================================
|
|
30
|
+
// 内部实现
|
|
31
|
+
// ============================================================================
|
|
32
|
+
/**
|
|
33
|
+
* 拦截 smartpage_get_export_result 的 MCP 响应
|
|
34
|
+
*
|
|
35
|
+
* 1. 从 MCP result 的 content[].text 中提取业务 JSON
|
|
36
|
+
* 2. 检测 task_done=true 且存在 content 字段
|
|
37
|
+
* 3. 将 content(markdown 文本)通过 saveMediaBuffer 保存到本地媒体目录
|
|
38
|
+
* 4. 构造新响应:移除 content,添加 content_path
|
|
39
|
+
*/
|
|
40
|
+
async function interceptExportResponse(result) {
|
|
41
|
+
// 1. 提取 MCP result 中的 content 数组
|
|
42
|
+
const content = result?.content;
|
|
43
|
+
if (!Array.isArray(content))
|
|
44
|
+
return result;
|
|
45
|
+
const textItem = content.find((c) => c.type === "text" && typeof c.text === "string");
|
|
46
|
+
if (!textItem)
|
|
47
|
+
return result;
|
|
48
|
+
// 2. 解析业务 JSON
|
|
49
|
+
let bizData;
|
|
50
|
+
try {
|
|
51
|
+
bizData = JSON.parse(textItem.text);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
// 非 JSON 格式,原样返回
|
|
55
|
+
return result;
|
|
56
|
+
}
|
|
57
|
+
// 3. 校验:errcode !== 0 或 task_done 不为 true 或无 content 时原样返回
|
|
58
|
+
if (bizData.errcode !== 0)
|
|
59
|
+
return result;
|
|
60
|
+
if (bizData.task_done !== true)
|
|
61
|
+
return result;
|
|
62
|
+
if (typeof bizData.content !== "string")
|
|
63
|
+
return result;
|
|
64
|
+
const markdownContent = bizData.content;
|
|
65
|
+
console.log(`[mcp] smartpage_get_export_result: 拦截 content (${markdownContent.length} chars),保存到本地文件`);
|
|
66
|
+
// 4. 将 markdown 内容通过 saveMediaBuffer 保存到本地媒体目录
|
|
67
|
+
// 使用 text/markdown 类型,与 msg-media 拦截器保持一致的路径管理
|
|
68
|
+
const buffer = Buffer.from(markdownContent, "utf-8");
|
|
69
|
+
const core = getWeComRuntime();
|
|
70
|
+
const saved = await core.channel.media.saveMediaBuffer(buffer, "text/markdown", "inbound", undefined, // maxBytes: markdown 文本通常不大,使用默认限制
|
|
71
|
+
"smartpage_export.md");
|
|
72
|
+
console.log(`[mcp] smartpage_get_export_result: 已保存到 ${saved.path}`);
|
|
73
|
+
// 5. 构造新响应:移除 content,添加 content_path
|
|
74
|
+
const newBizData = {
|
|
75
|
+
errcode: bizData.errcode,
|
|
76
|
+
errmsg: bizData.errmsg ?? "ok",
|
|
77
|
+
task_done: true,
|
|
78
|
+
content_path: saved.path,
|
|
79
|
+
};
|
|
80
|
+
// 6. 返回修改后的 MCP result 结构
|
|
81
|
+
return {
|
|
82
|
+
content: [{
|
|
83
|
+
type: "text",
|
|
84
|
+
text: JSON.stringify(newBizData),
|
|
85
|
+
}],
|
|
86
|
+
};
|
|
87
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Schema 清洗模块
|
|
3
|
+
*
|
|
4
|
+
* 负责内联 $ref/$defs 引用并移除 Gemini 不支持的 JSON Schema 关键词,
|
|
5
|
+
* 防止 Gemini 模型解析 function response 时报 400 错误。
|
|
6
|
+
*/
|
|
7
|
+
/** Gemini 不支持的 JSON Schema 关键词 */
|
|
8
|
+
const GEMINI_UNSUPPORTED_KEYWORDS = new Set([
|
|
9
|
+
"patternProperties", "additionalProperties", "$schema", "$id", "$ref", "$defs",
|
|
10
|
+
"definitions", "examples", "minLength", "maxLength", "minimum", "maximum",
|
|
11
|
+
"multipleOf", "pattern", "format", "minItems", "maxItems", "uniqueItems",
|
|
12
|
+
"minProperties", "maxProperties",
|
|
13
|
+
]);
|
|
14
|
+
/**
|
|
15
|
+
* 清洗 JSON Schema,内联 $ref 引用并移除 Gemini 不支持的关键词,
|
|
16
|
+
* 防止 Gemini 模型解析 function response 时报 400 错误。
|
|
17
|
+
*/
|
|
18
|
+
export function cleanSchemaForGemini(schema) {
|
|
19
|
+
if (!schema || typeof schema !== "object")
|
|
20
|
+
return schema;
|
|
21
|
+
if (Array.isArray(schema))
|
|
22
|
+
return schema.map(cleanSchemaForGemini);
|
|
23
|
+
const obj = schema;
|
|
24
|
+
// 收集 $defs/definitions 用于后续 $ref 内联解析
|
|
25
|
+
const defs = {
|
|
26
|
+
...(obj.$defs && typeof obj.$defs === "object" ? obj.$defs : {}),
|
|
27
|
+
...(obj.definitions && typeof obj.definitions === "object" ? obj.definitions : {}),
|
|
28
|
+
};
|
|
29
|
+
return cleanWithDefs(obj, defs, new Set());
|
|
30
|
+
}
|
|
31
|
+
function cleanWithDefs(schema, defs, refStack) {
|
|
32
|
+
if (!schema || typeof schema !== "object")
|
|
33
|
+
return schema;
|
|
34
|
+
if (Array.isArray(schema))
|
|
35
|
+
return schema.map((item) => cleanWithDefs(item, defs, refStack));
|
|
36
|
+
const obj = schema;
|
|
37
|
+
// 合并当前层级的 $defs/definitions 到 defs 中
|
|
38
|
+
if (obj.$defs && typeof obj.$defs === "object") {
|
|
39
|
+
Object.assign(defs, obj.$defs);
|
|
40
|
+
}
|
|
41
|
+
if (obj.definitions && typeof obj.definitions === "object") {
|
|
42
|
+
Object.assign(defs, obj.definitions);
|
|
43
|
+
}
|
|
44
|
+
// 处理 $ref 引用:尝试内联解析
|
|
45
|
+
if (typeof obj.$ref === "string") {
|
|
46
|
+
const ref = obj.$ref;
|
|
47
|
+
if (refStack.has(ref))
|
|
48
|
+
return {}; // 防止循环引用
|
|
49
|
+
const match = ref.match(/^#\/(?:\$defs|definitions)\/(.+)$/);
|
|
50
|
+
if (match && match[1] && defs[match[1]]) {
|
|
51
|
+
const nextStack = new Set(refStack);
|
|
52
|
+
nextStack.add(ref);
|
|
53
|
+
return cleanWithDefs(defs[match[1]], defs, nextStack);
|
|
54
|
+
}
|
|
55
|
+
return {}; // 无法解析的 $ref,返回空对象
|
|
56
|
+
}
|
|
57
|
+
const cleaned = {};
|
|
58
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
59
|
+
if (GEMINI_UNSUPPORTED_KEYWORDS.has(key))
|
|
60
|
+
continue;
|
|
61
|
+
if (key === "const") {
|
|
62
|
+
cleaned.enum = [value];
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (key === "properties" && value && typeof value === "object" && !Array.isArray(value)) {
|
|
66
|
+
cleaned[key] = Object.fromEntries(Object.entries(value).map(([k, v]) => [
|
|
67
|
+
k, cleanWithDefs(v, defs, refStack),
|
|
68
|
+
]));
|
|
69
|
+
}
|
|
70
|
+
else if (key === "items" && value) {
|
|
71
|
+
cleaned[key] = Array.isArray(value)
|
|
72
|
+
? value.map((item) => cleanWithDefs(item, defs, refStack))
|
|
73
|
+
: cleanWithDefs(value, defs, refStack);
|
|
74
|
+
}
|
|
75
|
+
else if ((key === "anyOf" || key === "oneOf" || key === "allOf") && Array.isArray(value)) {
|
|
76
|
+
// 过滤掉 null 类型的变体
|
|
77
|
+
const nonNull = value.filter((v) => {
|
|
78
|
+
if (!v || typeof v !== "object")
|
|
79
|
+
return true;
|
|
80
|
+
const r = v;
|
|
81
|
+
return r.type !== "null";
|
|
82
|
+
});
|
|
83
|
+
if (nonNull.length === 1) {
|
|
84
|
+
// 只剩一个变体时直接内联
|
|
85
|
+
const single = cleanWithDefs(nonNull[0], defs, refStack);
|
|
86
|
+
if (single && typeof single === "object" && !Array.isArray(single)) {
|
|
87
|
+
Object.assign(cleaned, single);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
cleaned[key] = nonNull.map((v) => cleanWithDefs(v, defs, refStack));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
cleaned[key] = value;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return cleaned;
|
|
99
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* wecom_mcp — 模拟 MCP 调用的 Agent Tool
|
|
3
|
+
*
|
|
4
|
+
* 通过 MCP Streamable HTTP 传输协议调用企业微信 MCP Server,
|
|
5
|
+
* 提供 list(列出所有工具)和 call(调用工具)两个操作。
|
|
6
|
+
*
|
|
7
|
+
* 在 skills 中的使用方式:
|
|
8
|
+
* wecom_mcp list <category>
|
|
9
|
+
* wecom_mcp call <category> <method> '<jsonArgs>'
|
|
10
|
+
*
|
|
11
|
+
* 示例:
|
|
12
|
+
* wecom_mcp list contact
|
|
13
|
+
* wecom_mcp call contact getContact '{}'
|
|
14
|
+
*/
|
|
15
|
+
import { sendJsonRpc } from "./transport.js";
|
|
16
|
+
import { cleanSchemaForGemini } from "./schema.js";
|
|
17
|
+
import { resolveBeforeCall, runAfterCall } from "./interceptors/index.js";
|
|
18
|
+
// ============================================================================
|
|
19
|
+
// 响应构造辅助
|
|
20
|
+
// ============================================================================
|
|
21
|
+
/** 构造统一的文本响应结构 */
|
|
22
|
+
const textResult = (data) => ({
|
|
23
|
+
content: [{ type: "text", text: JSON.stringify(data, null, 2) }],
|
|
24
|
+
});
|
|
25
|
+
/** 构造错误响应 */
|
|
26
|
+
const errorResult = (err) => {
|
|
27
|
+
// 适配企业微信 API 返回的 { errcode, errmsg } 结构
|
|
28
|
+
if (err && typeof err === "object" && "errcode" in err) {
|
|
29
|
+
const { errcode, errmsg } = err;
|
|
30
|
+
return textResult({ error: errmsg ?? `错误码: ${errcode}`, errcode });
|
|
31
|
+
}
|
|
32
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
33
|
+
return textResult({ error: message });
|
|
34
|
+
};
|
|
35
|
+
// ============================================================================
|
|
36
|
+
// list 操作:列出某品类的所有 MCP 工具
|
|
37
|
+
// ============================================================================
|
|
38
|
+
const handleList = async (category) => {
|
|
39
|
+
const result = await sendJsonRpc(category, "tools/list");
|
|
40
|
+
const tools = result?.tools ?? [];
|
|
41
|
+
if (tools.length === 0) {
|
|
42
|
+
return { message: `品类 "${category}" 下暂无可用工具`, tools: [] };
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
category,
|
|
46
|
+
count: tools.length,
|
|
47
|
+
tools: tools.map((t) => ({
|
|
48
|
+
name: t.name,
|
|
49
|
+
description: t.description ?? "",
|
|
50
|
+
// 清洗 inputSchema,内联 $ref/$defs 引用并移除 Gemini 不支持的关键词,
|
|
51
|
+
// 避免 Gemini 模型解析 function response 时报 400 错误
|
|
52
|
+
inputSchema: t.inputSchema ? cleanSchemaForGemini(t.inputSchema) : undefined,
|
|
53
|
+
})),
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
// ============================================================================
|
|
57
|
+
// call 操作:调用某品类的某个 MCP 工具
|
|
58
|
+
// ============================================================================
|
|
59
|
+
const handleCall = async (category, method, args) => {
|
|
60
|
+
const ctx = { category, method, args };
|
|
61
|
+
const callStart = performance.now();
|
|
62
|
+
console.log(`[mcp] handleCall ${category}/${method} 入参: ${JSON.stringify(args)}`);
|
|
63
|
+
// 1. 收集拦截器的 beforeCall 配置(如超时时间、替换 args)
|
|
64
|
+
const { options, args: resolvedArgs } = await resolveBeforeCall(ctx);
|
|
65
|
+
const finalArgs = resolvedArgs ?? args;
|
|
66
|
+
if (resolvedArgs) {
|
|
67
|
+
console.log(`[mcp] handleCall ${category}/${method} 拦截器替换 args: ${JSON.stringify(resolvedArgs).slice(0, 500)}` +
|
|
68
|
+
(JSON.stringify(resolvedArgs).length > 500 ? "...(truncated)" : ""));
|
|
69
|
+
}
|
|
70
|
+
if (options) {
|
|
71
|
+
console.log(`[mcp] handleCall ${category}/${method} 拦截器选项: ${JSON.stringify(options)}`);
|
|
72
|
+
}
|
|
73
|
+
// 2. 执行 MCP 调用
|
|
74
|
+
const result = await sendJsonRpc(category, "tools/call", {
|
|
75
|
+
name: method,
|
|
76
|
+
arguments: finalArgs,
|
|
77
|
+
}, options);
|
|
78
|
+
const rpcDone = performance.now();
|
|
79
|
+
const rpcMs = (rpcDone - callStart).toFixed(1);
|
|
80
|
+
const resultStr = JSON.stringify(result);
|
|
81
|
+
console.log(`[mcp] handleCall ${category}/${method} MCP 响应 (${rpcMs}ms): ${resultStr.slice(0, 800)}` +
|
|
82
|
+
(resultStr.length > 800 ? "...(truncated)" : ""));
|
|
83
|
+
// 3. 管道式执行 afterCall 拦截器(业务错误码检查、响应变换等)
|
|
84
|
+
const finalResult = await runAfterCall(ctx, result);
|
|
85
|
+
const totalMs = (performance.now() - callStart).toFixed(1);
|
|
86
|
+
const interceptMs = (performance.now() - rpcDone).toFixed(1);
|
|
87
|
+
// 有拦截器处理时打印详细耗时,否则只打印 RPC 耗时
|
|
88
|
+
if (finalResult !== result) {
|
|
89
|
+
const finalStr = JSON.stringify(finalResult);
|
|
90
|
+
console.log(`[mcp] handleCall ${category}/${method} afterCall 变换后 (${interceptMs}ms): ${finalStr.slice(0, 500)}` +
|
|
91
|
+
(finalStr.length > 500 ? "...(truncated)" : ""));
|
|
92
|
+
console.log(`[mcp] handleCall ${category}/${method} 总耗时: ${totalMs}ms` +
|
|
93
|
+
` (MCP请求: ${rpcMs}ms, 拦截处理: ${interceptMs}ms)`);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
console.log(`[mcp] handleCall ${category}/${method} 耗时: ${rpcMs}ms`);
|
|
97
|
+
}
|
|
98
|
+
return finalResult;
|
|
99
|
+
};
|
|
100
|
+
// ============================================================================
|
|
101
|
+
// 参数解析
|
|
102
|
+
// ============================================================================
|
|
103
|
+
/**
|
|
104
|
+
* 解析 args 参数:支持 JSON 字符串或直接的对象
|
|
105
|
+
*/
|
|
106
|
+
const parseArgs = (args) => {
|
|
107
|
+
if (!args)
|
|
108
|
+
return {};
|
|
109
|
+
if (typeof args === "object")
|
|
110
|
+
return args;
|
|
111
|
+
try {
|
|
112
|
+
return JSON.parse(args);
|
|
113
|
+
}
|
|
114
|
+
catch (err) {
|
|
115
|
+
const detail = err instanceof SyntaxError ? err.message : String(err);
|
|
116
|
+
throw new Error(`args 参数不是合法的 JSON: ${args} (${detail})`);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
// ============================================================================
|
|
120
|
+
// 工具定义 & 导出
|
|
121
|
+
// ============================================================================
|
|
122
|
+
/**
|
|
123
|
+
* 创建 wecom_mcp Agent Tool 定义
|
|
124
|
+
*/
|
|
125
|
+
export function createWeComMcpTool() {
|
|
126
|
+
return {
|
|
127
|
+
name: "wecom_mcp",
|
|
128
|
+
label: "企业微信 MCP 工具",
|
|
129
|
+
description: [
|
|
130
|
+
"通过 HTTP 直接调用企业微信 MCP Server。",
|
|
131
|
+
"支持两种操作:",
|
|
132
|
+
" - list: 列出指定品类的所有 MCP 工具",
|
|
133
|
+
" - call: 调用指定品类的某个 MCP 工具",
|
|
134
|
+
"",
|
|
135
|
+
"使用方式:",
|
|
136
|
+
" wecom_mcp list <category>",
|
|
137
|
+
" wecom_mcp call <category> <method> '<jsonArgs>'",
|
|
138
|
+
"",
|
|
139
|
+
"示例:",
|
|
140
|
+
" 列出 contact 品类所有工具:wecom_mcp list contact",
|
|
141
|
+
" 调用 contact 的 getContact:wecom_mcp call contact getContact '{}'",
|
|
142
|
+
].join("\n"),
|
|
143
|
+
parameters: {
|
|
144
|
+
type: "object",
|
|
145
|
+
properties: {
|
|
146
|
+
action: {
|
|
147
|
+
type: "string",
|
|
148
|
+
enum: ["list", "call"],
|
|
149
|
+
description: "操作类型:list(列出工具)或 call(调用工具)",
|
|
150
|
+
},
|
|
151
|
+
category: {
|
|
152
|
+
type: "string",
|
|
153
|
+
description: "MCP 品类名称,如 doc、contact 等,对应 mcpConfig 中的 key",
|
|
154
|
+
},
|
|
155
|
+
method: {
|
|
156
|
+
type: "string",
|
|
157
|
+
description: "要调用的 MCP 方法名(action=call 时必填)",
|
|
158
|
+
},
|
|
159
|
+
args: {
|
|
160
|
+
type: ["string", "object"],
|
|
161
|
+
description: "调用 MCP 方法的参数,可以是 JSON 字符串或对象(action=call 时使用,默认 {})",
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
required: ["action", "category"],
|
|
165
|
+
},
|
|
166
|
+
async execute(_toolCallId, params) {
|
|
167
|
+
const p = params;
|
|
168
|
+
console.log(`[mcp] execute: action=${p.action}, category=${p.category}` +
|
|
169
|
+
(p.method ? `, method=${p.method}` : "") +
|
|
170
|
+
(p.args ? `, args=${typeof p.args === "string" ? p.args : JSON.stringify(p.args)}` : ""));
|
|
171
|
+
try {
|
|
172
|
+
let result;
|
|
173
|
+
switch (p.action) {
|
|
174
|
+
case "list":
|
|
175
|
+
result = textResult(await handleList(p.category));
|
|
176
|
+
break;
|
|
177
|
+
case "call": {
|
|
178
|
+
if (!p.method) {
|
|
179
|
+
result = textResult({ error: "action 为 call 时必须提供 method 参数" });
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
const args = parseArgs(p.args);
|
|
183
|
+
result = textResult(await handleCall(p.category, p.method, args));
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
default:
|
|
187
|
+
result = textResult({ error: `未知操作类型: ${String(p.action)},支持 list 和 call` });
|
|
188
|
+
}
|
|
189
|
+
console.log(`[mcp] execute: action=${p.action}, category=${p.category}` +
|
|
190
|
+
(p.method ? `, method=${p.method}` : "") +
|
|
191
|
+
` → 响应长度=${result.content[0].text.length} chars`);
|
|
192
|
+
return result;
|
|
193
|
+
}
|
|
194
|
+
catch (err) {
|
|
195
|
+
console.error(`[mcp] execute: action=${p.action}, category=${p.category}` +
|
|
196
|
+
(p.method ? `, method=${p.method}` : "") +
|
|
197
|
+
` → 异常: ${err instanceof Error ? err.message : String(err)}`);
|
|
198
|
+
return errorResult(err);
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
};
|
|
202
|
+
}
|