fix: propagate x-api-key and authorization headers to nested MCP calls - #12541
Conversation
fixes langflow-ai#12529) When Langflow is used as an MCP server containing flows with nested MCP components, authentication headers like x-api-key were silently dropped because extract_global_variables_from_headers() only captured headers with the X-LANGFLOW-GLOBAL-VAR-* prefix. Add _AUTH_HEADERS_TO_PROPAGATE to also capture x-api-key and authorization under their lowercase header names. These values are stored in the request_variables context, making them available for resolution in nested MCP server configs. Users can now reference them in their server headers config as {x-api-key: x-api-key} to propagate the incoming key.
|
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
WalkthroughThe Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Important Pre-merge checks failedPlease resolve all errors before merging. Addressing warnings is optional. ❌ Failed checks (1 error, 2 warnings, 1 inconclusive)
✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
erichare
left a comment
There was a problem hiding this comment.
Thanks for the fix — this is much needed and much appreciated 😄, but a few things to address before merging:
1. No tests. The repo already has homes for this (tests/unit/api/v1/test_mcp_projects.py, test_mcp_utils.py). Please add a unit test covering:
x-api-key/authorizationare extracted under their lowercase namesX-LANGFLOW-GLOBAL-VAR-*extraction still works (regression guard)- Case-insensitive matching (
X-Api-Key,AUTHORIZATION) - Both categories present together
2. Broader blast radius than MCP. extract_global_variables_from_headers is also called from api/v1/endpoints.py, api/v1/openai_responses.py, and api/v2/workflow.py. On those routes, x-api-key is Langflow's own auth key (see mcp_projects.py:171). After this change it lands in request_variables for every flow run, and any component that reads request_variables from graph context can see it. Either scope the auth-header extraction to MCP call sites only, or add a clear security note in the docstring.
The duplicate function in core.py referenced undefined names (uuid, session_scope) and shadowed the canonical implementation in flow_utils.py, which is the one re-exported from __init__.py and has the fuller signature including authenticated_user_id. Removing the dead copy resolves the F821 ruff errors.
…hub.com:octo-patch/langflow into fix/issue-12529-propagate-api-key-to-nested-mcp
erichare
left a comment
There was a problem hiding this comment.
Added tests. LGTM now, thanks @octo-patch !
3ea28ed
#12541) * fix: propagate x-api-key and authorization headers to nested MCP calls (fixes #12529) When Langflow is used as an MCP server containing flows with nested MCP components, authentication headers like x-api-key were silently dropped because extract_global_variables_from_headers() only captured headers with the X-LANGFLOW-GLOBAL-VAR-* prefix. Add _AUTH_HEADERS_TO_PROPAGATE to also capture x-api-key and authorization under their lowercase header names. These values are stored in the request_variables context, making them available for resolution in nested MCP server configs. Users can now reference them in their server headers config as {x-api-key: x-api-key} to propagate the incoming key. * fix: remove duplicate verify_public_flow_and_get_user from core.py The duplicate function in core.py referenced undefined names (uuid, session_scope) and shadowed the canonical implementation in flow_utils.py, which is the one re-exported from __init__.py and has the fuller signature including authenticated_user_id. Removing the dead copy resolves the F821 ruff errors. * Tighten scope of fix * Update .secrets.baseline * Clean up test locations * Update .secrets.baseline --------- Co-authored-by: Eric Hare <ericrhare@gmail.com>
Fixes #12529
Problem
When Langflow is used as an MCP server containing flows with nested MCP components, authentication headers like
x-api-keysent by the MCP client were silently dropped.extract_global_variables_from_headers()inapi/utils/core.pyonly captured headers with theX-LANGFLOW-GLOBAL-VAR-*prefix, so standard auth headers never made it into therequest_variablescontext.Solution
Add a
_AUTH_HEADERS_TO_PROPAGATEconstant (frozensetof"x-api-key"and"authorization") and extendextract_global_variables_from_headers()to also store these headers under their lowercase names when present.With this change the full propagation chain works:
x-api-key: <key>to Langflow MCP serverextract_global_variables_from_headerscaptures it →request_variables = {"x-api-key": "<key>"}current_request_variables_ctxstores the value and it flows intoself.graph.contextrequest_variables = self.graph.context.get("request_variables")_resolve_global_variables_in_headersresolves{"x-api-key": "x-api-key"}→{"x-api-key": "<key>"}Users configure nested server headers as
{"x-api-key": "x-api-key"}to opt-in to propagation — existing behaviour is unchanged.Testing
x-api-keyis now present inrequest_variablesafter extractionX-LANGFLOW-GLOBAL-VAR-*extraction still works as beforeSummary by CodeRabbit
x-api-keyandauthorization) alongside existing custom headers.