Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,12 +3,27 @@ import httpx
|
|
| 3 |
from fastapi import FastAPI, Request, HTTPException
|
| 4 |
from fastmcp import FastMCP
|
| 5 |
|
| 6 |
-
# Create
|
|
|
|
|
|
|
|
|
|
| 7 |
mcp = FastMCP(name="SAP MCP Proxy", version="0.1")
|
| 8 |
|
| 9 |
API_GATEWAY_KEY = os.getenv("API_GATEWAY_KEY")
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@mcp.tool(description="Fetch 50 supplier invoices from SAP S/4HANA OData API")
|
| 13 |
async def get_supplier_invoices():
|
| 14 |
api_key = os.getenv("API_KEY")
|
|
@@ -27,33 +42,12 @@ async def get_supplier_invoices():
|
|
| 27 |
else:
|
| 28 |
return {"error": resp.status_code, "body": resp.text}
|
| 29 |
|
| 30 |
-
# Create HTTP ASGI app from MCP server for mounting
|
| 31 |
-
mcp_app = mcp.http_app(path='/mcp')
|
| 32 |
-
|
| 33 |
-
# Create a new FastAPI app and combine routes
|
| 34 |
-
app = FastAPI(
|
| 35 |
-
title="SAP MCP Proxy",
|
| 36 |
-
routes=[*mcp_app.routes], # Include MCP routes
|
| 37 |
-
lifespan=mcp_app.lifespan # Important: pass lifespan for MCP initialization
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
# Add custom middleware for authentication
|
| 41 |
-
@app.middleware("http")
|
| 42 |
-
async def check_api_key(request: Request, call_next):
|
| 43 |
-
if request.url.path in ["/", "/docs", "/redoc", "/routes"]:
|
| 44 |
-
return await call_next(request)
|
| 45 |
-
token = request.headers.get("X-API-Key")
|
| 46 |
-
if not token or token != API_GATEWAY_KEY:
|
| 47 |
-
raise HTTPException(status_code=401, detail="Invalid or missing API key")
|
| 48 |
-
response = await call_next(request)
|
| 49 |
-
return response
|
| 50 |
-
|
| 51 |
# Health check endpoint
|
| 52 |
@app.get("/")
|
| 53 |
def root():
|
| 54 |
return {"message": "SAP MCP Proxy is running"}
|
| 55 |
|
| 56 |
-
# Debug endpoint
|
| 57 |
@app.get("/routes")
|
| 58 |
def get_routes():
|
| 59 |
return [route.path for route in app.routes]
|
|
|
|
| 3 |
from fastapi import FastAPI, Request, HTTPException
|
| 4 |
from fastmcp import FastMCP
|
| 5 |
|
| 6 |
+
# Create separate FastAPI app for our endpoints
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# Create MCP server
|
| 10 |
mcp = FastMCP(name="SAP MCP Proxy", version="0.1")
|
| 11 |
|
| 12 |
API_GATEWAY_KEY = os.getenv("API_GATEWAY_KEY")
|
| 13 |
|
| 14 |
+
# Authentication middleware
|
| 15 |
+
@app.middleware("http")
|
| 16 |
+
async def check_api_key(request: Request, call_next):
|
| 17 |
+
# Allow unauthenticated access to health check and docs
|
| 18 |
+
if request.url.path in ["/", "/docs", "/redoc", "/routes"]:
|
| 19 |
+
return await call_next(request)
|
| 20 |
+
token = request.headers.get("X-API-Key")
|
| 21 |
+
if not token or token != API_GATEWAY_KEY:
|
| 22 |
+
raise HTTPException(status_code=401, detail="Invalid or missing API key")
|
| 23 |
+
response = await call_next(request)
|
| 24 |
+
return response
|
| 25 |
+
|
| 26 |
+
# Define MCP tool
|
| 27 |
@mcp.tool(description="Fetch 50 supplier invoices from SAP S/4HANA OData API")
|
| 28 |
async def get_supplier_invoices():
|
| 29 |
api_key = os.getenv("API_KEY")
|
|
|
|
| 42 |
else:
|
| 43 |
return {"error": resp.status_code, "body": resp.text}
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
# Health check endpoint
|
| 46 |
@app.get("/")
|
| 47 |
def root():
|
| 48 |
return {"message": "SAP MCP Proxy is running"}
|
| 49 |
|
| 50 |
+
# Debug endpoint
|
| 51 |
@app.get("/routes")
|
| 52 |
def get_routes():
|
| 53 |
return [route.path for route in app.routes]
|