prthm11 commited on
Commit
8c6eb1d
·
verified ·
1 Parent(s): cc9578f

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +16 -41
server.py CHANGED
@@ -1,65 +1,40 @@
1
  from fastmcp import FastMCP
2
  import logging
3
- import sys
4
- from pathlib import Path
5
  import os
6
 
7
- # Configure logging to stderr
8
- logging.basicConfig(
9
- level=logging.INFO,
10
- stream=sys.stderr,
11
- format='%(asctime)s [%(name)s] [%(levelname)s] %(message)s'
12
- )
13
- logger = logging.getLogger("MathServer")
14
-
15
- # Initialize FastMCP server
16
  mcp = FastMCP("Math-Education-Server")
17
 
18
- # Use Path for cross-platform compatibility
19
- current_dir = Path(__file__).parent
20
- MARKDOWN_FILE = current_dir / "resource" / "jemh114-min (1).md"
 
21
 
22
  def get_local_resource():
23
- """Helper to read the markdown file safely."""
24
- try:
25
- return MARKDOWN_FILE.read_text(encoding="utf-8")
26
- except FileNotFoundError:
27
- logger.error(f"Resource not found at {MARKDOWN_FILE}")
28
- return "Resource file not found."
29
 
30
  @mcp.tool()
31
  async def generate_chapter_summary(chapter_name: str) -> str:
32
- """Generate a summary of a math chapter."""
33
  raw_data = get_local_resource()
34
  return f"""
35
  <source_material>
36
  {raw_data[:4000]}
37
  </source_material>
38
-
39
  SYSTEM_INSTRUCTION: You are a Mathematics Expert.
40
- 1. Read the text inside <source_material>.
41
- 2. Create a summary with 3 bullet points for chapter: {chapter_name}
42
- 3. List all formulas found in LaTeX format.
43
- 4. Do not repeat the raw text; only provide the summary.
44
  """
45
 
46
  @mcp.tool()
47
  async def generate_quiz(chapter_name: str, difficulty: str = "medium") -> str:
48
- """Generate a quiz for a math chapter."""
49
  raw_data = get_local_resource()
50
- return (
51
- f"Here is the raw material for {chapter_name}:\n"
52
- f"{raw_data[:4000]}\n"
53
- f"Difficulty: {difficulty}\n"
54
- "Task: Create a 3-question quiz using the formulas found in the text."
55
- )
56
 
57
  if __name__ == "__main__":
58
- port = int(os.getenv("PORT", 7860))
59
- host = os.getenv("HOST", "0.0.0.0")
60
-
61
- logger.info(f"Starting FastMCP Server via SSE on {host}:{port}")
62
-
63
- # Use SSE transport for Hugging Face Spaces
64
- # FastMCP handles host validation automatically
65
- mcp.run(transport="stdio")
 
1
  from fastmcp import FastMCP
2
  import logging
 
 
3
  import os
4
 
5
+ # Initialize the server
 
 
 
 
 
 
 
 
6
  mcp = FastMCP("Math-Education-Server")
7
 
8
+ # SENIOR TIP: Use absolute paths relative to the script location
9
+ # This prevents "File Not Found" errors in Docker.
10
+ CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
11
+ MARKDOWN_FILE = os.path.join(CURRENT_DIR, "resource", "jemh114-min (1).md")
12
 
13
  def get_local_resource():
14
+ if os.path.exists(MARKDOWN_FILE):
15
+ with open(MARKDOWN_FILE, "r", encoding="utf-8") as f:
16
+ return f.read()
17
+ return "Resource file not found."
 
 
18
 
19
  @mcp.tool()
20
  async def generate_chapter_summary(chapter_name: str) -> str:
21
+ """Provides source material and instructions for a math summary."""
22
  raw_data = get_local_resource()
23
  return f"""
24
  <source_material>
25
  {raw_data[:4000]}
26
  </source_material>
 
27
  SYSTEM_INSTRUCTION: You are a Mathematics Expert.
28
+ 1. Summarize in 3 bullets.
29
+ 2. List formulas like $x = \\frac{{-b \\pm \\sqrt{{b^2-4ac}}}}{{2a}}$.
 
 
30
  """
31
 
32
  @mcp.tool()
33
  async def generate_quiz(chapter_name: str, difficulty: str = "medium") -> str:
 
34
  raw_data = get_local_resource()
35
+ return f"Material: {raw_data[:2000]}\nDifficulty: {difficulty}\nCreate a 3-question quiz."
 
 
 
 
 
36
 
37
  if __name__ == "__main__":
38
+ # Change stdio to sse for Hugging Face deployment
39
+ # HF Spaces listen on port 7860 by default
40
+ mcp.run(transport="sse")