Skip to content

Improve SSE endpoint sessionId parameter handling #177

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

Merged
merged 4 commits into from
Apr 9, 2025

Conversation

akazwz
Copy link
Contributor

@akazwz akazwz commented Mar 9, 2025

Add dynamic query parameter separator for sessionId to handle endpoints with existing query parameters more robustly

This PR improves the URL construction when adding the sessionId parameter to endpoints by dynamically selecting the appropriate separator ('?' or '&') based on whether the endpoint already contains query parameters.

Motivation and Context

When constructing the endpoint URL with the sessionId parameter, the code needs to determine whether to use '?' or '&' as a separator. If the endpoint already contains query parameters (e.g., /messages?filter=all), we should use '&' to append additional parameters. Otherwise, if there are no existing query parameters (e.g., /messages), we should use '?' to start the query string.

This change ensures that the SSE endpoint URLs are correctly formed regardless of whether the original endpoint already contains query parameters, preventing malformed URLs in the SSE connection.

How Has This Been Tested?

Tested with various endpoint formats:

  • Basic endpoints: /messages/messages?sessionId=xxx
  • Endpoints with existing query parameters: /messages?filter=all/messages?filter=all&sessionId=xxx

Breaking Changes

None. This is a non-breaking change that improves the robustness of URL handling without requiring any changes to user code.

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

The implementation uses a simple check for the presence of '?' in the endpoint string. While this approach works for standard endpoints, it has a limitation: it doesn't handle the edge case where '?' might be part of the path itself rather than a query parameter separator. However, this is an extremely rare case in practice, and the simplicity of the solution outweighs the benefit of handling such edge cases.

Add dynamic query parameter separator for sessionId to handle endpoints with existing query parameters more robustly
Copy link
Member

@jspahrsummers jspahrsummers left a comment

Choose a reason for hiding this comment

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

Thanks for flagging. To fix this more robustly, can you please use URL and URLSearchParams?

@akazwz
Copy link
Contributor Author

akazwz commented Apr 8, 2025

@jspahrsummers Thanks! Updated the code to use URL and URLSearchParams for adding the sessionId parameter, handling the relative path correctly. Pushed the changes.

Copy link
Member

@jspahrsummers jspahrsummers 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! I underestimated how complex this would be (because of the need for URLs to always be absolute), so can you please add some tests as well?

@akazwz
Copy link
Contributor Author

akazwz commented Apr 9, 2025

@jspahrsummers Okay, I've added unit tests covering various relative endpoint scenarios (simple path, with query params, with hash fragment, root path, empty path) as requested. All tests are passing now. Thanks for the suggestion!

Copy link
Member

@jspahrsummers jspahrsummers left a comment

Choose a reason for hiding this comment

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

Great, thank you!

@jspahrsummers jspahrsummers merged commit d205ad2 into modelcontextprotocol:main Apr 9, 2025
2 checks passed
threepointone added a commit to cloudflare/agents that referenced this pull request Apr 25, 2025
* do not remove search params and hash from mcp endpoint message

Reference: modelcontextprotocol/typescript-sdk#177

* Create twenty-jeans-love.md

* pass checks

---------

Co-authored-by: Sunil Pai <[email protected]>
Co-authored-by: Sunil Pai <[email protected]>
Pizzaface pushed a commit to RewstApp/mcp-inspector that referenced this pull request May 2, 2025
…fig-file-support

Add CLI and config file support
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