from fastapi import FastAPI import subprocess import os app = FastAPI() @app.get("/mcp/tools") async def list_tools(): return { "tools": [ { "name": "execute_shell", "description": "Executes a shell command in a secure sandbox.", "input_schema": {"type": "object", "properties": {"command": {"type": "string"}}, "required": ["command"]}, }, { "name": "list_files", "description": "Lists files and directories in a given path within the sandbox.", "input_schema": {"type": "object", "properties": {"path": {"type": "string"}}, "required": ["path"]}, } ] } @app.post("/mcp/tools/execute_shell") async def execute_shell(payload: dict): params = payload.get("params", {}) command = params.get("command") if not command: return {"status": "error", "result": "No command provided."} print(f"[Sandbox Server] Executing: {command}") try: # In a real-world scenario, this would be a heavily secured, isolated container. # For this demo, we use subprocess with a timeout. result = subprocess.run(command, shell=True, capture_output=True, text=True, timeout=30, check=True) output = f"STDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}" return {"status": "success", "result": output} except subprocess.CalledProcessError as e: return {"status": "error", "result": f"Command failed with exit code {e.returncode}.\nSTDOUT:\n{e.stdout}\nSTDERR:\n{e.stderr}"} except subprocess.TimeoutExpired: return {"status": "error", "result": "Command timed out after 30 seconds."} @app.post("/mcp/tools/list_files") async def list_files(payload: dict): path = payload.get("params", {}).get("path", ".") try: files = os.listdir(path) return {"status": "success", "result": f"Files in '{path}': {files}"} except FileNotFoundError: return {"status": "error", "result": f"Path not found: {path}"}