Skip to main content
Glama
register.ts1.5 kB
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import fs from "fs"; import path from "path"; import { fileURLToPath } from "url"; export async function registerTools(server: McpServer) { // 获取当前文件的目录路径 const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // 读取tools目录下的所有文件 const files = fs.readdirSync(__dirname); // 过滤出.ts或.js文件,但排除index文件和register文件 const toolFiles = files.filter( (file) => (file.endsWith(".ts") || file.endsWith(".js")) && file !== "index.ts" && file !== "index.js" && file !== "register.ts" && file !== "register.js" ); // 动态导入并注册每个工具 for (const file of toolFiles) { try { // 构建导入路径 const importPath = `./${file.replace(/\.(ts|js)$/, ".js")}`; // 动态导入模块 const module = await import(importPath); // 查找并执行注册函数 const registerFunctionName = Object.keys(module).find( (key) => key.startsWith("register") && typeof module[key] === "function" ); if (registerFunctionName) { module[registerFunctionName](server); console.error(`已注册工具: ${file}`); } else { console.warn(`警告: 在文件 ${file} 中未找到注册函数`); } } catch (error) { console.error(`注册工具 ${file} 时出错:`, error); } } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/revit-mcp/revit-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server