@chaoyue-erp/ysy-openapi-mcp 1.0.1 → 1.0.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/README.md +28 -23
- package/config.example.json +1 -1
- package/package.json +1 -1
- package/skills/ysy-openapi/SKILL.md +8 -7
- package/src/api-client.mjs +1 -0
- package/src/config.mjs +22 -13
- package/src/data-map.json +17680 -114
- package/src/server.mjs +109 -15
package/src/server.mjs
CHANGED
|
@@ -26,6 +26,44 @@ const wrap = async (fn) => {
|
|
|
26
26
|
}
|
|
27
27
|
};
|
|
28
28
|
|
|
29
|
+
// 分页按 Unicode 字符计数,避免切断汉字或 emoji;每页始终返回续读位置。
|
|
30
|
+
function pageDocument(value, offset = 0, limit = 3000) {
|
|
31
|
+
const format = typeof value === "string" ? "text" : "json";
|
|
32
|
+
const content = format === "text" ? value : JSON.stringify(value, null, 2);
|
|
33
|
+
const chars = Array.from(content ?? "");
|
|
34
|
+
const start = Math.min(offset, chars.length);
|
|
35
|
+
const next = Math.min(start + limit, chars.length);
|
|
36
|
+
return {
|
|
37
|
+
format,
|
|
38
|
+
text: chars.slice(start, next).join(""),
|
|
39
|
+
offset: start,
|
|
40
|
+
returnedChars: next - start,
|
|
41
|
+
totalChars: chars.length,
|
|
42
|
+
hasMore: next < chars.length,
|
|
43
|
+
nextOffset: next < chars.length ? next : null,
|
|
44
|
+
hint: format === "json" && chars.length > limit ? "JSON 按字符分页;需要完整 JSON 时请按 nextOffset 拼接所有 text。" : undefined,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const DOC_PAGE_SCHEMA = {
|
|
49
|
+
offset: z.number().int().min(0).optional().describe("字符偏移,从 0 开始;续读时传上次返回的 nextOffset"),
|
|
50
|
+
limit: z.number().int().min(1).max(4000).optional().describe("每页最多 4000 个 Unicode 字符,默认 3000"),
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// 保留旧字段,限制摘要大小;被省略的内容可通过 read_api_document 完整续读。
|
|
54
|
+
function briefArray(value, maxItems = 40, maxChars = 5000) {
|
|
55
|
+
const all = Array.isArray(value) ? value : [];
|
|
56
|
+
const items = [];
|
|
57
|
+
let used = 0;
|
|
58
|
+
for (const item of all) {
|
|
59
|
+
const size = JSON.stringify(item).length;
|
|
60
|
+
if (items.length >= maxItems || used + size > maxChars) break;
|
|
61
|
+
items.push(item);
|
|
62
|
+
used += size;
|
|
63
|
+
}
|
|
64
|
+
return { items, total: all.length, returned: items.length, hasMore: items.length < all.length };
|
|
65
|
+
}
|
|
66
|
+
|
|
29
67
|
function resolveApi(args) {
|
|
30
68
|
const code = args.apiCode || args.actionCode;
|
|
31
69
|
if (!code) throw new Error("请提供 apiCode(文档中的字段名为 apiCode,不是 actionCode;actionCode 仅作为兼容别名)。");
|
|
@@ -38,12 +76,13 @@ function resolveApi(args) {
|
|
|
38
76
|
}
|
|
39
77
|
|
|
40
78
|
function createServer() {
|
|
41
|
-
const
|
|
79
|
+
const { version } = JSON.parse(fs.readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
80
|
+
const server = new McpServer({ name: "ysy-openapi", version });
|
|
42
81
|
|
|
43
82
|
server.registerTool(
|
|
44
83
|
"list_modules",
|
|
45
84
|
{
|
|
46
|
-
description: "
|
|
85
|
+
description: "列出开放接口的业务模块与密钥配置状态。配置状态不代表模块或接口已获授权,实际权限由服务端校验。",
|
|
47
86
|
inputSchema: {},
|
|
48
87
|
},
|
|
49
88
|
() =>
|
|
@@ -59,7 +98,7 @@ function createServer() {
|
|
|
59
98
|
credentialsConfigured: ready.has(code),
|
|
60
99
|
})),
|
|
61
100
|
notConfigured: missingModules(cfg),
|
|
62
|
-
hint: "
|
|
101
|
+
hint: "密钥已配置只表示客户端已填写凭据;各模块及接口能否调用,以服务端授权为准。",
|
|
63
102
|
};
|
|
64
103
|
}),
|
|
65
104
|
);
|
|
@@ -81,7 +120,7 @@ function createServer() {
|
|
|
81
120
|
if (args.kind) list = list.filter((a) => a.kind === args.kind);
|
|
82
121
|
if (args.keyword) {
|
|
83
122
|
const k = args.keyword.toLowerCase();
|
|
84
|
-
list = list.filter((a) => a.apiCode.toLowerCase().includes(k) || a.name.toLowerCase().includes(k) || (a.summary || "").toLowerCase().includes(k));
|
|
123
|
+
list = list.filter((a) => a.apiCode.toLowerCase().includes(k) || a.name.toLowerCase().includes(k) || (a.summary || "").toLowerCase().includes(k) || (a.searchText || "").toLowerCase().includes(k));
|
|
85
124
|
}
|
|
86
125
|
return {
|
|
87
126
|
total: list.length,
|
|
@@ -94,9 +133,9 @@ function createServer() {
|
|
|
94
133
|
server.registerTool(
|
|
95
134
|
"describe_api",
|
|
96
135
|
{
|
|
97
|
-
description: "
|
|
136
|
+
description: "查看接口摘要与参数。完整请求、响应、公共说明、附表和所有示例请用 read_api_document 分节分页读取。",
|
|
98
137
|
inputSchema: {
|
|
99
|
-
apiCode: z.string().describe("完整接口 code,区分大小写,如 sale.list"),
|
|
138
|
+
apiCode: z.string().optional().describe("完整接口 code,区分大小写,如 sale.list"),
|
|
100
139
|
actionCode: z.string().optional().describe("apiCode 的兼容别名,二选一"),
|
|
101
140
|
},
|
|
102
141
|
},
|
|
@@ -104,6 +143,9 @@ function createServer() {
|
|
|
104
143
|
wrap(() => {
|
|
105
144
|
const a = resolveApi(args);
|
|
106
145
|
const rules = DATA_MAP.commonRules;
|
|
146
|
+
const fieldNames = ["params", "formFields", "executeFields", "searchKeys", "responseFields"];
|
|
147
|
+
const brief = Object.fromEntries(fieldNames.map((name) => [name, briefArray(a[name])]));
|
|
148
|
+
const exampleOmitted = JSON.stringify(a.requestExample ?? null).length > 5000;
|
|
107
149
|
const out = {
|
|
108
150
|
apiCode: a.apiCode,
|
|
109
151
|
name: a.name,
|
|
@@ -112,12 +154,12 @@ function createServer() {
|
|
|
112
154
|
entry: a.entry,
|
|
113
155
|
resultPath: a.resultPath,
|
|
114
156
|
summary: a.summary,
|
|
115
|
-
params:
|
|
116
|
-
formFields:
|
|
117
|
-
executeFields:
|
|
118
|
-
searchKeys:
|
|
119
|
-
requestExample: a.requestExample,
|
|
120
|
-
responseFields:
|
|
157
|
+
params: brief.params.items,
|
|
158
|
+
formFields: brief.formFields.items,
|
|
159
|
+
executeFields: brief.executeFields.items,
|
|
160
|
+
searchKeys: brief.searchKeys.items,
|
|
161
|
+
requestExample: exampleOmitted ? null : a.requestExample,
|
|
162
|
+
responseFields: brief.responseFields.items,
|
|
121
163
|
responseNotes: a.responseNotes,
|
|
122
164
|
rules: {
|
|
123
165
|
success: rules.success,
|
|
@@ -127,6 +169,11 @@ function createServer() {
|
|
|
127
169
|
detailId: rules.detailId,
|
|
128
170
|
sort: rules.sort,
|
|
129
171
|
},
|
|
172
|
+
documentAccess: {
|
|
173
|
+
fields: Object.fromEntries(fieldNames.map((name) => [name, { total: brief[name].total, returned: brief[name].returned, hasMore: brief[name].hasMore }])),
|
|
174
|
+
requestExampleOmitted: exampleOmitted,
|
|
175
|
+
hint: "摘要和部分字段可能省略;调用 read_api_document 按 section 与 nextOffset 查看完整内容。",
|
|
176
|
+
},
|
|
130
177
|
};
|
|
131
178
|
if (a.kind !== "query") {
|
|
132
179
|
out.warning = `此 code 属于 ${a.entry} 入口(写入类),不能用 query 工具调用;且写入工具默认禁用。`;
|
|
@@ -137,10 +184,56 @@ function createServer() {
|
|
|
137
184
|
}),
|
|
138
185
|
);
|
|
139
186
|
|
|
187
|
+
server.registerTool(
|
|
188
|
+
"read_api_document",
|
|
189
|
+
{
|
|
190
|
+
description: "按章节和字符偏移完整读取指定接口文档。大章节按 nextOffset 续读;所有示例、附表、请求、响应和公共说明均可读取。",
|
|
191
|
+
inputSchema: {
|
|
192
|
+
apiCode: z.string().describe("完整接口 code,例如 sale.list"),
|
|
193
|
+
section: z.enum(["requestText", "responseText", "commonText", "searchText", "params", "searchKeys", "formFields", "executeFields", "responseFields", "extraTables", "sourceTables", "requestExamples", "responseExamples", "commonExamples"]).describe("接口文档章节名称"),
|
|
194
|
+
...DOC_PAGE_SCHEMA,
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
(args) => wrap(() => {
|
|
198
|
+
const a = resolveApi(args);
|
|
199
|
+
const value = a[args.section];
|
|
200
|
+
if (value === undefined) throw new Error(`该接口尚未包含 ${args.section} 章节,请先用新版生成脚本更新数据地图。`);
|
|
201
|
+
return { apiCode: a.apiCode, section: args.section, ...pageDocument(value, args.offset, args.limit) };
|
|
202
|
+
}),
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
server.registerTool(
|
|
206
|
+
"read_reference_document",
|
|
207
|
+
{
|
|
208
|
+
description: "列出或按章节分页读取公共指南与公共规则。category=guides 时不传 guideId 列出指南;传 guideId 和 section 读取内容;category=rules 按 rule 读取公共规则。",
|
|
209
|
+
inputSchema: {
|
|
210
|
+
category: z.enum(["guides", "rules"]).describe("guides=文档公共指南;rules=连接器公共规则"),
|
|
211
|
+
guideId: z.string().optional().describe("指南 id,先不传以查看目录"),
|
|
212
|
+
section: z.enum(["text", "tables", "examples"]).optional().describe("指南章节;默认 text"),
|
|
213
|
+
rule: z.string().optional().describe("公共规则键;不传时列出可用键"),
|
|
214
|
+
...DOC_PAGE_SCHEMA,
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
(args) => wrap(() => {
|
|
218
|
+
if (args.category === "rules") {
|
|
219
|
+
const rules = DATA_MAP.commonRules || {};
|
|
220
|
+
if (!args.rule) return { category: "rules", keys: Object.keys(rules) };
|
|
221
|
+
if (!Object.hasOwn(rules, args.rule)) throw new Error(`未找到公共规则 ${args.rule}`);
|
|
222
|
+
return { category: "rules", rule: args.rule, ...pageDocument(rules[args.rule], args.offset, args.limit) };
|
|
223
|
+
}
|
|
224
|
+
const guides = DATA_MAP.guides || [];
|
|
225
|
+
if (!args.guideId) return { category: "guides", total: guides.length, ...pageDocument(guides.map(({ id, title }) => ({ id, title })), args.offset, args.limit) };
|
|
226
|
+
const guide = guides.find((g) => g.id === args.guideId);
|
|
227
|
+
if (!guide) throw new Error(`未找到公共指南 ${args.guideId}`);
|
|
228
|
+
const section = args.section || "text";
|
|
229
|
+
return { category: "guides", guideId: guide.id, title: guide.title, section, ...pageDocument(guide[section] ?? (section === "text" ? "" : []), args.offset, args.limit) };
|
|
230
|
+
}),
|
|
231
|
+
);
|
|
232
|
+
|
|
140
233
|
server.registerTool(
|
|
141
234
|
"query",
|
|
142
235
|
{
|
|
143
|
-
description: "
|
|
236
|
+
description: "查询经营数据(POST /openapi/v1/query,策略网关)。请求外壳为 {moduleCode, apiCode, params}。自动取 Token、缓存并在 401 时刷新;自动把 YYYY-MM-DD 日期转为毫秒时间戳;自动按数据地图校验 code 并截断超大结果。先用 list_apis 找 apiCode,再用 describe_api 查参数。",
|
|
144
237
|
inputSchema: {
|
|
145
238
|
apiCode: z.string().optional().describe("完整接口 code,区分大小写,如 sale.list、goods.list"),
|
|
146
239
|
actionCode: z.string().optional().describe("apiCode 的兼容别名,与 apiCode 二选一"),
|
|
@@ -194,7 +287,7 @@ function createServer() {
|
|
|
194
287
|
server.registerTool(
|
|
195
288
|
"check_connection",
|
|
196
289
|
{
|
|
197
|
-
description: "
|
|
290
|
+
description: "自检配置和 Token 获取情况,不返回 Token 明文。获取成功并不证明任一模块或接口已获授权;接口权限以实际调用和服务端授权为准。",
|
|
198
291
|
inputSchema: {
|
|
199
292
|
moduleCode: z.string().optional().describe("只检查某个模块;省略则检查全部已配置模块"),
|
|
200
293
|
},
|
|
@@ -219,7 +312,7 @@ function createServer() {
|
|
|
219
312
|
try {
|
|
220
313
|
const before = Date.now();
|
|
221
314
|
await tokens.get(m, { force: true });
|
|
222
|
-
results.push({ moduleCode: m, ok: true, elapsedMs: Date.now() - before, message: "Token
|
|
315
|
+
results.push({ moduleCode: m, ok: true, elapsedMs: Date.now() - before, message: "Token 获取成功;这不代表该模块或接口已获授权,请以实际接口调用和服务端授权为准。" });
|
|
223
316
|
} catch (err) {
|
|
224
317
|
results.push({ moduleCode: m, ok: false, message: err.message });
|
|
225
318
|
}
|
|
@@ -231,6 +324,7 @@ function createServer() {
|
|
|
231
324
|
queryUrl: cfg.queryUrl,
|
|
232
325
|
allowWrites: cfg.allowWrites,
|
|
233
326
|
results,
|
|
327
|
+
authorizationNote: "此检查仅验证能否获取 Token;成功不代表每个模块或接口都已获授权。",
|
|
234
328
|
notConfigured: missingModules(cfg),
|
|
235
329
|
};
|
|
236
330
|
}),
|