11import { RESOURCE_MIME_TYPE , getToolUiResourceUri , type McpUiSandboxProxyReadyNotification , AppBridge , PostMessageTransport , type McpUiResourceCsp , type McpUiResourcePermissions , buildAllowAttribute , type McpUiUpdateModelContextRequest , type McpUiMessageRequest } from "@modelcontextprotocol/ext-apps/app-bridge" ;
22import { Client } from "@modelcontextprotocol/sdk/client/index.js" ;
3+ import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js" ;
34import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js" ;
45import type { CallToolResult , Tool } from "@modelcontextprotocol/sdk/types.js" ;
56
@@ -24,11 +25,8 @@ export interface ServerInfo {
2425
2526
2627export async function connectToServer ( serverUrl : URL ) : Promise < ServerInfo > {
27- const client = new Client ( IMPLEMENTATION ) ;
28-
2928 log . info ( "Connecting to server:" , serverUrl . href ) ;
30- await client . connect ( new StreamableHTTPClientTransport ( serverUrl ) ) ;
31- log . info ( "Connection successful" ) ;
29+ const client = await connectWithFallback ( serverUrl ) ;
3230
3331 const name = client . getServerVersion ( ) ?. name ?? serverUrl . href ;
3432
@@ -39,6 +37,28 @@ export async function connectToServer(serverUrl: URL): Promise<ServerInfo> {
3937 return { name, client, tools, appHtmlCache : new Map ( ) } ;
4038}
4139
40+ async function connectWithFallback ( serverUrl : URL ) : Promise < Client > {
41+ // Try Streamable HTTP first (modern transport)
42+ try {
43+ const client = new Client ( IMPLEMENTATION ) ;
44+ await client . connect ( new StreamableHTTPClientTransport ( serverUrl ) ) ;
45+ log . info ( "Connected via Streamable HTTP transport" ) ;
46+ return client ;
47+ } catch ( streamableError ) {
48+ log . info ( "Streamable HTTP failed:" , streamableError ) ;
49+ }
50+
51+ // Fall back to SSE (deprecated but needed for older servers)
52+ try {
53+ const client = new Client ( IMPLEMENTATION ) ;
54+ await client . connect ( new SSEClientTransport ( serverUrl ) ) ;
55+ log . info ( "Connected via SSE transport" ) ;
56+ return client ;
57+ } catch ( sseError ) {
58+ throw new Error ( `Could not connect with any transport. SSE error: ${ sseError } ` ) ;
59+ }
60+ }
61+
4262
4363interface UiResourceData {
4464 html : string ;
0 commit comments