Skip to content

Commit b7ec382

Browse files
committed
Use transport-specific options
1 parent dd6f528 commit b7ec382

File tree

1 file changed

+65
-35
lines changed

1 file changed

+65
-35
lines changed

client/src/lib/hooks/useConnection.ts

Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
22
import {
33
SSEClientTransport,
44
SseError,
5+
SSEClientTransportOptions,
56
} from "@modelcontextprotocol/sdk/client/sse.js";
6-
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
7+
import {
8+
StreamableHTTPClientTransport,
9+
StreamableHTTPClientTransportOptions,
10+
} from "@modelcontextprotocol/sdk/client/streamableHttp.js";
711
import {
812
ClientNotification,
913
ClientRequest,
@@ -279,29 +283,6 @@ export function useConnection({
279283
setConnectionStatus("error-connecting-to-proxy");
280284
return;
281285
}
282-
let mcpProxyServerUrl;
283-
switch (transportType) {
284-
case "stdio":
285-
mcpProxyServerUrl = new URL(`${getMCPProxyAddress(config)}/stdio`);
286-
mcpProxyServerUrl.searchParams.append("command", command);
287-
mcpProxyServerUrl.searchParams.append("args", args);
288-
mcpProxyServerUrl.searchParams.append("env", JSON.stringify(env));
289-
break;
290-
291-
case "sse":
292-
mcpProxyServerUrl = new URL(`${getMCPProxyAddress(config)}/sse`);
293-
mcpProxyServerUrl.searchParams.append("url", sseUrl);
294-
break;
295-
296-
case "streamable-http":
297-
mcpProxyServerUrl = new URL(`${getMCPProxyAddress(config)}/mcp`);
298-
mcpProxyServerUrl.searchParams.append("url", sseUrl);
299-
break;
300-
}
301-
(mcpProxyServerUrl as URL).searchParams.append(
302-
"transportType",
303-
transportType,
304-
);
305286

306287
try {
307288
// Inject auth manually instead of using SSEClientTransport, because we're
@@ -320,17 +301,66 @@ export function useConnection({
320301
}
321302

322303
// Create appropriate transport
323-
const transportOptions = {
324-
eventSourceInit: {
325-
fetch: (
326-
url: string | URL | globalThis.Request,
327-
init: RequestInit | undefined,
328-
) => fetch(url, { ...init, headers }),
329-
},
330-
requestInit: {
331-
headers,
332-
},
333-
};
304+
let transportOptions:
305+
| StreamableHTTPClientTransportOptions
306+
| SSEClientTransportOptions;
307+
308+
let mcpProxyServerUrl;
309+
switch (transportType) {
310+
case "stdio":
311+
mcpProxyServerUrl = new URL(`${getMCPProxyAddress(config)}/stdio`);
312+
mcpProxyServerUrl.searchParams.append("command", command);
313+
mcpProxyServerUrl.searchParams.append("args", args);
314+
mcpProxyServerUrl.searchParams.append("env", JSON.stringify(env));
315+
transportOptions = {};
316+
break;
317+
318+
case "sse":
319+
mcpProxyServerUrl = new URL(`${getMCPProxyAddress(config)}/sse`);
320+
mcpProxyServerUrl.searchParams.append("url", sseUrl);
321+
transportOptions = {
322+
authProvider: serverAuthProvider,
323+
eventSourceInit: {
324+
fetch: (
325+
url: string | URL | globalThis.Request,
326+
init: RequestInit | undefined,
327+
) => fetch(url, { ...init, headers }),
328+
},
329+
requestInit: {
330+
headers,
331+
},
332+
};
333+
break;
334+
335+
case "streamable-http":
336+
mcpProxyServerUrl = new URL(`${getMCPProxyAddress(config)}/mcp`);
337+
mcpProxyServerUrl.searchParams.append("url", sseUrl);
338+
transportOptions = {
339+
authProvider: serverAuthProvider,
340+
eventSourceInit: {
341+
fetch: (
342+
url: string | URL | globalThis.Request,
343+
init: RequestInit | undefined,
344+
) => fetch(url, { ...init, headers }),
345+
},
346+
requestInit: {
347+
headers,
348+
},
349+
// TODO these should be configurable...
350+
reconnectionOptions: {
351+
maxReconnectionDelay: 30000,
352+
initialReconnectionDelay: 1000,
353+
reconnectionDelayGrowFactor: 1.5,
354+
maxRetries: 2,
355+
},
356+
};
357+
break;
358+
}
359+
(mcpProxyServerUrl as URL).searchParams.append(
360+
"transportType",
361+
transportType,
362+
);
363+
334364
const clientTransport =
335365
transportType === "streamable-http"
336366
? new StreamableHTTPClientTransport(mcpProxyServerUrl as URL, {

0 commit comments

Comments
 (0)