run_command
Execute shell commands to automate tasks, retrieve system information, or manage processes through a secure subprocess interface.
Instructions
Execute a shell command using subprocess and return the output
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| command | Yes |
Implementation Reference
- server.py:6-26 (handler)The main handler function for the 'run_command' tool. It executes the provided shell command using subprocess.run with shell=True, captures output, handles success/error/timeout cases, and returns the appropriate string response.@mcp.tool def run_command(command: str) -> str: """Execute a shell command using subprocess and return the output""" try: result = subprocess.run( command, shell=True, capture_output=True, text=True, timeout=30 # 30秒でタイムアウト ) if result.returncode == 0: return result.stdout else: return f"Error (exit code {result.returncode}): {result.stderr}" except subprocess.TimeoutExpired: return "Error: Command timed out after 30 seconds" except Exception as e: return f"Error executing command: {str(e)}"
- server.py:6-6 (registration)The @mcp.tool decorator registers the run_command function as an MCP tool.@mcp.tool
- server.py:7-8 (schema)Type hints and docstring define the input schema (command: str) and output (str), used by FastMCP for tool schema.def run_command(command: str) -> str: """Execute a shell command using subprocess and return the output"""