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

Appearance settings

Improve error handling during request validation in BaseSession #774

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v1.7.x
Choose a base branch
Loading
from

Conversation

painkiller90
Copy link

@painkiller90 painkiller90 commented May 21, 2025

Added a try block around the handling of JSONRPCRequest messages in the _receive_loop method, mirroring the error handling already present for JSONRPCNotification. This ensures that validation or processing errors for requests are caught and logged, preventing the receive loop from crashing and improving robustness.

Motivation and Context

Previously, only notification handling was wrapped in a try block to catch and log validation or processing errors. However, requests can also fail validation or processing, which would cause the receive loop to crash or propagate exceptions. Wrapping the request handling in a try block ensures that such errors are logged and do not interrupt the session's message processing loop.

Also trying to interact with MCPs like Twilio MCP it crashes when other clients works perfectly.

How Has This Been Tested?

I manually tested it with the next MCP's:

import pprint
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

# This MCP works
mcp1_args = ["-y", "@modelcontextprotocol/server-filesystem", ".", "."]

# This doesn't work
mcp2_args = [
    "-y",
    "@twilio-alpha/mcp",
    "YOUR_ACCOUNT_SID/YOUR_API_KEY:YOUR_API_SECRET",
]

server_params = StdioServerParameters(command="npx", args=mcp2_args, env=None)


async def run():
    async with stdio_client(server_params) as (read, write):
        async with ClientSession(read, write) as session:
            await session.initialize()

            tools = await session.list_tools()
            pprint.pprint(tools)


if __name__ == "__main__":
    import asyncio

    asyncio.run(run())

After the changes both MCPs works with no problem.

Breaking Changes

No breaking changes.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Copy link
Contributor

@ihrpr ihrpr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this!

There are a few ares that need to be addressed

  1. Silent failure: When a request fails validation, the client won't receive any response. This could leave the client hanging, waiting for a response that will never come.
  2. Lost request tracking: Failed requests won't be added to _in_flight, which is probably correct, but worth noting
  3. No tests covering this scenario

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.

2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.