ylib-wecom-openclaw-plugin 2026.4.29
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 +596 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +99 -0
- package/dist/src/accounts.d.ts +57 -0
- package/dist/src/accounts.js +247 -0
- package/dist/src/agent/api-client.d.ts +95 -0
- package/dist/src/agent/api-client.js +425 -0
- package/dist/src/agent/handler.d.ts +64 -0
- package/dist/src/agent/handler.js +731 -0
- package/dist/src/agent/index.d.ts +5 -0
- package/dist/src/agent/index.js +21 -0
- package/dist/src/agent/webhook.d.ts +25 -0
- package/dist/src/agent/webhook.js +294 -0
- package/dist/src/agent/xml.d.ts +21 -0
- package/dist/src/agent/xml.js +43 -0
- package/dist/src/channel.d.ts +5 -0
- package/dist/src/channel.js +815 -0
- package/dist/src/chat-queue.d.ts +31 -0
- package/dist/src/chat-queue.js +53 -0
- package/dist/src/config-schema.d.ts +587 -0
- package/dist/src/config-schema.js +146 -0
- package/dist/src/const.d.ts +128 -0
- package/dist/src/const.js +168 -0
- package/dist/src/dm-policy.d.ts +29 -0
- package/dist/src/dm-policy.js +146 -0
- package/dist/src/dynamic-agent.d.ts +37 -0
- package/dist/src/dynamic-agent.js +67 -0
- package/dist/src/dynamic-routing.d.ts +65 -0
- package/dist/src/dynamic-routing.js +62 -0
- package/dist/src/endpoint-dispatch.d.ts +54 -0
- package/dist/src/endpoint-dispatch.js +967 -0
- package/dist/src/endpoint-event-adapter.d.ts +15 -0
- package/dist/src/endpoint-event-adapter.js +427 -0
- package/dist/src/group-policy.d.ts +30 -0
- package/dist/src/group-policy.js +126 -0
- package/dist/src/http.d.ts +27 -0
- package/dist/src/http.js +168 -0
- package/dist/src/im-runtime-telemetry.d.ts +25 -0
- package/dist/src/im-runtime-telemetry.js +68 -0
- package/dist/src/interface.d.ts +192 -0
- package/dist/src/interface.js +5 -0
- package/dist/src/markdown-chunk.d.ts +1 -0
- package/dist/src/markdown-chunk.js +396 -0
- package/dist/src/mcp/index.d.ts +6 -0
- package/dist/src/mcp/index.js +28 -0
- package/dist/src/mcp/interceptors/biz-error.d.ts +11 -0
- package/dist/src/mcp/interceptors/biz-error.js +73 -0
- package/dist/src/mcp/interceptors/doc-auth-error.d.ts +10 -0
- package/dist/src/mcp/interceptors/doc-auth-error.js +235 -0
- package/dist/src/mcp/interceptors/index.d.ts +35 -0
- package/dist/src/mcp/interceptors/index.js +143 -0
- package/dist/src/mcp/interceptors/msg-media.d.ts +11 -0
- package/dist/src/mcp/interceptors/msg-media.js +201 -0
- package/dist/src/mcp/interceptors/smartpage-create.d.ts +30 -0
- package/dist/src/mcp/interceptors/smartpage-create.js +252 -0
- package/dist/src/mcp/interceptors/smartpage-export.d.ts +17 -0
- package/dist/src/mcp/interceptors/smartpage-export.js +135 -0
- package/dist/src/mcp/interceptors/smartsheet-upload.d.ts +22 -0
- package/dist/src/mcp/interceptors/smartsheet-upload.js +388 -0
- package/dist/src/mcp/interceptors/types.d.ts +64 -0
- package/dist/src/mcp/interceptors/types.js +8 -0
- package/dist/src/mcp/schema.d.ts +11 -0
- package/dist/src/mcp/schema.js +115 -0
- package/dist/src/mcp/tool.d.ts +63 -0
- package/dist/src/mcp/tool.js +318 -0
- package/dist/src/mcp/transport.d.ts +94 -0
- package/dist/src/mcp/transport.js +702 -0
- package/dist/src/media-handler.d.ts +55 -0
- package/dist/src/media-handler.js +306 -0
- package/dist/src/media-uploader.d.ts +142 -0
- package/dist/src/media-uploader.js +446 -0
- package/dist/src/message-parser.d.ts +104 -0
- package/dist/src/message-parser.js +232 -0
- package/dist/src/message-sender.d.ts +54 -0
- package/dist/src/message-sender.js +210 -0
- package/dist/src/monitor.d.ts +69 -0
- package/dist/src/monitor.js +1846 -0
- package/dist/src/onboarding.d.ts +8 -0
- package/dist/src/onboarding.js +248 -0
- package/dist/src/openclaw-compat.d.ts +148 -0
- package/dist/src/openclaw-compat.js +839 -0
- package/dist/src/proactive-markdown-send.d.ts +14 -0
- package/dist/src/proactive-markdown-send.js +205 -0
- package/dist/src/reqid-store.d.ts +23 -0
- package/dist/src/reqid-store.js +136 -0
- package/dist/src/runtime.d.ts +2 -0
- package/dist/src/runtime.js +7 -0
- package/dist/src/shared/command-auth.d.ts +23 -0
- package/dist/src/shared/command-auth.js +112 -0
- package/dist/src/shared/xml-parser.d.ts +46 -0
- package/dist/src/shared/xml-parser.js +228 -0
- package/dist/src/state-dir-resolve.d.ts +2 -0
- package/dist/src/state-dir-resolve.js +33 -0
- package/dist/src/state-manager.d.ts +115 -0
- package/dist/src/state-manager.js +413 -0
- package/dist/src/target.d.ts +35 -0
- package/dist/src/target.js +71 -0
- package/dist/src/template-card-manager.d.ts +55 -0
- package/dist/src/template-card-manager.js +316 -0
- package/dist/src/template-card-parser.d.ts +37 -0
- package/dist/src/template-card-parser.js +672 -0
- package/dist/src/timeout.d.ts +20 -0
- package/dist/src/timeout.js +57 -0
- package/dist/src/types/account.d.ts +29 -0
- package/dist/src/types/account.js +5 -0
- package/dist/src/types/config.d.ts +98 -0
- package/dist/src/types/config.js +8 -0
- package/dist/src/types/constants.d.ts +42 -0
- package/dist/src/types/constants.js +45 -0
- package/dist/src/types/index.d.ts +7 -0
- package/dist/src/types/index.js +17 -0
- package/dist/src/types/message.d.ts +238 -0
- package/dist/src/types/message.js +6 -0
- package/dist/src/utils.d.ts +148 -0
- package/dist/src/utils.js +92 -0
- package/dist/src/version.d.ts +2 -0
- package/dist/src/version.js +28 -0
- package/dist/src/webhook/command-auth.d.ts +47 -0
- package/dist/src/webhook/command-auth.js +137 -0
- package/dist/src/webhook/gateway.d.ts +36 -0
- package/dist/src/webhook/gateway.js +297 -0
- package/dist/src/webhook/handler.d.ts +19 -0
- package/dist/src/webhook/handler.js +481 -0
- package/dist/src/webhook/helpers.d.ts +157 -0
- package/dist/src/webhook/helpers.js +936 -0
- package/dist/src/webhook/http.d.ts +27 -0
- package/dist/src/webhook/http.js +168 -0
- package/dist/src/webhook/index.d.ts +11 -0
- package/dist/src/webhook/index.js +43 -0
- package/dist/src/webhook/media.d.ts +30 -0
- package/dist/src/webhook/media.js +152 -0
- package/dist/src/webhook/monitor.d.ts +59 -0
- package/dist/src/webhook/monitor.js +1672 -0
- package/dist/src/webhook/state.d.ts +220 -0
- package/dist/src/webhook/state.js +568 -0
- package/dist/src/webhook/target.d.ts +41 -0
- package/dist/src/webhook/target.js +165 -0
- package/dist/src/webhook/types.d.ts +348 -0
- package/dist/src/webhook/types.js +36 -0
- package/dist/src/webhook/video-frame.d.ts +13 -0
- package/dist/src/webhook/video-frame.js +108 -0
- package/openclaw.plugin.json +19 -0
- package/package.json +96 -0
- package/schema.json +534 -0
- package/scripts/generate-schema.mjs +33 -0
- package/skills/wecom-contact/SKILL.md +162 -0
- package/skills/wecom-doc/SKILL.md +162 -0
- package/skills/wecom-doc/references/create-doc.md +56 -0
- package/skills/wecom-doc/references/edit-doc-content.md +68 -0
- package/skills/wecom-doc/references/get-doc-content.md +88 -0
- package/skills/wecom-doc/references/smartpage-create.md +125 -0
- package/skills/wecom-doc/references/smartpage-export.md +160 -0
- package/skills/wecom-meeting/SKILL.md +441 -0
- package/skills/wecom-meeting/references/example-full.md +30 -0
- package/skills/wecom-meeting/references/example-reminder.md +46 -0
- package/skills/wecom-meeting/references/example-security.md +22 -0
- package/skills/wecom-meeting/references/response-get-meeting-info.md +148 -0
- package/skills/wecom-msg/SKILL.md +157 -0
- package/skills/wecom-msg/references/api-get-messages.md +93 -0
- package/skills/wecom-msg/references/api-get-msg-chat-list.md +58 -0
- package/skills/wecom-msg/references/api-get-msg-media.md +44 -0
- package/skills/wecom-msg/references/api-send-message.md +39 -0
- package/skills/wecom-preflight/SKILL.md +141 -0
- package/skills/wecom-schedule/SKILL.md +161 -0
- package/skills/wecom-schedule/references/api-check-availability.md +56 -0
- package/skills/wecom-schedule/references/api-create-schedule.md +38 -0
- package/skills/wecom-schedule/references/api-get-schedule-detail.md +81 -0
- package/skills/wecom-schedule/references/api-update-schedule.md +32 -0
- package/skills/wecom-schedule/references/ref-reminders.md +24 -0
- package/skills/wecom-send-media/SKILL.md +68 -0
- package/skills/wecom-send-template-card/SKILL.md +157 -0
- package/skills/wecom-send-template-card/references/api-template-card-types.md +358 -0
- package/skills/wecom-smartsheet/SKILL.md +164 -0
- package/skills/wecom-smartsheet/references/smartsheet-cell-value-formats.md +163 -0
- package/skills/wecom-smartsheet/references/smartsheet-field-types.md +44 -0
- package/skills/wecom-smartsheet/references/smartsheet-get-records.md +96 -0
- package/skills/wecom-smartsheet/references/webhook-examples.md +185 -0
- package/skills/wecom-smartsheet/references/webhook-fallback.md +184 -0
- package/skills/wecom-todo/SKILL.md +392 -0
- package/skills/wecom-todo/examples/workflows.md +163 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* wecom_mcp — 模拟 MCP 调用的 Agent Tool
|
|
4
|
+
*
|
|
5
|
+
* 通过 MCP Streamable HTTP 传输协议调用企业微信 MCP Server,
|
|
6
|
+
* 提供 list(列出所有工具)和 call(调用工具)两个操作。
|
|
7
|
+
*
|
|
8
|
+
* 在 skills 中的使用方式:
|
|
9
|
+
* wecom_mcp list <category>
|
|
10
|
+
* wecom_mcp call <category> <method> '<jsonArgs>'
|
|
11
|
+
*
|
|
12
|
+
* 示例:
|
|
13
|
+
* wecom_mcp list contact
|
|
14
|
+
* wecom_mcp call contact getContact '{}'
|
|
15
|
+
*/
|
|
16
|
+
var __assign = (this && this.__assign) || function () {
|
|
17
|
+
__assign = Object.assign || function(t) {
|
|
18
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
19
|
+
s = arguments[i];
|
|
20
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
21
|
+
t[p] = s[p];
|
|
22
|
+
}
|
|
23
|
+
return t;
|
|
24
|
+
};
|
|
25
|
+
return __assign.apply(this, arguments);
|
|
26
|
+
};
|
|
27
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
28
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
29
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
30
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
31
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
32
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
33
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
37
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
38
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
39
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
40
|
+
function step(op) {
|
|
41
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
42
|
+
while (_) try {
|
|
43
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
44
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
45
|
+
switch (op[0]) {
|
|
46
|
+
case 0: case 1: t = op; break;
|
|
47
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
48
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
49
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
50
|
+
default:
|
|
51
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
52
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
53
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
54
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
55
|
+
if (t[2]) _.ops.pop();
|
|
56
|
+
_.trys.pop(); continue;
|
|
57
|
+
}
|
|
58
|
+
op = body.call(thisArg, _);
|
|
59
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
60
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
exports.__esModule = true;
|
|
64
|
+
exports.createWeComMcpTool = void 0;
|
|
65
|
+
var transport_js_1 = require("./transport.js");
|
|
66
|
+
var schema_js_1 = require("./schema.js");
|
|
67
|
+
var index_js_1 = require("./interceptors/index.js");
|
|
68
|
+
// ============================================================================
|
|
69
|
+
// 响应构造辅助
|
|
70
|
+
// ============================================================================
|
|
71
|
+
/** 构造统一的文本响应结构 */
|
|
72
|
+
var textResult = function (data) { return ({
|
|
73
|
+
content: [{ type: "text", text: JSON.stringify(data, null, 2) }],
|
|
74
|
+
details: data
|
|
75
|
+
}); };
|
|
76
|
+
var normalizeOptionalString = function (value) {
|
|
77
|
+
var trimmed = value === null || value === void 0 ? void 0 : value.trim();
|
|
78
|
+
return trimmed ? trimmed : undefined;
|
|
79
|
+
};
|
|
80
|
+
/** 构造错误响应 */
|
|
81
|
+
var errorResult = function (err) {
|
|
82
|
+
// 适配企业微信 API 返回的 { errcode, errmsg } 结构
|
|
83
|
+
if (err && typeof err === "object" && "errcode" in err) {
|
|
84
|
+
var _a = err, errcode = _a.errcode, errmsg = _a.errmsg;
|
|
85
|
+
return textResult({ error: errmsg !== null && errmsg !== void 0 ? errmsg : "\u9519\u8BEF\u7801: " + errcode, errcode: errcode });
|
|
86
|
+
}
|
|
87
|
+
var message = err instanceof Error ? err.message : String(err);
|
|
88
|
+
return textResult({ error: message });
|
|
89
|
+
};
|
|
90
|
+
// ============================================================================
|
|
91
|
+
// list 操作:列出某品类的所有 MCP 工具
|
|
92
|
+
// ============================================================================
|
|
93
|
+
var handleList = function (ctx) { return __awaiter(void 0, void 0, void 0, function () {
|
|
94
|
+
var result, tools;
|
|
95
|
+
var _a;
|
|
96
|
+
return __generator(this, function (_b) {
|
|
97
|
+
switch (_b.label) {
|
|
98
|
+
case 0: return [4 /*yield*/, transport_js_1.sendJsonRpc(ctx.category, "tools/list", undefined, {
|
|
99
|
+
requesterUserId: ctx.requesterUserId,
|
|
100
|
+
accountId: ctx.accountId
|
|
101
|
+
})];
|
|
102
|
+
case 1:
|
|
103
|
+
result = _b.sent();
|
|
104
|
+
tools = (_a = result === null || result === void 0 ? void 0 : result.tools) !== null && _a !== void 0 ? _a : [];
|
|
105
|
+
if (tools.length === 0) {
|
|
106
|
+
return [2 /*return*/, { message: "\u54C1\u7C7B \"" + ctx.category + "\" \u4E0B\u6682\u65E0\u53EF\u7528\u5DE5\u5177", tools: [] }];
|
|
107
|
+
}
|
|
108
|
+
return [2 /*return*/, {
|
|
109
|
+
category: ctx.category,
|
|
110
|
+
count: tools.length,
|
|
111
|
+
tools: tools.map(function (t) {
|
|
112
|
+
var _a;
|
|
113
|
+
return ({
|
|
114
|
+
name: t.name,
|
|
115
|
+
description: (_a = t.description) !== null && _a !== void 0 ? _a : "",
|
|
116
|
+
// 清洗 inputSchema,内联 $ref/$defs 引用并移除 Gemini 不支持的关键词,
|
|
117
|
+
// 避免 Gemini 模型解析 function response 时报 400 错误
|
|
118
|
+
inputSchema: t.inputSchema ? schema_js_1.cleanSchemaForGemini(t.inputSchema) : undefined
|
|
119
|
+
});
|
|
120
|
+
})
|
|
121
|
+
}];
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}); };
|
|
125
|
+
// ============================================================================
|
|
126
|
+
// call 操作:调用某品类的某个 MCP 工具
|
|
127
|
+
// ============================================================================
|
|
128
|
+
var handleCall = function (ctx) { return __awaiter(void 0, void 0, void 0, function () {
|
|
129
|
+
var category, method, args, requesterUserId, accountId, callStart, _a, options, resolvedArgs, finalArgs, requestOptions, result, rpcDone, rpcMs, resultStr, finalResult, totalMs, interceptMs, finalStr;
|
|
130
|
+
return __generator(this, function (_b) {
|
|
131
|
+
switch (_b.label) {
|
|
132
|
+
case 0:
|
|
133
|
+
category = ctx.category, method = ctx.method, args = ctx.args, requesterUserId = ctx.requesterUserId, accountId = ctx.accountId;
|
|
134
|
+
callStart = performance.now();
|
|
135
|
+
console.log("[mcp] handleCall " + category + "/" + method + " \u5165\u53C2: " + JSON.stringify(args));
|
|
136
|
+
return [4 /*yield*/, index_js_1.resolveBeforeCall(ctx)];
|
|
137
|
+
case 1:
|
|
138
|
+
_a = _b.sent(), options = _a.options, resolvedArgs = _a.args;
|
|
139
|
+
finalArgs = resolvedArgs !== null && resolvedArgs !== void 0 ? resolvedArgs : args;
|
|
140
|
+
requestOptions = __assign(__assign(__assign({}, options), (requesterUserId ? { requesterUserId: requesterUserId } : {})), (accountId ? { accountId: accountId } : {}));
|
|
141
|
+
if (resolvedArgs) {
|
|
142
|
+
console.log("[mcp] handleCall " + category + "/" + method + " \u62E6\u622A\u5668\u66FF\u6362 args: " + JSON.stringify(resolvedArgs).slice(0, 500) +
|
|
143
|
+
(JSON.stringify(resolvedArgs).length > 500 ? "...(truncated)" : ""));
|
|
144
|
+
}
|
|
145
|
+
if (options) {
|
|
146
|
+
console.log("[mcp] handleCall " + category + "/" + method + " \u62E6\u622A\u5668\u9009\u9879: " + JSON.stringify(options));
|
|
147
|
+
}
|
|
148
|
+
return [4 /*yield*/, transport_js_1.sendJsonRpc(category, "tools/call", {
|
|
149
|
+
name: method,
|
|
150
|
+
arguments: finalArgs
|
|
151
|
+
}, requestOptions)];
|
|
152
|
+
case 2:
|
|
153
|
+
result = _b.sent();
|
|
154
|
+
rpcDone = performance.now();
|
|
155
|
+
rpcMs = (rpcDone - callStart).toFixed(1);
|
|
156
|
+
resultStr = JSON.stringify(result);
|
|
157
|
+
console.log("[mcp] handleCall " + category + "/" + method + " MCP \u54CD\u5E94 (" + rpcMs + "ms): " + resultStr.slice(0, 800) +
|
|
158
|
+
(resultStr.length > 800 ? "...(truncated)" : ""));
|
|
159
|
+
return [4 /*yield*/, index_js_1.runAfterCall(ctx, result)];
|
|
160
|
+
case 3:
|
|
161
|
+
finalResult = _b.sent();
|
|
162
|
+
totalMs = (performance.now() - callStart).toFixed(1);
|
|
163
|
+
interceptMs = (performance.now() - rpcDone).toFixed(1);
|
|
164
|
+
// 有拦截器处理时打印详细耗时,否则只打印 RPC 耗时
|
|
165
|
+
if (finalResult !== result) {
|
|
166
|
+
finalStr = JSON.stringify(finalResult);
|
|
167
|
+
console.log("[mcp] handleCall " + category + "/" + method + " afterCall \u53D8\u6362\u540E (" + interceptMs + "ms): " + finalStr.slice(0, 500) +
|
|
168
|
+
(finalStr.length > 500 ? "...(truncated)" : ""));
|
|
169
|
+
console.log("[mcp] handleCall " + category + "/" + method + " \u603B\u8017\u65F6: " + totalMs + "ms" +
|
|
170
|
+
(" (MCP\u8BF7\u6C42: " + rpcMs + "ms, \u62E6\u622A\u5904\u7406: " + interceptMs + "ms)"));
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
console.log("[mcp] handleCall " + category + "/" + method + " \u8017\u65F6: " + rpcMs + "ms");
|
|
174
|
+
}
|
|
175
|
+
return [2 /*return*/, finalResult];
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}); };
|
|
179
|
+
// ============================================================================
|
|
180
|
+
// 参数解析
|
|
181
|
+
// ============================================================================
|
|
182
|
+
/**
|
|
183
|
+
* 解析 args 参数:支持 JSON 字符串或直接的对象
|
|
184
|
+
*/
|
|
185
|
+
var parseArgs = function (args) {
|
|
186
|
+
if (!args)
|
|
187
|
+
return {};
|
|
188
|
+
if (typeof args === "object")
|
|
189
|
+
return args;
|
|
190
|
+
try {
|
|
191
|
+
return JSON.parse(args);
|
|
192
|
+
}
|
|
193
|
+
catch (err) {
|
|
194
|
+
var detail = err instanceof SyntaxError ? err.message : String(err);
|
|
195
|
+
throw new Error("args \u53C2\u6570\u4E0D\u662F\u5408\u6CD5\u7684 JSON: " + args + " (" + detail + ")");
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
// ============================================================================
|
|
199
|
+
// 工具定义 & 导出
|
|
200
|
+
// ============================================================================
|
|
201
|
+
/**
|
|
202
|
+
* 创建 wecom_mcp Agent Tool 定义
|
|
203
|
+
*/
|
|
204
|
+
function createWeComMcpTool(options) {
|
|
205
|
+
if (options === void 0) { options = {}; }
|
|
206
|
+
var requesterUserId = normalizeOptionalString(options.requesterUserId);
|
|
207
|
+
var accountId = normalizeOptionalString(options.accountId);
|
|
208
|
+
var chatId = normalizeOptionalString(options.chatId);
|
|
209
|
+
var chatType = options.chatType;
|
|
210
|
+
return {
|
|
211
|
+
name: "wecom_mcp",
|
|
212
|
+
label: "企业微信 MCP 工具",
|
|
213
|
+
description: [
|
|
214
|
+
"通过 HTTP 直接调用企业微信 MCP Server。",
|
|
215
|
+
"支持两种操作:",
|
|
216
|
+
" - list: 列出指定品类的所有 MCP 工具",
|
|
217
|
+
" - call: 调用指定品类的某个 MCP 工具",
|
|
218
|
+
"",
|
|
219
|
+
"使用方式:",
|
|
220
|
+
" wecom_mcp list <category>",
|
|
221
|
+
" wecom_mcp call <category> <method> '<jsonArgs>'",
|
|
222
|
+
"",
|
|
223
|
+
"示例:",
|
|
224
|
+
" 列出 contact 品类所有工具:wecom_mcp list contact",
|
|
225
|
+
" 调用 contact 的 getContact:wecom_mcp call contact getContact '{}'",
|
|
226
|
+
].join("\n"),
|
|
227
|
+
parameters: {
|
|
228
|
+
type: "object",
|
|
229
|
+
properties: {
|
|
230
|
+
action: {
|
|
231
|
+
type: "string",
|
|
232
|
+
"enum": ["list", "call"],
|
|
233
|
+
description: "操作类型:list(列出工具)或 call(调用工具)"
|
|
234
|
+
},
|
|
235
|
+
category: {
|
|
236
|
+
type: "string",
|
|
237
|
+
description: "MCP 品类名称,如 doc、contact 等,对应 mcpConfig 中的 key"
|
|
238
|
+
},
|
|
239
|
+
method: {
|
|
240
|
+
type: "string",
|
|
241
|
+
description: "要调用的 MCP 方法名(action=call 时必填)"
|
|
242
|
+
},
|
|
243
|
+
args: {
|
|
244
|
+
type: ["string", "object"],
|
|
245
|
+
description: "调用 MCP 方法的参数,可以是 JSON 字符串或对象(action=call 时使用,默认 {})"
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
required: ["action", "category"]
|
|
249
|
+
},
|
|
250
|
+
execute: function (_toolCallId, params) {
|
|
251
|
+
var _a;
|
|
252
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
253
|
+
var p, result, ctx, _b, _c, _d, err_1;
|
|
254
|
+
return __generator(this, function (_e) {
|
|
255
|
+
switch (_e.label) {
|
|
256
|
+
case 0:
|
|
257
|
+
p = params;
|
|
258
|
+
console.log("[mcp] execute: action=" + p.action + ", category=" + p.category +
|
|
259
|
+
(p.method ? ", method=" + p.method : "") +
|
|
260
|
+
(p.args ? ", args=" + (typeof p.args === "string" ? p.args : JSON.stringify(p.args)) : ""));
|
|
261
|
+
_e.label = 1;
|
|
262
|
+
case 1:
|
|
263
|
+
_e.trys.push([1, 8, , 9]);
|
|
264
|
+
result = void 0;
|
|
265
|
+
ctx = {
|
|
266
|
+
category: p.category,
|
|
267
|
+
method: (_a = p.method) !== null && _a !== void 0 ? _a : "",
|
|
268
|
+
args: {},
|
|
269
|
+
requesterUserId: requesterUserId,
|
|
270
|
+
accountId: accountId,
|
|
271
|
+
chatId: chatId,
|
|
272
|
+
chatType: chatType
|
|
273
|
+
};
|
|
274
|
+
_b = p.action;
|
|
275
|
+
switch (_b) {
|
|
276
|
+
case "list": return [3 /*break*/, 2];
|
|
277
|
+
case "call": return [3 /*break*/, 4];
|
|
278
|
+
}
|
|
279
|
+
return [3 /*break*/, 6];
|
|
280
|
+
case 2:
|
|
281
|
+
_c = textResult;
|
|
282
|
+
return [4 /*yield*/, handleList(ctx)];
|
|
283
|
+
case 3:
|
|
284
|
+
result = _c.apply(void 0, [_e.sent()]);
|
|
285
|
+
return [3 /*break*/, 7];
|
|
286
|
+
case 4:
|
|
287
|
+
if (!p.method) {
|
|
288
|
+
result = textResult({ error: "action 为 call 时必须提供 method 参数" });
|
|
289
|
+
return [3 /*break*/, 7];
|
|
290
|
+
}
|
|
291
|
+
ctx.args = parseArgs(p.args);
|
|
292
|
+
_d = textResult;
|
|
293
|
+
return [4 /*yield*/, handleCall(ctx)];
|
|
294
|
+
case 5:
|
|
295
|
+
result = _d.apply(void 0, [_e.sent()]);
|
|
296
|
+
return [3 /*break*/, 7];
|
|
297
|
+
case 6:
|
|
298
|
+
result = textResult({ error: "\u672A\u77E5\u64CD\u4F5C\u7C7B\u578B: " + String(p.action) + "\uFF0C\u652F\u6301 list \u548C call" });
|
|
299
|
+
_e.label = 7;
|
|
300
|
+
case 7:
|
|
301
|
+
console.log("[mcp] execute: action=" + p.action + ", category=" + p.category +
|
|
302
|
+
(p.method ? ", method=" + p.method : "") +
|
|
303
|
+
(" \u2192 \u54CD\u5E94\u957F\u5EA6=" + result.content[0].text.length + " chars"));
|
|
304
|
+
return [2 /*return*/, result];
|
|
305
|
+
case 8:
|
|
306
|
+
err_1 = _e.sent();
|
|
307
|
+
console.error("[mcp] execute: action=" + p.action + ", category=" + p.category +
|
|
308
|
+
(p.method ? ", method=" + p.method : "") +
|
|
309
|
+
(" \u2192 \u5F02\u5E38: " + (err_1 instanceof Error ? err_1.message : JSON.stringify(err_1))));
|
|
310
|
+
return [2 /*return*/, errorResult(err_1)];
|
|
311
|
+
case 9: return [2 /*return*/];
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
exports.createWeComMcpTool = createWeComMcpTool;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Streamable HTTP 传输层模块
|
|
3
|
+
*
|
|
4
|
+
* 负责:
|
|
5
|
+
* - MCP JSON-RPC over HTTP 通信(发送请求、解析响应)
|
|
6
|
+
* - Streamable HTTP session 生命周期管理(initialize 握手 → Mcp-Session-Id 维护 → 失效重建)
|
|
7
|
+
* - 自动检测无状态 Server:如果 initialize 响应未返回 Mcp-Session-Id,
|
|
8
|
+
* 则标记为无状态模式,后续请求跳过握手和 session 管理
|
|
9
|
+
* - SSE 流式响应解析
|
|
10
|
+
* - MCP 配置运行时缓存(通过 WSClient 拉取 URL 并缓存在内存中)
|
|
11
|
+
*/
|
|
12
|
+
/** 媒体下载请求超时时间(毫秒),base64 编码的媒体文件最大可达 ~27MB */
|
|
13
|
+
export declare const MEDIA_DOWNLOAD_TIMEOUT_MS = 120000;
|
|
14
|
+
/** 请求 MCP Server 时透传可信企业微信 userid 的 header 名 */
|
|
15
|
+
export declare const WECOM_USERID_HEADER = "x-openclaw-wecom-userid";
|
|
16
|
+
/**
|
|
17
|
+
* MCP JSON-RPC 错误
|
|
18
|
+
*
|
|
19
|
+
* 携带服务端返回的 JSON-RPC error.code,
|
|
20
|
+
* 用于上层按错误码进行差异化处理(如特定错误码触发缓存清理)。
|
|
21
|
+
*/
|
|
22
|
+
export declare class McpRpcError extends Error {
|
|
23
|
+
readonly code: number;
|
|
24
|
+
readonly data?: unknown;
|
|
25
|
+
constructor(code: number, message: string, data?: unknown);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* MCP HTTP 错误
|
|
29
|
+
*
|
|
30
|
+
* 携带 HTTP 状态码,用于精确判断 session 失效(404)等场景,
|
|
31
|
+
* 避免通过字符串匹配 "404" 导致的误判。
|
|
32
|
+
*/
|
|
33
|
+
export declare class McpHttpError extends Error {
|
|
34
|
+
readonly statusCode: number;
|
|
35
|
+
constructor(statusCode: number, message: string);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 根据配置解析当前用于 MCP 的账户 ID
|
|
39
|
+
*
|
|
40
|
+
* 通过 PluginRuntime 读取全局配置,按以下优先级选择账户:
|
|
41
|
+
* 1. 默认账户(如果配置了 botId + secret)
|
|
42
|
+
* 2. 第一个配置了 botId + secret 的长连接账户
|
|
43
|
+
* 3. 无可用长连接账户时回退到默认账户(会导致后续 WSClient 获取失败)
|
|
44
|
+
*/
|
|
45
|
+
export declare function resolveCurrentAccountId(): string;
|
|
46
|
+
/**
|
|
47
|
+
* 清理指定账户+品类的所有 MCP 缓存(配置、会话、无状态标记)
|
|
48
|
+
*
|
|
49
|
+
* 当 MCP Server 返回特定错误码时调用,确保下次请求重新拉取配置并重建会话。
|
|
50
|
+
*
|
|
51
|
+
* @param accountId - 账户 ID
|
|
52
|
+
* @param category - MCP 品类名称
|
|
53
|
+
*/
|
|
54
|
+
export declare function clearCategoryCache(accountId: string, category: string): void;
|
|
55
|
+
/**
|
|
56
|
+
* 清理指定账户下所有品类的 MCP 缓存
|
|
57
|
+
*
|
|
58
|
+
* 适用于账户掉线、凭据变更等需要整体清理的场景。
|
|
59
|
+
*
|
|
60
|
+
* @param accountId - 账户 ID
|
|
61
|
+
*/
|
|
62
|
+
export declare function clearAccountCache(accountId: string): void;
|
|
63
|
+
/** tools/list 返回的工具描述 */
|
|
64
|
+
export interface McpToolInfo {
|
|
65
|
+
name: string;
|
|
66
|
+
description?: string;
|
|
67
|
+
inputSchema?: Record<string, unknown>;
|
|
68
|
+
}
|
|
69
|
+
/** sendJsonRpc 的可选配置 */
|
|
70
|
+
export interface SendJsonRpcOptions {
|
|
71
|
+
/** 自定义 HTTP 请求超时时间(毫秒),默认使用 HTTP_REQUEST_TIMEOUT_MS */
|
|
72
|
+
timeoutMs?: number;
|
|
73
|
+
/** 当前会话可信的企业微信 userid,有值时会注入自定义 header */
|
|
74
|
+
requesterUserId?: string;
|
|
75
|
+
/** 指定使用的账户 ID,有值时优先使用,否则 fallback 到 resolveCurrentAccountId() */
|
|
76
|
+
accountId?: string;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 发送 JSON-RPC 请求到 MCP Server(Streamable HTTP 协议)
|
|
80
|
+
*
|
|
81
|
+
* 自动管理 session 生命周期:
|
|
82
|
+
* - 无状态 Server:跳过 session 管理,直接发送请求
|
|
83
|
+
* - 有状态 Server:首次调用先执行 initialize 握手,session 失效(404)时自动重建并重试
|
|
84
|
+
*
|
|
85
|
+
* accountId 优先使用 options.accountId(由工具工厂从 ctx.agentAccountId 传入),
|
|
86
|
+
* 未提供时 fallback 到 resolveCurrentAccountId()。解析结果贯穿整条调用链,避免跨账号污染。
|
|
87
|
+
*
|
|
88
|
+
* @param category - MCP 品类名称
|
|
89
|
+
* @param method - JSON-RPC 方法名
|
|
90
|
+
* @param params - JSON-RPC 参数
|
|
91
|
+
* @param options - 可选配置(如自定义超时、指定账户)
|
|
92
|
+
* @returns JSON-RPC result
|
|
93
|
+
*/
|
|
94
|
+
export declare function sendJsonRpc(category: string, method: string, params?: Record<string, unknown>, options?: SendJsonRpcOptions): Promise<unknown>;
|