Fix MCP server session validation for LiteLLM proxy #2119
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes an issue where (MCP servers fail to recognize their session when using LiteLLM Proxy as the model) #2074. The agent was responding with "MCPClientNotConfigured" errors even though the MCP server was initialized and connected successfully.
Problem:
When using
MCPServerSsewith LiteLLM Proxy, the agent fails to recognize the MCP session during tool execution. The agent responds with errors like "MCP client is not available in this session; tool execution cannot be performed" even thoughserver.connect()was called and the session was established.Solution:
Added validation to ensure MCP server sessions are available at two critical points:
get_function_tools()- Checks if the server has a session before retrieving tools, catching connection issues earlyinvoke_mcp_tool()- Validates session availability before tool invocation with improved error messagesChanges:
MCPUtil.get_function_tools()to checkserver.sessionbefore listing toolsMCPUtil.invoke_mcp_tool()to checkserver.sessionbefore calling toolsUserErrorabout server initialization with more contextBenefits:
ClientSession(hassessionattribute)Test plan
Manual Testing:
MCPServerSsewith LiteLLM proxyserver.connect()to establish sessionAutomated Testing:
tests/mcp/test_mcp_util.pycontinue to passFakeMCPServer(used in tests) doesn't have asessionattribute, so validation is skipped for test serversmypy)ruff check)ruff format)Validation Logic:
hasattr(server, "session")to only check servers that actually use sessionsMCPServerSse,MCPServerStdio) that inherit from_MCPServerWithClientSessionwill be validatedIssue number
Fixes the issue described in: [Issue description about MCP server session not recognized with LiteLLM proxy]
Checks
make lintandmake formatruff format- Code formattedruff check- All checks passedmypy- Type checking passedtests/mcp/test_mcp_util.pyare compatiblesessionattributeCode Changes
File Modified:
src/agents/mcp/util.pyKey Changes:
Lines 147-155: Added session validation in
get_function_tools()methodLines 195-210: Added session validation in
invoke_mcp_tool()methodLines 230-239: Enhanced error handling for better context
Additional Notes
sessionattributeFakeMCPServerdon't have asessionattribute, so they're not affected