ABO4SAMRA commited on
Commit
decfd66
·
verified ·
1 Parent(s): 62bdcc8

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +69 -13
server.py CHANGED
@@ -1,24 +1,51 @@
 
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:
@@ -26,23 +53,52 @@ def read_file(filename: str) -> str:
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()
 
1
+ import os
2
  import sys
3
  import subprocess
4
  from mcp.server.fastmcp import FastMCP
5
 
6
+ # 1. Initialize with System Instructions
7
+ # These instructions tell the LLM *how* to use this server when it connects.
8
+ mcp = FastMCP(
9
+ "VibeCodingTutor",
10
+ instructions="""
11
+ This server provides file system access and code execution for a Python Tutor.
12
+ - Always 'list_directory' first to understand the student's current workspace.
13
+ - Use 'read_file' to diagnose errors in existing code.
14
+ - Use 'run_python_script' to verify your examples work before showing them.
15
+ """
16
+ )
17
 
18
+ # --- 2. New Tool: Context Awareness ---
19
+ @mcp.tool()
20
+ def list_directory() -> str:
21
+ """Lists all files in the current directory to understand the project structure."""
22
+ try:
23
+ files = os.listdir(".")
24
+ # Filter out hidden files/dirs to keep context clean
25
+ visible_files = [f for f in files if not f.startswith('.')]
26
+ if not visible_files:
27
+ return "The directory is empty."
28
+ return "Current Project Files:\n- " + "\n- ".join(visible_files)
29
+ except Exception as e:
30
+ return f"Error listing directory: {str(e)}"
31
+
32
+ # --- 3. Existing Tools (Enhanced) ---
33
  @mcp.tool()
34
  def write_file(filename: str, content: str) -> str:
35
+ """Writes code or text to a file. Overwrites if exists."""
36
  try:
37
  with open(filename, "w") as f:
38
  f.write(content)
39
+ return f"Successfully wrote {len(content)} bytes to '{filename}'.\nYou can now run this file."
40
  except Exception as e:
41
+ return f"Error writing file: {str(e)}"
42
 
43
  @mcp.tool()
44
  def read_file(filename: str) -> str:
45
+ """Reads a file's content. Use this to debug code."""
46
  try:
47
+ if not os.path.exists(filename):
48
+ return f"❌ File '{filename}' not found. Did you forget to write it first?"
49
  with open(filename, "r") as f:
50
  return f.read()
51
  except Exception as e:
 
53
 
54
  @mcp.tool()
55
  def run_python_script(filename: str) -> str:
56
+ """Runs a Python script and captures output/errors."""
57
  try:
58
+ # Check if file exists first to avoid confusing subprocess errors
59
+ if not os.path.exists(filename):
60
+ return f"❌ Cannot run '{filename}' because it does not exist. Write it first."
61
+
62
+ # Run with timeout to prevent infinite loops (common with students)
63
  result = subprocess.run(
64
  [sys.executable, filename],
65
  capture_output=True,
66
  text=True,
67
+ timeout=10
68
  )
69
+
70
+ output = ""
71
+ if result.stdout:
72
+ output += f"[STDOUT]\n{result.stdout}\n"
73
  if result.stderr:
74
+ output += f"[STDERR]\n{result.stderr}\n"
75
+
76
+ if not output:
77
+ return "[Process finished with no output]"
78
+
79
  return output
80
+ except subprocess.TimeoutExpired:
81
+ return "❌ Error: Script execution timed out (limit: 10s). Check for infinite loops."
82
  except Exception as e:
83
  return f"Execution failed: {str(e)}"
84
 
85
+ # --- 4. New Resource: Static Knowledge ---
86
+ # The agent can "read" this resource URL to get its core curriculum
87
+ @mcp.resource("tutor://curriculum")
88
+ def get_curriculum() -> str:
89
+ """Returns the standard teaching curriculum for this session."""
90
+ return """
91
+ # Python Vibe Coding Curriculum
92
+ 1. Basics: Variables, Loops, functions.
93
+ 2. Data: Lists, Dictionaries, Sets.
94
+ 3. Advanced: Decorators, Generators, Context Managers.
95
+ 4. Data Science: Pandas, Matplotlib.
96
+
97
+ TEACHING STYLE:
98
+ - Don't just explain. CODING IS DOING.
99
+ - Always create a file, run it, and show the output.
100
+ - If the user is wrong, be encouraging but precise.
101
+ """
102
+
103
  if __name__ == "__main__":
 
104
  mcp.run()