Skip to content

add custom headers on initial _startOrAuth call #318

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

anthonjn
Copy link

@anthonjn anthonjn commented Apr 12, 2025

Ensure that custom headers provided in this._requestInit?.headers are included in the initial call to the /sse endpoint.

Motivation and Context

Fixes #317, where custom headers (e.g. Host, X-*) were not sent in the initial /sse request, but were correctly sent in /messages. This is needed for MCP servers behind proxies that rely on these headers.

This PR aligns the behavior of the /sse request with send() by ensuring that all custom headers (when provided) are forwarded appropriately.

How Has This Been Tested?

  • Added a test case to verify that custom headers, when specified, are included in the initial /sse request.

Breaking Changes

No

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

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

N/a

@cnjsstong
Copy link

This fix for requestInit.headers not working bug looks good. Please review and merge. Thanks!

@autoexpect
Copy link

@ihrpr Could you please help with the merge? Thanks!

Pizzaface pushed a commit to RewstApp/mcp-inspector that referenced this pull request May 2, 2025
@ihrpr ihrpr added this to 🐛 🛠 May 29, 2025
@github-project-automation github-project-automation bot moved this to To triage in 🐛 🛠 May 29, 2025
@ihrpr ihrpr added this to the auth milestone Jun 25, 2025
@ochafik ochafik self-assigned this Jun 30, 2025
@ochafik ochafik self-requested a review June 30, 2025 10:46
@ochafik ochafik removed their assignment Jun 30, 2025
Copy link
Contributor

@ochafik ochafik left a comment

Choose a reason for hiding this comment

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

Thanks @anthonjn, a much needed update indeed! Suggested a slight refactor :-)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should update the commonHeaders method & align it w/ the streamableHttp version. Something like:

  private async _commonHeaders(): Promise<Headers> {
    const headers: HeadersInit = {};
    if (this._authProvider) {
      const tokens = await this._authProvider.tokens();
      if (tokens) {
        headers["Authorization"] = `Bearer ${tokens.access_token}`;
      }
    }

    return new Headers(
      { ...headers, ...this._requestInit?.headers }
    );
  }

Could you please also update the header usage in the send method?

      const headers = await this._commonHeaders();
      headers.set("content-type", "application/json");

Comment on lines +117 to +124
const commonHeaders = await this._commonHeaders();
const allHeaders = { ...commonHeaders, ...this._requestInit?.headers};
return fetch(url, {
...init,
headers: {
...allHeaders,
Accept: "text/event-stream"
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const commonHeaders = await this._commonHeaders();
const allHeaders = { ...commonHeaders, ...this._requestInit?.headers};
return fetch(url, {
...init,
headers: {
...allHeaders,
Accept: "text/event-stream"
}
const headers = await this._commonHeaders();
headers.set("Accept", "text/event-stream");
return fetch(url, {
...init,
headers,
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: To triage
Development

Successfully merging this pull request may close these issues.

SSEClientTransport's custom header support seems inconsistent
5 participants