mcp-use 0.1.20 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/dist/src/browser.d.ts +49 -0
  2. package/dist/src/browser.d.ts.map +1 -0
  3. package/dist/src/browser.js +75 -0
  4. package/dist/src/client/base.d.ts +32 -0
  5. package/dist/src/client/base.d.ts.map +1 -0
  6. package/dist/src/client/base.js +119 -0
  7. package/dist/src/client.d.ts +19 -16
  8. package/dist/src/client.d.ts.map +1 -1
  9. package/dist/src/client.js +24 -107
  10. package/dist/src/logging.d.ts +1 -1
  11. package/dist/src/logging.d.ts.map +1 -1
  12. package/dist/src/logging.js +31 -16
  13. package/dist/src/managers/server_manager.js +1 -1
  14. package/dist/src/oauth-helper.d.ts +135 -0
  15. package/dist/src/oauth-helper.d.ts.map +1 -0
  16. package/dist/src/oauth-helper.js +427 -0
  17. package/package.json +6 -1
  18. package/dist/examples/add_server_tool.d.ts +0 -8
  19. package/dist/examples/add_server_tool.d.ts.map +0 -1
  20. package/dist/examples/add_server_tool.js +0 -79
  21. package/dist/examples/ai_sdk_example.d.ts +0 -23
  22. package/dist/examples/ai_sdk_example.d.ts.map +0 -1
  23. package/dist/examples/ai_sdk_example.js +0 -213
  24. package/dist/examples/airbnb_use.d.ts +0 -10
  25. package/dist/examples/airbnb_use.d.ts.map +0 -1
  26. package/dist/examples/airbnb_use.js +0 -43
  27. package/dist/examples/blender_use.d.ts +0 -15
  28. package/dist/examples/blender_use.d.ts.map +0 -1
  29. package/dist/examples/blender_use.js +0 -39
  30. package/dist/examples/browser_use.d.ts +0 -10
  31. package/dist/examples/browser_use.d.ts.map +0 -1
  32. package/dist/examples/browser_use.js +0 -46
  33. package/dist/examples/chat_example.d.ts +0 -10
  34. package/dist/examples/chat_example.d.ts.map +0 -1
  35. package/dist/examples/chat_example.js +0 -86
  36. package/dist/examples/filesystem_use.d.ts +0 -11
  37. package/dist/examples/filesystem_use.d.ts.map +0 -1
  38. package/dist/examples/filesystem_use.js +0 -43
  39. package/dist/examples/http_example.d.ts +0 -18
  40. package/dist/examples/http_example.d.ts.map +0 -1
  41. package/dist/examples/http_example.js +0 -37
  42. package/dist/examples/mcp_everything.d.ts +0 -6
  43. package/dist/examples/mcp_everything.d.ts.map +0 -1
  44. package/dist/examples/mcp_everything.js +0 -25
  45. package/dist/examples/multi_server_example.d.ts +0 -10
  46. package/dist/examples/multi_server_example.d.ts.map +0 -1
  47. package/dist/examples/multi_server_example.js +0 -51
  48. package/dist/examples/observability.d.ts +0 -6
  49. package/dist/examples/observability.d.ts.map +0 -1
  50. package/dist/examples/observability.js +0 -50
  51. package/dist/examples/stream_example.d.ts +0 -12
  52. package/dist/examples/stream_example.d.ts.map +0 -1
  53. package/dist/examples/stream_example.js +0 -198
  54. package/dist/examples/structured_output.d.ts +0 -9
  55. package/dist/examples/structured_output.d.ts.map +0 -1
  56. package/dist/examples/structured_output.js +0 -95
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Browser-compatible exports for mcp-use
3
+ *
4
+ * This module provides browser-safe versions of mcp-use components
5
+ * that avoid Node.js-specific dependencies (like fs, path for file operations).
6
+ *
7
+ * The actual MCP protocol connectors (WebSocket, HTTP/SSE) work fine in browsers.
8
+ */
9
+ import type { BaseConnector } from './connectors/base.js';
10
+ import { BaseMCPClient } from './client/base.js';
11
+ import { HttpConnector } from './connectors/http.js';
12
+ import { WebSocketConnector } from './connectors/websocket.js';
13
+ import { logger } from './logging.js';
14
+ import { MCPSession } from './session.js';
15
+ /**
16
+ * Browser-compatible MCP Client
17
+ *
18
+ * Unlike the Node.js version, this doesn't support:
19
+ * - Loading config from files (loadConfigFile)
20
+ * - Saving config to files (saveConfig)
21
+ * - StdioConnector (requires child_process)
22
+ *
23
+ * Supported connectors:
24
+ * - WebSocketConnector: Connect to MCP servers via WebSocket
25
+ * - HttpConnector: Connect to MCP servers via HTTP/SSE
26
+ */
27
+ export declare class MCPClient extends BaseMCPClient {
28
+ constructor(config?: Record<string, any>);
29
+ static fromDict(cfg: Record<string, any>): MCPClient;
30
+ /**
31
+ * Create a connector from server configuration (browser-safe version)
32
+ *
33
+ * Supports:
34
+ * - WebSocket connections: { ws_url: "ws://..." }
35
+ * - HTTP connections: { url: "http://..." }
36
+ *
37
+ * Does NOT support:
38
+ * - Stdio connections: { command: "...", args: [...] }
39
+ */
40
+ protected createConnectorFromConfig(serverConfig: Record<string, any>): BaseConnector;
41
+ }
42
+ export { HttpConnector, WebSocketConnector };
43
+ export { MCPSession };
44
+ export { logger };
45
+ export { createOAuthMCPConfig, LINEAR_OAUTH_CONFIG, OAuthHelper } from './oauth-helper.js';
46
+ export type { ClientRegistration, OAuthConfig, OAuthDiscovery, OAuthResult, OAuthState } from './oauth-helper.js';
47
+ export type { AIMessage, BaseMessage, HumanMessage, SystemMessage, ToolMessage } from '@langchain/core/messages';
48
+ export type { StreamEvent } from '@langchain/core/tracers/log_stream';
49
+ //# sourceMappingURL=browser.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser.d.ts","sourceRoot":"","sources":["../../src/browser.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAA;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AACrC,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAEzC;;;;;;;;;;;GAWG;AACH,qBAAa,SAAU,SAAQ,aAAa;gBAC9B,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;WAI1B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,SAAS;IAI3D;;;;;;;;;OASG;IACH,SAAS,CAAC,yBAAyB,CAAC,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,aAAa;CA8BtF;AAGD,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,CAAA;AAG5C,OAAO,EAAE,UAAU,EAAE,CAAA;AAGrB,OAAO,EAAE,MAAM,EAAE,CAAA;AAGjB,OAAO,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC1F,YAAY,EAAE,kBAAkB,EAAE,WAAW,EAAE,cAAc,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAGjH,YAAY,EAAE,SAAS,EAAE,WAAW,EAAE,YAAY,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAA;AAChH,YAAY,EAAE,WAAW,EAAE,MAAM,oCAAoC,CAAA"}
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Browser-compatible exports for mcp-use
3
+ *
4
+ * This module provides browser-safe versions of mcp-use components
5
+ * that avoid Node.js-specific dependencies (like fs, path for file operations).
6
+ *
7
+ * The actual MCP protocol connectors (WebSocket, HTTP/SSE) work fine in browsers.
8
+ */
9
+ import { BaseMCPClient } from './client/base.js';
10
+ import { HttpConnector } from './connectors/http.js';
11
+ import { WebSocketConnector } from './connectors/websocket.js';
12
+ import { logger } from './logging.js';
13
+ import { MCPSession } from './session.js';
14
+ /**
15
+ * Browser-compatible MCP Client
16
+ *
17
+ * Unlike the Node.js version, this doesn't support:
18
+ * - Loading config from files (loadConfigFile)
19
+ * - Saving config to files (saveConfig)
20
+ * - StdioConnector (requires child_process)
21
+ *
22
+ * Supported connectors:
23
+ * - WebSocketConnector: Connect to MCP servers via WebSocket
24
+ * - HttpConnector: Connect to MCP servers via HTTP/SSE
25
+ */
26
+ export class MCPClient extends BaseMCPClient {
27
+ constructor(config) {
28
+ super(config);
29
+ }
30
+ static fromDict(cfg) {
31
+ return new MCPClient(cfg);
32
+ }
33
+ /**
34
+ * Create a connector from server configuration (browser-safe version)
35
+ *
36
+ * Supports:
37
+ * - WebSocket connections: { ws_url: "ws://..." }
38
+ * - HTTP connections: { url: "http://..." }
39
+ *
40
+ * Does NOT support:
41
+ * - Stdio connections: { command: "...", args: [...] }
42
+ */
43
+ createConnectorFromConfig(serverConfig) {
44
+ // WebSocket connector
45
+ if ('ws_url' in serverConfig) {
46
+ return new WebSocketConnector(serverConfig.ws_url, {
47
+ headers: serverConfig.headers,
48
+ authToken: serverConfig.auth_token || serverConfig.authToken,
49
+ });
50
+ }
51
+ // HTTP/SSE connector
52
+ if ('url' in serverConfig) {
53
+ const transport = serverConfig.transport || 'http';
54
+ return new HttpConnector(serverConfig.url, {
55
+ headers: serverConfig.headers,
56
+ authToken: serverConfig.auth_token || serverConfig.authToken,
57
+ preferSse: serverConfig.preferSse || transport === 'sse',
58
+ });
59
+ }
60
+ // Stdio is not supported in browser
61
+ if ('command' in serverConfig && 'args' in serverConfig) {
62
+ throw new Error('StdioConnector is not supported in browser environments. '
63
+ + 'Use WebSocket (ws_url) or HTTP (url) connectors instead.');
64
+ }
65
+ throw new Error('Cannot determine connector type from config. Use "url" for HTTP or "ws_url" for WebSocket.');
66
+ }
67
+ }
68
+ // Re-export browser-safe connectors
69
+ export { HttpConnector, WebSocketConnector };
70
+ // Re-export session
71
+ export { MCPSession };
72
+ // Re-export logger (already browser-safe)
73
+ export { logger };
74
+ // Re-export OAuth helper for browser authentication
75
+ export { createOAuthMCPConfig, LINEAR_OAUTH_CONFIG, OAuthHelper } from './oauth-helper.js';
@@ -0,0 +1,32 @@
1
+ import type { BaseConnector } from '../connectors/base.js';
2
+ import { MCPSession } from '../session.js';
3
+ /**
4
+ * Base MCPClient class with shared functionality
5
+ *
6
+ * This class contains all the common logic that works in both Node.js and browser environments.
7
+ * Platform-specific implementations should extend this class and override methods as needed.
8
+ */
9
+ export declare abstract class BaseMCPClient {
10
+ protected config: Record<string, any>;
11
+ protected sessions: Record<string, MCPSession>;
12
+ activeSessions: string[];
13
+ constructor(config?: Record<string, any>);
14
+ static fromDict(_cfg: Record<string, any>): BaseMCPClient;
15
+ addServer(name: string, serverConfig: Record<string, any>): void;
16
+ removeServer(name: string): void;
17
+ getServerNames(): string[];
18
+ getServerConfig(name: string): Record<string, any>;
19
+ getConfig(): Record<string, any>;
20
+ /**
21
+ * Create a connector from server configuration
22
+ * This method must be implemented by platform-specific subclasses
23
+ */
24
+ protected abstract createConnectorFromConfig(serverConfig: Record<string, any>): BaseConnector;
25
+ createSession(serverName: string, autoInitialize?: boolean): Promise<MCPSession>;
26
+ createAllSessions(autoInitialize?: boolean): Promise<Record<string, MCPSession>>;
27
+ getSession(serverName: string): MCPSession | null;
28
+ getAllActiveSessions(): Record<string, MCPSession>;
29
+ closeSession(serverName: string): Promise<void>;
30
+ closeAllSessions(): Promise<void>;
31
+ }
32
+ //# sourceMappingURL=base.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../src/client/base.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAA;AAE1D,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAE1C;;;;;GAKG;AACH,8BAAsB,aAAa;IACjC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAK;IAC1C,SAAS,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAK;IAC5C,cAAc,EAAE,MAAM,EAAE,CAAK;gBAExB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;WAM1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,aAAa;IAKzD,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI;IAKhE,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAOhC,cAAc,IAAI,MAAM,EAAE;IAI1B,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAIlD,SAAS,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAIvC;;;OAGG;IACH,SAAS,CAAC,QAAQ,CAAC,yBAAyB,CAAC,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,aAAa;IAEjF,aAAa,CACxB,UAAU,EAAE,MAAM,EAClB,cAAc,UAAO,GACpB,OAAO,CAAC,UAAU,CAAC;IAyBT,iBAAiB,CAC5B,cAAc,UAAO,GACpB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IAc/B,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI;IAQjD,oBAAoB,IAAI,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC;IAM5C,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAmB/C,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC;CAqB/C"}
@@ -0,0 +1,119 @@
1
+ import { logger } from '../logging.js';
2
+ import { MCPSession } from '../session.js';
3
+ /**
4
+ * Base MCPClient class with shared functionality
5
+ *
6
+ * This class contains all the common logic that works in both Node.js and browser environments.
7
+ * Platform-specific implementations should extend this class and override methods as needed.
8
+ */
9
+ export class BaseMCPClient {
10
+ config = {};
11
+ sessions = {};
12
+ activeSessions = [];
13
+ constructor(config) {
14
+ if (config) {
15
+ this.config = config;
16
+ }
17
+ }
18
+ static fromDict(_cfg) {
19
+ // This will be overridden by concrete implementations
20
+ throw new Error('fromDict must be implemented by concrete class');
21
+ }
22
+ addServer(name, serverConfig) {
23
+ this.config.mcpServers = this.config.mcpServers || {};
24
+ this.config.mcpServers[name] = serverConfig;
25
+ }
26
+ removeServer(name) {
27
+ if (this.config.mcpServers?.[name]) {
28
+ delete this.config.mcpServers[name];
29
+ this.activeSessions = this.activeSessions.filter(n => n !== name);
30
+ }
31
+ }
32
+ getServerNames() {
33
+ return Object.keys(this.config.mcpServers ?? {});
34
+ }
35
+ getServerConfig(name) {
36
+ return this.config.mcpServers?.[name];
37
+ }
38
+ getConfig() {
39
+ return this.config ?? {};
40
+ }
41
+ async createSession(serverName, autoInitialize = true) {
42
+ const servers = this.config.mcpServers ?? {};
43
+ if (Object.keys(servers).length === 0) {
44
+ logger.warn('No MCP servers defined in config');
45
+ }
46
+ if (!servers[serverName]) {
47
+ throw new Error(`Server '${serverName}' not found in config`);
48
+ }
49
+ const connector = this.createConnectorFromConfig(servers[serverName]);
50
+ const session = new MCPSession(connector);
51
+ if (autoInitialize) {
52
+ await session.initialize();
53
+ }
54
+ this.sessions[serverName] = session;
55
+ if (!this.activeSessions.includes(serverName)) {
56
+ this.activeSessions.push(serverName);
57
+ }
58
+ return session;
59
+ }
60
+ async createAllSessions(autoInitialize = true) {
61
+ const servers = this.config.mcpServers ?? {};
62
+ if (Object.keys(servers).length === 0) {
63
+ logger.warn('No MCP servers defined in config');
64
+ }
65
+ for (const name of Object.keys(servers)) {
66
+ await this.createSession(name, autoInitialize);
67
+ }
68
+ return this.sessions;
69
+ }
70
+ getSession(serverName) {
71
+ const session = this.sessions[serverName];
72
+ if (!session) {
73
+ return null;
74
+ }
75
+ return session;
76
+ }
77
+ getAllActiveSessions() {
78
+ return Object.fromEntries(this.activeSessions.map(n => [n, this.sessions[n]]));
79
+ }
80
+ async closeSession(serverName) {
81
+ const session = this.sessions[serverName];
82
+ if (!session) {
83
+ logger.warn(`No session exists for server ${serverName}, nothing to close`);
84
+ return;
85
+ }
86
+ try {
87
+ logger.debug(`Closing session for server ${serverName}`);
88
+ await session.disconnect();
89
+ }
90
+ catch (e) {
91
+ logger.error(`Error closing session for server '${serverName}': ${e}`);
92
+ }
93
+ finally {
94
+ delete this.sessions[serverName];
95
+ this.activeSessions = this.activeSessions.filter(n => n !== serverName);
96
+ }
97
+ }
98
+ async closeAllSessions() {
99
+ const serverNames = Object.keys(this.sessions);
100
+ const errors = [];
101
+ for (const serverName of serverNames) {
102
+ try {
103
+ logger.debug(`Closing session for server ${serverName}`);
104
+ await this.closeSession(serverName);
105
+ }
106
+ catch (e) {
107
+ const errorMsg = `Failed to close session for server '${serverName}': ${e}`;
108
+ logger.error(errorMsg);
109
+ errors.push(errorMsg);
110
+ }
111
+ }
112
+ if (errors.length) {
113
+ logger.error(`Encountered ${errors.length} errors while closing sessions`);
114
+ }
115
+ else {
116
+ logger.debug('All sessions closed successfully');
117
+ }
118
+ }
119
+ }
@@ -1,22 +1,25 @@
1
- import { MCPSession } from './session.js';
2
- export declare class MCPClient {
3
- private config;
4
- private sessions;
5
- activeSessions: string[];
1
+ import type { BaseConnector } from './connectors/base.js';
2
+ import { BaseMCPClient } from './client/base.js';
3
+ /**
4
+ * Node.js-specific MCPClient implementation
5
+ *
6
+ * Extends the base client with Node.js-specific features like:
7
+ * - File system operations (saveConfig)
8
+ * - Config file loading (fromConfigFile)
9
+ * - All connector types including StdioConnector
10
+ */
11
+ export declare class MCPClient extends BaseMCPClient {
6
12
  constructor(config?: string | Record<string, any>);
7
13
  static fromDict(cfg: Record<string, any>): MCPClient;
8
14
  static fromConfigFile(path: string): MCPClient;
9
- addServer(name: string, serverConfig: Record<string, any>): void;
10
- removeServer(name: string): void;
11
- getServerNames(): string[];
12
- getServerConfig(name: string): Record<string, any>;
13
- getConfig(): Record<string, any>;
15
+ /**
16
+ * Save configuration to a file (Node.js only)
17
+ */
14
18
  saveConfig(filepath: string): void;
15
- createSession(serverName: string, autoInitialize?: boolean): Promise<MCPSession>;
16
- createAllSessions(autoInitialize?: boolean): Promise<Record<string, MCPSession>>;
17
- getSession(serverName: string): MCPSession | null;
18
- getAllActiveSessions(): Record<string, MCPSession>;
19
- closeSession(serverName: string): Promise<void>;
20
- closeAllSessions(): Promise<void>;
19
+ /**
20
+ * Create a connector from server configuration (Node.js version)
21
+ * Supports all connector types including StdioConnector
22
+ */
23
+ protected createConnectorFromConfig(serverConfig: Record<string, any>): BaseConnector;
21
24
  }
22
25
  //# sourceMappingURL=client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAEzC,qBAAa,SAAS;IACpB,OAAO,CAAC,MAAM,CAA0B;IACxC,OAAO,CAAC,QAAQ,CAAiC;IAC1C,cAAc,EAAE,MAAM,EAAE,CAAK;gBAExB,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;WAWnC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,SAAS;WAI7C,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS;IAI9C,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI;IAKhE,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAOhC,cAAc,IAAI,MAAM,EAAE;IAI1B,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAIlD,SAAS,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAIhC,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAQ5B,aAAa,CACxB,UAAU,EAAE,MAAM,EAClB,cAAc,UAAO,GACpB,OAAO,CAAC,UAAU,CAAC;IAwBT,iBAAiB,CAC5B,cAAc,UAAO,GACpB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IAc/B,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI;IAWjD,oBAAoB,IAAI,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC;IAM5C,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAmB/C,gBAAgB,IAAI,OAAO,CAAC,IAAI,CAAC;CAqB/C"}
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;AAGzD,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAA;AAGhD;;;;;;;GAOG;AACH,qBAAa,SAAU,SAAQ,aAAa;gBAC9B,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;WAcnC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,SAAS;WAI7C,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS;IAIrD;;OAEG;IACI,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI;IAQzC;;;OAGG;IACH,SAAS,CAAC,yBAAyB,CAAC,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,aAAa;CAGtF"}
@@ -1,21 +1,28 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
+ import { BaseMCPClient } from './client/base.js';
3
4
  import { createConnectorFromConfig, loadConfigFile } from './config.js';
4
- import { logger } from './logging.js';
5
- import { MCPSession } from './session.js';
6
- export class MCPClient {
7
- config = {};
8
- sessions = {};
9
- activeSessions = [];
5
+ /**
6
+ * Node.js-specific MCPClient implementation
7
+ *
8
+ * Extends the base client with Node.js-specific features like:
9
+ * - File system operations (saveConfig)
10
+ * - Config file loading (fromConfigFile)
11
+ * - All connector types including StdioConnector
12
+ */
13
+ export class MCPClient extends BaseMCPClient {
10
14
  constructor(config) {
11
15
  if (config) {
12
16
  if (typeof config === 'string') {
13
- this.config = loadConfigFile(config);
17
+ super(loadConfigFile(config));
14
18
  }
15
19
  else {
16
- this.config = config;
20
+ super(config);
17
21
  }
18
22
  }
23
+ else {
24
+ super();
25
+ }
19
26
  }
20
27
  static fromDict(cfg) {
21
28
  return new MCPClient(cfg);
@@ -23,25 +30,9 @@ export class MCPClient {
23
30
  static fromConfigFile(path) {
24
31
  return new MCPClient(loadConfigFile(path));
25
32
  }
26
- addServer(name, serverConfig) {
27
- this.config.mcpServers = this.config.mcpServers || {};
28
- this.config.mcpServers[name] = serverConfig;
29
- }
30
- removeServer(name) {
31
- if (this.config.mcpServers?.[name]) {
32
- delete this.config.mcpServers[name];
33
- this.activeSessions = this.activeSessions.filter(n => n !== name);
34
- }
35
- }
36
- getServerNames() {
37
- return Object.keys(this.config.mcpServers ?? {});
38
- }
39
- getServerConfig(name) {
40
- return this.config.mcpServers?.[name];
41
- }
42
- getConfig() {
43
- return this.config ?? {};
44
- }
33
+ /**
34
+ * Save configuration to a file (Node.js only)
35
+ */
45
36
  saveConfig(filepath) {
46
37
  const dir = path.dirname(filepath);
47
38
  if (!fs.existsSync(dir)) {
@@ -49,85 +40,11 @@ export class MCPClient {
49
40
  }
50
41
  fs.writeFileSync(filepath, JSON.stringify(this.config, null, 2), 'utf-8');
51
42
  }
52
- async createSession(serverName, autoInitialize = true) {
53
- const servers = this.config.mcpServers ?? {};
54
- if (Object.keys(servers).length === 0) {
55
- logger.warn('No MCP servers defined in config');
56
- }
57
- if (!servers[serverName]) {
58
- throw new Error(`Server '${serverName}' not found in config`);
59
- }
60
- const connector = createConnectorFromConfig(servers[serverName]);
61
- const session = new MCPSession(connector);
62
- if (autoInitialize) {
63
- await session.initialize();
64
- }
65
- this.sessions[serverName] = session;
66
- if (!this.activeSessions.includes(serverName)) {
67
- this.activeSessions.push(serverName);
68
- }
69
- return session;
70
- }
71
- async createAllSessions(autoInitialize = true) {
72
- const servers = this.config.mcpServers ?? {};
73
- if (Object.keys(servers).length === 0) {
74
- logger.warn('No MCP servers defined in config');
75
- }
76
- for (const name of Object.keys(servers)) {
77
- await this.createSession(name, autoInitialize);
78
- }
79
- return this.sessions;
80
- }
81
- getSession(serverName) {
82
- const session = this.sessions[serverName];
83
- // if (!session) {
84
- // throw new Error(`No session exists for server '${serverName}'`)
85
- // }
86
- if (!session) {
87
- return null;
88
- }
89
- return session;
90
- }
91
- getAllActiveSessions() {
92
- return Object.fromEntries(this.activeSessions.map(n => [n, this.sessions[n]]));
93
- }
94
- async closeSession(serverName) {
95
- const session = this.sessions[serverName];
96
- if (!session) {
97
- logger.warn(`No session exists for server ${serverName}, nothing to close`);
98
- return;
99
- }
100
- try {
101
- logger.debug(`Closing session for server ${serverName}`);
102
- await session.disconnect();
103
- }
104
- catch (e) {
105
- logger.error(`Error closing session for server '${serverName}': ${e}`);
106
- }
107
- finally {
108
- delete this.sessions[serverName];
109
- this.activeSessions = this.activeSessions.filter(n => n !== serverName);
110
- }
111
- }
112
- async closeAllSessions() {
113
- const serverNames = Object.keys(this.sessions);
114
- const errors = [];
115
- for (const serverName of serverNames) {
116
- try {
117
- logger.debug(`Closing session for server ${serverName}`);
118
- await this.closeSession(serverName);
119
- }
120
- catch (e) {
121
- const errorMsg = `Failed to close session for server '${serverName}': ${e}`;
122
- logger.error(errorMsg);
123
- errors.push(errorMsg);
124
- }
125
- }
126
- if (errors.length) {
127
- logger.error(`Encountered ${errors.length} errors while closing sessions`);
128
- }
129
- else {
130
- logger.debug('All sessions closed successfully');
131
- }
43
+ /**
44
+ * Create a connector from server configuration (Node.js version)
45
+ * Supports all connector types including StdioConnector
46
+ */
47
+ createConnectorFromConfig(serverConfig) {
48
+ return createConnectorFromConfig(serverConfig);
132
49
  }
133
50
  }
@@ -28,7 +28,7 @@ export declare class Logger {
28
28
  private static currentFormat;
29
29
  static get(name?: string): WinstonLogger | SimpleConsoleLogger;
30
30
  private static getFormatter;
31
- static configure(options?: LoggerOptions): void;
31
+ static configure(options?: LoggerOptions): Promise<void>;
32
32
  static setDebug(enabled: boolean | 0 | 1 | 2): void;
33
33
  static setFormat(format: 'minimal' | 'detailed' | 'emoji'): void;
34
34
  }
@@ -1 +1 @@
1
- {"version":3,"file":"logging.d.ts","sourceRoot":"","sources":["../../src/logging.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,IAAI,aAAa,EAAE,MAAM,SAAS,CAAA;AAOtD,MAAM,MAAM,QAAQ,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,OAAO,GAAG,OAAO,CAAA;AAEzF,UAAU,aAAa;IACrB,KAAK,CAAC,EAAE,QAAQ,CAAA;IAChB,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,MAAM,CAAC,EAAE,SAAS,GAAG,UAAU,GAAG,OAAO,CAAA;CAC1C;AAsCD,cAAM,mBAAmB;IACvB,OAAO,CAAC,MAAM,CAAU;IACxB,OAAO,CAAC,IAAI,CAAQ;gBAER,IAAI,GAAE,MAA4B,EAAE,KAAK,GAAE,QAAiB;IAKxE,OAAO,CAAC,SAAS;IAOjB,OAAO,CAAC,aAAa;IAKrB,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAM5B,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAM3B,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAM3B,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAM5B,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAM3B,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAM9B,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAO5B,IAAI,KAAK,IAAI,QAAQ,CAEpB;IAED,IAAI,KAAK,CAAC,QAAQ,EAAE,QAAQ,EAE3B;CACF;AA4BD,qBAAa,MAAM;IACjB,OAAO,CAAC,MAAM,CAAC,SAAS,CAA0D;IAClF,OAAO,CAAC,MAAM,CAAC,eAAe,CAA0C;IACxE,OAAO,CAAC,MAAM,CAAC,aAAa,CAA8C;WAE5D,GAAG,CAAC,IAAI,GAAE,MAA4B,GAAG,aAAa,GAAG,mBAAmB;IA4B1F,OAAO,CAAC,MAAM,CAAC,YAAY;WAab,SAAS,CAAC,OAAO,GAAE,aAAkB,GAAG,IAAI;WAiD5C,QAAQ,CAAC,OAAO,EAAE,OAAO,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI;WAyB5C,SAAS,CAAC,MAAM,EAAE,SAAS,GAAG,UAAU,GAAG,OAAO,GAAG,IAAI;CAIxE;AAWD,eAAO,MAAM,MAAM,qCAAe,CAAA"}
1
+ {"version":3,"file":"logging.d.ts","sourceRoot":"","sources":["../../src/logging.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,IAAI,aAAa,EAAE,MAAM,SAAS,CAAA;AAqBtD,MAAM,MAAM,QAAQ,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,OAAO,GAAG,OAAO,CAAA;AAEzF,UAAU,aAAa;IACrB,KAAK,CAAC,EAAE,QAAQ,CAAA;IAChB,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,MAAM,CAAC,EAAE,SAAS,GAAG,UAAU,GAAG,OAAO,CAAA;CAC1C;AAqCD,cAAM,mBAAmB;IACvB,OAAO,CAAC,MAAM,CAAU;IACxB,OAAO,CAAC,IAAI,CAAQ;gBAER,IAAI,GAAE,MAA4B,EAAE,KAAK,GAAE,QAAiB;IAKxE,OAAO,CAAC,SAAS;IAOjB,OAAO,CAAC,aAAa;IAKrB,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAM5B,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAM3B,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAM3B,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAM5B,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAM3B,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAM9B,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAO5B,IAAI,KAAK,IAAI,QAAQ,CAEpB;IAED,IAAI,KAAK,CAAC,QAAQ,EAAE,QAAQ,EAE3B;CACF;AA4BD,qBAAa,MAAM;IACjB,OAAO,CAAC,MAAM,CAAC,SAAS,CAA0D;IAClF,OAAO,CAAC,MAAM,CAAC,eAAe,CAA0C;IACxE,OAAO,CAAC,MAAM,CAAC,aAAa,CAA8C;WAE5D,GAAG,CAAC,IAAI,GAAE,MAA4B,GAAG,aAAa,GAAG,mBAAmB;IA4B1F,OAAO,CAAC,MAAM,CAAC,YAAY;WAaP,SAAS,CAAC,OAAO,GAAE,aAAkB,GAAG,OAAO,CAAC,IAAI,CAAC;WAqD3D,QAAQ,CAAC,OAAO,EAAE,OAAO,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI;WAyB5C,SAAS,CAAC,MAAM,EAAE,SAAS,GAAG,UAAU,GAAG,OAAO,GAAG,IAAI;CAIxE;AAWD,eAAO,MAAM,MAAM,qCAAe,CAAA"}
@@ -1,6 +1,19 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
1
  import { createLogger, format, transports } from 'winston';
2
+ // Conditional imports for Node.js-only modules
3
+ async function getNodeModules() {
4
+ if (typeof process !== 'undefined' && process.platform) {
5
+ try {
6
+ // Use dynamic imports for Node.js environments
7
+ const fs = await import('node:fs');
8
+ const path = await import('node:path');
9
+ return { fs: fs.default, path: path.default };
10
+ }
11
+ catch {
12
+ return { fs: null, path: null };
13
+ }
14
+ }
15
+ return { fs: null, path: null };
16
+ }
4
17
  const { combine, timestamp, label, printf, colorize, splat } = format;
5
18
  const DEFAULT_LOGGER_NAME = 'mcp-use';
6
19
  // Environment detection function (similar to telemetry)
@@ -19,8 +32,7 @@ function isNodeJSEnvironment() {
19
32
  && typeof process.platform !== 'undefined'
20
33
  && typeof __dirname !== 'undefined');
21
34
  // Check for Node.js modules
22
- const hasNodeModules = (typeof fs !== 'undefined'
23
- && typeof createLogger === 'function');
35
+ const hasNodeModules = (typeof createLogger === 'function');
24
36
  return hasNodeGlobals && hasNodeModules;
25
37
  }
26
38
  catch {
@@ -57,27 +69,27 @@ class SimpleConsoleLogger {
57
69
  }
58
70
  info(message) {
59
71
  if (this.shouldLog('info')) {
60
- console.info(this.formatMessage('info', message)); // eslint-disable-line no-console
72
+ console.info(this.formatMessage('info', message));
61
73
  }
62
74
  }
63
75
  debug(message) {
64
76
  if (this.shouldLog('debug')) {
65
- console.debug(this.formatMessage('debug', message)); // eslint-disable-line no-console
77
+ console.debug(this.formatMessage('debug', message));
66
78
  }
67
79
  }
68
80
  http(message) {
69
81
  if (this.shouldLog('http')) {
70
- console.log(this.formatMessage('http', message)); // eslint-disable-line no-console
82
+ console.log(this.formatMessage('http', message));
71
83
  }
72
84
  }
73
85
  verbose(message) {
74
86
  if (this.shouldLog('verbose')) {
75
- console.log(this.formatMessage('verbose', message)); // eslint-disable-line no-console
87
+ console.log(this.formatMessage('verbose', message));
76
88
  }
77
89
  }
78
90
  silly(message) {
79
91
  if (this.shouldLog('silly')) {
80
- console.log(this.formatMessage('silly', message)); // eslint-disable-line no-console
92
+ console.log(this.formatMessage('silly', message));
81
93
  }
82
94
  }
83
95
  // Make it compatible with Winston interface
@@ -144,13 +156,15 @@ export class Logger {
144
156
  return minimalFormatter;
145
157
  }
146
158
  }
147
- static configure(options = {}) {
159
+ static async configure(options = {}) {
148
160
  const { level, console = true, file, format = 'minimal' } = options;
149
161
  const debugEnv = (typeof process !== 'undefined' && process.env?.DEBUG) || undefined;
150
162
  const resolvedLevel = level ?? resolveLevel(debugEnv);
151
163
  this.currentFormat = format;
152
164
  const root = this.get();
153
165
  root.level = resolvedLevel;
166
+ // Winston-specific configuration for Node.js environments
167
+ const winstonRoot = root;
154
168
  // For non-Node.js environments, just update the level
155
169
  if (!isNodeJSEnvironment()) {
156
170
  Object.values(this.simpleInstances).forEach((logger) => {
@@ -158,18 +172,19 @@ export class Logger {
158
172
  });
159
173
  return;
160
174
  }
161
- // Winston-specific configuration for Node.js environments
162
- const winstonRoot = root;
163
175
  winstonRoot.clear();
164
176
  if (console) {
165
177
  winstonRoot.add(new transports.Console());
166
178
  }
167
179
  if (file) {
168
- const dir = path.dirname(path.resolve(file));
169
- if (!fs.existsSync(dir)) {
170
- fs.mkdirSync(dir, { recursive: true });
180
+ const { fs: nodeFs, path: nodePath } = await getNodeModules();
181
+ if (nodeFs && nodePath) {
182
+ const dir = nodePath.dirname(nodePath.resolve(file));
183
+ if (!nodeFs.existsSync(dir)) {
184
+ nodeFs.mkdirSync(dir, { recursive: true });
185
+ }
186
+ winstonRoot.add(new transports.File({ filename: file }));
171
187
  }
172
- winstonRoot.add(new transports.File({ filename: file }));
173
188
  }
174
189
  // Update all existing Winston loggers with new format
175
190
  Object.values(this.instances).forEach((logger) => {
@@ -36,7 +36,7 @@ export class ServerManager {
36
36
  'Active': this.activeServer === name ? '✅' : '❌',
37
37
  }));
38
38
  logger.info(`Server Manager State: [${context}]`);
39
- console.table(tableData); // eslint-disable-line no-console
39
+ console.table(tableData);
40
40
  }
41
41
  initialize() {
42
42
  const serverNames = this.client.getServerNames?.();