进阶
给 dsh 接入 MCP server 或自定义 tool
给 dsh 接入 MCP server 或自定义 tool — 学习如何构建进阶 dsh 插件。
Tool 与 command:谁调用谁
command 由用户在 UI 里触发;tool 由模型自己决定需要时调用。想让 agent 自主行动,就注册 tool。
路径 A — 注册自定义 tool
ctx.tools.register({
name: 'read_local_file',
description: 'Read a text file from the workspace',
parameters: {
type: 'object',
properties: { path: { type: 'string' } },
required: ['path'],
},
async execute(args) {
return { ok: true, content: await fs.readFile(args.path, 'utf8') };
},
});三个细节很关键:清晰的 description(它决定模型选不选这个 tool)、严格的 JSON Schema parameters、以及结构化错误返回。