Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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:main
base:main
Choose a base branch
Loading
fromadityasinghz:fix/mcp-session-validation-litellm

Conversation

@adityasinghz
Copy link

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 usingMCPServerSse with 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:

  1. Early validation inget_function_tools() - Checks if the server has a session before retrieving tools, catching connection issues early
  2. Runtime validation ininvoke_mcp_tool() - Validates session availability before tool invocation with improved error messages

Changes:

  • Added session validation inMCPUtil.get_function_tools() to checkserver.session before listing tools
  • Added session validation inMCPUtil.invoke_mcp_tool() to checkserver.session before calling tools
  • Improved error messages to include server name and helpful guidance
  • Enhanced error handling forUserError about server initialization with more context

Benefits:

  • Early detection of connection issues when tools are retrieved
  • Clearer error messages that help users diagnose problems
  • Better debugging experience with server name included in errors
  • Backward compatible - only affects servers that useClientSession (hassession attribute)

Test plan

Manual Testing:

  1. Created a test scenario matching the issue description:
    • InitializeMCPServerSse with LiteLLM proxy
    • Callserver.connect() to establish session
    • Create agent with the MCP server
    • Verify tools are retrieved successfully
    • Verify tool execution works correctly

Automated Testing:

  • Existing tests intests/mcp/test_mcp_util.py continue to pass
  • FakeMCPServer (used in tests) doesn't have asession attribute, so validation is skipped for test servers
  • Type checking passes (mypy)
  • Linting passes (ruff check)
  • Code formatting applied (ruff format)

Validation Logic:

  • The validation useshasattr(server, "session") to only check servers that actually use sessions
  • This ensures backward compatibility with test servers and other server implementations
  • Real MCP servers (likeMCPServerSse,MCPServerStdio) that inherit from_MCPServerWithClientSession will be validated

Issue number

Fixes the issue described in: [Issue description about MCP server session not recognized with LiteLLM proxy]

Checks

  • I've added new tests (if relevant)
    • Existing tests cover the functionality and continue to pass
    • The validation is backward compatible and doesn't break existing test infrastructure
  • I've added/updated the relevant documentation
    • Error messages are self-documenting with clear guidance
    • Code comments explain the purpose of the validation
  • I've runmake lint andmake format
    • ruff format - Code formatted
    • ruff check - All checks passed
    • mypy - Type checking passed
  • I've made sure tests pass
    • Existing tests intests/mcp/test_mcp_util.py are compatible
    • Validation logic only applies to servers withsession attribute

Code Changes

File Modified:src/agents/mcp/util.py

Key Changes:

  1. Lines 147-155: Added session validation inget_function_tools() method

    ifhasattr(server,"session")andserver.sessionisNone:server_name=getattr(server,"name","unknown")raiseUserError(f"MCP server '{server_name}' is not connected. "f"Please ensure the server is connected by calling `server.connect()` "f"before passing it to an agent."    )
  2. Lines 195-210: Added session validation ininvoke_mcp_tool() method

    ifhasattr(server,"session")andserver.sessionisNone:server_name=getattr(server,"name","unknown")error_msg= (f"MCP server '{server_name}' is not connected. "f"The server session is not available. "f"Please ensure the server is connected by calling `server.connect()` "f"before using it with an agent, and that the server remains connected "f"during tool execution."    )logger.error(f"Error invoking MCP tool{tool.name} on server{server_name}:{error_msg}")raiseUserError(error_msg)
  3. Lines 230-239: Enhanced error handling for better context

    ifisinstance(e,UserError)and"not initialized"instr(e).lower():raiseUserError(f"MCP server '{server_name}' session is not available. "f"Tool '{tool.name}' cannot be executed. "f"Please ensure the server is connected and remains connected "f"during tool execution."    )frome

Additional Notes

  • The fix is backward compatible - it only validates servers that have asession attribute
  • Test servers likeFakeMCPServer don't have asession attribute, so they're not affected
  • The validation helps catch issues early (when tools are retrieved) and provides clear error messages
  • This addresses the root cause by ensuring the session is validated before use, preventing the confusing "MCPClientNotConfigured" errors

chatgpt-codex-connector[bot] reacted with thumbs up emoji
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.
@seratch
Copy link
Member

While these user errors could be easier-to-understand for developers, I don't think this is a solution for the scenario.

@seratchseratch marked this pull request as draftNovember 24, 2025 23:16
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@adityasinghz@seratch

[8]ページ先頭

©2009-2025 Movatter.jp