Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
-
from fastapi import FastAPI, HTTPException,
|
| 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=
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
# -------------------------------------------------
|
| 20 |
-
#
|
| 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 |
-
"""
|
| 26 |
if AGENTKIT_API_KEY is None:
|
| 27 |
-
#
|
| 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
|
| 35 |
# -------------------------------------------------
|
| 36 |
@app.get("/.well-known/mcp/manifest.json", include_in_schema=False)
|
| 37 |
async def get_manifest():
|
| 38 |
"""
|
| 39 |
-
Returns the MCP manifest
|
| 40 |
-
|
| 41 |
"""
|
| 42 |
manifest = {
|
| 43 |
"name": "sap_mcp_server",
|
| 44 |
"description": "MCP server exposing a tool for retrieving SAP purchase orders.",
|
| 45 |
-
"version": "1.
|
| 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 |
-
|
| 77 |
-
Requires a valid
|
| 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)
|