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
| Name | Required | Description | Default |
|---|---|---|---|
| command | Yes | 実行するコマンド |
Implementation Reference
- src/index.ts:89-128 (handler)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, }; } });
- src/index.ts:75-84 (schema)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'], }, }, ], }));
- src/index.ts:63-67 (helper)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); }
- src/index.ts:26-32 (helper)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()); };