@voyager-0x/agent-mcp
Version:
Voyager MCP Agent - A powerful Model Context Protocol agent
131 lines (117 loc) • 4.5 kB
text/typescript
import z from "zod";
import { mcpModulesManager } from "./module.js";
type ModuleInfo = {
title: string;
description: string;
id: string;
};
export const GET_MORE_TOOLS = "getMoreTools";
export const innerToolsMcpModule = mcpModulesManager.createMcpBuiltinModule(
{
title: "工具发现与管理",
description: "用于发现和获取系统中可用的工具模块,帮助扩展AI助手的能力",
},
(mcpServer) => {
mcpServer.registerTool(
"getToolsGroups",
{
title: "查看可用工具组列表",
description: "获取系统中所有可用的工具组信息。每个工具组包含一组相关功能的工具,比如天气查询、文件操作、数据分析等。使用此工具来了解有哪些功能模块可以使用,然后根据用户需求选择合适的工具组进行加载。",
},
async () => {
const modules = mcpModulesManager.listMcpModules();
const _modules: ModuleInfo[] = [];
for (const module of modules) {
if (module.source !== "builtin") {
_modules.push({
title: module.title,
description: module.description,
id: module.uuid,
});
}
}
if (_modules.length === 0) {
return {
content: [
{
type: "text",
text: "当前系统中没有可用的外部工具组。",
},
],
};
}
const formattedText = `可用工具组列表:\n\n${_modules.map((module, index) =>
`${index + 1}. **${module.title}** (ID: ${module.id})\n 描述:${module.description}`
).join('\n\n')}`;
return {
content: [
{
type: "text",
text: formattedText,
},
],
};
}
);
mcpServer.registerTool(
GET_MORE_TOOLS,
{
title: "加载指定工具组的详细工具",
description: "根据工具组ID加载具体的工具和资源。当用户需要使用特定功能时,先通过getToolsGroups查看可用工具组,然后使用此工具加载对应工具组中的具体工具。例如:用户想查天气,先查看工具组列表找到天气相关的工具组,然后加载该工具组获取具体的天气查询工具。",
inputSchema: {
ids: z.array(z.string()).describe("要加载的工具组ID数组。可以同时加载多个工具组,每个ID对应一个工具组。"),
},
},
async ({ ids }) => {
if (!ids || ids.length === 0) {
return {
content: [
{
type: "text",
text: "错误:请提供至少一个工具组ID。",
},
],
};
}
const tools = await mcpModulesManager.getTools({ uuids: ids });
const resources = await mcpModulesManager.getResources({
uuids: ids,
});
let resultText = "已成功加载工具组!\n\n";
if (tools.length > 0) {
resultText += "## 📋 可用工具列表:\n";
tools.forEach((tool, index) => {
resultText += `${index + 1}. **${tool.name}**\n`;
resultText += ` 描述:${tool.description || '暂无描述'}\n`;
if (tool.inputSchema) {
resultText += ` 参数:${JSON.stringify(tool.inputSchema, null, 2)}\n`;
}
resultText += ` 模块ID:${tool.moduleId}\n\n`;
});
} else {
resultText += "## 📋 工具列表:暂无可用工具\n\n";
}
if (resources.length > 0) {
resultText += "## 📚 可用资源列表:\n";
resources.forEach((resource, index) => {
resultText += `${index + 1}. **${resource.name}**\n`;
resultText += ` 描述:${resource.description || '暂无描述'}\n`;
resultText += ` URI:${resource.uri}\n`;
resultText += ` 模块ID:${resource.moduleId}\n\n`;
});
} else {
resultText += "## 📚 资源列表:暂无可用资源\n\n";
}
resultText += "现在你可以使用这些工具来帮助用户完成任务了!";
return {
content: [
{
type: "text",
text: resultText,
},
],
};
}
);
}
);