11import { experimental_createMCPClient } from "ai" ;
22import type { ToolSet } from "ai" ;
3- import type {
4- MCPServerConfig ,
5- MCPConnectionStatus ,
6- MCPClientWrapper ,
7- MCPToolInfo ,
8- } from "./types" ;
9- import { getEnabledMCPServers } from "../../config/mcp" ;
3+ import type { MCPServerConfig , MCPClientWrapper } from "./types" ;
4+
5+ const MCP_SERVERS : MCPServerConfig [ ] = [
6+ {
7+ name : "context7" ,
8+ transport : "sse" ,
9+ url : process . env . MCP_CONTEXT7_URL || "https://mcp.context7.com/sse" ,
10+ enabled : process . env . MCP_CONTEXT7_ENABLED !== "false" , // Default to enabled
11+ timeout : 30000 , // 30 second timeout
12+ headers : {
13+ "User-Agent" : "Shadow-Agent/1.0" ,
14+ } ,
15+ } ,
16+ ] ;
1017
1118export class MCPManager {
1219 private clients : Map < string , MCPClientWrapper > = new Map ( ) ;
1320 private isShuttingDown : boolean = false ;
1421
1522 async initializeConnections ( ) : Promise < void > {
16- const servers = getEnabledMCPServers ( ) ;
17-
18- const connectionPromises = servers . map ( async ( config ) => {
23+ const connectionPromises = MCP_SERVERS . map ( async ( config ) => {
1924 try {
2025 await this . connectToServer ( config ) ;
2126 } catch ( error ) {
@@ -34,10 +39,6 @@ export class MCPManager {
3439 } ) ;
3540
3641 await Promise . allSettled ( connectionPromises ) ;
37-
38- const connectedCount = Array . from ( this . clients . values ( ) ) . filter (
39- ( c ) => c . connected
40- ) . length ;
4142 }
4243
4344 /**
@@ -169,55 +170,9 @@ export class MCPManager {
169170 }
170171 }
171172
172- const toolCount = Object . keys ( toolSet ) . length ;
173-
174173 return toolSet ;
175174 }
176175
177- /**
178- * Get connection status for all servers
179- */
180- getConnectionStatus ( ) : MCPConnectionStatus [ ] {
181- return Array . from ( this . clients . values ( ) ) . map ( ( client ) => ( {
182- serverName : client . serverName ,
183- connected : client . connected ,
184- lastConnected : client . lastConnected ,
185- lastError : client . lastError ,
186- toolCount : 0 , // TODO: Track tool count per server
187- } ) ) ;
188- }
189-
190- /**
191- * Get list of available tools with metadata
192- */
193- async getToolInfo ( ) : Promise < MCPToolInfo [ ] > {
194- const tools : MCPToolInfo [ ] = [ ] ;
195-
196- for ( const client of this . clients . values ( ) ) {
197- if ( ! client . connected || ! client . client ) continue ;
198-
199- try {
200- const serverTools = await client . client . tools ( ) ;
201- if ( serverTools && typeof serverTools === "object" ) {
202- for ( const toolName of Object . keys ( serverTools ) ) {
203- tools . push ( {
204- name : `${ client . serverName } :${ toolName } ` ,
205- serverName : client . serverName ,
206- description : `Tool from ${ client . serverName } MCP server` ,
207- } ) ;
208- }
209- }
210- } catch ( error ) {
211- console . error (
212- `[MCP_MANAGER] Failed to get tool info from ${ client . serverName } :` ,
213- error
214- ) ;
215- }
216- }
217-
218- return tools ;
219- }
220-
221176 /**
222177 * Close all MCP connections
223178 */
@@ -242,48 +197,4 @@ export class MCPManager {
242197 await Promise . allSettled ( closePromises ) ;
243198 this . clients . clear ( ) ;
244199 }
245-
246- /**
247- * Reconnect to a specific server
248- */
249- async reconnectToServer ( serverName : string ) : Promise < boolean > {
250- const existingClient = this . clients . get ( serverName ) ;
251- if ( existingClient ?. connected ) {
252- return true ;
253- }
254-
255- // Close existing connection if any
256- if ( existingClient ?. client ) {
257- try {
258- await existingClient . client . close ( ) ;
259- } catch ( error ) {
260- console . error (
261- `[MCP_MANAGER] Error closing existing connection to ${ serverName } :` ,
262- error
263- ) ;
264- }
265- }
266-
267- // Find server config
268- const servers = getEnabledMCPServers ( ) ;
269- const config = servers . find ( ( s ) => s . name === serverName ) ;
270-
271- if ( ! config ) {
272- console . error (
273- `[MCP_MANAGER] No configuration found for server ${ serverName } `
274- ) ;
275- return false ;
276- }
277-
278- try {
279- await this . connectToServer ( config ) ;
280- return true ;
281- } catch ( error ) {
282- console . error (
283- `[MCP_MANAGER] Reconnection failed for ${ serverName } :` ,
284- error
285- ) ;
286- return false ;
287- }
288- }
289200}
0 commit comments