1- import { Client } from "@modelcontextprotocol/sdk/client/index.js"
2- const packageJson = require ( "../../../../../package.json" )
3- const version : string = packageJson . version ?? "1.0.0"
41import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js"
5- import {
6- ListToolsResultSchema ,
7- ListResourcesResultSchema ,
8- ListResourceTemplatesResultSchema ,
9- } from "@modelcontextprotocol/sdk/types.js"
10- import { ConnectionHandler } from "../ConnectionHandler"
11- import {
12- ServerConfig ,
13- McpConnection ,
14- ConfigSource ,
15- McpTool ,
16- McpResource ,
17- McpResourceTemplate ,
18- McpServer ,
19- } from "../../types"
2+ import { ServerConfig , McpConnection } from "../../types"
3+ import { ConfigSource , McpServer } from "../../../../shared/mcp"
4+ import { BaseHandler } from "./base/BaseHandler"
205
216/**
227 * SSE connection handler
238 * Responsible for creating and managing MCP connections based on Server-Sent Events
249 */
25- export class SseHandler implements ConnectionHandler {
10+ export class SseHandler extends BaseHandler {
2611 /**
2712 * Check if a specific connection type is supported
2813 * @param type Connection type
@@ -51,15 +36,7 @@ export class SseHandler implements ConnectionHandler {
5136 }
5237
5338 // Create client
54- const client = new Client (
55- {
56- name : "Roo Code" ,
57- version,
58- } ,
59- {
60- capabilities : { } ,
61- } ,
62- )
39+ const client = this . createClient ( )
6340
6441 // Create transport
6542 const transport = new SSEClientTransport ( new URL ( config . url ) , {
@@ -79,6 +56,7 @@ export class SseHandler implements ConnectionHandler {
7956 status : "connecting" ,
8057 disabled : config . disabled ,
8158 source,
59+ errorHistory : [ ] ,
8260 } ,
8361 client,
8462 transport,
@@ -100,38 +78,20 @@ export class SseHandler implements ConnectionHandler {
10078 connection . server . resourceTemplates = await this . fetchResourceTemplatesList ( connection )
10179 } catch ( error ) {
10280 connection . server . status = "disconnected"
103- connection . server . error = error instanceof Error ? error . message : `${ error } `
81+ this . appendErrorMessage ( connection , error instanceof Error ? error . message : `${ error } ` )
10482 if ( onStatusChange ) onStatusChange ( connection . server )
10583 }
10684
10785 return connection
10886 }
10987
110- /**
111- * Close connection
112- * @param connection Connection to close
113- */
114- async closeConnection ( connection : McpConnection ) : Promise < void > {
115- try {
116- await connection . client . close ( )
117- } catch ( error ) {
118- console . error ( `Error disconnecting client for ${ connection . server . name } :` , error )
119- }
120-
121- try {
122- await connection . transport . close ( )
123- } catch ( error ) {
124- console . error ( `Error closing transport for ${ connection . server . name } :` , error )
125- }
126- }
127-
12888 /**
12989 * Setup error handling
13090 * @param connection MCP connection
13191 * @param transport SSE transport
13292 * @param onStatusChange
13393 */
134- private setupErrorHandling (
94+ protected setupErrorHandling (
13595 connection : McpConnection ,
13696 transport : SSEClientTransport ,
13797 onStatusChange ?: ( server : McpServer ) => void ,
@@ -151,70 +111,4 @@ export class SseHandler implements ConnectionHandler {
151111 if ( onStatusChange ) onStatusChange ( connection . server )
152112 }
153113 }
154-
155- /**
156- * Fetch tool list
157- * @param connection MCP connection
158- * @returns Tool list
159- */
160- private async fetchToolsList ( connection : McpConnection ) : Promise < McpTool [ ] > {
161- try {
162- const result = await connection . client . listTools ( )
163- const parsed = ListToolsResultSchema . parse ( result )
164-
165- return parsed . tools . map ( ( tool : any ) => ( {
166- name : tool . name ,
167- description : tool . description ,
168- inputSchema : tool . input_schema as object | undefined ,
169- alwaysAllow : false ,
170- } ) )
171- } catch ( error ) {
172- // console.error(`Failed to fetch tools list for ${connection.server.name}:`, error)
173- return [ ]
174- }
175- }
176-
177- /**
178- * Fetch resource list
179- * @param connection MCP connection
180- * @returns Resource list
181- */
182- private async fetchResourcesList ( connection : McpConnection ) : Promise < McpResource [ ] > {
183- try {
184- const result = await connection . client . listResources ( )
185- const parsed = ListResourcesResultSchema . parse ( result )
186-
187- return parsed . resources . map ( ( resource : any ) => ( {
188- uri : resource . uri ,
189- name : resource . name ,
190- mimeType : resource . mime_type as string | undefined ,
191- description : resource . description ,
192- } ) )
193- } catch ( error ) {
194- // console.error(`Failed to fetch resources list for ${connection.server.name}:`, error)
195- return [ ]
196- }
197- }
198-
199- /**
200- * Fetch resource template list
201- * @param connection MCP connection
202- * @returns Resource template list
203- */
204- private async fetchResourceTemplatesList ( connection : McpConnection ) : Promise < McpResourceTemplate [ ] > {
205- try {
206- const result = await connection . client . listResourceTemplates ( )
207- const parsed = ListResourceTemplatesResultSchema . parse ( result )
208-
209- return ( parsed as any ) . templates . map ( ( template : any ) => ( {
210- uri : template . uri ,
211- name : template . name ,
212- description : template . description ,
213- inputSchema : template . input_schema as object | undefined ,
214- } ) )
215- } catch ( error ) {
216- // console.error(`Failed to fetch resource templates list for ${connection.server.name}:`, error)
217- return [ ]
218- }
219- }
220114}
0 commit comments