execute_command
Executes pre-approved commands on the user's system, enabling AI assistants to perform secure system interactions.
Instructions
事前に許可されたコマンドを実行します
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| command | Yes | 実行するコマンド |
Implementation Reference
- src/index.ts:69-87 (registration)Registration of the 'execute_command' tool via ListToolsRequestSchema handler. Defines the tool name, description, and inputSchema (expects a 'command' string).
private setupToolHandlers() { this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [ { name: 'execute_command', description: '事前に許可されたコマンドを実行します', inputSchema: { type: 'object', properties: { command: { type: 'string', description: '実行するコマンド', }, }, required: ['command'], }, }, ], })); - src/index.ts:89-128 (handler)Main handler for executing the 'execute_command' tool. Validates the tool name, checks if the command is allowed via isCommandAllowed(), runs it via execAsync (promisified child_process.exec), and returns stdout/stderr.
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, }; } }); - src/index.ts:63-67 (helper)Helper function isCommandAllowed that checks whether the command's prefix (first space-separated word) is in the allowed commands list.
private isCommandAllowed(command: string): boolean { // コマンドの最初の部分(スペース区切りの最初の単語)を取得 const commandPrefix = command.split(' ')[0]; return this.allowedCommands.some(allowed => commandPrefix === allowed); } - src/index.ts:26-32 (helper)Helper function getAllowedCommands that reads comma-separated allowed commands from the ALLOWED_COMMANDS environment variable, falling back to a default list (git, ls, mkdir, cd, npm, npx, python).
const getAllowedCommands = (): string[] => { const envCommands = process.env.ALLOWED_COMMANDS; if (!envCommands) { return DEFAULT_ALLOWED_COMMANDS; } return envCommands.split(',').map(cmd => cmd.trim()); };