PD03 commited on
Commit
a556d91
·
verified ·
1 Parent(s): 8f0fdfb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -36
app.py CHANGED
@@ -1,53 +1,62 @@
1
  import os
2
  import httpx
 
 
 
 
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")
30
- if not api_key:
31
- return {"error": "SAP API_KEY not configured"}
32
-
33
- url = "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_SUPPLIERINVOICE_PROCESS_SRV/A_BR_SupplierInvoiceNFDocument?$top=50&$inlinecount=allpages"
34
- headers = {
35
- "APIKey": api_key,
36
- "Accept": "application/json"
37
- }
38
- async with httpx.AsyncClient() as client:
39
- resp = await client.get(url, headers=headers)
40
- if resp.status_code == 200:
41
- return resp.json()
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]
 
1
  import os
2
  import httpx
3
+ import asyncio
4
+ from mcp.server.fastapi import FastMCPServer
5
+ from mcp.server import Server
6
+ from mcp.types import Tool, TextContent
7
  from fastapi import FastAPI, Request, HTTPException
 
8
 
9
+ # Initialize MCP Server
10
+ mcp_server = Server("sap-mcp-proxy")
11
+
12
+ # SAP API helper
13
+ async def fetch_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 = {"APIKey": api_key, "Accept": "application/json"}
20
+
21
+ async with httpx.AsyncClient() as client:
22
+ resp = await client.get(url, headers=headers)
23
+ return resp.json() if resp.status_code == 200 else {"error": resp.status_code}
24
+
25
+ # Register MCP tool
26
+ @mcp_server.list_tools()
27
+ async def list_tools():
28
+ return [
29
+ Tool(
30
+ name="get_supplier_invoices",
31
+ description="Fetch 50 supplier invoices from SAP S/4HANA OData API",
32
+ inputSchema={"type": "object", "properties": {}, "required": []}
33
+ )
34
+ ]
35
 
36
+ @mcp_server.call_tool()
37
+ async def call_tool(name: str, arguments: dict):
38
+ if name == "get_supplier_invoices":
39
+ result = await fetch_supplier_invoices()
40
+ return [TextContent(type="text", text=str(result))]
41
+ raise ValueError(f"Unknown tool: {name}")
42
 
43
+ # Create FastAPI app with MCP endpoints
44
+ app = FastAPI()
45
+ mcp_app = FastMCPServer(mcp_server, path="/mcp")
46
+ app.mount("/mcp", mcp_app)
47
+
48
+ # Auth middleware
49
  API_GATEWAY_KEY = os.getenv("API_GATEWAY_KEY")
50
 
 
51
  @app.middleware("http")
52
  async def check_api_key(request: Request, call_next):
53
+ if request.url.path in ["/", "/docs"]:
 
54
  return await call_next(request)
55
  token = request.headers.get("X-API-Key")
56
  if not token or token != API_GATEWAY_KEY:
57
  raise HTTPException(status_code=401, detail="Invalid or missing API key")
58
+ return await call_next(request)
 
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  @app.get("/")
61
  def root():
62
+ return {"message": "SAP MCP Proxy is running", "mcp_endpoint": "/mcp"}