Create server.py
Browse files
server.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import subprocess
|
| 3 |
+
from mcp.server.fastmcp import FastMCP
|
| 4 |
+
|
| 5 |
+
# Initialize the MCP Server
|
| 6 |
+
mcp = FastMCP("VibeCodingTutor")
|
| 7 |
+
|
| 8 |
+
@mcp.tool()
|
| 9 |
+
def write_file(filename: str, content: str) -> str:
|
| 10 |
+
"""Writes code or text to a file in the current directory."""
|
| 11 |
+
try:
|
| 12 |
+
with open(filename, "w") as f:
|
| 13 |
+
f.write(content)
|
| 14 |
+
return f"Successfully wrote {len(content)} characters to {filename}."
|
| 15 |
+
except Exception as e:
|
| 16 |
+
return f"Error writing file: {str(e)}"
|
| 17 |
+
|
| 18 |
+
@mcp.tool()
|
| 19 |
+
def read_file(filename: str) -> str:
|
| 20 |
+
"""Reads the content of a file."""
|
| 21 |
+
try:
|
| 22 |
+
with open(filename, "r") as f:
|
| 23 |
+
return f.read()
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"Error reading file: {str(e)}"
|
| 26 |
+
|
| 27 |
+
@mcp.tool()
|
| 28 |
+
def run_python_script(filename: str) -> str:
|
| 29 |
+
"""Runs a Python script and returns the stdout/stderr."""
|
| 30 |
+
try:
|
| 31 |
+
# Security Note: In production, use a Docker sandbox (e.g., E2B).
|
| 32 |
+
# For this Hackathon demo, we run locally with subprocess.
|
| 33 |
+
result = subprocess.run(
|
| 34 |
+
[sys.executable, filename],
|
| 35 |
+
capture_output=True,
|
| 36 |
+
text=True,
|
| 37 |
+
timeout=10
|
| 38 |
+
)
|
| 39 |
+
output = result.stdout
|
| 40 |
+
if result.stderr:
|
| 41 |
+
output += f"\n[Errors]:\n{result.stderr}"
|
| 42 |
+
return output
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return f"Execution failed: {str(e)}"
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
# This starts the server over Stdio (standard input/output)
|
| 48 |
+
mcp.run()
|