@@ -16,33 +16,44 @@ app.use(cors());
16
16
17
17
let webAppTransports : SSEServerTransport [ ] = [ ] ;
18
18
19
- app . get ( "/sse" , async ( req , res ) => {
20
- console . log ( "New SSE connection" ) ;
21
- const transportType = req . query . transportType as string ;
22
- console . log ( `Transport type: ${ transportType } ` ) ;
19
+ const createTransport = async ( query : express . Request [ "query" ] ) => {
20
+ console . log ( "Query parameters:" , query ) ;
23
21
24
- let backingServerTransport ;
25
- console . log ( "Query parameters:" , req . query ) ;
22
+ const transportType = query . transportType as string ;
26
23
27
24
if ( transportType === "stdio" ) {
28
- const command = decodeURIComponent ( req . query . command as string ) ;
29
- const args = decodeURIComponent ( req . query . args as string ) . split ( "," ) ;
25
+ const command = decodeURIComponent ( query . command as string ) ;
26
+ const args = decodeURIComponent ( query . args as string ) . split ( "," ) ;
30
27
console . log ( `Stdio transport: command=${ command } , args=${ args } ` ) ;
31
- backingServerTransport = new StdioClientTransport ( ) ;
32
- await backingServerTransport . spawn ( { command, args } ) ;
28
+ const transport = new StdioClientTransport ( ) ;
29
+ await transport . spawn ( { command, args } ) ;
33
30
console . log ( "Spawned stdio transport" ) ;
31
+ return transport ;
34
32
} else if ( transportType === "sse" ) {
35
- const url = decodeURIComponent ( req . query . url as string ) ;
33
+ const url = decodeURIComponent ( query . url as string ) ;
36
34
console . log ( `SSE transport: url=${ url } ` ) ;
37
- backingServerTransport = new SSEClientTransport ( ) ;
38
- await backingServerTransport . connect ( new URL ( url ) ) ;
35
+ const transport = new SSEClientTransport ( ) ;
36
+ await transport . connect ( new URL ( url ) ) ;
39
37
console . log ( "Connected to SSE transport" ) ;
38
+ return transport ;
40
39
} else {
41
40
console . error ( `Invalid transport type: ${ transportType } ` ) ;
42
41
throw new Error ( "Invalid transport type specified" ) ;
43
42
}
43
+ } ;
44
+
45
+ app . get ( "/sse" , async ( req , res ) => {
46
+ console . log ( "New SSE connection" ) ;
47
+ const transportType = req . query . transportType as string ;
48
+ console . log ( `Transport type: ${ transportType } ` ) ;
49
+
50
+ const backingServerTransport = await createTransport ( req . query ) ;
51
+
52
+ console . log ( "Connected MCP client to backing server transport" ) ;
44
53
45
54
const webAppTransport = new SSEServerTransport ( "/message" ) ;
55
+ console . log ( "Created web app transport" ) ;
56
+
46
57
webAppTransports . push ( webAppTransport ) ;
47
58
console . log ( "Created web app transport" ) ;
48
59
0 commit comments