Skip to content

fix(mcp): set LITELLM_MASTER_KEY env var in e2e tests - #22413

Merged
jquinter merged 1 commit into
mainfrom
fix/mcp-contextvar-propagation
Feb 28, 2026
Merged

fix(mcp): set LITELLM_MASTER_KEY env var in e2e tests#22413
jquinter merged 1 commit into
mainfrom
fix/mcp-contextvar-propagation

Conversation

@jquinter

Copy link
Copy Markdown
Contributor

Summary

  • Fixes MCP e2e test failures where all tests fail with "User not allowed to call this tool"
  • Root cause: The FastAPI lifespan event (proxy_startup_event) re-reads master_key from the LITELLM_MASTER_KEY env var, overriding whatever initialize() set from the YAML config. Without this env var set, master_key becomes None, causing all users to be treated as INTERNAL_USER with no MCP server access.
  • Fix: Set LITELLM_MASTER_KEY=sk-1234 in the session-scoped test fixture so the lifespan event preserves the master key.

Root Cause Analysis

  1. _initialize_proxy() calls initialize(config=config_path) which sets master_key = "sk-1234" from the YAML config
  2. uvicorn.Server.serve() triggers the FastAPI lifespan handler proxy_startup_event
  3. proxy_startup_event at line 768 does master_key = get_secret_str("LITELLM_MASTER_KEY") — since the env var isn't set, this returns None, wiping out the previously configured master key
  4. All subsequent requests see master_key is None → auth returns user_role=INTERNAL_USERget_allowed_mcp_servers() returns [] → 403 error

Test plan

  • All 4 MCP e2e tests pass locally (pytest tests/mcp_tests/test_proxy_mcp_e2e.py)

Closes #22330

🤖 Generated with Claude Code

…span reset

The FastAPI lifespan event (proxy_startup_event) re-reads master_key
from the LITELLM_MASTER_KEY env var, overriding whatever initialize()
set from the YAML config. Without this env var, master_key becomes None,
causing all users to be treated as INTERNAL_USER with no MCP server
access — resulting in "User not allowed to call this tool" errors.

Closes #22330

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@vercel

vercel Bot commented Feb 28, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Building Building Preview, Comment Feb 28, 2026 5:49pm

Request Review

@greptile-apps

greptile-apps Bot commented Feb 28, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Fixes MCP e2e test failures by setting the LITELLM_MASTER_KEY env var in the session-scoped test fixture. The FastAPI lifespan handler (proxy_startup_event) re-reads master_key from the env var, overriding the value set by initialize() from the YAML config. Without the env var, master_key becomes None, causing all users to be treated as INTERNAL_USER with no MCP server access.

  • Sets LITELLM_MASTER_KEY in the _clear_proxy_database_env fixture, matching the value in the test YAML config and the PROXY_AUTHORIZATION_HEADER constant
  • Root cause verified: proxy_server.py:768 calls get_secret_str("LITELLM_MASTER_KEY") which returns None when the env var is not set
  • No issues found in this change

Confidence Score: 5/5

  • This PR is safe to merge — it's a minimal, well-targeted test fixture fix with no production code changes.
  • The change is a single line addition to a test fixture that sets an environment variable. The value (sk-1234) is consistent with the existing YAML config and auth header. The root cause analysis is thorough and verified. No production code is modified.
  • No files require special attention.

Important Files Changed

Filename Overview
tests/mcp_tests/test_proxy_mcp_e2e.py Adds LITELLM_MASTER_KEY env var to the session-scoped fixture to prevent proxy_startup_event from resetting the master key to None. Value is consistent with YAML config and auth header constant.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Test Fixture: _clear_proxy_database_env] --> B[Set LITELLM env var]
    B --> C[initialize from YAML config]
    C --> D[uvicorn.Server.serve triggers lifespan]
    D --> E[proxy_startup_event reads env var]
    E --> F{Env var set?}
    F -->|Yes - After Fix| G[Auth credentials preserved]
    F -->|No - Before Fix| H[Auth credentials reset to None]
    G --> I[MCP requests succeed with 200]
    H --> J[All users treated as INTERNAL_USER]
    J --> K[MCP access denied with 403]
Loading

Last reviewed commit: 456ad50

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, no comments

Edit Code Review Agent Settings | Greptile

@jquinter

Copy link
Copy Markdown
Contributor Author

CI Failure Analysis: test_sse_mcp_handler_mock

The failing test tests/mcp_tests/test_mcp_server.py::test_sse_mcp_handler_mock is not related to this PR.

  • This PR only modifies tests/mcp_tests/test_proxy_mcp_e2e.py (adds 4 lines to set LITELLM_MASTER_KEY env var)
  • The failing test is in a completely different file (test_mcp_server.py)
  • The test passes on both main and this branch when run locally
  • This appears to be a flaky test (likely a race condition in the mock setup)

Retrying CI.

@jquinter
jquinter merged commit aa62923 into main Feb 28, 2026
33 of 35 checks passed
@ishaan-berri
ishaan-berri deleted the fix/mcp-contextvar-propagation branch March 26, 2026 22:29
fzowl pushed a commit to fzowl/litellm that referenced this pull request Jun 24, 2026
…agation

fix(mcp): set LITELLM_MASTER_KEY env var in e2e tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: MCP e2e tests fail — auth ContextVar not propagated into SessionManager task groups

1 participant