-
Notifications
You must be signed in to change notification settings - Fork 95
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
Comments
This is a correct understanding of how it works. Unfortunately, opening the SSE connection requires the use of the It's possible there are workarounds for Node.js, but I'm not sure there are good alternatives for the browser. |
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? |
In node, one option is to use the 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) |
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:
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). Thank you again for your help! |
@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:
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.
The text was updated successfully, but these errors were encountered: