Skip to content

MCP server session not recognized when using MCP servers along with LiteLLM proxy #2074

@anujag24

Description

@anujag24

Describe the bug

When using LiteLLM Proxy as the model in the OpenAI Agents Python SDK with an active MCPServerSse instance, the agent fails to recognize the MCP session during tool execution. The agent responds with MCPClientNotConfigured — no MCP server connection available in this session, even though the MCP server was initialized and connected successfully. This prevents any MCP tool calls from executing in the agent.

Debug information

Python==3.13.7
openai-agents[litellm]==0.4.2
openai==2.6.0
litellm[proxy]==1.79.0

Environment

LiteLLM: proxy mode, base_url=, api_key=dummy-key
Remote MCPServerSse

Repro steps

import asyncio
import logging
from agents.agent import Agent, ModelSettings
from agents.runner import Runner
from agents.extensions.models.litellm_model import LitellmModel
from agents.mcp.server import MCPServerSse

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

async def main():
    proxy_base_url = "http://localhost:4000"  # Replace with your LiteLLM proxy URL

    # Initialize MCP server
    async with MCPServerSse(
        name="MCPServer",
        params={"url": "<MCP_SERVER_URL>", "timeout": 600},
        cache_tools_list=True,
    ) as my_mcp_server:
        logger.info("🔗 MCP server context manager active")

        await my_mcp_server.connect()
        logger.info("✅ MCP server session established")

        # Create LiteLLM model
        litellm_model = LitellmModel(
            model="gpt-5",
            base_url=proxy_base_url,
            api_key="dummy-key"
        )

        # Create agent with active MCP server
        agent = Agent(
            name="data-collection-agent",
            model=litellm_model,
            mcp_servers=[my_mcp_server],
            model_settings=ModelSettings(tool_choice="required"),
            instructions="""
You are the Data Collection & Order Management Agent, specialized in gathering data using MCP Server.
- Execute MCP Tool calls for user requests.
- If MCP server is unavailable, report the server name, tool name, and error message.
"""
        )

        # Test user message
        test_messages = [{"role": "user", "content": "Show me top 10 recent sales orders"}]

        runner = Runner()
        logger.info("🚀 Running agent...")

        result = runner.run_streamed(agent, test_messages)

        # Print events
        async for event in result:
            print("Event:", event)
            if getattr(event, "tool_name", None):
                print(f"Tool call triggered: {event.tool_name}")

        logger.info("✅ Agent execution completed")

if __name__ == "__main__":
    asyncio.run(main())

Expected behavior

  • Agent should recognize that my_mcp_server is active.
  • MCP tool calls should execute successfully.
  • Agent should return the MCP tool output or a valid error from the MCP server itself, not a “no active MCP session” error.

Actual behavior

  • Agent fails to execute MCP tool calls.
  • Agent logs:
RunItemStreamEvent(name='message_output_created', item=MessageOutputItem(...))
...
Request understood: Top 10 most recent sales orders
Execution Status
- I cannot access the system right now.
- MCP Server: my_mcp_server
- MCP Tool: get_sales_orders
- Error: MCP client is not available in this session; tool execution cannot be performed. Please ensure my_mcp_server is reachable and the MCP runtime is configured.``` 
Even though MCP server connection was established and session active via await my_mcp_server.connect().

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions