Skip to content

fix(proxy): share temporary MCP OAuth sessions across instances via Redis - #26162

Closed
milan-berri wants to merge 6 commits into
BerriAI:litellm_internal_stagingfrom
milan-berri:litellm_temp_mcp_redis_cache
Closed

milan-berri wants to merge 6 commits into
BerriAI:litellm_internal_stagingfrom
milan-berri:litellm_temp_mcp_redis_cache

Conversation

@milan-berri

@milan-berri milan-berri commented Apr 21, 2026 •

Copy link
Copy Markdown
Contributor

Relevant issues

Fixes temporary MCP OAuth session lookup failures in multi-instance/LB proxy deployments where /v1/mcp/server/oauth/session is created on one instance and subsequent /server/oauth/{server_id}/... requests may land on another.

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/test_litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

CI (LiteLLM team)

CI status guideline:

  • 50-55 passing tests: main is stable with minor issues.
  • 45-49 passing tests: acceptable but needs attention
  • <= 40 passing tests: unstable; be careful with your merges and assess the risk.
  • Branch creation CI run
    Link:

  • CI run for the last commit
    Link:

  • Merge / cherry-pick CI run
    Links:

Screenshots / Proof of Fix

Targeted local test runs:

  • poetry run python -m pytest tests/test_litellm/proxy/management_endpoints/test_mcp_management_endpoints.py -v
    • Result: 56 passed

Focused path validation also covered temporary MCP OAuth handlers, Redis fallback logic, and encrypted Redis payload read/write paths in:

  • TestTemporaryMCPSessionEndpoints

Type

🐛 Bug Fix
✅ Test

Changes

  • Fixes multi-instance/LB temporary MCP OAuth flow by sharing temporary MCP session servers in Redis (instead of process-only memory).
  • Keeps current in-memory cache as fallback/L1; Redis is best-effort shared backing store.
  • Encrypts temporary MCP session payloads before Redis write and decrypts on Redis read using existing proxy encryption/decryption helpers.
  • Converts temporary MCP cache lookup helpers to async and updates OAuth endpoints (authorize, token, register) to await them.
  • Adds/updates unit tests for:
    • Redis fallback lookup on local-cache miss
    • Redis write key + TTL behavior
    • encrypted payload write/decrypt read paths
    • async lookup call path assertions

Share temporary MCP server session state across proxy instances via Redis with in-memory fallback, and extend MCP management endpoint tests to cover async cache lookup/write behavior.

Made-with: Cursor
@greptile-apps

greptile-apps Bot commented Apr 21, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes MCP OAuth session lookup failures in multi-instance/load-balanced proxy deployments by persisting temporary session records to Redis (encrypted) in addition to the existing process-local in-memory cache. OAuth endpoints (authorize, token, register) now await the converted-async get_cached_temporary_mcp_server, and the Redis fallback deliberately avoids L1 repopulation to prevent extending session TTL beyond what Redis would enforce.

Confidence Score: 5/5

Safe to merge; the single remaining finding is a minor P2 hardening gap in a best-effort helper.

All identified issues are P2 (style/defensive hardening). The prior P1 concerns raised in earlier reviews — TTL re-inflation and assert_called_once_with vs assert_awaited_once_with — have been fully addressed. The core logic (encryption, Redis fallback, async conversion) is correct.

The _cache_temporary_mcp_server_in_redis function in mcp_management_endpoints.py has a small unprotected code path, but it is unlikely to trigger in practice.

Important Files Changed

Filename Overview
litellm/proxy/management_endpoints/mcp_management_endpoints.py Adds Redis-backed L2 cache for temporary MCP OAuth sessions; encryption/decryption, TTL handling, and async conversion are correct, with one minor gap where model_dump/json.dumps in a best-effort helper are not wrapped.
tests/test_litellm/proxy/management_endpoints/test_mcp_management_endpoints.py New tests cover Redis write/read, encryption/decryption, TTL, and fallback paths; all async mock assertions correctly use assert_awaited_once_with.

Sequence Diagram

sequenceDiagram
    participant Client
    participant Instance1 as Proxy Instance 1
    participant Instance2 as Proxy Instance 2
    participant Redis
    participant Memory as In-Memory Cache

    Client->>Instance1: POST /v1/mcp/server/oauth/session
    Instance1->>Memory: _cache_temporary_mcp_server(server, TTL=300s)
    Instance1->>Redis: _cache_temporary_mcp_server_in_redis(encrypted, TTL=300s)
    Instance1-->>Client: 200 OK (credentials redacted)

    Client->>Instance2: GET /server/oauth/{server_id}/authorize
    Instance2->>Memory: get_cached_temporary_mcp_server(server_id) → miss
    Instance2->>Redis: _get_temporary_mcp_server_from_redis(server_id) → hit
    Note over Instance2,Redis: Decrypts payload, returns MCPServer (no L1 repopulation)
    Instance2-->>Client: Redirect to authorization URL
Loading

Reviews (6): Last reviewed commit: "fix(mcp): require encrypted string for t..." | Re-trigger Greptile

@codecov

codecov Bot commented Apr 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.95238% with 12 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...y/management_endpoints/mcp_management_endpoints.py 80.95% 12 Missing ⚠️

📢 Thoughts on this report? Let us know!

Avoid extending temporary MCP session lifetime on Redis cache hits by skipping local repopulation, and tighten async test assertion semantics with assert_awaited_once_with.

Made-with: Cursor

@veria-ai veria-ai 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.

Medium: Plaintext secrets written to Redis

This PR shares temporary MCP OAuth sessions across proxy instances by writing the full MCPServer object to Redis. The MCPServer model includes sensitive fields (client_secret, authentication_token, aws_secret_access_key, aws_session_token) that are stored as plaintext JSON in Redis after decryption.

Use existing proxy encrypt/decrypt helpers for temporary MCP session payloads in Redis, keep best-effort cache behavior on crypto failures, and add tests for encrypted write/decrypt read paths.

Made-with: Cursor
Apply Black-required line wrapping to satisfy formatting checks.

Made-with: Cursor
@milan-berri

Copy link
Copy Markdown
Contributor Author

re-opened it here #26318 with conflicts resolved and CI run

ghost pushed a commit that referenced this pull request Apr 23, 2026
…is (#26162) (#26318)

Temporary MCP OAuth sessions were kept in process-local memory, so on
multi-instance/LB proxy deployments a session created on instance A could
not be found when the follow-up /server/oauth/{server_id}/... request
landed on instance B.

Persist temporary session records to Redis (encrypted with the existing
proxy encryption helpers) as a best-effort L2 cache alongside the current
in-memory L1. Convert get_cached_temporary_mcp_server to async and await
it from the authorize/token/register OAuth endpoints.

Made-with: Cursor
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.

1 participant