Spaces:
Sleeping
Sleeping
Ronio Jerico Roque commited on
Commit ·
e94f698
1
Parent(s): b99cd1e
Add health check endpoint and echo tool functionality
Browse files1223456789101112813141011121516171819202122232425262728293031323334353637383940414243174445import osfrom fastapi import FastAPIfrom 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": "Hello from the root!"} return {"message": "Server is running and healthy!"}
@app.get("/test")async def read_test(): return {"message": "Hello from the test endpoint!"}class ToolInput(BaseModel): text: str operation: str
class 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 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 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 7860 port = int(os.environ.get("PORT", "7860")) port = int(os.environ.get("PORT", "7860")) uvicorn.run(app, host="0.0.0.0", port=port)app.py+33-6