@wecom/wecom-openclaw-plugin 2026.3.26 → 2026.3.30-beta.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/dist/index.cjs.js +436 -73
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +436 -73
- package/dist/index.esm.js.map +1 -1
- package/dist/src/mcp/interceptors/biz-error.d.ts +11 -0
- package/dist/src/mcp/interceptors/index.d.ts +35 -0
- package/dist/src/mcp/interceptors/msg-media.d.ts +11 -0
- package/dist/src/mcp/interceptors/smartpage-create.d.ts +30 -0
- package/dist/src/mcp/interceptors/smartpage-export.d.ts +17 -0
- package/dist/src/mcp/interceptors/types.d.ts +56 -0
- package/package.json +1 -1
- package/skills/wecom-doc-manager/SKILL.md +83 -4
- package/skills/wecom-doc-manager/references/api-smartpage-create.md +108 -0
- package/skills/wecom-doc-manager/references/api-smartpage-export-task.md +62 -0
- package/skills/wecom-doc-manager/references/api-smartpage-get-export-result.md +74 -0
- package/skills/wecom-schedule/SKILL.md +2 -0
- package/skills/wecom-schedule/references/api-update-schedule.md +2 -0
package/dist/index.cjs.js
CHANGED
|
@@ -2634,7 +2634,7 @@ async function finishThinkingStream(ctx) {
|
|
|
2634
2634
|
const trimmedRemaining = stripThinkTags(remainingText);
|
|
2635
2635
|
const finishText = trimmedRemaining
|
|
2636
2636
|
? remainingText
|
|
2637
|
-
: (state.hasTemplateCard ? "📋 卡片消息已发送。" : "
|
|
2637
|
+
: (state.hasTemplateCard ? "📋 卡片消息已发送。" : "");
|
|
2638
2638
|
runtime.log?.(`[wecom][template-card] finishThinkingStream: closing stream with finishText="${finishText.slice(0, 100)}...", hasTemplateCard=${state.hasTemplateCard}`);
|
|
2639
2639
|
await sendWeComReply({ wsClient, frame, text: finishText, runtime, finish: true, streamId: state.streamId });
|
|
2640
2640
|
return;
|
|
@@ -4244,59 +4244,16 @@ function cleanWithDefs(schema, defs, refStack) {
|
|
|
4244
4244
|
}
|
|
4245
4245
|
|
|
4246
4246
|
/**
|
|
4247
|
-
*
|
|
4248
|
-
*
|
|
4249
|
-
* 通过 MCP Streamable HTTP 传输协议调用企业微信 MCP Server,
|
|
4250
|
-
* 提供 list(列出所有工具)和 call(调用工具)两个操作。
|
|
4247
|
+
* 业务错误码检查拦截器
|
|
4251
4248
|
*
|
|
4252
|
-
*
|
|
4253
|
-
*
|
|
4254
|
-
*
|
|
4249
|
+
* 检查 tools/call 返回结果中是否包含需要清理缓存的业务错误码。
|
|
4250
|
+
* MCP Server 可能在正常的 JSON-RPC 响应中返回业务层错误,
|
|
4251
|
+
* 这些错误被包裹在 result.content[].text 中,需要解析后判断。
|
|
4255
4252
|
*
|
|
4256
|
-
*
|
|
4257
|
-
* wecom_mcp list contact
|
|
4258
|
-
* wecom_mcp call contact getContact '{}'
|
|
4253
|
+
* 此拦截器对所有 call 调用生效。
|
|
4259
4254
|
*/
|
|
4260
4255
|
// ============================================================================
|
|
4261
|
-
//
|
|
4262
|
-
// ============================================================================
|
|
4263
|
-
/** 构造统一的文本响应结构 */
|
|
4264
|
-
const textResult = (data) => ({
|
|
4265
|
-
content: [{ type: "text", text: JSON.stringify(data, null, 2) }],
|
|
4266
|
-
});
|
|
4267
|
-
/** 构造错误响应 */
|
|
4268
|
-
const errorResult = (err) => {
|
|
4269
|
-
// 适配企业微信 API 返回的 { errcode, errmsg } 结构
|
|
4270
|
-
if (err && typeof err === "object" && "errcode" in err) {
|
|
4271
|
-
const { errcode, errmsg } = err;
|
|
4272
|
-
return textResult({ error: errmsg ?? `错误码: ${errcode}`, errcode });
|
|
4273
|
-
}
|
|
4274
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
4275
|
-
return textResult({ error: message });
|
|
4276
|
-
};
|
|
4277
|
-
// ============================================================================
|
|
4278
|
-
// list 操作:列出某品类的所有 MCP 工具
|
|
4279
|
-
// ============================================================================
|
|
4280
|
-
const handleList = async (category) => {
|
|
4281
|
-
const result = await sendJsonRpc(category, "tools/list");
|
|
4282
|
-
const tools = result?.tools ?? [];
|
|
4283
|
-
if (tools.length === 0) {
|
|
4284
|
-
return { message: `品类 "${category}" 下暂无可用工具`, tools: [] };
|
|
4285
|
-
}
|
|
4286
|
-
return {
|
|
4287
|
-
category,
|
|
4288
|
-
count: tools.length,
|
|
4289
|
-
tools: tools.map((t) => ({
|
|
4290
|
-
name: t.name,
|
|
4291
|
-
description: t.description ?? "",
|
|
4292
|
-
// 清洗 inputSchema,内联 $ref/$defs 引用并移除 Gemini 不支持的关键词,
|
|
4293
|
-
// 避免 Gemini 模型解析 function response 时报 400 错误
|
|
4294
|
-
inputSchema: t.inputSchema ? cleanSchemaForGemini(t.inputSchema) : undefined,
|
|
4295
|
-
})),
|
|
4296
|
-
};
|
|
4297
|
-
};
|
|
4298
|
-
// ============================================================================
|
|
4299
|
-
// call 操作:调用某品类的某个 MCP 工具
|
|
4256
|
+
// 常量
|
|
4300
4257
|
// ============================================================================
|
|
4301
4258
|
/**
|
|
4302
4259
|
* 需要触发缓存清理的业务错误码集合
|
|
@@ -4307,13 +4264,27 @@ const handleList = async (category) => {
|
|
|
4307
4264
|
* - 850002: 机器人未被授权使用对应能力,需清理缓存以便下次重新拉取配置
|
|
4308
4265
|
*/
|
|
4309
4266
|
const BIZ_CACHE_CLEAR_ERROR_CODES = new Set([850002]);
|
|
4267
|
+
// ============================================================================
|
|
4268
|
+
// 拦截器实现
|
|
4269
|
+
// ============================================================================
|
|
4270
|
+
const bizErrorInterceptor = {
|
|
4271
|
+
name: "biz-error",
|
|
4272
|
+
/** 对所有 call 调用生效 */
|
|
4273
|
+
match: () => true,
|
|
4274
|
+
/** 检查返回结果中的业务错误码,必要时清理缓存 */
|
|
4275
|
+
afterCall(ctx, result) {
|
|
4276
|
+
checkBizErrorAndClearCache(result, ctx.category);
|
|
4277
|
+
// 不修改 result,透传给下一个拦截器
|
|
4278
|
+
return result;
|
|
4279
|
+
},
|
|
4280
|
+
};
|
|
4281
|
+
// ============================================================================
|
|
4282
|
+
// 内部实现
|
|
4283
|
+
// ============================================================================
|
|
4310
4284
|
/**
|
|
4311
4285
|
* 检查 tools/call 的返回结果中是否包含需要清理缓存的业务错误码
|
|
4312
|
-
*
|
|
4313
|
-
* MCP Server 可能在正常的 JSON-RPC 响应中返回业务层错误,
|
|
4314
|
-
* 这些错误被包裹在 result.content[].text 中,需要解析后判断。
|
|
4315
4286
|
*/
|
|
4316
|
-
|
|
4287
|
+
function checkBizErrorAndClearCache(result, category) {
|
|
4317
4288
|
if (!result || typeof result !== "object")
|
|
4318
4289
|
return;
|
|
4319
4290
|
const { content } = result;
|
|
@@ -4334,14 +4305,39 @@ const checkBizErrorAndClearCache = (result, category) => {
|
|
|
4334
4305
|
// text 不是 JSON 格式,跳过
|
|
4335
4306
|
}
|
|
4336
4307
|
}
|
|
4308
|
+
}
|
|
4309
|
+
|
|
4310
|
+
/**
|
|
4311
|
+
* get_msg_media 响应拦截器
|
|
4312
|
+
*
|
|
4313
|
+
* 核心逻辑:
|
|
4314
|
+
* 1. beforeCall: 设置延长的超时时间(120s),因为 base64 数据可达 ~27MB
|
|
4315
|
+
* 2. afterCall: 从 MCP result 的 content[].text 中提取 base64_data,
|
|
4316
|
+
* 解码为 Buffer 并通过 saveMediaBuffer 保存到本地媒体目录,
|
|
4317
|
+
* 替换响应中的 base64_data 为 local_path,避免大模型被 base64 数据消耗 token
|
|
4318
|
+
*/
|
|
4319
|
+
// ============================================================================
|
|
4320
|
+
// 拦截器实现
|
|
4321
|
+
// ============================================================================
|
|
4322
|
+
const mediaInterceptor = {
|
|
4323
|
+
name: "media",
|
|
4324
|
+
/** 仅对 get_msg_media 方法生效 */
|
|
4325
|
+
match: (ctx) => ctx.method === "get_msg_media",
|
|
4326
|
+
/** 设置延长的超时时间 */
|
|
4327
|
+
beforeCall() {
|
|
4328
|
+
return { timeoutMs: MEDIA_DOWNLOAD_TIMEOUT_MS };
|
|
4329
|
+
},
|
|
4330
|
+
/** 拦截响应:base64 → 本地文件 */
|
|
4331
|
+
async afterCall(ctx, result) {
|
|
4332
|
+
return interceptMediaResponse(result);
|
|
4333
|
+
},
|
|
4337
4334
|
};
|
|
4338
4335
|
// ============================================================================
|
|
4339
|
-
//
|
|
4336
|
+
// 内部实现
|
|
4340
4337
|
// ============================================================================
|
|
4341
4338
|
/**
|
|
4342
4339
|
* 拦截 get_msg_media 的 MCP 响应
|
|
4343
4340
|
*
|
|
4344
|
-
* 核心逻辑:
|
|
4345
4341
|
* 1. 从 MCP result 的 content[].text 中提取业务 JSON
|
|
4346
4342
|
* 2. 提取 media_item.base64_data,解码为 Buffer
|
|
4347
4343
|
* 3. 通过 openclaw SDK 的 saveMediaBuffer 保存到本地媒体目录
|
|
@@ -4439,31 +4435,384 @@ async function interceptMediaResponse(result) {
|
|
|
4439
4435
|
}],
|
|
4440
4436
|
};
|
|
4441
4437
|
}
|
|
4438
|
+
|
|
4439
|
+
/**
|
|
4440
|
+
* smartpage_create 请求拦截器
|
|
4441
|
+
*
|
|
4442
|
+
* 核心逻辑:
|
|
4443
|
+
* smartpage_create 的 pages 数组中,每个 page 可能包含 page_filepath 字段
|
|
4444
|
+
* (指向本地 markdown 文件),用于避免在命令行传递大段文本内容。
|
|
4445
|
+
* 本拦截器在 beforeCall 阶段遍历 pages 数组,逐个读取 page_filepath
|
|
4446
|
+
* 指向的本地文件内容,填入 page_content 字段,并移除 page_filepath。
|
|
4447
|
+
*
|
|
4448
|
+
* 入参约定:
|
|
4449
|
+
* wecom_mcp call doc smartpage_create '{
|
|
4450
|
+
* "title": "主页标题",
|
|
4451
|
+
* "pages": [
|
|
4452
|
+
* {"page_title": "页面1", "page_filepath": "/tmp/page1.md", "content_type": "markdown"},
|
|
4453
|
+
* {"page_title": "页面2", "page_filepath": "/tmp/page2.md", "content_type": "markdown"}
|
|
4454
|
+
* ]
|
|
4455
|
+
* }'
|
|
4456
|
+
*
|
|
4457
|
+
* 拦截器行为:
|
|
4458
|
+
* 1. 检测 args.pages 数组
|
|
4459
|
+
* 2. 校验文件大小:单文件不超过 10MB,所有文件总计不超过 20MB
|
|
4460
|
+
* 3. 遍历每个 page,若存在 page_filepath 则读取本地文件内容
|
|
4461
|
+
* 4. 将文件内容填入 page_content 字段,移除 page_filepath
|
|
4462
|
+
* 5. 返回修改后的完整 args
|
|
4463
|
+
*
|
|
4464
|
+
* 传给 MCP Server 的格式:
|
|
4465
|
+
* { "title": "...", "pages": [{"page_title": "...", "page_content": "...", "content_type": "..."}] }
|
|
4466
|
+
*/
|
|
4467
|
+
// ============================================================================
|
|
4468
|
+
// 常量
|
|
4469
|
+
// ============================================================================
|
|
4470
|
+
/** 单个 page_filepath 文件大小上限:10MB */
|
|
4471
|
+
const MAX_SINGLE_FILE_SIZE = 10 * 1024 * 1024;
|
|
4472
|
+
/** 所有 page_filepath 文件总大小上限:20MB */
|
|
4473
|
+
const MAX_TOTAL_FILE_SIZE = 20 * 1024 * 1024;
|
|
4474
|
+
// ============================================================================
|
|
4475
|
+
// 内部辅助函数
|
|
4476
|
+
// ============================================================================
|
|
4477
|
+
/**
|
|
4478
|
+
* 校验所有 page_filepath 的文件大小
|
|
4479
|
+
*
|
|
4480
|
+
* 使用 fs.stat 在读取文件内容之前检查大小,避免超大文件被加载到内存。
|
|
4481
|
+
* - 单文件 > 10MB → 报错
|
|
4482
|
+
* - 所有文件累计 > 20MB → 报错
|
|
4483
|
+
*/
|
|
4484
|
+
async function validateFileSize(pages) {
|
|
4485
|
+
let totalSize = 0;
|
|
4486
|
+
for (let i = 0; i < pages.length; i++) {
|
|
4487
|
+
const filePath = pages[i].page_filepath;
|
|
4488
|
+
if (typeof filePath !== "string" || !filePath)
|
|
4489
|
+
continue;
|
|
4490
|
+
let stat;
|
|
4491
|
+
try {
|
|
4492
|
+
stat = await fs__namespace.stat(filePath);
|
|
4493
|
+
}
|
|
4494
|
+
catch (err) {
|
|
4495
|
+
// stat 失败不在这里处理,留给后续 readFile 阶段抛出更详细的错误
|
|
4496
|
+
continue;
|
|
4497
|
+
}
|
|
4498
|
+
if (stat.size > MAX_SINGLE_FILE_SIZE) {
|
|
4499
|
+
console.error(`[mcp] smartpage_create: pages[${i}] 文件 "${filePath}" ` +
|
|
4500
|
+
`大小 ${(stat.size / 1024 / 1024).toFixed(1)}MB 超过单文件上限 10MB`);
|
|
4501
|
+
throw new Error("内容大小超出限制,无法创建");
|
|
4502
|
+
}
|
|
4503
|
+
totalSize += stat.size;
|
|
4504
|
+
if (totalSize > MAX_TOTAL_FILE_SIZE) {
|
|
4505
|
+
console.error(`[mcp] smartpage_create: 累计文件大小 ${(totalSize / 1024 / 1024).toFixed(1)}MB ` +
|
|
4506
|
+
`超过总上限 20MB(在 pages[${i}] "${filePath}" 处超出)`);
|
|
4507
|
+
throw new Error("内容大小超出限制,无法创建");
|
|
4508
|
+
}
|
|
4509
|
+
}
|
|
4510
|
+
if (totalSize > 0) {
|
|
4511
|
+
console.log(`[mcp] smartpage_create: 文件大小校验通过,总计 ${(totalSize / 1024 / 1024).toFixed(2)}MB`);
|
|
4512
|
+
}
|
|
4513
|
+
}
|
|
4514
|
+
/** 异步解析 pages 中的 page_filepath,返回 BeforeCallOptions */
|
|
4515
|
+
async function resolvePages(ctx, pages) {
|
|
4516
|
+
console.log(`[mcp] smartpage_create: 开始解析 ${pages.length} 个 page 的 page_filepath`);
|
|
4517
|
+
// 阶段 1:文件大小校验(stat 阶段,不读内容)
|
|
4518
|
+
await validateFileSize(pages);
|
|
4519
|
+
// 阶段 2:读取文件内容
|
|
4520
|
+
const resolvedPages = await Promise.all(pages.map(async (page, index) => {
|
|
4521
|
+
const filePath = page.page_filepath;
|
|
4522
|
+
if (typeof filePath !== "string" || !filePath) {
|
|
4523
|
+
// 该 page 没有 page_filepath,保留原样(可能已有 page_content)
|
|
4524
|
+
return page;
|
|
4525
|
+
}
|
|
4526
|
+
let fileContent;
|
|
4527
|
+
try {
|
|
4528
|
+
fileContent = await fs__namespace.readFile(filePath, "utf-8");
|
|
4529
|
+
}
|
|
4530
|
+
catch (err) {
|
|
4531
|
+
throw new Error(`smartpage_create: pages[${index}] 无法读取文件 "${filePath}": ${err instanceof Error ? err.message : String(err)}`);
|
|
4532
|
+
}
|
|
4533
|
+
console.log(`[mcp] smartpage_create: pages[${index}] 读取成功 "${filePath}" (${fileContent.length} chars)`);
|
|
4534
|
+
// 构造新的 page 对象:填入 page_content,移除 page_filepath
|
|
4535
|
+
const { page_filepath: _, ...rest } = page;
|
|
4536
|
+
return { ...rest, page_content: fileContent };
|
|
4537
|
+
}));
|
|
4538
|
+
console.log(`[mcp] smartpage_create: 所有 page_filepath 解析完成`);
|
|
4539
|
+
// 返回修改后的完整 args
|
|
4540
|
+
return {
|
|
4541
|
+
args: {
|
|
4542
|
+
...ctx.args,
|
|
4543
|
+
pages: resolvedPages,
|
|
4544
|
+
},
|
|
4545
|
+
};
|
|
4546
|
+
}
|
|
4547
|
+
// ============================================================================
|
|
4548
|
+
// 拦截器实现
|
|
4549
|
+
// ============================================================================
|
|
4550
|
+
const smartpageCreateInterceptor = {
|
|
4551
|
+
name: "smartpage-create",
|
|
4552
|
+
/** 仅对 doc 品类的 smartpage_create 方法生效 */
|
|
4553
|
+
match: (ctx) => ctx.category === "doc" && ctx.method === "smartpage_create",
|
|
4554
|
+
/** 遍历 pages 数组,逐个读取 page_filepath 填入 page_content */
|
|
4555
|
+
beforeCall(ctx) {
|
|
4556
|
+
const pages = ctx.args.pages;
|
|
4557
|
+
if (!Array.isArray(pages) || pages.length === 0) {
|
|
4558
|
+
// 没有 pages 数组,不做拦截
|
|
4559
|
+
return undefined;
|
|
4560
|
+
}
|
|
4561
|
+
// 检查是否有任何 page 包含 page_filepath
|
|
4562
|
+
const hasFilePath = pages.some((p) => typeof p.page_filepath === "string" && p.page_filepath);
|
|
4563
|
+
if (!hasFilePath) {
|
|
4564
|
+
// 所有 page 都没有 page_filepath(可能直接传了 page_content),不做拦截
|
|
4565
|
+
return undefined;
|
|
4566
|
+
}
|
|
4567
|
+
return resolvePages(ctx, pages);
|
|
4568
|
+
},
|
|
4569
|
+
};
|
|
4570
|
+
|
|
4571
|
+
/**
|
|
4572
|
+
* smartpage_get_export_result 响应拦截器
|
|
4573
|
+
*
|
|
4574
|
+
* 核心逻辑:
|
|
4575
|
+
* MCP Server 返回的 smartpage_get_export_result 响应中,当 task_done=true 时
|
|
4576
|
+
* 会包含 content 字段(markdown 文本内容)。该内容可能很大,直接返回给 LLM
|
|
4577
|
+
* 会消耗大量 token。
|
|
4578
|
+
*
|
|
4579
|
+
* 本拦截器在 afterCall 阶段:
|
|
4580
|
+
* 1. 检测 task_done=true 且存在 content 字段
|
|
4581
|
+
* 2. 将 content 保存到本地文件(使用与 msg-media 一致的媒体目录)
|
|
4582
|
+
* 3. 用 content_path(文件路径)替换 content 字段
|
|
4583
|
+
*
|
|
4584
|
+
* 这样 LLM 只看到轻量的文件路径信息,Skill 可通过文件路径读取完整内容。
|
|
4585
|
+
*/
|
|
4586
|
+
// ============================================================================
|
|
4587
|
+
// 拦截器实现
|
|
4588
|
+
// ============================================================================
|
|
4589
|
+
const smartpageExportInterceptor = {
|
|
4590
|
+
name: "smartpage-export",
|
|
4591
|
+
/** 仅对 doc 品类的 smartpage_get_export_result 方法生效 */
|
|
4592
|
+
match: (ctx) => ctx.category === "doc" && ctx.method === "smartpage_get_export_result",
|
|
4593
|
+
/** 拦截响应:将 markdown content 保存为本地文件 */
|
|
4594
|
+
async afterCall(_ctx, result) {
|
|
4595
|
+
return interceptExportResponse(result);
|
|
4596
|
+
},
|
|
4597
|
+
};
|
|
4598
|
+
// ============================================================================
|
|
4599
|
+
// 内部实现
|
|
4600
|
+
// ============================================================================
|
|
4601
|
+
/**
|
|
4602
|
+
* 拦截 smartpage_get_export_result 的 MCP 响应
|
|
4603
|
+
*
|
|
4604
|
+
* 1. 从 MCP result 的 content[].text 中提取业务 JSON
|
|
4605
|
+
* 2. 检测 task_done=true 且存在 content 字段
|
|
4606
|
+
* 3. 将 content(markdown 文本)通过 saveMediaBuffer 保存到本地媒体目录
|
|
4607
|
+
* 4. 构造新响应:移除 content,添加 content_path
|
|
4608
|
+
*/
|
|
4609
|
+
async function interceptExportResponse(result) {
|
|
4610
|
+
// 1. 提取 MCP result 中的 content 数组
|
|
4611
|
+
const content = result?.content;
|
|
4612
|
+
if (!Array.isArray(content))
|
|
4613
|
+
return result;
|
|
4614
|
+
const textItem = content.find((c) => c.type === "text" && typeof c.text === "string");
|
|
4615
|
+
if (!textItem)
|
|
4616
|
+
return result;
|
|
4617
|
+
// 2. 解析业务 JSON
|
|
4618
|
+
let bizData;
|
|
4619
|
+
try {
|
|
4620
|
+
bizData = JSON.parse(textItem.text);
|
|
4621
|
+
}
|
|
4622
|
+
catch {
|
|
4623
|
+
// 非 JSON 格式,原样返回
|
|
4624
|
+
return result;
|
|
4625
|
+
}
|
|
4626
|
+
// 3. 校验:errcode !== 0 或 task_done 不为 true 或无 content 时原样返回
|
|
4627
|
+
if (bizData.errcode !== 0)
|
|
4628
|
+
return result;
|
|
4629
|
+
if (bizData.task_done !== true)
|
|
4630
|
+
return result;
|
|
4631
|
+
if (typeof bizData.content !== "string")
|
|
4632
|
+
return result;
|
|
4633
|
+
const markdownContent = bizData.content;
|
|
4634
|
+
console.log(`[mcp] smartpage_get_export_result: 拦截 content (${markdownContent.length} chars),保存到本地文件`);
|
|
4635
|
+
// 4. 将 markdown 内容通过 saveMediaBuffer 保存到本地媒体目录
|
|
4636
|
+
// 使用 text/markdown 类型,与 msg-media 拦截器保持一致的路径管理
|
|
4637
|
+
const buffer = Buffer.from(markdownContent, "utf-8");
|
|
4638
|
+
const core = getWeComRuntime();
|
|
4639
|
+
const saved = await core.channel.media.saveMediaBuffer(buffer, "text/markdown", "inbound", undefined, // maxBytes: markdown 文本通常不大,使用默认限制
|
|
4640
|
+
"smartpage_export.md");
|
|
4641
|
+
console.log(`[mcp] smartpage_get_export_result: 已保存到 ${saved.path}`);
|
|
4642
|
+
// 5. 构造新响应:移除 content,添加 content_path
|
|
4643
|
+
const newBizData = {
|
|
4644
|
+
errcode: bizData.errcode,
|
|
4645
|
+
errmsg: bizData.errmsg ?? "ok",
|
|
4646
|
+
task_done: true,
|
|
4647
|
+
content_path: saved.path,
|
|
4648
|
+
};
|
|
4649
|
+
// 6. 返回修改后的 MCP result 结构
|
|
4650
|
+
return {
|
|
4651
|
+
content: [{
|
|
4652
|
+
type: "text",
|
|
4653
|
+
text: JSON.stringify(newBizData),
|
|
4654
|
+
}],
|
|
4655
|
+
};
|
|
4656
|
+
}
|
|
4657
|
+
|
|
4658
|
+
/**
|
|
4659
|
+
* MCP call 拦截器注册表与调度入口
|
|
4660
|
+
*
|
|
4661
|
+
* 所有 call 拦截器在此注册,按注册顺序执行。
|
|
4662
|
+
* 新增拦截器只需:
|
|
4663
|
+
* 1. 在 interceptors/ 目录下新建文件,实现 CallInterceptor 接口
|
|
4664
|
+
* 2. 在下方 interceptors 数组中注册
|
|
4665
|
+
*
|
|
4666
|
+
* tool.ts 的 handleCall 无需任何改动。
|
|
4667
|
+
*/
|
|
4668
|
+
// ============================================================================
|
|
4669
|
+
// 拦截器注册表(按注册顺序执行)
|
|
4670
|
+
// ============================================================================
|
|
4671
|
+
const interceptors = [
|
|
4672
|
+
bizErrorInterceptor, // 业务错误码检查(所有 call 生效)
|
|
4673
|
+
mediaInterceptor, // get_msg_media base64 拦截
|
|
4674
|
+
smartpageCreateInterceptor, // smartpage_create 本地文件读取
|
|
4675
|
+
smartpageExportInterceptor, // smartpage_get_export_result content → 本地文件
|
|
4676
|
+
];
|
|
4677
|
+
/**
|
|
4678
|
+
* 收集匹配的 beforeCall 配置,合并后返回
|
|
4679
|
+
*
|
|
4680
|
+
* 合并策略:
|
|
4681
|
+
* - timeoutMs: 取所有拦截器返回值中的最大值
|
|
4682
|
+
* - args: 后注册的拦截器覆盖前者(一般同一调用只有一个拦截器会返回 args)
|
|
4683
|
+
*/
|
|
4684
|
+
async function resolveBeforeCall(ctx) {
|
|
4685
|
+
let mergedTimeoutMs;
|
|
4686
|
+
let mergedArgs;
|
|
4687
|
+
for (const interceptor of interceptors) {
|
|
4688
|
+
if (!interceptor.match(ctx) || !interceptor.beforeCall)
|
|
4689
|
+
continue;
|
|
4690
|
+
const opts = await interceptor.beforeCall(ctx);
|
|
4691
|
+
if (opts?.timeoutMs !== undefined) {
|
|
4692
|
+
mergedTimeoutMs = mergedTimeoutMs === undefined
|
|
4693
|
+
? opts.timeoutMs
|
|
4694
|
+
: Math.max(mergedTimeoutMs, opts.timeoutMs);
|
|
4695
|
+
}
|
|
4696
|
+
if (opts?.args !== undefined) {
|
|
4697
|
+
mergedArgs = opts.args;
|
|
4698
|
+
}
|
|
4699
|
+
}
|
|
4700
|
+
return {
|
|
4701
|
+
options: mergedTimeoutMs !== undefined ? { timeoutMs: mergedTimeoutMs } : undefined,
|
|
4702
|
+
args: mergedArgs,
|
|
4703
|
+
};
|
|
4704
|
+
}
|
|
4705
|
+
/**
|
|
4706
|
+
* 依次执行匹配的 afterCall 拦截器,管道式传递 result
|
|
4707
|
+
*
|
|
4708
|
+
* 前一个拦截器的返回值作为下一个拦截器的输入。
|
|
4709
|
+
* 拦截器若不需要修改 result,应原样返回。
|
|
4710
|
+
*/
|
|
4711
|
+
async function runAfterCall(ctx, result) {
|
|
4712
|
+
let current = result;
|
|
4713
|
+
for (const interceptor of interceptors) {
|
|
4714
|
+
if (!interceptor.match(ctx) || !interceptor.afterCall)
|
|
4715
|
+
continue;
|
|
4716
|
+
current = await interceptor.afterCall(ctx, current);
|
|
4717
|
+
}
|
|
4718
|
+
return current;
|
|
4719
|
+
}
|
|
4720
|
+
|
|
4721
|
+
/**
|
|
4722
|
+
* wecom_mcp — 模拟 MCP 调用的 Agent Tool
|
|
4723
|
+
*
|
|
4724
|
+
* 通过 MCP Streamable HTTP 传输协议调用企业微信 MCP Server,
|
|
4725
|
+
* 提供 list(列出所有工具)和 call(调用工具)两个操作。
|
|
4726
|
+
*
|
|
4727
|
+
* 在 skills 中的使用方式:
|
|
4728
|
+
* wecom_mcp list <category>
|
|
4729
|
+
* wecom_mcp call <category> <method> '<jsonArgs>'
|
|
4730
|
+
*
|
|
4731
|
+
* 示例:
|
|
4732
|
+
* wecom_mcp list contact
|
|
4733
|
+
* wecom_mcp call contact getContact '{}'
|
|
4734
|
+
*/
|
|
4735
|
+
// ============================================================================
|
|
4736
|
+
// 响应构造辅助
|
|
4737
|
+
// ============================================================================
|
|
4738
|
+
/** 构造统一的文本响应结构 */
|
|
4739
|
+
const textResult = (data) => ({
|
|
4740
|
+
content: [{ type: "text", text: JSON.stringify(data, null, 2) }],
|
|
4741
|
+
});
|
|
4742
|
+
/** 构造错误响应 */
|
|
4743
|
+
const errorResult = (err) => {
|
|
4744
|
+
// 适配企业微信 API 返回的 { errcode, errmsg } 结构
|
|
4745
|
+
if (err && typeof err === "object" && "errcode" in err) {
|
|
4746
|
+
const { errcode, errmsg } = err;
|
|
4747
|
+
return textResult({ error: errmsg ?? `错误码: ${errcode}`, errcode });
|
|
4748
|
+
}
|
|
4749
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
4750
|
+
return textResult({ error: message });
|
|
4751
|
+
};
|
|
4752
|
+
// ============================================================================
|
|
4753
|
+
// list 操作:列出某品类的所有 MCP 工具
|
|
4754
|
+
// ============================================================================
|
|
4755
|
+
const handleList = async (category) => {
|
|
4756
|
+
const result = await sendJsonRpc(category, "tools/list");
|
|
4757
|
+
const tools = result?.tools ?? [];
|
|
4758
|
+
if (tools.length === 0) {
|
|
4759
|
+
return { message: `品类 "${category}" 下暂无可用工具`, tools: [] };
|
|
4760
|
+
}
|
|
4761
|
+
return {
|
|
4762
|
+
category,
|
|
4763
|
+
count: tools.length,
|
|
4764
|
+
tools: tools.map((t) => ({
|
|
4765
|
+
name: t.name,
|
|
4766
|
+
description: t.description ?? "",
|
|
4767
|
+
// 清洗 inputSchema,内联 $ref/$defs 引用并移除 Gemini 不支持的关键词,
|
|
4768
|
+
// 避免 Gemini 模型解析 function response 时报 400 错误
|
|
4769
|
+
inputSchema: t.inputSchema ? cleanSchemaForGemini(t.inputSchema) : undefined,
|
|
4770
|
+
})),
|
|
4771
|
+
};
|
|
4772
|
+
};
|
|
4442
4773
|
// ============================================================================
|
|
4443
4774
|
// call 操作:调用某品类的某个 MCP 工具
|
|
4444
4775
|
// ============================================================================
|
|
4445
4776
|
const handleCall = async (category, method, args) => {
|
|
4777
|
+
const ctx = { category, method, args };
|
|
4446
4778
|
const callStart = performance.now();
|
|
4447
|
-
|
|
4448
|
-
|
|
4779
|
+
console.log(`[mcp] handleCall ${category}/${method} 入参: ${JSON.stringify(args)}`);
|
|
4780
|
+
// 1. 收集拦截器的 beforeCall 配置(如超时时间、替换 args)
|
|
4781
|
+
const { options, args: resolvedArgs } = await resolveBeforeCall(ctx);
|
|
4782
|
+
const finalArgs = resolvedArgs ?? args;
|
|
4783
|
+
if (resolvedArgs) {
|
|
4784
|
+
console.log(`[mcp] handleCall ${category}/${method} 拦截器替换 args: ${JSON.stringify(resolvedArgs).slice(0, 500)}` +
|
|
4785
|
+
(JSON.stringify(resolvedArgs).length > 500 ? "...(truncated)" : ""));
|
|
4786
|
+
}
|
|
4787
|
+
if (options) {
|
|
4788
|
+
console.log(`[mcp] handleCall ${category}/${method} 拦截器选项: ${JSON.stringify(options)}`);
|
|
4789
|
+
}
|
|
4790
|
+
// 2. 执行 MCP 调用
|
|
4449
4791
|
const result = await sendJsonRpc(category, "tools/call", {
|
|
4450
4792
|
name: method,
|
|
4451
|
-
arguments:
|
|
4793
|
+
arguments: finalArgs,
|
|
4452
4794
|
}, options);
|
|
4453
4795
|
const rpcDone = performance.now();
|
|
4454
4796
|
const rpcMs = (rpcDone - callStart).toFixed(1);
|
|
4455
|
-
|
|
4456
|
-
|
|
4457
|
-
|
|
4458
|
-
|
|
4459
|
-
|
|
4460
|
-
|
|
4797
|
+
const resultStr = JSON.stringify(result);
|
|
4798
|
+
console.log(`[mcp] handleCall ${category}/${method} MCP 响应 (${rpcMs}ms): ${resultStr.slice(0, 800)}` +
|
|
4799
|
+
(resultStr.length > 800 ? "...(truncated)" : ""));
|
|
4800
|
+
// 3. 管道式执行 afterCall 拦截器(业务错误码检查、响应变换等)
|
|
4801
|
+
const finalResult = await runAfterCall(ctx, result);
|
|
4802
|
+
const totalMs = (performance.now() - callStart).toFixed(1);
|
|
4803
|
+
const interceptMs = (performance.now() - rpcDone).toFixed(1);
|
|
4804
|
+
// 有拦截器处理时打印详细耗时,否则只打印 RPC 耗时
|
|
4805
|
+
if (finalResult !== result) {
|
|
4806
|
+
const finalStr = JSON.stringify(finalResult);
|
|
4807
|
+
console.log(`[mcp] handleCall ${category}/${method} afterCall 变换后 (${interceptMs}ms): ${finalStr.slice(0, 500)}` +
|
|
4808
|
+
(finalStr.length > 500 ? "...(truncated)" : ""));
|
|
4461
4809
|
console.log(`[mcp] handleCall ${category}/${method} 总耗时: ${totalMs}ms` +
|
|
4462
|
-
` (MCP请求: ${rpcMs}ms, 拦截处理: ${
|
|
4463
|
-
|
|
4810
|
+
` (MCP请求: ${rpcMs}ms, 拦截处理: ${interceptMs}ms)`);
|
|
4811
|
+
}
|
|
4812
|
+
else {
|
|
4813
|
+
console.log(`[mcp] handleCall ${category}/${method} 耗时: ${rpcMs}ms`);
|
|
4464
4814
|
}
|
|
4465
|
-
|
|
4466
|
-
return result;
|
|
4815
|
+
return finalResult;
|
|
4467
4816
|
};
|
|
4468
4817
|
// ============================================================================
|
|
4469
4818
|
// 参数解析
|
|
@@ -4533,22 +4882,36 @@ function createWeComMcpTool() {
|
|
|
4533
4882
|
},
|
|
4534
4883
|
async execute(_toolCallId, params) {
|
|
4535
4884
|
const p = params;
|
|
4885
|
+
console.log(`[mcp] execute: action=${p.action}, category=${p.category}` +
|
|
4886
|
+
(p.method ? `, method=${p.method}` : "") +
|
|
4887
|
+
(p.args ? `, args=${typeof p.args === "string" ? p.args : JSON.stringify(p.args)}` : ""));
|
|
4536
4888
|
try {
|
|
4889
|
+
let result;
|
|
4537
4890
|
switch (p.action) {
|
|
4538
4891
|
case "list":
|
|
4539
|
-
|
|
4892
|
+
result = textResult(await handleList(p.category));
|
|
4893
|
+
break;
|
|
4540
4894
|
case "call": {
|
|
4541
4895
|
if (!p.method) {
|
|
4542
|
-
|
|
4896
|
+
result = textResult({ error: "action 为 call 时必须提供 method 参数" });
|
|
4897
|
+
break;
|
|
4543
4898
|
}
|
|
4544
4899
|
const args = parseArgs(p.args);
|
|
4545
|
-
|
|
4900
|
+
result = textResult(await handleCall(p.category, p.method, args));
|
|
4901
|
+
break;
|
|
4546
4902
|
}
|
|
4547
4903
|
default:
|
|
4548
|
-
|
|
4904
|
+
result = textResult({ error: `未知操作类型: ${String(p.action)},支持 list 和 call` });
|
|
4549
4905
|
}
|
|
4906
|
+
console.log(`[mcp] execute: action=${p.action}, category=${p.category}` +
|
|
4907
|
+
(p.method ? `, method=${p.method}` : "") +
|
|
4908
|
+
` → 响应长度=${result.content[0].text.length} chars`);
|
|
4909
|
+
return result;
|
|
4550
4910
|
}
|
|
4551
4911
|
catch (err) {
|
|
4912
|
+
console.error(`[mcp] execute: action=${p.action}, category=${p.category}` +
|
|
4913
|
+
(p.method ? `, method=${p.method}` : "") +
|
|
4914
|
+
` → 异常: ${err instanceof Error ? err.message : String(err)}`);
|
|
4552
4915
|
return errorResult(err);
|
|
4553
4916
|
}
|
|
4554
4917
|
},
|