Skip to main content
Glama

buildReload_tool

Execute npm run build and reload tools to update MCP server tools without restarting the server.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • Main handler function implementing the tool: runs 'npm run build' in project root, reloads tools, handles success/error responses.
    export default async function (request: any) {
      try {
        //  执行编译命令
        const buildOutput = execSync('npm run build', {
          cwd: PROJECT_ROOT,  // 使用计算出的项目根目录
          encoding: 'utf-8',
          stdio: 'pipe',
          timeout: 300000  // 5分钟超时
        });
    
        // 阶段 2: 重新加载工具
        const handlers = await loadTools(true);
    
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify({
                build: {
                  success: true,
                  output: buildOutput,
                  directory: PROJECT_ROOT  // 返回实际使用的目录
                },
                reload: {
                  success: !!handlers,
                  tools: Object.keys(handlers).length
                }
              }, null, 2)
            }
          ]
        };
    
      } catch (error) {
        const errorInfo = {
          message: error instanceof Error ? error.message : String(error),
          stack: error instanceof Error ? error.stack : undefined,
          directory: PROJECT_ROOT,  // 包含目录信息
          system: {
            platform: process.platform,
            versions: process.versions
          }
        };
    
        // 如果是执行命令错误,添加额外信息
        if (error instanceof Error && 'code' in error) {
          Object.assign(errorInfo, {
            exitCode: error.code,
            stdout: (error as any).stdout,
            stderr: (error as any).stderr
          });
        }
    
        return {
          content: [{
            type: "text",
            text: JSON.stringify({
              error: "BuildReload_FAILED",
              details: errorInfo
            }, null, 2)
          }],
          isError: true
        };
      }
    }
  • Input schema for the tool: no parameters required.
    export const schema = {
      name: "buildReload_tool",
      description: "Execute 'npm run build' and reload tools",
      type: "object",
      properties: {},
      required: []
    };
  • Dynamic loading and registration of all tools (including buildReload_tool) from src/tools directory by importing schema, handler (default), and destroy functions.
    for (const file of toolFiles) {
      const toolPath = path.join(toolsDir, file);
      try {
        // 如果是重新加载,清除模块缓存
        if (reload) clearModuleCache(toolPath);
    
        // 导入模块,重新加载时添加时间戳防止缓存
        const importPath = 'file://' + toolPath + (reload ? `?update=${Date.now()}` : '');
        const { default: tool, schema, destroy } = await import(importPath);
        const toolName = path.parse(toolPath).name;
    
        // 注册工具
        tools.push({
          name: toolName,
          description: tool.description,
          inputSchema: schema,
          destroy: destroy
        });
    
        // 注册处理函数
        handlers[toolName] = async (request: ToolRequest) => { return await tool(request); };
      } catch (error) {
        console.error(`Failed to ${reload ? 'reload' : 'load'} tool ${file}:`, error);
      }
    }
  • Tool cleanup function called during unload/reload.
    export async function destroy() {
      // Release resources, stop timers, disconnect, etc.
      console.log("Destroy buildReload_tool");
    }

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/xiaoguomeiyitian/ToolBox'

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