Spaces:
Sleeping
Sleeping
| """ | |
| HTTP wrapper for MCP server deployment on Hugging Face Spaces. | |
| """ | |
| from fastapi import FastAPI, Request | |
| from fastapi.responses import JSONResponse, StreamingResponse | |
| import uvicorn | |
| import os | |
| from dotenv import load_dotenv | |
| from server import mcp | |
| # Load environment variables from .env file | |
| load_dotenv() | |
| # Create FastAPI app | |
| app = FastAPI( | |
| title="Basicsearch MCP Server", | |
| description="YouTube video search MCP server" | |
| ) | |
| async def root(): | |
| """Health check endpoint""" | |
| return { | |
| "status": "healthy", | |
| "service": "basicsearch-mcp-server", | |
| "tools": ["search_youtube_videos"] | |
| } | |
| async def health(): | |
| """Health check endpoint""" | |
| return {"status": "ok"} | |
| async def debug_env(): | |
| """Debug endpoint to check if YOUTUBE_API_KEY is loaded""" | |
| api_key = os.getenv('YOUTUBE_API_KEY') | |
| return { | |
| "youtube_api_key_exists": api_key is not None, | |
| "youtube_api_key_length": len(api_key) if api_key else 0, | |
| "youtube_api_key_prefix": api_key[:10] + "..." if api_key else "NOT SET", | |
| "all_env_vars": [key for key in os.environ.keys() if 'YOUTUBE' in key or 'API' in key] | |
| } | |
| async def sse_endpoint(request: Request): | |
| """SSE endpoint for MCP protocol""" | |
| # This endpoint would handle SSE connections for MCP | |
| # For now, return a basic response | |
| return {"message": "SSE endpoint for MCP protocol"} | |
| async def list_tools(): | |
| """List available tools""" | |
| return { | |
| "tools": [ | |
| { | |
| "name": "search_youtube_videos", | |
| "description": "Searches YouTube for videos based on a query.", | |
| "parameters": { | |
| "query": "string (required)", | |
| "max_results": "integer (optional, default: 5)" | |
| } | |
| } | |
| ] | |
| } | |
| async def search_youtube(request: Request): | |
| """Direct HTTP endpoint for YouTube search""" | |
| from server import search_youtube_videos | |
| data = await request.json() | |
| query = data.get("query") | |
| max_results = data.get("max_results", 5) | |
| if not query: | |
| return JSONResponse( | |
| status_code=400, | |
| content={"error": "Query parameter is required"} | |
| ) | |
| try: | |
| results = search_youtube_videos(query, max_results) | |
| return {"results": results} | |
| except Exception as e: | |
| return JSONResponse( | |
| status_code=500, | |
| content={"error": str(e)} | |
| ) | |
| if __name__ == "__main__": | |
| port = int(os.environ.get("PORT", 7860)) | |
| uvicorn.run(app, host="0.0.0.0", port=port) | |