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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -13
app.py CHANGED
@@ -1,6 +1,6 @@
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
 
@@ -9,40 +9,44 @@ from fastapi.security.api_key import APIKeyHeader
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",
@@ -60,7 +64,7 @@ async def get_manifest():
60
  "output_schema": {"type": "object"},
61
  "http": {
62
  "method": "GET",
63
- "url": "/tools/get_purchase_orders"
64
  }
65
  }
66
  ]
@@ -73,8 +77,8 @@ async def get_manifest():
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)
 
1
  import os
2
  import requests
3
+ from fastapi import FastAPI, HTTPException, Depends
4
  from fastapi.responses import JSONResponse
5
  from fastapi.security.api_key import APIKeyHeader
6
 
 
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
+ BASE_URL = os.getenv("PUBLIC_URL", "https://<your-space>.hf.space") # Replace if testing locally
13
 
14
  app = FastAPI(
15
  title="SAP MCP Server",
16
+ description=(
17
+ "MCP-compatible FastAPI server exposing the SAP Purchase Order API tool "
18
+ "for OpenAI AgentKit (October 2025+)."
19
+ ),
20
+ version="1.2.0",
21
  )
22
 
23
  # -------------------------------------------------
24
+ # AUTHENTICATION
25
  # -------------------------------------------------
26
  api_key_header = APIKeyHeader(name="x-agentkit-api-key", auto_error=False)
27
 
28
  def verify_api_key(api_key: str = Depends(api_key_header)):
29
+ """Verifies the x-agentkit-api-key header, if configured."""
30
  if AGENTKIT_API_KEY is None:
31
+ # Open mode (no auth configured)
32
  return True
33
  if api_key != AGENTKIT_API_KEY:
34
  raise HTTPException(status_code=401, detail="Invalid or missing API key")
35
  return True
36
 
37
  # -------------------------------------------------
38
+ # MCP MANIFEST (AgentKit autodiscovery)
39
  # -------------------------------------------------
40
  @app.get("/.well-known/mcp/manifest.json", include_in_schema=False)
41
  async def get_manifest():
42
  """
43
+ Returns the MCP manifest that AgentKit uses to discover tools.
44
+ The manifest defines auth and the tool's absolute URL.
45
  """
46
  manifest = {
47
  "name": "sap_mcp_server",
48
  "description": "MCP server exposing a tool for retrieving SAP purchase orders.",
49
+ "version": "1.2.0",
50
  "auth": {
51
  "type": "api_key",
52
  "location": "header",
 
64
  "output_schema": {"type": "object"},
65
  "http": {
66
  "method": "GET",
67
+ "url": f"{BASE_URL}/tools/get_purchase_orders"
68
  }
69
  }
70
  ]
 
77
  @app.get("/tools/get_purchase_orders", tags=["MCP Tools"])
78
  async def get_purchase_orders(auth=Depends(verify_api_key)):
79
  """
80
+ Fetches SAP purchase orders via the Hugging Face backend.
81
+ Requires a valid x-agentkit-api-key header if configured.
82
  """
83
  try:
84
  resp = requests.get(HUGGINGFACE_BACKEND, timeout=30)