AI-Python-Tutor / server.py
ABO4SAMRA's picture
Create server.py
f4fced9 verified
raw
history blame
1.47 kB
import sys
import subprocess
from mcp.server.fastmcp import FastMCP
# Initialize the MCP Server
mcp = FastMCP("VibeCodingTutor")
@mcp.tool()
def write_file(filename: str, content: str) -> str:
"""Writes code or text to a file in the current directory."""
try:
with open(filename, "w") as f:
f.write(content)
return f"Successfully wrote {len(content)} characters to {filename}."
except Exception as e:
return f"Error writing file: {str(e)}"
@mcp.tool()
def read_file(filename: str) -> str:
"""Reads the content of a file."""
try:
with open(filename, "r") as f:
return f.read()
except Exception as e:
return f"Error reading file: {str(e)}"
@mcp.tool()
def run_python_script(filename: str) -> str:
"""Runs a Python script and returns the stdout/stderr."""
try:
# Security Note: In production, use a Docker sandbox (e.g., E2B).
# For this Hackathon demo, we run locally with subprocess.
result = subprocess.run(
[sys.executable, filename],
capture_output=True,
text=True,
timeout=10
)
output = result.stdout
if result.stderr:
output += f"\n[Errors]:\n{result.stderr}"
return output
except Exception as e:
return f"Execution failed: {str(e)}"
if __name__ == "__main__":
# This starts the server over Stdio (standard input/output)
mcp.run()