-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoding_server.py
More file actions
36 lines (30 loc) · 1017 Bytes
/
coding_server.py
File metadata and controls
36 lines (30 loc) · 1017 Bytes
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
# python coding_server.py
from mcp.server.fastmcp import FastMCP
import json
from server_utils import run_code_in_subprocess, log_code
import logging
#logging.basicConfig(level=logging.INFO)
#import logging
logging.basicConfig(level=logging.DEBUG)
# Create an MCP server
mcp = FastMCP("coding-server", host="0.0.0.0", port=8003)
@mcp.tool()
def execute_python(code_str: str) -> str:
"""
Execute a Python code string and returns its standard output as a string
Args:
code_str (str): The Python code to execute.
Returns:
str: The standard output from the executed code, preferably in JSON format.
"""
log_code(code_str)
print(code_str)
stdout, stderr, exit_code = run_code_in_subprocess(code_str)
if exit_code:
return json.dumps({"exit_code": exit_code, "error_message": stderr.strip()})
else:
logging.warning(stdout)
print('STDOUT:', stdout)
return stdout
if __name__ == "__main__":
mcp.run(transport="streamable-http")