Skip to content

[fix] mcp chat completions#19129

Merged
uc4w6c merged 4 commits intomainfrom
litellm_fix_mcp_chat_completions
Jan 15, 2026
Merged

[fix] mcp chat completions#19129
uc4w6c merged 4 commits intomainfrom
litellm_fix_mcp_chat_completions

Conversation

@uc4w6c
Copy link
Contributor

@uc4w6c uc4w6c commented Jan 15, 2026

Relevant issues

#N/A

https://www.loom.com/share/c05d94351ed94dcd8f03d3d64bce5393

Pre-Submission checklist

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

  • I have Added testing in the tests/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

CI (LiteLLM team)

  • Branch creation CI run
    Link:

  • CI run for the last commit
    Link:

  • Merge / cherry-pick CI run
    Links:

Type

🐛 Bug Fix
✅ Test

Changes

Fix MCP Chat Completions Streaming Error

Problem

When using MCP tools with /chat/completions endpoint with stream: true, the request failed with:

RuntimeError: Timeout context manager should be used inside a task
litellm.APIConnectionError: Timeout context manager should be used inside a task

Example request that failed:

bash curl -X POST http://localhost:4000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-1234" \
  -d '{
    "model": "claude-sonnet-4",
    "messages": [
      {
        "role": "user",
        "content": "use echo tool"}
    ],
    "tools": [
      {
        "type": "mcp",
        "server_url": "litellm_proxy/mcp/everything",
        "server_label": "everything",
        "require_approval": "never"
      }
    ],
    "stream": true
}'

Root Cause

The MCP handler implementation created nested event loops:
acompletion (async context)
→ run_in_executor → completion (new thread)
→ run_async_function (NEW EVENT LOOP in new thread)
→ handle_chat_completion_with_mcp
→ acompletion (ANOTHER EVENT LOOP)
→ streaming response
❌ aiohttp timeout error

This multi-layer nesting caused aiohttp's timeout context manager to fail because it couldn't find a proper asyncio task context during streaming.

The issue only appeared during streaming (not in debug mode with slower execution) due to timing-sensitive asyncio task context resolution.

Solution

Aligned MCP chat completions with the responses API pattern to eliminate event loop nesting:

After fix:
acompletion (async context)
→ run_in_executor → completion (new thread)
→ MCP check → acompletion_with_mcp() coroutine
→ await coroutine in SAME event loop
→ streaming response
✅ works correctly

Problem: When using MCP tools with  and , the following error occurred:

RuntimeError: Timeout context manager should be used inside a task
@vercel
Copy link

vercel bot commented Jan 15, 2026

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

Project Deployment Review Updated (UTC)
litellm Ready Ready Preview, Comment Jan 15, 2026 8:16am

@uc4w6c uc4w6c merged commit f541bc0 into main Jan 15, 2026
50 of 62 checks passed
@uc4w6c uc4w6c deleted the litellm_fix_mcp_chat_completions branch January 15, 2026 09:21
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