Execute bash command Args: command: Bash command to execute timeout: Timeout in seconds Returns: JSON string of execution result
(command: str, timeout: int = 30)
| 765 | |
| 766 | @mcp.tool() |
| 767 | async def execute_bash(command: str, timeout: int = 30) -> str: |
| 768 | """ |
| 769 | Execute bash command |
| 770 | |
| 771 | Args: |
| 772 | command: Bash command to execute |
| 773 | timeout: Timeout in seconds |
| 774 | |
| 775 | Returns: |
| 776 | JSON string of execution result |
| 777 | """ |
| 778 | try: |
| 779 | # 安全检查:禁止危险命令 |
| 780 | dangerous_commands = ["rm -rf", "sudo", "chmod 777", "mkfs", "dd if="] |
| 781 | if any(dangerous in command.lower() for dangerous in dangerous_commands): |
| 782 | result = { |
| 783 | "status": "error", |
| 784 | "message": f"Dangerous command execution prohibited: {command}", |
| 785 | } |
| 786 | log_operation( |
| 787 | "execute_bash_blocked", |
| 788 | {"command": command, "reason": "dangerous_command"}, |
| 789 | ) |
| 790 | return json.dumps(result, ensure_ascii=False, indent=2) |
| 791 | |
| 792 | # Ensure workspace directory exists |
| 793 | ensure_workspace_exists() |
| 794 | |
| 795 | # Execute command |
| 796 | result = subprocess.run( |
| 797 | command, |
| 798 | shell=True, |
| 799 | cwd=WORKSPACE_DIR, |
| 800 | capture_output=True, |
| 801 | text=True, |
| 802 | timeout=timeout, |
| 803 | encoding="utf-8", |
| 804 | ) |
| 805 | |
| 806 | execution_result = { |
| 807 | "status": "success" if result.returncode == 0 else "error", |
| 808 | "return_code": result.returncode, |
| 809 | "stdout": result.stdout, |
| 810 | "stderr": result.stderr, |
| 811 | "command": command, |
| 812 | "timeout": timeout, |
| 813 | } |
| 814 | |
| 815 | if result.returncode != 0: |
| 816 | execution_result["message"] = "Bash command execution failed" |
| 817 | else: |
| 818 | execution_result["message"] = "Bash command execution successful" |
| 819 | |
| 820 | log_operation( |
| 821 | "execute_bash", |
| 822 | { |
| 823 | "command": command, |
| 824 | "return_code": result.returncode, |
nothing calls this directly
no test coverage detected