hono-mcp 1.4.4 → 1.4.7
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/tools/decorator-tools.js +67 -42
- package/package.json +1 -1
|
@@ -5,8 +5,11 @@ export const executeCommandTool = (server) => {
|
|
|
5
5
|
type: z
|
|
6
6
|
.string()
|
|
7
7
|
.describe("Command type (e.g., math.add, math.subtract, math.multiply, math.divide)"),
|
|
8
|
-
params: z
|
|
9
|
-
|
|
8
|
+
params: z
|
|
9
|
+
.record(z.unknown())
|
|
10
|
+
.optional()
|
|
11
|
+
.describe("Command parameters (optional for commands without params)"),
|
|
12
|
+
}, async ({ type, params = {}, }) => {
|
|
10
13
|
console.log("executeCommand called with:", { type, params });
|
|
11
14
|
const command = getCommandRegistry().get(type);
|
|
12
15
|
if (!command) {
|
|
@@ -38,51 +41,73 @@ export const executeCommandTool = (server) => {
|
|
|
38
41
|
return await command.handler(validationResult.data);
|
|
39
42
|
});
|
|
40
43
|
};
|
|
44
|
+
// 从 Zod schema 提取类型名
|
|
45
|
+
function getZodTypeName(schema) {
|
|
46
|
+
const def = schema._def;
|
|
47
|
+
if (def.typeName === "ZodOptional") {
|
|
48
|
+
return getZodTypeName(def.innerType) + "?";
|
|
49
|
+
}
|
|
50
|
+
if (def.typeName === "ZodDefault") {
|
|
51
|
+
return getZodTypeName(def.innerType);
|
|
52
|
+
}
|
|
53
|
+
if (def.typeName === "ZodString")
|
|
54
|
+
return "string";
|
|
55
|
+
if (def.typeName === "ZodNumber")
|
|
56
|
+
return "number";
|
|
57
|
+
if (def.typeName === "ZodBoolean")
|
|
58
|
+
return "boolean";
|
|
59
|
+
if (def.typeName === "ZodArray")
|
|
60
|
+
return "array";
|
|
61
|
+
if (def.typeName === "ZodObject")
|
|
62
|
+
return "object";
|
|
63
|
+
return "any";
|
|
64
|
+
}
|
|
65
|
+
// 生成示例值
|
|
66
|
+
function getExampleValue(schema) {
|
|
67
|
+
const def = schema._def;
|
|
68
|
+
if (def.typeName === "ZodOptional" || def.typeName === "ZodDefault") {
|
|
69
|
+
return undefined; // optional 参数不生成示例
|
|
70
|
+
}
|
|
71
|
+
if (def.typeName === "ZodString")
|
|
72
|
+
return "example";
|
|
73
|
+
if (def.typeName === "ZodNumber")
|
|
74
|
+
return 1;
|
|
75
|
+
if (def.typeName === "ZodBoolean")
|
|
76
|
+
return true;
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
41
79
|
export const helpTool = (server) => {
|
|
42
|
-
server.tool("help", "Get help information about available commands", {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
]
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
.join("\n");
|
|
69
|
-
return {
|
|
70
|
-
content: [
|
|
71
|
-
{
|
|
72
|
-
type: "text",
|
|
73
|
-
text: `Command: ${command.type}\nDescription: ${command.description}\n\nParameters:\n${paramsInfo}`,
|
|
74
|
-
},
|
|
75
|
-
],
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
const commandList = Array.from(getCommandRegistry().values())
|
|
79
|
-
.map((cmd) => `- ${cmd.type}: ${cmd.description}`)
|
|
80
|
-
.join("\n");
|
|
80
|
+
server.tool("help", "Get help information about available commands", {}, async () => {
|
|
81
|
+
const commands = Array.from(getCommandRegistry().values());
|
|
82
|
+
const sections = commands.map((cmd) => {
|
|
83
|
+
// 构建参数类型字符串
|
|
84
|
+
const paramEntries = Array.from(cmd.params.entries());
|
|
85
|
+
const paramsStr = paramEntries.length === 0
|
|
86
|
+
? "{}"
|
|
87
|
+
: "{ " +
|
|
88
|
+
paramEntries
|
|
89
|
+
.map(([key, schema]) => `${key}: ${getZodTypeName(schema)}`)
|
|
90
|
+
.join(", ") +
|
|
91
|
+
" }";
|
|
92
|
+
// 构建示例 params
|
|
93
|
+
const exampleParams = {};
|
|
94
|
+
paramEntries.forEach(([key, schema]) => {
|
|
95
|
+
const val = getExampleValue(schema);
|
|
96
|
+
if (val !== undefined) {
|
|
97
|
+
exampleParams[key] = val;
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
const example = JSON.stringify({
|
|
101
|
+
type: cmd.type,
|
|
102
|
+
params: exampleParams,
|
|
103
|
+
});
|
|
104
|
+
return `### ${cmd.type}\n${cmd.description}\nParams: ${paramsStr}\nExample: ${example}`;
|
|
105
|
+
});
|
|
81
106
|
return {
|
|
82
107
|
content: [
|
|
83
108
|
{
|
|
84
109
|
type: "text",
|
|
85
|
-
text:
|
|
110
|
+
text: `## Commands\n\n${sections.join("\n\n")}`,
|
|
86
111
|
},
|
|
87
112
|
],
|
|
88
113
|
};
|