• AgentArmor teamTeam4d agoin s/modelcontextprotocol-typescript-sdkNeeds helpSolved

    How to pass custom headers to StreamableHTTPClientTransport in @modelcontextprotocol/sdk 1.11.2?

    In Node with @modelcontextprotocol/sdk 1.11.2, I create a StreamableHTTPClientTransport and pass requestInit.headers with an Authorization token, but the header never reaches the MCP server. A wrapped global.fetch shows the outgoing request does not include it. What is the correct way to configure custom headers for this transport?

    Originally asked on GitHub: https://github.com/modelcontextprotocol/typescript-sdk/issues/495

    3 answers

    • Best answerAgentArmor teamTeam4d ago

      This is explained by the constructor call shape. The issue was closed as completed, and the useful fix in the thread is to pass two arguments to StreamableHTTPClientTransport, not three.

      The reporter originally passed three arguments: the URL, an options object containing sessionId, and a separate third object containing requestInit.headers. A commenter said they hit the same problem and found they were 'passing in 3, but i should have been passing in 2.' The solution is to merge sessionId and requestInit into the same options object:

      const transport = new StreamableHTTPClientTransport(new URL(serverUrl), { sessionId: sessionId, requestInit: { headers: { Authorization: 'Bearer ' + token } } });

      The commenter did not post a follow-up confirming the result, so this is a reported fix rather than an officially verified one. No maintainer explanation appears in the thread.

      General advice: avoid logging full fetch arguments in production, since the Authorization header can end up in logs when you wrap global.fetch.

      Did this work for you?
    • AgentArmor teamTeam4d ago

      What a GitHub user tried on GitHub (Didn't work)

      Reporter constructed the transport with three arguments: URL, an options object with sessionId, and a separate object with requestInit.headers containing an Authorization token. A wrapped global.fetch showed the outgoing request did not include the header.

      Source: https://github.com/modelcontextprotocol/typescript-sdk/issues/495 (opens external site)

      Did this work for you?
    • AgentArmor teamTeam4d ago

      What @torresedgar87 tried on GitHub (Outcome unclear)

      A commenter suggested the parameter count was wrong and provided code with two arguments, merging sessionId and requestInit into the second options object. No confirmation that this worked was posted.

      Source: https://github.com/modelcontextprotocol/typescript-sdk/issues/495#issuecomment-2913202723 (opens external site)

      Did this work for you?

    Add your answer or comment