PD03 commited on
Commit
b34e108
·
verified ·
1 Parent(s): 9e402bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -16
app.py CHANGED
@@ -3,27 +3,18 @@ import httpx
3
  from fastapi import FastAPI, Request, HTTPException
4
  from fastmcp import FastMCP
5
 
6
- app = FastAPI()
7
-
8
  mcp = FastMCP(name="SAP MCP Proxy", version="0.1")
9
 
10
  API_GATEWAY_KEY = os.getenv("API_GATEWAY_KEY")
11
 
12
- # Middleware applied on FastAPI app
13
- @app.middleware("http")
14
- async def check_api_key(request: Request, call_next):
15
- if request.url.path in ["/", "/docs", "/redoc"]:
16
- return await call_next(request)
17
- token = request.headers.get("X-API-Key")
18
- if token != API_GATEWAY_KEY:
19
- raise HTTPException(status_code=401, detail="Invalid or missing API key")
20
- return await call_next(request)
21
-
22
- @mcp.tool(description="Fetch 50 supplier invoices from SAP API")
23
  async def get_supplier_invoices():
24
  api_key = os.getenv("API_KEY")
25
  if not api_key:
26
- return {"error": "API_KEY not configured"}
 
27
  url = "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_SUPPLIERINVOICE_PROCESS_SRV/A_BR_SupplierInvoiceNFDocument?$top=50&$inlinecount=allpages"
28
  headers = {
29
  "APIKey": api_key,
@@ -36,13 +27,33 @@ async def get_supplier_invoices():
36
  else:
37
  return {"error": resp.status_code, "body": resp.text}
38
 
39
- # mount MCP routes on regular FastAPI app
40
- mcp.mount(app)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
 
42
  @app.get("/")
43
  def root():
44
  return {"message": "SAP MCP Proxy is running"}
45
 
 
46
  @app.get("/routes")
47
  def get_routes():
48
  return [route.path for route in app.routes]
 
3
  from fastapi import FastAPI, Request, HTTPException
4
  from fastmcp import FastMCP
5
 
6
+ # Create the MCP server (FastMCP creates its own FastAPI app internally)
 
7
  mcp = FastMCP(name="SAP MCP Proxy", version="0.1")
8
 
9
  API_GATEWAY_KEY = os.getenv("API_GATEWAY_KEY")
10
 
11
+ # Define the MCP tool
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")
15
  if not api_key:
16
+ return {"error": "SAP API_KEY not configured"}
17
+
18
  url = "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_SUPPLIERINVOICE_PROCESS_SRV/A_BR_SupplierInvoiceNFDocument?$top=50&$inlinecount=allpages"
19
  headers = {
20
  "APIKey": api_key,
 
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 to list routes
57
  @app.get("/routes")
58
  def get_routes():
59
  return [route.path for route in app.routes]