Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,25 +8,26 @@ app = FastAPI()
|
|
| 8 |
# MCP server initialization
|
| 9 |
mcp = FastMCP(name="SAP MCP Proxy", version="0.1")
|
| 10 |
|
| 11 |
-
# API key for MCP authentication,
|
| 12 |
-
API_GATEWAY_KEY = os.getenv("API_GATEWAY_KEY")
|
| 13 |
|
| 14 |
-
#
|
| 15 |
@app.middleware("http")
|
| 16 |
async def check_api_key(request: Request, call_next):
|
| 17 |
-
|
| 18 |
-
|
| 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 |
-
# MCP
|
| 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")
|
| 30 |
if not api_key:
|
| 31 |
return {"error": "SAP API_KEY not configured"}
|
| 32 |
url = "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_SUPPLIERINVOICE_PROCESS_SRV/A_BR_SupplierInvoiceNFDocument?$top=50&$inlinecount=allpages"
|
|
@@ -41,10 +42,12 @@ async def get_supplier_invoices():
|
|
| 41 |
else:
|
| 42 |
return {"error": resp.status_code, "body": resp.text}
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
# (No need to call attach_to_app for latest fastmcp. MCP tools auto-registered.)
|
| 46 |
-
|
| 47 |
-
# Health check/root endpoint
|
| 48 |
@app.get("/")
|
| 49 |
def root():
|
| 50 |
return {"message": "SAP MCP Proxy is running"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
# MCP server initialization
|
| 9 |
mcp = FastMCP(name="SAP MCP Proxy", version="0.1")
|
| 10 |
|
| 11 |
+
# API key for MCP authentication, set as secret in Hugging Face
|
| 12 |
+
API_GATEWAY_KEY = os.getenv("API_GATEWAY_KEY")
|
| 13 |
|
| 14 |
+
# Middleware to require API key except on root and docs for health and docs access
|
| 15 |
@app.middleware("http")
|
| 16 |
async def check_api_key(request: Request, call_next):
|
| 17 |
+
# Allow unauthenticated access to these paths
|
| 18 |
+
if request.url.path in ["/", "/docs", "/redoc", "/routes"]:
|
| 19 |
return await call_next(request)
|
| 20 |
token = request.headers.get("X-API-Key")
|
| 21 |
+
print(f"Received X-API-Key header: {token}") # Debug log
|
| 22 |
if not token or token != API_GATEWAY_KEY:
|
| 23 |
raise HTTPException(status_code=401, detail="Invalid or missing API key")
|
| 24 |
response = await call_next(request)
|
| 25 |
return response
|
| 26 |
|
| 27 |
+
# MCP Tool definition to wrap SAP invoice API
|
| 28 |
@mcp.tool(description="Fetch 50 supplier invoices from SAP S/4HANA OData API")
|
| 29 |
async def get_supplier_invoices():
|
| 30 |
+
api_key = os.getenv("API_KEY")
|
| 31 |
if not api_key:
|
| 32 |
return {"error": "SAP API_KEY not configured"}
|
| 33 |
url = "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_SUPPLIERINVOICE_PROCESS_SRV/A_BR_SupplierInvoiceNFDocument?$top=50&$inlinecount=allpages"
|
|
|
|
| 42 |
else:
|
| 43 |
return {"error": resp.status_code, "body": resp.text}
|
| 44 |
|
| 45 |
+
# Root endpoint to verify server running
|
|
|
|
|
|
|
|
|
|
| 46 |
@app.get("/")
|
| 47 |
def root():
|
| 48 |
return {"message": "SAP MCP Proxy is running"}
|
| 49 |
+
|
| 50 |
+
# Debug route to list all registered routes (for discovering MCP endpoint)
|
| 51 |
+
@app.get("/routes")
|
| 52 |
+
def get_routes():
|
| 53 |
+
return [route.path for route in app.routes]
|