File size: 1,471 Bytes
f4fced9 |
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 |
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() |