Skip to main content
Glama
Sunwood-ai-labs

Command Executor MCP Server

execute_command

Execute pre-approved system commands securely through the Command Executor MCP Server to enable AI assistants to interact with your system safely.

Instructions

事前に許可されたコマンドを実行します

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
commandYes実行するコマンド

Implementation Reference

  • Handler for CallToolRequestSchema that checks for 'execute_command', validates the command against allowed list, executes it using child_process.exec (promisified), and returns text content with stdout/stderr or error message.
    this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
      if (request.params.name !== 'execute_command') {
        throw new McpError(
          ErrorCode.MethodNotFound,
          `Unknown tool: ${request.params.name}`
        );
      }
    
      const { command } = request.params.arguments as { command: string };
    
      // コマンドが許可されているか確認
      if (!this.isCommandAllowed(command)) {
        throw new McpError(
          ErrorCode.InvalidParams,
          `Command not allowed: ${command}. Allowed commands: ${this.allowedCommands.join(', ')}`
        );
      }
    
      try {
        const { stdout, stderr } = await execAsync(command);
        return {
          content: [
            {
              type: 'text',
              text: stdout || stderr,
            },
          ],
        };
      } catch (error: any) {
        return {
          content: [
            {
              type: 'text',
              text: `Command execution failed: ${error.message}`,
            },
          ],
          isError: true,
        };
      }
    });
  • Input schema definition for the execute_command tool, specifying a required 'command' string property.
    inputSchema: {
      type: 'object',
      properties: {
        command: {
          type: 'string',
          description: '実行するコマンド',
        },
      },
      required: ['command'],
    },
  • src/index.ts:70-87 (registration)
    Tool registration in the ListToolsRequestSchema handler, defining the name, description, and input schema for 'execute_command'.
    this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
      tools: [
        {
          name: 'execute_command',
          description: '事前に許可されたコマンドを実行します',
          inputSchema: {
            type: 'object',
            properties: {
              command: {
                type: 'string',
                description: '実行するコマンド',
              },
            },
            required: ['command'],
          },
        },
      ],
    }));
  • Helper function to check if a command prefix matches the list of allowed commands.
    private isCommandAllowed(command: string): boolean {
      // コマンドの最初の部分(スペース区切りの最初の単語)を取得
      const commandPrefix = command.split(' ')[0];
      return this.allowedCommands.some(allowed => commandPrefix === allowed);
    }
  • Helper function to retrieve the list of allowed commands from environment variable or defaults.
    const getAllowedCommands = (): string[] => {
      const envCommands = process.env.ALLOWED_COMMANDS;
      if (!envCommands) {
        return DEFAULT_ALLOWED_COMMANDS;
      }
      return envCommands.split(',').map(cmd => cmd.trim());
    };

Tool Definition Quality

Score is being calculated. Check back soon.

Install Server

Other Tools

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/Sunwood-ai-labs/command-executor-mcp-server'

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