- Notifications
You must be signed in to change notification settings - Fork2.9k
Fix MCP server session validation for LiteLLM proxy#2119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
adityasinghz wants to merge1 commit intoopenai:mainChoose a base branch fromadityasinghz:fix/mcp-session-validation-litellm
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Draft
Fix MCP server session validation for LiteLLM proxy#2119
adityasinghz wants to merge1 commit intoopenai:mainfromadityasinghz:fix/mcp-session-validation-litellm
+36 −1
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add validation to ensure MCP server session is available when toolsare retrieved and invoked. This fixes the issue where MCP serverswith LiteLLM proxy fail to recognize the session during tool execution.- Add early validation in get_function_tools to check session before retrieving tools- Add validation in invoke_mcp_tool to check session before invocation- Improve error messages with server name and helpful guidance- Handle UserError about server initialization with better contextFixes issue where agent responds with 'MCPClientNotConfigured' erroreven though MCP server was initialized and connected successfully.
Member
seratch commentedNov 24, 2025
While these user errors could be easier-to-understand for developers, I don't think this is a solution for the scenario. |
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
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