import os def get_tool_prefix() -> str: """Get the MCP tool prefix for HuggingFace Spaces. Gradio adds a prefix based on the Space ID. This function replicates that logic to ensure tool names match. Returns: Tool prefix string (empty on local, "space_name_" on HF Spaces). """ space_id = os.environ.get("SPACE_ID", "") if space_id: prefix = space_id.split("/")[-1] + "_" prefix = prefix.replace("-", "_") return prefix return "" def update_guide_with_tool_prefix(prefix: str, mcp_guide: str) -> str: """Apply tool prefix to MCP guide content.""" for tool_name in [ "init_game", "get_game_state", "list_available_advice", "execute_turn", ]: # Replace tool names in backticks and in function calls mcp_guide = mcp_guide.replace(f"`{tool_name}`", f"`{prefix}{tool_name}`") mcp_guide = mcp_guide.replace(f"`{tool_name}(", f"`{prefix}{tool_name}(") return mcp_guide