File size: 2,071 Bytes
915668d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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}"}