Update asgi_app.py
This commit is contained in:
+10
-27
@@ -126,7 +126,7 @@ async def mcp_protocol_handler(request: Request):
|
|||||||
content="Session terminated successfully"
|
content="Session terminated successfully"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Optional: Validate Clerk Bearer JWT tokens for direct API access
|
# Optional: Validate Bearer JWT tokens as secondary auth method
|
||||||
auth_header = request.headers.get("Authorization")
|
auth_header = request.headers.get("Authorization")
|
||||||
if auth_header and auth_header.startswith("Bearer "):
|
if auth_header and auth_header.startswith("Bearer "):
|
||||||
token = auth_header.split(" ")[1]
|
token = auth_header.split(" ")[1]
|
||||||
@@ -148,41 +148,24 @@ async def mcp_protocol_handler(request: Request):
|
|||||||
user_id = jwt_claims.get("sub")
|
user_id = jwt_claims.get("sub")
|
||||||
|
|
||||||
if user_id:
|
if user_id:
|
||||||
# Extract user information from JWT payload
|
logger.info(f"JWT Bearer token validated successfully for user: {user_id}")
|
||||||
user_email = jwt_claims.get('email') # Primary identifier
|
|
||||||
user_name = jwt_claims.get('name')
|
|
||||||
user_plan = jwt_claims.get('plan', 'free') # Default to free plan
|
|
||||||
scopes = jwt_claims.get('scopes', ['yargi.read'])
|
|
||||||
|
|
||||||
# Use email as primary user identifier if available
|
|
||||||
user_id = user_email or user_id
|
|
||||||
|
|
||||||
logger.info(f"Clerk JWT token validated successfully")
|
|
||||||
logger.info(f"User: {user_email}")
|
|
||||||
logger.info(f"Plan: {user_plan}")
|
|
||||||
logger.info(f"Scopes: {scopes}")
|
|
||||||
|
|
||||||
# Add user info to request state
|
# Add user info to request state
|
||||||
request.state.user_id = user_id
|
request.state.user_id = user_id
|
||||||
request.state.user_email = user_email
|
request.state.auth_method = "bearer_jwt"
|
||||||
request.state.user_name = user_name
|
|
||||||
request.state.user_plan = user_plan
|
|
||||||
request.state.token_scopes = scopes
|
|
||||||
else:
|
else:
|
||||||
logger.warning(f"Clerk JWT token validation failed - no user_id in claims")
|
logger.warning("JWT Bearer token validation failed - no user_id in claims")
|
||||||
user_id = None
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"Clerk Bearer token validation failed: {str(e)}")
|
logger.warning(f"JWT Bearer token validation failed: {str(e)}")
|
||||||
# Don't fail here - let MCP Auth Toolkit handle it
|
# Don't fail here - let MCP Auth Toolkit handle it as fallback
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Forward the request to the mounted MCP app
|
# Forward the request to the MCP app
|
||||||
async def receive():
|
async def receive():
|
||||||
return await request.receive()
|
return await request.receive()
|
||||||
|
|
||||||
# Create new scope for the mounted app
|
# Create new scope for the MCP app
|
||||||
scope = request.scope.copy()
|
scope = request.scope.copy()
|
||||||
scope["path"] = "/" # Root path for mounted app
|
scope["path"] = "/" # Root path for MCP app
|
||||||
scope["path_info"] = "/"
|
scope["path_info"] = "/"
|
||||||
|
|
||||||
# Capture the response
|
# Capture the response
|
||||||
@@ -195,7 +178,7 @@ async def mcp_protocol_handler(request: Request):
|
|||||||
elif message["type"] == "http.response.body":
|
elif message["type"] == "http.response.body":
|
||||||
response_parts["body"] += message.get("body", b"")
|
response_parts["body"] += message.get("body", b"")
|
||||||
|
|
||||||
# Call the mounted MCP app
|
# Call the MCP app directly
|
||||||
await mcp_app(scope, receive, send)
|
await mcp_app(scope, receive, send)
|
||||||
|
|
||||||
# Return the response
|
# Return the response
|
||||||
|
|||||||
Reference in New Issue
Block a user