Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/client/streamableHttp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,37 @@ describe("StreamableHTTPClientTransport", () => {
await expect(transport.send(message)).rejects.toThrow(UnauthorizedError);
expect(mockAuthProvider.redirectToAuthorization.mock.calls).toHaveLength(1);
});

it("properly fails fast on 401 when sending initialize message", async () => {
const message: JSONRPCMessage = {
jsonrpc: "2.0",
method: "initialize",
params: {
clientInfo: { name: "test-client", version: "1.0" },
protocolVersion: "2025-03-26"
},
id: "init-id"
};

// First call returns 401
(global.fetch as jest.Mock)
.mockResolvedValueOnce({
ok: false,
status: 401,
statusText: "Unauthorized",
headers: new Headers()
});

// Mock the auth provider to trigger the auth flow
mockAuthProvider.tokens.mockResolvedValueOnce(undefined);
mockAuthProvider.redirectToAuthorization.mockImplementationOnce(async () => {
// This simulates the auth flow being triggered
});

// The send method should properly reject and not hang
await expect(transport.send(message)).rejects.toThrow(UnauthorizedError);

// Check that auth flow was triggered
expect(mockAuthProvider.redirectToAuthorization).toHaveBeenCalled();
});
});
4 changes: 2 additions & 2 deletions src/client/streamableHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ export class StreamableHTTPClientTransport implements Transport {
throw new UnauthorizedError();
}

// Purposely _not_ awaited, so we don't call onerror twice
return this.send(message);
// Retry the send operation after successful auth
return await this.send(message);
}

const text = await response.text().catch(() => null);
Expand Down