Spaces:
Sleeping
Sleeping
Ronio Jerico Roque commited on
Commit ·
e94f698
1
Parent(s): b99cd1e
Add health check endpoint and echo tool functionality
Browse files123456789101112131415161718import osfrom fastapi import FastAPIapp = FastAPI()@app.get("/")async def read_root():return {"message": "Hello from the root!"}@app.get("/test")async def read_test():return {"message": "Hello from the test endpoint!"}if __name__ == "__main__":import uvicorn# Use the PORT environment variable provided by Hugging Face, default to 7860port = int(os.environ.get("PORT", "7860"))uvicorn.run(app, host="0.0.0.0", port=port)123456789101112131415161718192021222324252627282930313233343536373839404142434445import osfrom fastapi import FastAPI, HTTPExceptionfrom fastapi_mcp import FastApiMCPfrom pydantic import BaseModelfrom typing import Optional# Create a FastAPI instanceapp = FastAPI()# --- Keep this root endpoint for health checks and basic access ---@app.get("/")async def read_root():return {"message": "Server is running and healthy!"}class ToolInput(BaseModel):text: stroperation: strclass ToolOutput(BaseModel):result: str@app.post("/tools/echo", operation_id="echo_tool")async def echo_tool(input_data: ToolInput) -> ToolOutput:"""A simple echo tool that returns the input text with a prefix"""try:# Process the inputresult = 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 endpointmcp = FastApiMCP(app,include_operations=["echo_tool"])mcp.mount()if __name__ == "__main__":import uvicorn# Use the PORT environment variable provided by Hugging Face, default to 7860port = int(os.environ.get("PORT", "7860"))uvicorn.run(app, host="0.0.0.0", port=port)
app.py+33-6