-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
49 lines (40 loc) · 1.21 KB
/
server.py
File metadata and controls
49 lines (40 loc) · 1.21 KB
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
import subprocess
import json
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("python-runner")
@mcp.tool()
def run_script(code: str, timeout: int = 30) -> str:
"""Execute Python code and return the result.
Args:
code: Python code to execute
timeout: Execution timeout in seconds (default: 30)
Returns:
JSON string with success, stdout, stderr, error, and return_code
"""
try:
result = subprocess.run(
["python", "-c", code],
capture_output=True,
text=True,
timeout=timeout
)
return json.dumps({
"success": result.returncode == 0,
"stdout": result.stdout,
"stderr": result.stderr,
"error": None if result.returncode == 0 else result.stderr,
"return_code": result.returncode
})
except subprocess.TimeoutExpired:
return json.dumps({
"success": False,
"stdout": "",
"stderr": "",
"error": f"Execution timed out after {timeout} seconds",
"return_code": -1
})
def main():
"""Entry point for the MCP server."""
mcp.run()
if __name__ == "__main__":
main()