@@ -33,18 +33,24 @@ class CodeAliveContext:
3333
3434def get_api_key_from_context (ctx : Context ) -> str :
3535 """Extract API key based on transport mode"""
36- transport_mode = os .environ .get ("TRANSPORT_MODE" , "stdio" )
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
3739
38- if transport_mode == "http" :
39- # HTTP mode - extract from Authorization header
40- # Check if we have HTTP request context
41- if hasattr (ctx , 'request' ) and ctx .request :
42- auth_header = ctx .request .headers .get ("Authorization" , "" )
43- if not auth_header or not auth_header .startswith ("Bearer " ):
44- raise ValueError ("HTTP mode: Authorization: Bearer <api-key> header required" )
45- return auth_header [7 :] # Remove "Bearer "
46- else :
47- raise ValueError ("HTTP mode: No request context available for Authorization header" )
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
4854 else :
4955 # STDIO mode - use environment variable
5056 api_key = os .environ .get ("CODEALIVE_API_KEY" , "" )
@@ -720,19 +726,21 @@ async def search_code(
720726 sys .exit (1 )
721727 print (f"STDIO mode: Using API key from environment (ends with: ...{ api_key [- 4 :] if len (api_key ) > 4 else '****' } )" )
722728 else :
723- # HTTP mode: prohibit API key in environment
729+ # HTTP mode: allow API key in environment for AWS Fargate deployment
724730 if api_key :
725- print ("ERROR: HTTP mode detected CODEALIVE_API_KEY in environment." )
726- print ("Remove the environment variable. API keys must be provided via Authorization: Bearer headers." )
727- sys .exit (1 )
728- print ("HTTP mode: API keys will be extracted from Authorization: Bearer headers" )
731+ print ("HTTP mode: Using API key from environment for Fargate deployment" )
732+ # Set HTTP API key for the auth function
733+ os .environ ["CODEALIVE_HTTP_API_KEY" ] = api_key
734+ else :
735+ print ("HTTP mode: No environment API key found. API keys will be extracted from Authorization: Bearer headers" )
729736
730737 if not base_url :
731738 print ("WARNING: CODEALIVE_BASE_URL environment variable is not set, using default." )
732739 print ("CodeAlive will connect to the production API at https://app.codealive.ai" )
733740
734741 # Run the server with the selected transport
735742 if args .transport == "http" :
736- mcp .run (transport = "http" , host = args .host , port = args .port )
743+ # Use root path instead of /mcp/
744+ mcp .run (transport = "http" , host = args .host , port = args .port , path = "/" )
737745 else :
738746 mcp .run (transport = "stdio" )
0 commit comments