Skip to content

Commit 68bc19c

Browse files
IvanBirukclaude
andcommitted
Implement proper Bearer token authentication using FastMCP dependencies
- Use get_http_headers() from fastmcp.server.dependencies for HTTP header access - Support both Bearer token authentication and environment variable fallback - Tested locally: Bearer tokens now work correctly in HTTP mode - Maintains backward compatibility with STDIO mode 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 1a02b36 commit 68bc19c

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

src/codealive_mcp_server.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
# Import FastMCP components
2222
from fastmcp import Context, FastMCP
23+
from fastmcp.server.dependencies import get_http_headers
2324
from starlette.requests import Request
2425
from starlette.responses import JSONResponse
2526
import datetime
@@ -33,26 +34,29 @@ class CodeAliveContext:
3334

3435
def get_api_key_from_context(ctx: Context) -> str:
3536
"""Extract API key based on transport mode"""
36-
# Try to detect HTTP mode by checking if we're in a web context
37-
# In HTTP mode, we'll use a fixed API key for now (passed via environment)
38-
# This is a temporary fix until we understand FastMCP HTTP context better
39-
40-
# Check if we have any HTTP-like context attributes
41-
http_mode = (
42-
hasattr(ctx, 'request') or
43-
hasattr(ctx, 'session') or
44-
os.environ.get("TRANSPORT_MODE") == "http"
45-
)
46-
47-
if http_mode:
48-
# HTTP mode - for now, use environment variable as fallback
49-
# TODO: Figure out proper FastMCP HTTP header access
50-
api_key = os.environ.get("CODEALIVE_HTTP_API_KEY") or os.environ.get("CODEALIVE_API_KEY", "")
51-
if not api_key:
52-
raise ValueError("HTTP mode: CODEALIVE_HTTP_API_KEY environment variable required")
53-
return api_key
54-
else:
55-
# STDIO mode - use environment variable
37+
# Try to get HTTP headers safely using FastMCP dependency function
38+
try:
39+
headers = get_http_headers()
40+
auth_header = headers.get("authorization", "")
41+
42+
if auth_header and auth_header.startswith("Bearer "):
43+
# HTTP mode with Bearer token
44+
return auth_header[7:] # Remove "Bearer " prefix
45+
elif headers:
46+
# HTTP mode but no/invalid Authorization header
47+
# Fall back to environment variable for containerized deployment
48+
api_key = os.environ.get("CODEALIVE_HTTP_API_KEY") or os.environ.get("CODEALIVE_API_KEY", "")
49+
if not api_key:
50+
raise ValueError("HTTP mode: Authorization: Bearer <api-key> header required or CODEALIVE_API_KEY environment variable")
51+
return api_key
52+
else:
53+
# STDIO mode - no HTTP headers available
54+
api_key = os.environ.get("CODEALIVE_API_KEY", "")
55+
if not api_key:
56+
raise ValueError("STDIO mode: CODEALIVE_API_KEY environment variable required")
57+
return api_key
58+
except Exception:
59+
# Fallback to STDIO mode if header access fails
5660
api_key = os.environ.get("CODEALIVE_API_KEY", "")
5761
if not api_key:
5862
raise ValueError("STDIO mode: CODEALIVE_API_KEY environment variable required")

0 commit comments

Comments
 (0)