Ronio Jerico Roque commited on
Commit
e94f698
·
1 Parent(s): b99cd1e

Add health check endpoint and echo tool functionality

Browse files
Files changed (1) hide show
  1. app.py +33 -6
app.py CHANGED
@@ -1,18 +1,45 @@
1
  import os
2
- from fastapi import FastAPI
 
 
 
3
 
 
4
  app = FastAPI()
5
 
 
6
  @app.get("/")
7
  async def read_root():
8
- return {"message": "Hello from the root!"}
9
 
10
- @app.get("/test")
11
- async def read_test():
12
- return {"message": "Hello from the test endpoint!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  if __name__ == "__main__":
15
  import uvicorn
16
  # Use the PORT environment variable provided by Hugging Face, default to 7860
17
- port = int(os.environ.get("PORT", "7860"))
18
  uvicorn.run(app, host="0.0.0.0", port=port)
 
1
  import os
2
+ from fastapi import FastAPI, HTTPException
3
+ from fastapi_mcp import FastApiMCP
4
+ from pydantic import BaseModel
5
+ from typing import Optional
6
 
7
+ # Create a FastAPI instance
8
  app = FastAPI()
9
 
10
+ # --- Keep this root endpoint for health checks and basic access ---
11
  @app.get("/")
12
  async def read_root():
13
+ return {"message": "Server is running and healthy!"}
14
 
15
+ class ToolInput(BaseModel):
16
+ text: str
17
+ operation: str
18
+
19
+ class ToolOutput(BaseModel):
20
+ result: str
21
+
22
+ @app.post("/tools/echo", operation_id="echo_tool")
23
+ async def echo_tool(input_data: ToolInput) -> ToolOutput:
24
+ """
25
+ A simple echo tool that returns the input text with a prefix
26
+ """
27
+ try:
28
+ # Process the input
29
+ result = f"Echo: {input_data.text}"
30
+ return ToolOutput(result=result)
31
+ except Exception as e:
32
+ raise HTTPException(status_code=500, detail=str(e))
33
+
34
+ # Initialize MCP with the tool endpoint
35
+ mcp = FastApiMCP(
36
+ app,
37
+ include_operations=["echo_tool"]
38
+ )
39
+ mcp.mount()
40
 
41
  if __name__ == "__main__":
42
  import uvicorn
43
  # Use the PORT environment variable provided by Hugging Face, default to 7860
44
+ port = int(os.environ.get("PORT", "7860"))
45
  uvicorn.run(app, host="0.0.0.0", port=port)