Spaces:
Paused
Paused
File size: 3,520 Bytes
f647629 d1338b0 f647629 ceeb737 d1338b0 ceeb737 d1338b0 561151f d1338b0 561151f f647629 561151f ceeb737 d1338b0 561151f d1338b0 561151f d1338b0 561151f d1338b0 561151f d1338b0 561151f d1338b0 561151f d1338b0 f647629 ceeb737 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
#!/usr/bin/env python3
"""
HuggingFace Spaces entry point for the Weights & Biases MCP Server.
This script runs the MCP server with streamable HTTP transport for HF Spaces.
"""
import os
import sys
import logging
from pathlib import Path
# Add the src directory to Python path
sys.path.insert(0, str(Path(__file__).parent / "src"))
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from mcp.server.fastmcp import FastMCP
# Import and configure the MCP server components
from wandb_mcp_server.server import (
validate_and_get_api_key,
setup_wandb_login,
configure_wandb_logging,
initialize_weave_tracing,
register_tools,
ServerMCPArgs
)
from wandb_mcp_server.utils import get_rich_logger
# Configure logging for the app
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = get_rich_logger("huggingface-spaces-app")
# Read the index.html file content
INDEX_HTML_PATH = Path(__file__).parent / "index.html"
with open(INDEX_HTML_PATH, "r") as f:
INDEX_HTML_CONTENT = f.read()
def main():
"""Main entry point for HuggingFace Spaces."""
logger.info("Starting Weights & Biases MCP Server on HuggingFace Spaces")
# Configure W&B logging behavior
configure_wandb_logging()
# Create minimal args for HF Spaces (always use HTTP transport)
args = ServerMCPArgs(
transport="http",
host="0.0.0.0",
port=7860,
wandb_api_key=os.environ.get("WANDB_API_KEY")
)
# Validate and get API key
try:
api_key = validate_and_get_api_key(args)
# Perform W&B login
setup_wandb_login(api_key)
# Initialize Weave tracing for MCP tool calls
weave_initialized = initialize_weave_tracing()
logger.info("W&B API configured successfully")
logger.info(f"Weave tracing: {'Enabled' if weave_initialized else 'Disabled'}")
except ValueError as e:
logger.warning(f"W&B API key not configured: {e}")
logger.warning("MCP server will start but operations will fail without a valid API key")
logger.warning("Please set WANDB_API_KEY in the Space's environment variables")
# Create the MCP server
logger.info("Creating MCP server for HTTP transport")
mcp = FastMCP(
"wandb-mcp-server",
host="0.0.0.0",
port=7860,
stateless_http=True
)
# Register all W&B tools
register_tools(mcp)
# Add custom routes to the MCP server
# FastMCP creates routes using decorators, we can add our own
@mcp.get("/")
async def index():
"""Serve the landing page."""
return HTMLResponse(content=INDEX_HTML_CONTENT)
@mcp.get("/health")
async def health():
"""Health check endpoint."""
wandb_configured = bool(os.environ.get("WANDB_API_KEY"))
return {
"status": "healthy",
"service": "wandb-mcp-server",
"wandb_configured": wandb_configured
}
logger.info("Landing page available at: /")
logger.info("Health check available at: /health")
logger.info("MCP endpoint (Streamable HTTP) available at: /mcp")
logger.info(f"Starting server on 0.0.0.0:7860")
# Run the MCP server with streamable-http transport
# This will start the uvicorn server internally
mcp.run(transport="streamable-http")
if __name__ == "__main__":
main() |