Skip to content

Commit a119e40

Browse files
Add missing auth header (microsoft#249767)
Forgot to add the auth header on `_attachStreamableBackchannel`
1 parent 9bb394b commit a119e40

File tree

1 file changed

+21
-39
lines changed

1 file changed

+21
-39
lines changed

src/vs/workbench/api/common/extHostMcp.ts

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -231,17 +231,7 @@ class McpHTTPHandle extends Disposable {
231231
if (sessionId) {
232232
headers['Mcp-Session-Id'] = sessionId;
233233
}
234-
235-
if (this._authMetadata) {
236-
try {
237-
const token = await this._proxy.$getTokenFromServerMetadata(this._id, this._authMetadata);
238-
if (token) {
239-
headers['Authorization'] = `Bearer ${token}`;
240-
}
241-
} catch (e) {
242-
this._log(LogLevel.Warning, `Error getting token from server metadata: ${String(e)}`);
243-
}
244-
}
234+
await this._addAuthHeader(headers);
245235

246236
const doFetch = () => fetch(
247237
this._launch.uri.toString(true),
@@ -257,14 +247,9 @@ class McpHTTPHandle extends Disposable {
257247
if (res.status === 401) {
258248
if (!this._authMetadata) {
259249
await this._populateAuthMetadata(res);
260-
if (this._authMetadata) {
261-
try {
262-
const token = await this._proxy.$getTokenFromServerMetadata(this._id, this._authMetadata);
263-
headers['Authorization'] = `Bearer ${token}`;
264-
res = await doFetch();
265-
} catch (e) {
266-
this._log(LogLevel.Warning, `Error getting token from server metadata: ${String(e)}`);
267-
}
250+
await this._addAuthHeader(headers);
251+
if (headers['Authorization']) {
252+
res = await doFetch();
268253
}
269254
}
270255
}
@@ -493,6 +478,7 @@ class McpHTTPHandle extends Disposable {
493478
...Object.fromEntries(this._launch.headers),
494479
'Accept': 'text/event-stream',
495480
};
481+
await this._addAuthHeader(headers);
496482

497483
if (this._mode.value === HttpMode.Http && this._mode.sessionId !== undefined) {
498484
headers['Mcp-Session-Id'] = this._mode.sessionId;
@@ -545,16 +531,7 @@ class McpHTTPHandle extends Disposable {
545531
...Object.fromEntries(this._launch.headers),
546532
'Accept': 'text/event-stream',
547533
};
548-
if (this._authMetadata) {
549-
try {
550-
const token = await this._proxy.$getTokenFromServerMetadata(this._id, this._authMetadata);
551-
if (token) {
552-
headers['Authorization'] = `Bearer ${token}`;
553-
}
554-
} catch (e) {
555-
this._log(LogLevel.Warning, `Error getting token from server metadata: ${String(e)}`);
556-
}
557-
}
534+
await this._addAuthHeader(headers);
558535

559536
let res: Response;
560537
try {
@@ -599,16 +576,7 @@ class McpHTTPHandle extends Disposable {
599576
'Content-Type': 'application/json',
600577
'Content-Length': String(asBytes.length),
601578
};
602-
if (this._authMetadata) {
603-
try {
604-
const token = await this._proxy.$getTokenFromServerMetadata(this._id, this._authMetadata);
605-
if (token) {
606-
headers['Authorization'] = `Bearer ${token}`;
607-
}
608-
} catch (e) {
609-
this._log(LogLevel.Warning, `Error getting token from server metadata: ${String(e)}`);
610-
}
611-
}
579+
await this._addAuthHeader(headers);
612580
const res = await fetch(url, {
613581
method: 'POST',
614582
signal: this._abortCtrl.signal,
@@ -647,6 +615,20 @@ class McpHTTPHandle extends Disposable {
647615
} while (!chunk.done);
648616
}
649617

618+
private async _addAuthHeader(headers: Record<string, string>) {
619+
if (this._authMetadata) {
620+
try {
621+
const token = await this._proxy.$getTokenFromServerMetadata(this._id, this._authMetadata);
622+
if (token) {
623+
headers['Authorization'] = `Bearer ${token}`;
624+
}
625+
} catch (e) {
626+
this._log(LogLevel.Warning, `Error getting token from server metadata: ${String(e)}`);
627+
}
628+
}
629+
return headers;
630+
}
631+
650632
private _log(level: LogLevel, message: string) {
651633
if (!this._store.isDisposed) {
652634
this._proxy.$onDidPublishLog(this._id, level, message);

0 commit comments

Comments
 (0)