11import type { BaseConnector } from './connectors/base.js'
2+ import { readFileSync } from 'node:fs'
23import { HttpConnector } from './connectors/http.js'
4+ import { StdioConnector } from './connectors/stdio.js'
35import { WebSocketConnector } from './connectors/websocket.js'
46
5- // Check if we're in a Node.js environment
6- const isNodeJS = typeof process !== 'undefined'
7- && process . versions
8- && process . versions . node
9- && typeof window === 'undefined'
10-
11- // Conditionally import Node.js-specific modules
12- let StdioConnector : any
13- let readFileSync : any
14-
15- if ( isNodeJS ) {
16- try {
17- // Dynamic import for Node.js-specific modules
18- Promise . all ( [
19- import ( './connectors/stdio.js' ) ,
20- import ( 'node:fs' ) ,
21- ] ) . then ( ( [ stdioModule , fsModule ] ) => {
22- StdioConnector = stdioModule . StdioConnector
23- readFileSync = fsModule . readFileSync
24- } ) . catch ( ( error ) => {
25- console . warn ( 'Failed to load Node.js modules:' , error )
26- } )
27- }
28- catch ( error ) {
29- console . warn ( 'Failed to load Node.js modules:' , error )
30- }
31- }
32-
337export function loadConfigFile ( filepath : string ) : Record < string , any > {
34- if ( ! isNodeJS || ! readFileSync ) {
35- throw new Error ( 'loadConfigFile is only available in Node.js environments' )
36- }
37-
388 const raw = readFileSync ( filepath , 'utf-8' )
399 return JSON . parse ( raw )
4010}
@@ -43,10 +13,6 @@ export function createConnectorFromConfig(
4313 serverConfig : Record < string , any > ,
4414) : BaseConnector {
4515 if ( 'command' in serverConfig && 'args' in serverConfig ) {
46- if ( ! isNodeJS || ! StdioConnector ) {
47- throw new Error ( 'StdioConnector is not available in browser environments. Use HTTP or WebSocket connectors instead.' )
48- }
49-
5016 return new StdioConnector ( {
5117 command : serverConfig . command ,
5218 args : serverConfig . args ,
@@ -73,5 +39,5 @@ export function createConnectorFromConfig(
7339 } )
7440 }
7541
76- throw new Error ( 'Cannot determine connector type from config. Browser environments only support HTTP and WebSocket connectors. ' )
42+ throw new Error ( 'Cannot determine connector type from config' )
7743}
0 commit comments