[v2] SSEClientTransport resolves the endpoint event against the configured URL instead of the URL it was redirected to
I maintainer di solito rispondono entro 1 giorno
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 35/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Ferma
- Stack tecnologico
- typescript
- Ambito
- backend-api-design
Direzione di ricerca
Start with packages/client/src/client/sse.ts, especially _startOrAuth and the endpoint event handler; trace how the final response URL and configured URL are used. The issue includes a reproduction and expected behavior: a relative endpoint after a redirect should resolve against the URL that served the stream, while the origin check remains bounded. A linked open pull request (#3241) is already addressing this issue.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
What happened?
Hi folks, hope you're doing well! When the SSE GET is redirected, SSEClientTransport resolves a relative endpoint event against the URL I passed in, not the URL the stream really came from, so the POSTs go to the wrong place.
For context, this is the legacy HTTP+SSE client: it opens the stream, waits for the endpoint event, and uses that URL for every later POST. Thanks a lot for the redirect policy work in #2901 and #2902, the GET now follows same origin redirects nicely, and that's exactly what makes this show up.
What happens today:
SSEClientTransportopenshttp://host/old/sse.- The server answers
307(or301) to/new/sse, and the fetch wrapper follows it (sse.ts#L258-L279, viafetchWithinOrigin). The finalresponse.urlis not kept anywhere. /new/ssesendsevent: endpointwithdata: messages?sessionId=1.- The handler does
new URL(messageEvent.data, this._url)and compares againstthis._url.origin(sse.ts#L337-L339), wherethis._urlis still the configured URL. - So the endpoint becomes
/old/messages?sessionId=1, the POST gets a 404, andinitializefails.
The same thing happens with an http to https upgrade on the same host, which isWithinOrigin treats as same origin (transport.ts#L52-L55):
- endpoint
/messages?sessionId=1: the POST goes to plain http, the server answers 301, and the error saysError POSTing to endpoint (HTTP 301): Redirect to https://localhost/messages not followed; use that URL as the endpoint if it is the intended server (redirectPolicy: 'same-origin'). That hint is a bit confusing here, since the endpoint came from the server and not from the user. - same, but POSTs answered with 307: it connects, but every POST takes an extra plain http hop.
- endpoint
https://localhost/messages?sessionId=1:Endpoint origin does not match connection origin: https://localhost.
An absolute path endpoint like /new/messages works fine, so it's only relative paths and the upgrade cases.
sequenceDiagram
participant C as SSEClientTransport
participant S as Server
C->>S: GET /old/sse
S-->>C: 307 Location /new/sse
C->>S: GET /new/sse
S-->>C: endpoint messages?sessionId=1
Note over C: resolved against /old/sse
C->>S: POST /old/messages?sessionId=1
S-->>C: 404

The connect path in SSEClientTransport._startOrAuth follows redirects through fetchWithinOrigin, Flow view in Architect
What did you expect?
The endpoint resolved against the URL the stream was really served from, so messages?sessionId=1 becomes /new/messages?sessionId=1. RFC 3986 section 5.1.3 says a relative reference resolves against the URI used to retrieve the representation, which is the final URL after redirects. The 2024-11-05 HTTP+SSE text doesn't name a base, but browsers' EventSource also sets MessageEvent.origin from the final URL, so I think it lines up.
Code to reproduce
import http from 'node:http';
import { Client, SSEClientTransport } from '@modelcontextprotocol/client';
const server = http.createServer((req, res) => {
console.log(req.method, req.url);
if (req.url === '/old/sse') {
res.writeHead(307, { Location: '/new/sse' }).end();
} else if (req.url === '/new/sse') {
res.writeHead(200, { 'Content-Type': 'text/event-stream' });
res.write('event: endpoint\ndata: messages?sessionId=1\n\n');
} else if (req.method === 'POST' && req.url === '/new/messages?sessionId=1') {
res.writeHead(202).end();
} else {
res.writeHead(404).end();
}
});
await new Promise(r => server.listen(0, r));
const url = new URL(`http://localhost:${server.address().port}/old/sse`);
const client = new Client({ name: 'repro', version: '1.0.0' });
try {
await client.connect(new SSEClientTransport(url));
} catch (error) {
console.log('connect failed:', error.message);
}
process.exit(0);
Output on main:
GET /old/sse
GET /new/sse
POST /old/messages?sessionId=1
connect failed: Error POSTing to endpoint (HTTP 404):
SDK version
main at 5ee54ee. v1.x has the same handler at src/client/sse.ts#L213-L214.
Area
Transports
One idea, if it sounds right to you: keep the final GET response.url in the fetch wrapper and use it as the base for the endpoint and its origin check, still bounded by isWithinOrigin(this._url, final) so a 'follow' policy doesn't widen what's trusted. Would you prefer that, or would you rather keep the configured URL as the base on purpose and just make the error clearer? Either way I'd be happy to open a small PR with a test on main, and a v1.x backport too if you'd like one. Thanks so much!
- Lingua principale
- TypeScript
- Stelle
- 13.8k
- Fork
- 2.5k
- Merge medio
- 6g 23h
- PR unite (30g)
- 2
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di modelcontextprotocol/typescript-sdk
-
v2
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
modelcontextprotocol/typescript-sdk#3270 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
`callTool` with `allowInputRequired: true` throws on an `input_required` result when the tool declares an `outputSchema`Forse già presa @mgoldsborough l’ha presa 6 giorni fa. Apertav2
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
modelcontextprotocol/typescript-sdk#3249 ·
I maintainer di solito rispondono entro 1 giorno
-
v2
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
modelcontextprotocol/typescript-sdk#3243 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
TypeScript client accepts an unknown `resultType` in the first `server/discover` replyForse già presa @Tidehyc l’ha presa 7 giorni fa. Apertav2
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
modelcontextprotocol/typescript-sdk#3235 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
UriTemplate.match() throws a TypeError on an empty expression it acceptsForse già presa @yunusemrejs l’ha presa 12 giorni fa. Apertav1 v2
Difficoltà 2/5 1-3 ore Idoneità per principianti 80/100
modelcontextprotocol/typescript-sdk#3221 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di modelcontextprotocol/typescript-sdk
Issue simili
-
clawsweeper:intake:bug clawsweeper:intake:needs-environment
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
openclaw/openclaw#212827 · 1 commento · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno
-
KT-2245 e2e specs that call the backend directly fall back to the developer's own Kronn on :3140Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Use one constant for the 10,000-character input limitForse già presa @NginxL l’ha presa oggi. Apertagood first issue refactor
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
aniru-dh21/snap-validate#51 ·
I maintainer di solito rispondono entro 1 giorno
-
dependencies effort: small good first issue priority: low
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
TMHSDigital/screencast-mcp#509 ·
I maintainer di solito rispondono entro 1 giorno
-
from-review tech-debt testing
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
blamechris/chroxy#9520 ·
I maintainer di solito rispondono entro 1 giorno