@momeidejiyi/dsh-general-mcp 0.1.9 → 0.2.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/lib/index.js +54 -24
- package/package.json +1 -1
- package/src/catalog.ts +62 -23
- package/src/register.ts +2 -3
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// @momeidejiyi/dsh-general-mcp@0.1
|
|
1
|
+
// @momeidejiyi/dsh-general-mcp@0.2.1 (generated)
|
|
2
2
|
|
|
3
3
|
// packages/general-mcp/src/index.ts
|
|
4
4
|
import { appendFileSync } from "node:fs";
|
|
@@ -23,29 +23,53 @@ function validateTool(tool) {
|
|
|
23
23
|
if (typeof tool.name !== "string" || !NAME_PATTERN.test(tool.name)) {
|
|
24
24
|
return { code: "name", message: "name \u5FC5\u987B\u5339\u914D " + NAME_PATTERN.source };
|
|
25
25
|
}
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
return void 0;
|
|
27
|
+
}
|
|
28
|
+
function inputSchemaOf(tool) {
|
|
29
|
+
let value = tool.inputSchema;
|
|
30
|
+
if (typeof value === "string") {
|
|
31
|
+
try {
|
|
32
|
+
value = JSON.parse(value);
|
|
33
|
+
} catch {
|
|
34
|
+
value = void 0;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (value === void 0 || value === null) {
|
|
38
|
+
return { schema: { type: "object", properties: {} }, warning: { code: "input-schema", message: "\u7F3A\u5C11 inputSchema\uFF0C\u6309\u65E0\u53C2\u6570\u5DE5\u5177\u5904\u7406" } };
|
|
28
39
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return { code: "input-schema", message: 'inputSchema \u5FC5\u987B\u662F type:"object" \u7684 JSON Schema' };
|
|
40
|
+
if (typeof value !== "object" || Array.isArray(value)) {
|
|
41
|
+
return { schema: { type: "object", properties: {} }, warning: { code: "input-schema", message: "inputSchema \u4E0D\u662F\u5BF9\u8C61\uFF0C\u6309\u65E0\u53C2\u6570\u5DE5\u5177\u5904\u7406" } };
|
|
32
42
|
}
|
|
43
|
+
const type = value.type;
|
|
44
|
+
if (type !== void 0 && type !== "object") {
|
|
45
|
+
return { schema: { type: "object", properties: {} }, warning: { code: "input-schema", message: "inputSchema.type \u4E0D\u662F object\uFF0C\u6309\u65E0\u53C2\u6570\u5DE5\u5177\u5904\u7406" } };
|
|
46
|
+
}
|
|
47
|
+
return { schema: { type: "object", properties: {}, ...value } };
|
|
48
|
+
}
|
|
49
|
+
function toolWarnings(tool) {
|
|
50
|
+
const out = [];
|
|
51
|
+
if (typeof tool.description !== "string" || tool.description.trim() === "") {
|
|
52
|
+
out.push({ code: "description", message: "\u7F3A\u5C11 description\uFF0C\u7528\u5DE5\u5177\u540D\u4EE3\u66FF" });
|
|
53
|
+
}
|
|
54
|
+
const schema = inputSchemaOf(tool).warning;
|
|
55
|
+
if (schema !== void 0) out.push(schema);
|
|
33
56
|
const meta = tool._meta ?? {};
|
|
34
57
|
const folder = meta[CANONICAL_FOLDER];
|
|
35
58
|
if (folder !== void 0) {
|
|
36
59
|
if (!isStringArray(folder) || folder.length === 0 || folder.length > MAX_FOLDER_DEPTH) {
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return { code: "folder", message: '_meta["dsh/folder"] \u6BB5 "' + bad + '" \u975E\u6CD5\uFF08\u4E0D\u80FD\u542B / \u6216 .\uFF0C\u957F\u5EA6 1..64\uFF09' };
|
|
60
|
+
out.push({ code: "folder", message: '_meta["dsh/folder"] \u5FC5\u987B\u662F 1..8 \u4E2A\u5B57\u7B26\u4E32\u7684\u6570\u7EC4\uFF0C\u5DF2\u5FFD\u7565' });
|
|
61
|
+
} else {
|
|
62
|
+
const bad = folder.find((segment) => !SEGMENT_PATTERN.test(segment));
|
|
63
|
+
if (bad !== void 0) out.push({ code: "folder", message: '_meta["dsh/folder"] \u6BB5 "' + bad + '" \u975E\u6CD5\uFF0C\u5DF2\u5FFD\u7565' });
|
|
42
64
|
}
|
|
43
65
|
}
|
|
44
66
|
const title = meta[CANONICAL_TITLE];
|
|
45
67
|
if (title !== void 0 && (typeof title !== "string" || title.length > MAX_TITLE)) {
|
|
46
|
-
|
|
68
|
+
out.push({ code: "title", message: '_meta["dsh/title"] \u5FC5\u987B\u662F\u4E0D\u8D85\u8FC7 ' + MAX_TITLE + " \u5B57\u7684\u5B57\u7B26\u4E32\uFF0C\u5DF2\u622A\u65AD" });
|
|
47
69
|
}
|
|
48
|
-
|
|
70
|
+
const alias = aliasWarning(tool);
|
|
71
|
+
if (alias !== void 0) out.push(alias);
|
|
72
|
+
return out;
|
|
49
73
|
}
|
|
50
74
|
function aliasWarning(tool) {
|
|
51
75
|
const meta = tool._meta ?? {};
|
|
@@ -54,15 +78,19 @@ function aliasWarning(tool) {
|
|
|
54
78
|
if (used.length === 0) return void 0;
|
|
55
79
|
return { code: "alias", message: "\u4F7F\u7528\u4E86\u975E\u89C4\u8303\u5B57\u6BB5 " + used.join(", ") + '\uFF0C\u8BF7\u6539\u7528 _meta["dsh/folder"] / _meta["dsh/title"]' };
|
|
56
80
|
}
|
|
81
|
+
function usableFolder(value) {
|
|
82
|
+
if (!isStringArray(value) || value.length === 0 || value.length > MAX_FOLDER_DEPTH) return void 0;
|
|
83
|
+
return value.every((segment) => SEGMENT_PATTERN.test(segment)) ? value : void 0;
|
|
84
|
+
}
|
|
57
85
|
function folderFromMeta(tool) {
|
|
58
86
|
const meta = tool._meta ?? {};
|
|
59
|
-
const canonical = meta[CANONICAL_FOLDER];
|
|
60
|
-
if (
|
|
87
|
+
const canonical = usableFolder(meta[CANONICAL_FOLDER]);
|
|
88
|
+
if (canonical !== void 0) return canonical;
|
|
61
89
|
for (const key of ALIAS_NAMESPACES) {
|
|
62
90
|
const nested = meta[key];
|
|
63
91
|
if (nested === null || typeof nested !== "object") continue;
|
|
64
|
-
const inner = nested.folder;
|
|
65
|
-
if (
|
|
92
|
+
const inner = usableFolder(nested.folder);
|
|
93
|
+
if (inner !== void 0) return inner;
|
|
66
94
|
}
|
|
67
95
|
return void 0;
|
|
68
96
|
}
|
|
@@ -85,14 +113,16 @@ function buildCatalog(serverName, tools, publicName, settings = {}) {
|
|
|
85
113
|
return tools.map((tool) => {
|
|
86
114
|
const override = settings.overrides?.[tool.name];
|
|
87
115
|
const invalid = validateTool(tool);
|
|
88
|
-
const
|
|
116
|
+
const warnings = toolWarnings(tool);
|
|
117
|
+
const schema = inputSchemaOf(tool).schema;
|
|
89
118
|
const folder = (override?.folder ?? folderFromMeta(tool) ?? folderFromName(tool.name)).map(slug).filter(Boolean);
|
|
90
119
|
const leaf = slug(tool.name.split(/[./]/).pop() ?? tool.name) || "tool";
|
|
91
120
|
const path = [...folder, leaf].join("/");
|
|
92
|
-
const metaTitle = typeof tool._meta?.[CANONICAL_TITLE] === "string" ? String(tool._meta[CANONICAL_TITLE]) : void 0;
|
|
121
|
+
const metaTitle = typeof tool._meta?.[CANONICAL_TITLE] === "string" ? String(tool._meta[CANONICAL_TITLE]).slice(0, MAX_TITLE) : void 0;
|
|
93
122
|
const annotationTitle = typeof tool.annotations?.title === "string" ? tool.annotations.title : void 0;
|
|
94
123
|
const title = override?.title ?? tool.title ?? annotationTitle ?? metaTitle;
|
|
95
|
-
const
|
|
124
|
+
const described = typeof tool.description === "string" && tool.description.trim() !== "";
|
|
125
|
+
const description = described ? tool.description : tool.name;
|
|
96
126
|
return {
|
|
97
127
|
toolName: publicName(tool.name),
|
|
98
128
|
server: serverName,
|
|
@@ -102,14 +132,14 @@ function buildCatalog(serverName, tools, publicName, settings = {}) {
|
|
|
102
132
|
folder,
|
|
103
133
|
...title === void 0 ? {} : { title },
|
|
104
134
|
description,
|
|
105
|
-
inputSchema:
|
|
135
|
+
inputSchema: schema,
|
|
106
136
|
...tool.outputSchema === void 0 ? {} : { outputSchema: tool.outputSchema },
|
|
107
|
-
hash: shortHash(description + JSON.stringify(
|
|
137
|
+
hash: shortHash(description + JSON.stringify(schema) + JSON.stringify(folder)),
|
|
108
138
|
disabled: disabled.has(tool.name),
|
|
109
139
|
hidden: hidden.has(tool.name),
|
|
110
140
|
...removed.has(tool.name) ? { removed: true } : {},
|
|
111
141
|
...invalid !== void 0 ? { invalid } : {},
|
|
112
|
-
...invalid === void 0 &&
|
|
142
|
+
...invalid === void 0 && warnings.length > 0 ? { warning: { code: warnings[0].code, message: warnings.map((item) => item.message).join("\uFF1B") } } : {}
|
|
113
143
|
};
|
|
114
144
|
});
|
|
115
145
|
}
|
|
@@ -644,7 +674,7 @@ function textOf(blocks) {
|
|
|
644
674
|
return blocks.map((block) => block.text).join("\n");
|
|
645
675
|
}
|
|
646
676
|
function createToolDefinition(serverName, tool, client) {
|
|
647
|
-
const parameters = tool.
|
|
677
|
+
const parameters = inputSchemaOf(tool).schema;
|
|
648
678
|
return {
|
|
649
679
|
name: publicToolName(serverName, tool.name),
|
|
650
680
|
description: tool.description ?? tool.name,
|
package/package.json
CHANGED
package/src/catalog.ts
CHANGED
|
@@ -59,34 +59,64 @@ function isStringArray(value: unknown): value is string[] {
|
|
|
59
59
|
return Array.isArray(value) && value.every(item => typeof item === 'string')
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
/**
|
|
62
|
+
/**
|
|
63
|
+
* Only an unusable name is fatal: many servers (Spring AI's @Tool for one)
|
|
64
|
+
* omit description or inputSchema, and hiding those tools helps nobody.
|
|
65
|
+
*/
|
|
63
66
|
export function validateTool(tool: McpTool): ToolDiagnostic | undefined {
|
|
64
67
|
if (typeof tool.name !== 'string' || !NAME_PATTERN.test(tool.name)) {
|
|
65
68
|
return { code: 'name', message: 'name 必须匹配 ' + NAME_PATTERN.source }
|
|
66
69
|
}
|
|
67
|
-
|
|
68
|
-
|
|
70
|
+
return undefined
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Coerce whatever the server sent into a usable object schema. Strings are
|
|
75
|
+
* parsed (some servers double-encode), everything else falls back to "no args".
|
|
76
|
+
*/
|
|
77
|
+
export function inputSchemaOf(tool: McpTool): { schema: Record<string, unknown>; warning?: ToolDiagnostic } {
|
|
78
|
+
let value: any = (tool as any).inputSchema
|
|
79
|
+
if (typeof value === 'string') {
|
|
80
|
+
try { value = JSON.parse(value) } catch { value = undefined }
|
|
81
|
+
}
|
|
82
|
+
if (value === undefined || value === null) {
|
|
83
|
+
return { schema: { type: 'object', properties: {} }, warning: { code: 'input-schema', message: '缺少 inputSchema,按无参数工具处理' } }
|
|
84
|
+
}
|
|
85
|
+
if (typeof value !== 'object' || Array.isArray(value)) {
|
|
86
|
+
return { schema: { type: 'object', properties: {} }, warning: { code: 'input-schema', message: 'inputSchema 不是对象,按无参数工具处理' } }
|
|
87
|
+
}
|
|
88
|
+
const type = (value as any).type
|
|
89
|
+
if (type !== undefined && type !== 'object') {
|
|
90
|
+
return { schema: { type: 'object', properties: {} }, warning: { code: 'input-schema', message: 'inputSchema.type 不是 object,按无参数工具处理' } }
|
|
69
91
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
92
|
+
return { schema: { type: 'object', properties: {}, ...value } }
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Non-fatal diagnostics: the tool still registers, the UI just flags it. */
|
|
96
|
+
export function toolWarnings(tool: McpTool): ToolDiagnostic[] {
|
|
97
|
+
const out: ToolDiagnostic[] = []
|
|
98
|
+
if (typeof tool.description !== 'string' || tool.description.trim() === '') {
|
|
99
|
+
out.push({ code: 'description', message: '缺少 description,用工具名代替' })
|
|
73
100
|
}
|
|
101
|
+
const schema = inputSchemaOf(tool).warning
|
|
102
|
+
if (schema !== undefined) out.push(schema)
|
|
74
103
|
const meta = (tool._meta ?? {}) as Record<string, unknown>
|
|
75
104
|
const folder = meta[CANONICAL_FOLDER]
|
|
76
105
|
if (folder !== undefined) {
|
|
77
106
|
if (!isStringArray(folder) || folder.length === 0 || folder.length > MAX_FOLDER_DEPTH) {
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return { code: 'folder', message: '_meta["dsh/folder"] 段 "' + bad + '" 非法(不能含 / 或 .,长度 1..64)' }
|
|
107
|
+
out.push({ code: 'folder', message: '_meta["dsh/folder"] 必须是 1..8 个字符串的数组,已忽略' })
|
|
108
|
+
} else {
|
|
109
|
+
const bad = folder.find(segment => !SEGMENT_PATTERN.test(segment))
|
|
110
|
+
if (bad !== undefined) out.push({ code: 'folder', message: '_meta["dsh/folder"] 段 "' + bad + '" 非法,已忽略' })
|
|
83
111
|
}
|
|
84
112
|
}
|
|
85
113
|
const title = meta[CANONICAL_TITLE]
|
|
86
114
|
if (title !== undefined && (typeof title !== 'string' || title.length > MAX_TITLE)) {
|
|
87
|
-
|
|
115
|
+
out.push({ code: 'title', message: '_meta["dsh/title"] 必须是不超过 ' + MAX_TITLE + ' 字的字符串,已截断' })
|
|
88
116
|
}
|
|
89
|
-
|
|
117
|
+
const alias = aliasWarning(tool)
|
|
118
|
+
if (alias !== undefined) out.push(alias)
|
|
119
|
+
return out
|
|
90
120
|
}
|
|
91
121
|
|
|
92
122
|
/** Non-fatal: the tool uses a legacy alias instead of the canonical keys. */
|
|
@@ -98,15 +128,20 @@ export function aliasWarning(tool: McpTool): ToolDiagnostic | undefined {
|
|
|
98
128
|
return { code: 'alias', message: '使用了非规范字段 ' + used.join(', ') + ',请改用 _meta["dsh/folder"] / _meta["dsh/title"]' }
|
|
99
129
|
}
|
|
100
130
|
|
|
131
|
+
function usableFolder(value: unknown): string[] | undefined {
|
|
132
|
+
if (!isStringArray(value) || value.length === 0 || value.length > MAX_FOLDER_DEPTH) return undefined
|
|
133
|
+
return value.every(segment => SEGMENT_PATTERN.test(segment)) ? value : undefined
|
|
134
|
+
}
|
|
135
|
+
|
|
101
136
|
function folderFromMeta(tool: McpTool): string[] | undefined {
|
|
102
137
|
const meta = (tool._meta ?? {}) as Record<string, unknown>
|
|
103
|
-
const canonical = meta[CANONICAL_FOLDER]
|
|
104
|
-
if (
|
|
138
|
+
const canonical = usableFolder(meta[CANONICAL_FOLDER])
|
|
139
|
+
if (canonical !== undefined) return canonical
|
|
105
140
|
for (const key of ALIAS_NAMESPACES) {
|
|
106
141
|
const nested = meta[key]
|
|
107
142
|
if (nested === null || typeof nested !== 'object') continue
|
|
108
|
-
const inner = (nested as any).folder
|
|
109
|
-
if (
|
|
143
|
+
const inner = usableFolder((nested as any).folder)
|
|
144
|
+
if (inner !== undefined) return inner
|
|
110
145
|
}
|
|
111
146
|
return undefined
|
|
112
147
|
}
|
|
@@ -137,16 +172,18 @@ export function buildCatalog(
|
|
|
137
172
|
return tools.map(tool => {
|
|
138
173
|
const override = settings.overrides?.[tool.name]
|
|
139
174
|
const invalid = validateTool(tool)
|
|
140
|
-
const
|
|
175
|
+
const warnings = toolWarnings(tool)
|
|
176
|
+
const schema = inputSchemaOf(tool).schema
|
|
141
177
|
const folder = (override?.folder ?? folderFromMeta(tool) ?? folderFromName(tool.name))
|
|
142
178
|
.map(slug)
|
|
143
179
|
.filter(Boolean)
|
|
144
180
|
const leaf = slug(tool.name.split(/[./]/).pop() ?? tool.name) || 'tool'
|
|
145
181
|
const path = [...folder, leaf].join('/')
|
|
146
|
-
const metaTitle = typeof tool._meta?.[CANONICAL_TITLE] === 'string' ? String(tool._meta[CANONICAL_TITLE]) : undefined
|
|
182
|
+
const metaTitle = typeof tool._meta?.[CANONICAL_TITLE] === 'string' ? String(tool._meta[CANONICAL_TITLE]).slice(0, MAX_TITLE) : undefined
|
|
147
183
|
const annotationTitle = typeof tool.annotations?.title === 'string' ? tool.annotations.title : undefined
|
|
148
184
|
const title = override?.title ?? tool.title ?? annotationTitle ?? metaTitle
|
|
149
|
-
const
|
|
185
|
+
const described = typeof tool.description === 'string' && tool.description.trim() !== ''
|
|
186
|
+
const description = described ? tool.description as string : tool.name
|
|
150
187
|
return {
|
|
151
188
|
toolName: publicName(tool.name),
|
|
152
189
|
server: serverName,
|
|
@@ -156,14 +193,16 @@ export function buildCatalog(
|
|
|
156
193
|
folder,
|
|
157
194
|
...(title === undefined ? {} : { title }),
|
|
158
195
|
description,
|
|
159
|
-
inputSchema:
|
|
196
|
+
inputSchema: schema,
|
|
160
197
|
...(tool.outputSchema === undefined ? {} : { outputSchema: tool.outputSchema }),
|
|
161
|
-
hash: shortHash(description + JSON.stringify(
|
|
198
|
+
hash: shortHash(description + JSON.stringify(schema) + JSON.stringify(folder)),
|
|
162
199
|
disabled: disabled.has(tool.name),
|
|
163
200
|
hidden: hidden.has(tool.name),
|
|
164
201
|
...(removed.has(tool.name) ? { removed: true } : {}),
|
|
165
202
|
...(invalid !== undefined ? { invalid } : {}),
|
|
166
|
-
...(invalid === undefined &&
|
|
203
|
+
...(invalid === undefined && warnings.length > 0
|
|
204
|
+
? { warning: { code: warnings[0].code, message: warnings.map(item => item.message).join(';') } }
|
|
205
|
+
: {}),
|
|
167
206
|
}
|
|
168
207
|
})
|
|
169
208
|
}
|
package/src/register.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/** Adapt one MCP tool to a DSH ToolDefinition, projecting MCP blocks safely. */
|
|
2
2
|
import type { McpClient, McpTool } from './client.ts'
|
|
3
3
|
import { publicToolName } from './naming.ts'
|
|
4
|
+
import { inputSchemaOf } from './catalog.ts'
|
|
4
5
|
|
|
5
6
|
interface DshBlock {
|
|
6
7
|
type: 'text'
|
|
@@ -53,9 +54,7 @@ export function createToolDefinition(
|
|
|
53
54
|
tool: McpTool,
|
|
54
55
|
client: McpClient,
|
|
55
56
|
): Record<string, unknown> {
|
|
56
|
-
const parameters = tool.
|
|
57
|
-
? tool.inputSchema
|
|
58
|
-
: { type: 'object', properties: {} }
|
|
57
|
+
const parameters = inputSchemaOf(tool).schema
|
|
59
58
|
|
|
60
59
|
return {
|
|
61
60
|
name: publicToolName(serverName, tool.name),
|