Skip to content

Commit f383a9c

Browse files
committed
* In sse.test.ts
- More specific expectations in test "uses custom fetch implementation from options" * In sse.ts - Import FetchLike from transport.ts * In steramableHttp.ts - Import FetchLike from transport.ts - Export StartSSEOptions interface for testing * In streamableHttp.test.ts - import StartSSEOptions from streamableHttp.ts - use StartSSEOptions instead of any in test "uses custom fetch implementation" * In transport.ts - Add FetchLike function type
1 parent 242c824 commit f383a9c

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

src/client/sse.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,9 @@ describe("SSEClientTransport", () => {
289289

290290
await transport.send(message);
291291

292-
expect(fetchWithAuth).toHaveBeenCalled();
292+
expect(fetchWithAuth).toHaveBeenCalledTimes(2);
293+
expect(lastServerRequest.method).toBe("POST");
294+
expect(lastServerRequest.headers.authorization).toBe(authToken);
293295
});
294296

295297
it("passes custom headers to fetch requests", async () => {

src/client/sse.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { EventSource, type ErrorEvent, type EventSourceInit } from "eventsource";
2-
import { Transport } from "../shared/transport.js";
2+
import { Transport, FetchLike } from "../shared/transport.js";
33
import { JSONRPCMessage, JSONRPCMessageSchema } from "../types.js";
44
import { auth, AuthResult, extractResourceMetadataUrl, OAuthClientProvider, UnauthorizedError } from "./auth.js";
55

6-
export type FetchLike = (url: string | URL, init?: RequestInit) => Promise<Response>;
7-
86
export class SseError extends Error {
97
constructor(
108
public readonly code: number | undefined,

src/client/streamableHttp.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { StreamableHTTPClientTransport, StreamableHTTPReconnectionOptions } from "./streamableHttp.js";
1+
import { StreamableHTTPClientTransport, StreamableHTTPReconnectionOptions, StartSSEOptions } from "./streamableHttp.js";
22
import { OAuthClientProvider, UnauthorizedError } from "./auth.js";
33
import { JSONRPCMessage } from "../types.js";
44

@@ -461,7 +461,7 @@ describe("StreamableHTTPClientTransport", () => {
461461
transport = new StreamableHTTPClientTransport(new URL("http://localhost:1234/mcp"), { fetch: fetchWithAuth });
462462

463463
await transport.start();
464-
await (transport as unknown as { _startOrAuthSse: (opts: any) => Promise<void> })._startOrAuthSse({});
464+
await (transport as unknown as { _startOrAuthSse: (opts: StartSSEOptions) => Promise<void> })._startOrAuthSse({});
465465

466466
await transport.send({ jsonrpc: "2.0", method: "test", params: {}, id: "1" } as JSONRPCMessage);
467467

@@ -559,7 +559,7 @@ describe("StreamableHTTPClientTransport", () => {
559559
// Second retry - should double (2^1 * 100 = 200)
560560
expect(getDelay(1)).toBe(200);
561561

562-
// Third retry - should double again (2^2 * 100 = 400)
562+
// Third retry - should double again (2^2 * 100 = 400)
563563
expect(getDelay(2)).toBe(400);
564564

565565
// Fourth retry - should double again (2^3 * 100 = 800)

src/client/streamableHttp.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import { Transport } from "../shared/transport.js";
1+
import { Transport, FetchLike } from "../shared/transport.js";
22
import { isInitializedNotification, isJSONRPCRequest, isJSONRPCResponse, JSONRPCMessage, JSONRPCMessageSchema } from "../types.js";
33
import { auth, AuthResult, extractResourceMetadataUrl, OAuthClientProvider, UnauthorizedError } from "./auth.js";
44
import { EventSourceParserStream } from "eventsource-parser/stream";
55

6-
export type FetchLike = (url: string | URL, init?: RequestInit) => Promise<Response>;
7-
86
// Default reconnection options for StreamableHTTP connections
97
const DEFAULT_STREAMABLE_HTTP_RECONNECTION_OPTIONS: StreamableHTTPReconnectionOptions = {
108
initialReconnectionDelay: 1000,
@@ -25,7 +23,7 @@ export class StreamableHTTPError extends Error {
2523
/**
2624
* Options for starting or authenticating an SSE connection
2725
*/
28-
interface StartSSEOptions {
26+
export interface StartSSEOptions {
2927
/**
3028
* The resumption token used to continue long-running requests that were interrupted.
3129
*
@@ -260,15 +258,15 @@ const response = await (this._fetch ?? fetch)(this._url, {
260258

261259
private _normalizeHeaders(headers: HeadersInit | undefined): Record<string, string> {
262260
if (!headers) return {};
263-
261+
264262
if (headers instanceof Headers) {
265263
return Object.fromEntries(headers.entries());
266264
}
267-
265+
268266
if (Array.isArray(headers)) {
269267
return Object.fromEntries(headers);
270268
}
271-
269+
272270
return { ...headers as Record<string, string> };
273271
}
274272

src/shared/transport.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { JSONRPCMessage, MessageExtraInfo, RequestId } from "../types.js";
22

3+
export type FetchLike = (url: string | URL, init?: RequestInit) => Promise<Response>;
4+
35
/**
46
* Options for sending a JSON-RPC message.
57
*/
68
export type TransportSendOptions = {
7-
/**
9+
/**
810
* If present, `relatedRequestId` is used to indicate to the transport which incoming request to associate this outgoing message with.
911
*/
1012
relatedRequestId?: RequestId;
@@ -38,7 +40,7 @@ export interface Transport {
3840

3941
/**
4042
* Sends a JSON-RPC message (request or response).
41-
*
43+
*
4244
* If present, `relatedRequestId` is used to indicate to the transport which incoming request to associate this outgoing message with.
4345
*/
4446
send(message: JSONRPCMessage, options?: TransportSendOptions): Promise<void>;
@@ -64,9 +66,9 @@ export interface Transport {
6466

6567
/**
6668
* Callback for when a message (request or response) is received over the connection.
67-
*
69+
*
6870
* Includes the requestInfo and authInfo if the transport is authenticated.
69-
*
71+
*
7072
* The requestInfo can be used to get the original request information (headers, etc.)
7173
*/
7274
onmessage?: (message: JSONRPCMessage, extra?: MessageExtraInfo) => void;

0 commit comments

Comments
 (0)