Download cases/course_registration/raw/mcp_servers/server.py from JoeYing/ReScopeBench: direct link, hf CLI and curl.
- Browser
- Download file 3.3 kB
-
https://huggingface.co/datasets/JoeYing/ReScopeBench/resolve/main/cases/course_registration/raw/mcp_servers/server.py
- Command line
-
hf download hf://datasets/JoeYing/ReScopeBench/cases/course_registration/raw/mcp_servers/server.py
-
curl -L -o server.py https://huggingface.co/datasets/JoeYing/ReScopeBench/resolve/main/cases/course_registration/raw/mcp_servers/server.py
3.3 kB
| """Tool registry and dispatcher for C_11_08_49.""" | |
| from __future__ import annotations | |
| import importlib | |
| import shutil | |
| import tempfile | |
| from pathlib import Path | |
| from typing import Any, Callable, Dict, List, Optional | |
| TOOL_REGISTRY: Dict[str, Callable[..., Dict[str, Any]]] = {} | |
| _TOOL_METADATA: Dict[str, Dict[str, Any]] = {} | |
| _state_manager = None | |
| _runtime_root: Optional[Path] = None | |
| _PROJECT_ROOT = Path(__file__).resolve().parents[1] | |
| def register_tool(name, func, description, input_schema): | |
| TOOL_REGISTRY[name] = func | |
| _TOOL_METADATA[name] = {"description": description, "input_schema": input_schema} | |
| def _ensure_runtime_state_manager(): | |
| global _state_manager, _runtime_root | |
| if _state_manager is not None: | |
| return _state_manager | |
| from mcp_servers.state.state_manager import StateManager | |
| _runtime_root = Path(tempfile.mkdtemp(prefix="c_11_08_49_runtime_")) | |
| data_dir = _runtime_root / "data" | |
| shutil.copytree(_PROJECT_ROOT / "data" / "database", data_dir / "database") | |
| (data_dir / "temp").mkdir(parents=True, exist_ok=True) | |
| _state_manager = StateManager(str(data_dir)) | |
| return _state_manager | |
| def cleanup_runtime(): | |
| global _state_manager, _runtime_root | |
| _state_manager = None | |
| if _runtime_root is not None: | |
| shutil.rmtree(_runtime_root, ignore_errors=True) | |
| _runtime_root = None | |
| def call_tool(tool_name, state_manager=None, **kwargs): | |
| func = TOOL_REGISTRY.get(tool_name) | |
| if func is None: | |
| load_tools() | |
| func = TOOL_REGISTRY.get(tool_name) | |
| if func is None: | |
| return {"type": "tool_result", "tool": tool_name, "content": {"status": "error", "data": {}, "message": f"UNKNOWN_TOOL: {tool_name}"}} | |
| sm = state_manager if state_manager is not None else _ensure_runtime_state_manager() | |
| try: | |
| content = func(state_manager=sm, **kwargs) | |
| except Exception as exc: | |
| content = {"status": "error", "data": {}, "message": f"INTERNAL_ERROR: {type(exc).__name__}: {exc}"} | |
| return {"type": "tool_result", "tool": tool_name, "content": content} | |
| def get_tool_names_for_environment(): | |
| load_tools() | |
| return sorted(TOOL_REGISTRY) | |
| def get_tool_metadata(): | |
| load_tools() | |
| return dict(_TOOL_METADATA) | |
| TOOL_MODULES: List[str] = [ | |
| "mcp_servers.tools.list_students", | |
| "mcp_servers.tools.get_student_profile", | |
| "mcp_servers.tools.list_courses", | |
| "mcp_servers.tools.search_course_sections", | |
| "mcp_servers.tools.get_course_section_details", | |
| "mcp_servers.tools.list_student_registrations", | |
| "mcp_servers.tools.get_waitlist_snapshot", | |
| "mcp_servers.tools.register_student_in_section", | |
| "mcp_servers.tools.add_student_to_waitlist", | |
| "mcp_servers.tools.change_registration_grading_basis", | |
| "mcp_servers.tools.cancel_student_registration", | |
| "mcp_servers.tools.remove_student_from_waitlist", | |
| "mcp_servers.tools.approve_waitlist_promotion", | |
| "mcp_servers.tools.revise_section_capacity", | |
| "mcp_servers.tools.manage_student_advising_hold", | |
| ] | |
| STATIC_TOOL_REGISTRY: Dict[str, Callable[..., Dict[str, Any]]] = {} | |
| _TOOLS_LOADED = False | |
| def load_tools() -> None: | |
| global _TOOLS_LOADED, STATIC_TOOL_REGISTRY | |
| if _TOOLS_LOADED: | |
| return | |
| for module_name in TOOL_MODULES: | |
| importlib.import_module(module_name) | |
| STATIC_TOOL_REGISTRY = dict(TOOL_REGISTRY) | |
| _TOOLS_LOADED = True | |