Skip to content

Commit 2f207ac

Browse files
dsp-antclaude
andcommitted
Refactor SSEClientTransport to cache wrapped fetch function
Match the pattern used in StreamableHTTPClientTransport by creating the wrapped fetch function once in the constructor instead of recreating it on every auth call. This improves consistency between the two transport implementations and avoids unnecessary function recreation. Changes: - Add _fetchWithInit field to store the wrapped fetch function - Initialize _fetchWithInit in constructor using createFetchWithInit - Use _fetchWithInit in all auth() calls instead of creating inline - Remove redundant createFetchWithInit calls from 3 locations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent aa7df3f commit 2f207ac

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

src/client/sse.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export class SSEClientTransport implements Transport {
7070
private _requestInit?: RequestInit;
7171
private _authProvider?: OAuthClientProvider;
7272
private _fetch?: FetchLike;
73+
private _fetchWithInit: FetchLike;
7374
private _protocolVersion?: string;
7475

7576
onclose?: () => void;
@@ -84,6 +85,7 @@ export class SSEClientTransport implements Transport {
8485
this._requestInit = opts?.requestInit;
8586
this._authProvider = opts?.authProvider;
8687
this._fetch = opts?.fetch;
88+
this._fetchWithInit = createFetchWithInit(opts?.fetch, opts?.requestInit);
8789
}
8890

8991
private async _authThenStart(): Promise<void> {
@@ -93,14 +95,11 @@ export class SSEClientTransport implements Transport {
9395

9496
let result: AuthResult;
9597
try {
96-
// Wrap fetch to automatically include base RequestInit options
97-
const fetchFn = createFetchWithInit(this._fetch, this._requestInit);
98-
9998
result = await auth(this._authProvider, {
10099
serverUrl: this._url,
101100
resourceMetadataUrl: this._resourceMetadataUrl,
102101
scope: this._scope,
103-
fetchFn
102+
fetchFn: this._fetchWithInit
104103
});
105104
} catch (error) {
106105
this.onerror?.(error as Error);
@@ -218,15 +217,12 @@ export class SSEClientTransport implements Transport {
218217
throw new UnauthorizedError('No auth provider');
219218
}
220219

221-
// Wrap fetch to automatically include base RequestInit options
222-
const fetchFn = createFetchWithInit(this._fetch, this._requestInit);
223-
224220
const result = await auth(this._authProvider, {
225221
serverUrl: this._url,
226222
authorizationCode,
227223
resourceMetadataUrl: this._resourceMetadataUrl,
228224
scope: this._scope,
229-
fetchFn
225+
fetchFn: this._fetchWithInit
230226
});
231227
if (result !== 'AUTHORIZED') {
232228
throw new UnauthorizedError('Failed to authorize');
@@ -262,14 +258,11 @@ export class SSEClientTransport implements Transport {
262258
this._resourceMetadataUrl = resourceMetadataUrl;
263259
this._scope = scope;
264260

265-
// Wrap fetch to automatically include base RequestInit options
266-
const fetchFn = createFetchWithInit(this._fetch, this._requestInit);
267-
268261
const result = await auth(this._authProvider, {
269262
serverUrl: this._url,
270263
resourceMetadataUrl: this._resourceMetadataUrl,
271264
scope: this._scope,
272-
fetchFn
265+
fetchFn: this._fetchWithInit
273266
});
274267
if (result !== 'AUTHORIZED') {
275268
throw new UnauthorizedError();

0 commit comments

Comments
 (0)