Skip to content

fix: preserve subpaths in SSE endpoint URL construction #500

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 3 commits into
base: main
Choose a base branch
from

Conversation

Njuelle
Copy link

@Njuelle Njuelle commented May 16, 2025

Motivation and Context

When MCP Server is deployed under a subpath URL (e.g., http://domain.com/subpath/sse), clients fail to connect because of the way the SSE transport constructs URLs. Specifically, the use of new URL() incorrectly discards the subpath, transforming requests intended for /messages/ into http://domain.com/messages/, rather than preserving the correct subpath (http://domain.com/subpath/messages/).

This fix ensures that when a server is deployed at a non-root path, clients can still connect successfully by preserving the subpath in all URL communications.

How Has This Been Tested?

  • Added unit tests for both client and server components that verify subpath preservation
  • Added tests for various URL structures (complex nested paths, paths with query parameters, etc.)
  • Verified through integration tests with actual HTTP requests
  • Manually tested with Inspector connecting to a server deployed at a subpath

Breaking Changes

None. This is a pure bug fix that maintains backward compatibility. Existing deployments at root paths continue to work as before.

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 fix involves two main changes:

  1. Server-side: Properly handling URL construction to preserve subpaths when generating endpoint URLs
  2. Client-side: Properly resolving relative URLs against the original URL to maintain subpath context

The fix handles edge cases including empty paths, invalid URLs, and absolute URLs with proper error handling.

fixes #470

@fvdcx
Copy link

fvdcx commented May 21, 2025

@Njuelle thanks for doing this, can you involve the author to review the code change?

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.

MCP SDK Path Handling Bug for Subpath Servers
2 participants