11import { type AssistantConfig } from "@continuedev/sdk" ;
22import { Client } from "@modelcontextprotocol/sdk/client/index.js" ;
3+ import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js" ;
34import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js" ;
5+ import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js" ;
6+ import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js" ;
47
58import { getErrorString } from "../util/error.js" ;
69import { logger } from "../util/logger.js" ;
@@ -9,9 +12,9 @@ import { BaseService, ServiceWithDependencies } from "./BaseService.js";
912import { serviceContainer } from "./ServiceContainer.js" ;
1013import {
1114 MCPConnectionInfo ,
15+ MCPServerConfig ,
1216 MCPServiceState ,
1317 SERVICE_NAMES ,
14- MCPServerConfig ,
1518} from "./types.js" ;
1619
1720interface ServerConnection extends MCPConnectionInfo {
@@ -214,31 +217,12 @@ export class MCPService
214217 this . updateState ( ) ;
215218
216219 try {
217- if ( serverConfig . type && serverConfig . type !== "stdio" ) {
218- throw new Error (
219- `${ serverConfig . type } MCP servers are not yet supported in the Continue CLI` ,
220- ) ;
221- }
222- if ( ! serverConfig . command ) {
223- throw new Error ( "MCP server command is not specified" ) ;
224- }
225-
226220 const client = new Client (
227221 { name : "continue-cli-client" , version : "1.0.0" } ,
228222 { capabilities : { } } ,
229223 ) ;
230224
231- const env : Record < string , string > = serverConfig . env || { } ;
232- if ( process . env . PATH !== undefined ) {
233- env . PATH = process . env . PATH ;
234- }
235-
236- const transport = new StdioClientTransport ( {
237- command : serverConfig . command ,
238- args : serverConfig . args ,
239- env,
240- stderr : "ignore" ,
241- } ) ;
225+ const transport = await this . constructTransport ( serverConfig ) ;
242226
243227 logger . debug ( "Connecting to MCP server" , {
244228 name : serverName ,
@@ -363,4 +347,68 @@ export class MCPService
363347 logger . debug ( "Shutting down MCPService" ) ;
364348 await this . shutdownConnections ( ) ;
365349 }
350+
351+ /**
352+ * Construct transport based on server configuration
353+ */
354+ private async constructTransport (
355+ serverConfig : MCPServerConfig ,
356+ ) : Promise < Transport > {
357+ const transportType = serverConfig . type || "stdio" ;
358+
359+ switch ( transportType ) {
360+ case "stdio" :
361+ if ( ! serverConfig . command ) {
362+ throw new Error (
363+ "MCP server command is not specified for stdio transport" ,
364+ ) ;
365+ }
366+
367+ const env : Record < string , string > = serverConfig . env || { } ;
368+ if ( process . env . PATH !== undefined ) {
369+ env . PATH = process . env . PATH ;
370+ }
371+
372+ return new StdioClientTransport ( {
373+ command : serverConfig . command ,
374+ args : serverConfig . args || [ ] ,
375+ env,
376+ cwd : serverConfig . cwd ,
377+ stderr : "ignore" ,
378+ } ) ;
379+
380+ case "sse" :
381+ if ( ! serverConfig . url ) {
382+ throw new Error ( "MCP server URL is not specified for SSE transport" ) ;
383+ }
384+ return new SSEClientTransport ( new URL ( serverConfig . url ) , {
385+ eventSourceInit : {
386+ fetch : ( input , init ) =>
387+ fetch ( input , {
388+ ...init ,
389+ headers : {
390+ ...init ?. headers ,
391+ ...( serverConfig . requestOptions ?. headers as
392+ | Record < string , string >
393+ | undefined ) ,
394+ } ,
395+ } ) ,
396+ } ,
397+ requestInit : { headers : serverConfig . requestOptions ?. headers } ,
398+ } ) ;
399+
400+ case "streamable-http" :
401+ if ( ! serverConfig . url ) {
402+ throw new Error (
403+ "MCP server URL is not specified for streamable-http transport" ,
404+ ) ;
405+ }
406+ return new StreamableHTTPClientTransport ( new URL ( serverConfig . url ) , {
407+ requestInit : { headers : serverConfig . requestOptions ?. headers } ,
408+ } ) ;
409+
410+ default :
411+ throw new Error ( `Unsupported transport type: ${ transportType } ` ) ;
412+ }
413+ }
366414}
0 commit comments