Download cases/filesystem/raw/mcp_servers/server.py from JoeYing/ReScopeBench: direct link, hf CLI and curl.
- Browser
- Download file 6.32 kB
-
https://huggingface.co/datasets/JoeYing/ReScopeBench/resolve/main/cases/filesystem/raw/mcp_servers/server.py
- Command line
-
hf download hf://datasets/JoeYing/ReScopeBench/cases/filesystem/raw/mcp_servers/server.py
-
curl -L -o server.py https://huggingface.co/datasets/JoeYing/ReScopeBench/resolve/main/cases/filesystem/raw/mcp_servers/server.py
6.32 kB
| """Tool registry and dispatcher for C_14_01_54.""" | |
| from __future__ import annotations | |
| import hashlib | |
| import importlib | |
| import json | |
| import shutil | |
| import tempfile | |
| from contextlib import contextmanager | |
| from pathlib import Path | |
| from typing import Any, Callable, Dict, List, Optional | |
| try: | |
| import fcntl | |
| except ImportError: | |
| fcntl = None | |
| TOOL_REGISTRY: Dict[str, Callable[..., Dict[str, Any]]] = {} | |
| _TOOL_METADATA: Dict[str, Dict[str, Any]] = {} | |
| _state_manager = None | |
| _runtime_root: Optional[Path] = None | |
| def register_tool(name: str, func: Callable[..., Dict[str, Any]], description: str, input_schema: Dict[str, Any]) -> None: | |
| _TOOL_METADATA[name] = {"name": name, "function": func, "description": description, "input_schema": input_schema} | |
| TOOL_REGISTRY[name] = func | |
| TOOL_MODULES: List[str] = [ | |
| "mcp_servers.tools.get_node", | |
| "mcp_servers.tools.list_directory", | |
| "mcp_servers.tools.search_nodes", | |
| "mcp_servers.tools.get_file_content", | |
| "mcp_servers.tools.get_effective_permissions", | |
| "mcp_servers.tools.list_snapshots", | |
| "mcp_servers.tools.inspect_quota", | |
| "mcp_servers.tools.create_directory", | |
| "mcp_servers.tools.create_file", | |
| "mcp_servers.tools.update_file_content", | |
| "mcp_servers.tools.update_file_by_appending_content", | |
| "mcp_servers.tools.rename_node", | |
| "mcp_servers.tools.update_node_location", | |
| "mcp_servers.tools.clone_node", | |
| "mcp_servers.tools.change_permissions", | |
| "mcp_servers.tools.remove_node", | |
| "mcp_servers.tools.archive_directory", | |
| "mcp_servers.tools.apply_snapshot_restore", | |
| "mcp_servers.tools.apply_permissions_batch", | |
| "mcp_servers.tools.update_nodes_location_batch", | |
| "mcp_servers.tools.materialize_manifest" | |
| ] | |
| STATIC_TOOL_REGISTRY: Dict[str, Callable[..., Dict[str, Any]]] = {} | |
| _TOOLS_LOADED = False | |
| def _resolve_data_root(db_path: str) -> Path: | |
| base = Path(db_path) | |
| if base.is_absolute(): | |
| return base.resolve() | |
| cwd_candidate = (Path.cwd() / base).resolve() | |
| if cwd_candidate.exists(): | |
| return cwd_candidate | |
| return (Path(__file__).resolve().parent.parent / base).resolve() | |
| def _database_init_lock(db_path: str): | |
| resolved_root = _resolve_data_root(db_path) | |
| digest = hashlib.sha256(str(resolved_root).encode("utf-8")).hexdigest()[:16] | |
| lock_dir = Path(tempfile.gettempdir()) / "c_14_01_54_locks" | |
| lock_dir.mkdir(parents=True, exist_ok=True) | |
| lock_file = (lock_dir / f"{digest}.lock").open("a+") | |
| try: | |
| if fcntl is not None: | |
| fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX) | |
| yield | |
| finally: | |
| if fcntl is not None: | |
| fcntl.flock(lock_file.fileno(), fcntl.LOCK_UN) | |
| lock_file.close() | |
| def _copy_seed_database(source_root: Path, runtime_root: Path) -> None: | |
| src = source_root / "database" | |
| runtime_db = runtime_root / "database" | |
| runtime_temp = runtime_root / "temp" | |
| if src.exists(): | |
| shutil.copytree(src, runtime_db) | |
| shutil.copytree(src, runtime_temp) | |
| else: | |
| runtime_db.mkdir(parents=True, exist_ok=True) | |
| runtime_temp.mkdir(parents=True, exist_ok=True) | |
| def cleanup_runtime() -> None: | |
| global _runtime_root, _state_manager | |
| runtime_root = _runtime_root | |
| _runtime_root = None | |
| _state_manager = None | |
| if runtime_root is not None and runtime_root.exists(): | |
| shutil.rmtree(runtime_root, ignore_errors=True) | |
| def init_database(db_path: str = "data") -> None: | |
| global _runtime_root, _state_manager | |
| source_root = _resolve_data_root(db_path) | |
| with _database_init_lock(str(source_root)): | |
| previous_runtime = _runtime_root | |
| runtime_root = Path(tempfile.mkdtemp(prefix="c_14_01_54_runtime_")) | |
| try: | |
| _copy_seed_database(source_root, runtime_root) | |
| from mcp_servers.state.state_manager import StateManager | |
| state_manager = StateManager(db_path=str(runtime_root)) | |
| state_manager.source_db_path = str(source_root) | |
| state_manager.runtime_root = str(runtime_root) | |
| _state_manager = state_manager | |
| _runtime_root = runtime_root | |
| except Exception: | |
| shutil.rmtree(runtime_root, ignore_errors=True) | |
| raise | |
| finally: | |
| if previous_runtime is not None and previous_runtime != _runtime_root: | |
| shutil.rmtree(previous_runtime, ignore_errors=True) | |
| def get_state_manager(): | |
| global _state_manager | |
| if _state_manager is None: | |
| init_database("data") | |
| return _state_manager | |
| def load_tools() -> None: | |
| global _TOOLS_LOADED | |
| if _TOOLS_LOADED: | |
| return | |
| for module_name in TOOL_MODULES: | |
| importlib.import_module(module_name) | |
| STATIC_TOOL_REGISTRY.clear() | |
| STATIC_TOOL_REGISTRY.update(TOOL_REGISTRY) | |
| _TOOLS_LOADED = True | |
| def call_tool(tool_name: str, state_manager=None, **kwargs) -> Dict[str, Any]: | |
| 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 get_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() -> List[str]: | |
| load_tools() | |
| return sorted(TOOL_REGISTRY) | |
| def get_tool_names() -> List[str]: | |
| return get_tool_names_for_environment() | |
| def get_tools_list() -> List[Dict[str, Any]]: | |
| load_tools() | |
| return [ | |
| {"name": name, "description": meta.get("description", ""), "input_schema": meta.get("input_schema", {})} | |
| for name, meta in sorted(_TOOL_METADATA.items()) | |
| ] | |
| def get_tool_info(tool_name: str) -> Optional[Dict[str, Any]]: | |
| load_tools() | |
| meta = _TOOL_METADATA.get(tool_name) | |
| if not meta: | |
| return None | |
| return {"name": tool_name, "description": meta.get("description", ""), "input_schema": meta.get("input_schema", {})} | |
| list_available_tools = get_tools_list | |
| get_env_tools = get_tool_names | |