Spaces:
Sleeping
Sleeping
| import os | |
| from fastapi import FastAPI, HTTPException | |
| from fastapi_mcp import FastApiMCP | |
| from pydantic import BaseModel | |
| from typing import Optional | |
| # Create a FastAPI instance | |
| app = FastAPI() | |
| # --- Keep this root endpoint for health checks and basic access --- | |
| async def read_root(): | |
| return {"message": "Server is running and healthy!"} | |
| class ToolInput(BaseModel): | |
| text: str | |
| operation: str | |
| class ToolOutput(BaseModel): | |
| result: str | |
| async def echo_tool(input_data: ToolInput) -> ToolOutput: | |
| """ | |
| A simple echo tool that returns the input text with a prefix | |
| """ | |
| try: | |
| # Process the input | |
| result = f"Echo: {input_data.text}" | |
| return ToolOutput(result=result) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| # Initialize MCP with the tool endpoint | |
| mcp = FastApiMCP( | |
| app, | |
| include_operations=["echo_tool"] | |
| ) | |
| mcp.mount() | |
| if __name__ == "__main__": | |
| import uvicorn | |
| # Use the PORT environment variable provided by Hugging Face, default to 7860 | |
| port = int(os.environ.get("PORT", "7860")) | |
| uvicorn.run(app, host="0.0.0.0", port=port) |