Add API_KEY Bearer auth for MCP endpoints
This commit is contained in:
+4
-1
@@ -1,7 +1,10 @@
|
|||||||
# MCP Email Server - Docker Environment Variables
|
# MCP Email Server - Docker Environment Variables
|
||||||
|
|
||||||
|
# MCP API key (Bearer auth for /mcp endpoints)
|
||||||
|
API_KEY=
|
||||||
|
|
||||||
# Logging
|
# Logging
|
||||||
LOG_LEVEL=INFO
|
LOG_LEVEL=DEBUG
|
||||||
|
|
||||||
# IMAP (incoming mail)
|
# IMAP (incoming mail)
|
||||||
IMAP_HOST=imap.example.com
|
IMAP_HOST=imap.example.com
|
||||||
|
|||||||
@@ -1950,6 +1950,8 @@ app = FastAPI(
|
|||||||
version="1.0.0",
|
version="1.0.0",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
API_KEY = os.getenv("API_KEY", "").strip()
|
||||||
|
|
||||||
|
|
||||||
@app.exception_handler(Exception)
|
@app.exception_handler(Exception)
|
||||||
async def global_exception_handler(request, exc):
|
async def global_exception_handler(request, exc):
|
||||||
@@ -1960,6 +1962,17 @@ async def global_exception_handler(request, exc):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def require_bearer(request: Request):
|
||||||
|
if not API_KEY:
|
||||||
|
return
|
||||||
|
auth = (request.headers.get("authorization") or "").strip()
|
||||||
|
if not auth.startswith("Bearer "):
|
||||||
|
raise HTTPException(status_code=401, detail="Missing or invalid Authorization header")
|
||||||
|
token = auth[len("Bearer "):].strip()
|
||||||
|
if token != API_KEY:
|
||||||
|
raise HTTPException(status_code=401, detail="Invalid API key")
|
||||||
|
|
||||||
|
|
||||||
@app.get("/health")
|
@app.get("/health")
|
||||||
async def health_check():
|
async def health_check():
|
||||||
return {
|
return {
|
||||||
@@ -1970,7 +1983,8 @@ async def health_check():
|
|||||||
|
|
||||||
|
|
||||||
@app.get("/mcp/openapi.json")
|
@app.get("/mcp/openapi.json")
|
||||||
async def mcp_openapi_spec():
|
async def mcp_openapi_spec(request: Request):
|
||||||
|
require_bearer(request)
|
||||||
# Minimal OpenAPI 3.1 document for compatibility with clients
|
# Minimal OpenAPI 3.1 document for compatibility with clients
|
||||||
# that expect an OpenAPI spec at /mcp/openapi.json.
|
# that expect an OpenAPI spec at /mcp/openapi.json.
|
||||||
# Actual tool definitions are provided via MCP tools/list.
|
# Actual tool definitions are provided via MCP tools/list.
|
||||||
@@ -2045,6 +2059,7 @@ async def mcp_openapi_spec():
|
|||||||
|
|
||||||
@app.post("/mcp")
|
@app.post("/mcp")
|
||||||
async def mcp_endpoint(request: Request):
|
async def mcp_endpoint(request: Request):
|
||||||
|
require_bearer(request)
|
||||||
if request.method != "POST":
|
if request.method != "POST":
|
||||||
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
raise HTTPException(status_code=405, detail="Method Not Allowed")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user