Skip to content
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

Use custom headers for both the /sse and /message endpoints #118

Open
jirispilka opened this issue Jan 8, 2025 · 4 comments
Open

Use custom headers for both the /sse and /message endpoints #118

jirispilka opened this issue Jan 8, 2025 · 4 comments
Labels
bug Something isn't working

Comments

@jirispilka
Copy link

@chrisdickinson thank you for this PR

Apologies, but I'm not very strong in JS.
I need to include an API token to access my MCP server, for both the /sse and /message endpoints.

I believe the headers are not being used during client.connect()?

Here’s the code snippet I’m working with:

const customHeaders = { Authorization: `Bearer ${token}` };

const transport = new SSEClientTransport(new URL(SERVER_URL), { requestInit: { headers: customHeaders } });
const client = new Client({ name: 'example-client', version: '1.0.0' }, { capabilities: {} });

await client.connect(transport);

I'm still getting 401.
As far as I can tell, the requestInit is only being used when a message is sent. Is that correct?

I’d appreciate your help. Thank you.

@jirispilka jirispilka added the bug Something isn't working label Jan 8, 2025
@jspahrsummers
Copy link
Member

This is a correct understanding of how it works. Unfortunately, opening the SSE connection requires the use of the EventSource class, which AFAIK doesn't support adding custom headers.

It's possible there are workarounds for Node.js, but I'm not sure there are good alternatives for the browser.

@jirispilka
Copy link
Author

Thank you for a quick reply! I appreciate it

What would be a workaround for node.js? Can you please point me into a right direction?

@chrisdickinson
Copy link
Contributor

chrisdickinson commented Jan 8, 2025

In node, one option is to use the eventsource package.

That looks something like this:

import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'
import { EventSource } from 'eventsource'

// define EventSource globally
globalThis.EventSource = EventSource

const remoteTransport = new SSEClientTransport(
  new URL('http://example.com/my/great/url'),
  {
    requestInit: {
      headers: { 'authorization': 'Bearer <secret>',
    },
    eventSourceInit: {
      // The EventSource package augments EventSourceInit with a "fetch" parameter.
      // You can use this to set additional headers on the outgoing request.
      fetch(input: Request | URL | string, init?: RequestInit) {
        const headers = new Headers(init?.headers || {})
        headers.set('authorization', 'Bearer <secret>')
        return fetch(input, {
          ...init,
          headers,
        })
      },
    } as any, // ... but you have to cast to "any" to use it, since it's non-standard
  }
)

// Assuming you have a "client: Client" available:
// client.connect(remoteTransport)

@jirispilka
Copy link
Author

jirispilka commented Jan 16, 2025

Thank you very much for your time! I was not able to make it work and started to use python implementation (not ideal).

Log error, full code example:

Error: SSE error: {}
Error: SSE error: {}
    at EventSource._eventSource.onerror (~/apify/actor-mcp-server/node_modules/@modelcontextprotocol/sdk/src/client/sse.ts:40:23)
    at EventSource.scheduleReconnect_fn (~/apify/actor-mcp-server/node_modules/eventsource/src/EventSource.ts:554:5)
    at <anonymous> (~/apify/actor-mcp-server/node_modules/eventsource/src/EventSource.ts:438:5)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

I’m just curious to see if this could work, but please don’t waste time on it now (as I did workaround with Python).
I’m sharing it here for future reference.

Thank you again for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants