PD03 commited on
Commit
1d99038
·
verified ·
1 Parent(s): 2393a75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -17
app.py CHANGED
@@ -1,37 +1,61 @@
1
  import os
2
  import requests
3
- from fastapi import FastAPI, HTTPException
4
  from fastapi.responses import JSONResponse
 
5
 
6
  # -------------------------------------------------
7
  # CONFIGURATION
8
  # -------------------------------------------------
9
  HUGGINGFACE_BACKEND = os.getenv("SAP_BACKEND_URL", "https://pd03-agentkit.hf.space/purchase-orders")
 
10
 
11
  app = FastAPI(
12
  title="SAP MCP Server",
13
- description=(
14
- "Minimal MCP-compatible FastAPI server that exposes a tool to fetch SAP "
15
- "purchase orders from the Hugging Face backend."
16
- ),
17
- version="1.0.0",
18
  )
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  # -------------------------------------------------
21
  # MCP Manifest (for AgentKit autodiscovery)
22
  # -------------------------------------------------
23
  @app.get("/.well-known/mcp/manifest.json", include_in_schema=False)
24
  async def get_manifest():
25
- """Returns MCP manifest so AgentKit can auto-discover the tool."""
 
 
 
26
  manifest = {
27
  "name": "sap_mcp_server",
28
  "description": "MCP server exposing a tool for retrieving SAP purchase orders.",
29
- "version": "1.0.0",
 
 
 
 
 
 
30
  "tools": [
31
  {
32
  "name": "get_purchase_orders",
33
- "description": "Fetches the top 50 purchase orders from the SAP Sandbox API "
34
- "via the Hugging Face backend.",
 
 
35
  "input_schema": {"type": "object", "properties": {}},
36
  "output_schema": {"type": "object"},
37
  "http": {
@@ -43,15 +67,14 @@ async def get_manifest():
43
  }
44
  return JSONResponse(content=manifest)
45
 
46
-
47
  # -------------------------------------------------
48
- # Tool Endpoint
49
  # -------------------------------------------------
50
  @app.get("/tools/get_purchase_orders", tags=["MCP Tools"])
51
- async def get_purchase_orders():
52
  """
53
- Fetch the top 50 SAP purchase orders from your Hugging Face backend.
54
- This endpoint is exposed as an MCP tool for AgentKit.
55
  """
56
  try:
57
  resp = requests.get(HUGGINGFACE_BACKEND, timeout=30)
@@ -60,9 +83,8 @@ async def get_purchase_orders():
60
  except requests.exceptions.RequestException as e:
61
  raise HTTPException(status_code=500, detail=f"Failed to call backend: {e}")
62
 
63
-
64
  # -------------------------------------------------
65
- # Health Route
66
  # -------------------------------------------------
67
  @app.get("/health", tags=["System"])
68
  async def health():
 
1
  import os
2
  import requests
3
+ from fastapi import FastAPI, HTTPException, Header, Depends
4
  from fastapi.responses import JSONResponse
5
+ from fastapi.security.api_key import APIKeyHeader
6
 
7
  # -------------------------------------------------
8
  # CONFIGURATION
9
  # -------------------------------------------------
10
  HUGGINGFACE_BACKEND = os.getenv("SAP_BACKEND_URL", "https://pd03-agentkit.hf.space/purchase-orders")
11
+ AGENTKIT_API_KEY = os.getenv("AGENTKIT_API_KEY", None)
12
 
13
  app = FastAPI(
14
  title="SAP MCP Server",
15
+ description="MCP-compatible server exposing SAP purchase order API for AgentKit.",
16
+ version="1.1.0",
 
 
 
17
  )
18
 
19
+ # -------------------------------------------------
20
+ # AUTH SETUP
21
+ # -------------------------------------------------
22
+ api_key_header = APIKeyHeader(name="x-agentkit-api-key", auto_error=False)
23
+
24
+ def verify_api_key(api_key: str = Depends(api_key_header)):
25
+ """Check for valid API key in request headers."""
26
+ if AGENTKIT_API_KEY is None:
27
+ # Running in open mode
28
+ return True
29
+ if api_key != AGENTKIT_API_KEY:
30
+ raise HTTPException(status_code=401, detail="Invalid or missing API key")
31
+ return True
32
+
33
  # -------------------------------------------------
34
  # MCP Manifest (for AgentKit autodiscovery)
35
  # -------------------------------------------------
36
  @app.get("/.well-known/mcp/manifest.json", include_in_schema=False)
37
  async def get_manifest():
38
+ """
39
+ Returns the MCP manifest so AgentKit can auto-discover the tool.
40
+ Includes auth metadata for API key header usage.
41
+ """
42
  manifest = {
43
  "name": "sap_mcp_server",
44
  "description": "MCP server exposing a tool for retrieving SAP purchase orders.",
45
+ "version": "1.1.0",
46
+ "auth": {
47
+ "type": "api_key",
48
+ "location": "header",
49
+ "header_name": "x-agentkit-api-key",
50
+ "description": "Custom header used to authenticate MCP requests."
51
+ },
52
  "tools": [
53
  {
54
  "name": "get_purchase_orders",
55
+ "description": (
56
+ "Fetches the top 50 purchase orders from the SAP Sandbox API "
57
+ "via the Hugging Face backend."
58
+ ),
59
  "input_schema": {"type": "object", "properties": {}},
60
  "output_schema": {"type": "object"},
61
  "http": {
 
67
  }
68
  return JSONResponse(content=manifest)
69
 
 
70
  # -------------------------------------------------
71
+ # TOOL ENDPOINT
72
  # -------------------------------------------------
73
  @app.get("/tools/get_purchase_orders", tags=["MCP Tools"])
74
+ async def get_purchase_orders(auth=Depends(verify_api_key)):
75
  """
76
+ Fetch the top 50 SAP purchase orders from the backend.
77
+ Requires a valid AgentKit API key (if configured).
78
  """
79
  try:
80
  resp = requests.get(HUGGINGFACE_BACKEND, timeout=30)
 
83
  except requests.exceptions.RequestException as e:
84
  raise HTTPException(status_code=500, detail=f"Failed to call backend: {e}")
85
 
 
86
  # -------------------------------------------------
87
+ # HEALTH CHECK
88
  # -------------------------------------------------
89
  @app.get("/health", tags=["System"])
90
  async def health():