@toolplex/client 0.1.1

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 (91) hide show
  1. package/LICENSE +98 -0
  2. package/README.md +112 -0
  3. package/dist/mcp-server/clientContext.d.ts +35 -0
  4. package/dist/mcp-server/clientContext.js +107 -0
  5. package/dist/mcp-server/index.d.ts +1 -0
  6. package/dist/mcp-server/index.js +22 -0
  7. package/dist/mcp-server/logging/telemetryLogger.d.ts +18 -0
  8. package/dist/mcp-server/logging/telemetryLogger.js +54 -0
  9. package/dist/mcp-server/policy/callToolObserver.d.ts +9 -0
  10. package/dist/mcp-server/policy/callToolObserver.js +25 -0
  11. package/dist/mcp-server/policy/feedbackPolicy.d.ts +27 -0
  12. package/dist/mcp-server/policy/feedbackPolicy.js +39 -0
  13. package/dist/mcp-server/policy/installObserver.d.ts +11 -0
  14. package/dist/mcp-server/policy/installObserver.js +35 -0
  15. package/dist/mcp-server/policy/playbookPolicy.d.ts +29 -0
  16. package/dist/mcp-server/policy/playbookPolicy.js +81 -0
  17. package/dist/mcp-server/policy/policyEnforcer.d.ts +57 -0
  18. package/dist/mcp-server/policy/policyEnforcer.js +105 -0
  19. package/dist/mcp-server/policy/serverPolicy.d.ts +39 -0
  20. package/dist/mcp-server/policy/serverPolicy.js +61 -0
  21. package/dist/mcp-server/promptsCache.d.ts +25 -0
  22. package/dist/mcp-server/promptsCache.js +51 -0
  23. package/dist/mcp-server/registry.d.ts +34 -0
  24. package/dist/mcp-server/registry.js +109 -0
  25. package/dist/mcp-server/serversCache.d.ts +53 -0
  26. package/dist/mcp-server/serversCache.js +100 -0
  27. package/dist/mcp-server/staticPrompts.d.ts +6 -0
  28. package/dist/mcp-server/staticPrompts.js +6 -0
  29. package/dist/mcp-server/toolDefinitionsCache.d.ts +33 -0
  30. package/dist/mcp-server/toolDefinitionsCache.js +67 -0
  31. package/dist/mcp-server/toolHandlers/callToolHandler.d.ts +3 -0
  32. package/dist/mcp-server/toolHandlers/callToolHandler.js +79 -0
  33. package/dist/mcp-server/toolHandlers/getServerConfigHandler.d.ts +3 -0
  34. package/dist/mcp-server/toolHandlers/getServerConfigHandler.js +69 -0
  35. package/dist/mcp-server/toolHandlers/initHandler.d.ts +3 -0
  36. package/dist/mcp-server/toolHandlers/initHandler.js +117 -0
  37. package/dist/mcp-server/toolHandlers/installServerHandler.d.ts +3 -0
  38. package/dist/mcp-server/toolHandlers/installServerHandler.js +151 -0
  39. package/dist/mcp-server/toolHandlers/listServersHandler.d.ts +2 -0
  40. package/dist/mcp-server/toolHandlers/listServersHandler.js +81 -0
  41. package/dist/mcp-server/toolHandlers/listToolsHandler.d.ts +3 -0
  42. package/dist/mcp-server/toolHandlers/listToolsHandler.js +112 -0
  43. package/dist/mcp-server/toolHandlers/logPlaybookUsageHandler.d.ts +3 -0
  44. package/dist/mcp-server/toolHandlers/logPlaybookUsageHandler.js +65 -0
  45. package/dist/mcp-server/toolHandlers/lookupEntityHandler.d.ts +3 -0
  46. package/dist/mcp-server/toolHandlers/lookupEntityHandler.js +112 -0
  47. package/dist/mcp-server/toolHandlers/savePlaybookHandler.d.ts +3 -0
  48. package/dist/mcp-server/toolHandlers/savePlaybookHandler.js +65 -0
  49. package/dist/mcp-server/toolHandlers/searchHandler.d.ts +3 -0
  50. package/dist/mcp-server/toolHandlers/searchHandler.js +114 -0
  51. package/dist/mcp-server/toolHandlers/serverManagerUtils.d.ts +2 -0
  52. package/dist/mcp-server/toolHandlers/serverManagerUtils.js +20 -0
  53. package/dist/mcp-server/toolHandlers/submitFeedbackHandler.d.ts +3 -0
  54. package/dist/mcp-server/toolHandlers/submitFeedbackHandler.js +70 -0
  55. package/dist/mcp-server/toolHandlers/uninstallServerHandler.d.ts +3 -0
  56. package/dist/mcp-server/toolHandlers/uninstallServerHandler.js +83 -0
  57. package/dist/mcp-server/toolplexApi/service.d.ts +32 -0
  58. package/dist/mcp-server/toolplexApi/service.js +222 -0
  59. package/dist/mcp-server/toolplexApi/types.d.ts +124 -0
  60. package/dist/mcp-server/toolplexApi/types.js +1 -0
  61. package/dist/mcp-server/toolplexServer.d.ts +3 -0
  62. package/dist/mcp-server/toolplexServer.js +249 -0
  63. package/dist/mcp-server/tools.d.ts +2 -0
  64. package/dist/mcp-server/tools.js +13 -0
  65. package/dist/mcp-server/utils/initServerManagers.d.ts +6 -0
  66. package/dist/mcp-server/utils/initServerManagers.js +31 -0
  67. package/dist/mcp-server/utils/resultAnnotators.d.ts +23 -0
  68. package/dist/mcp-server/utils/resultAnnotators.js +50 -0
  69. package/dist/mcp-server/utils/runtimeCheck.d.ts +4 -0
  70. package/dist/mcp-server/utils/runtimeCheck.js +30 -0
  71. package/dist/server-manager/index.d.ts +1 -0
  72. package/dist/server-manager/index.js +8 -0
  73. package/dist/server-manager/serverManager.d.ts +37 -0
  74. package/dist/server-manager/serverManager.js +419 -0
  75. package/dist/server-manager/stdioServer.d.ts +9 -0
  76. package/dist/server-manager/stdioServer.js +136 -0
  77. package/dist/server-manager/stdioTransportProtocol.d.ts +31 -0
  78. package/dist/server-manager/stdioTransportProtocol.js +67 -0
  79. package/dist/shared/enhancedPath.d.ts +7 -0
  80. package/dist/shared/enhancedPath.js +52 -0
  81. package/dist/shared/fileLogger.d.ts +13 -0
  82. package/dist/shared/fileLogger.js +66 -0
  83. package/dist/shared/mcpServerTypes.d.ts +398 -0
  84. package/dist/shared/mcpServerTypes.js +148 -0
  85. package/dist/shared/serverManagerTypes.d.ts +179 -0
  86. package/dist/shared/serverManagerTypes.js +73 -0
  87. package/dist/shared/stdioServerManagerClient.d.ts +12 -0
  88. package/dist/shared/stdioServerManagerClient.js +96 -0
  89. package/dist/version.d.ts +1 -0
  90. package/dist/version.js +1 -0
  91. package/package.json +70 -0
@@ -0,0 +1,81 @@
1
+ import Registry from '../registry.js';
2
+ export class PlaybookPolicy {
3
+ constructor(clientContext, callToolObserver) {
4
+ this.callToolObserver = callToolObserver;
5
+ this.clientContext = clientContext;
6
+ this.blockedMcpServersSet = new Set(clientContext.flags.blocked_mcp_servers || []);
7
+ }
8
+ /**
9
+ * Validates a playbook before saving by checking that:
10
+ * - Referenced servers and tools have been used
11
+ * - No blocked servers are referenced
12
+ * - Private playbooks are only created when enabled
13
+ *
14
+ * For each action with a 'call' property, verifies that:
15
+ * - Any referenced server has been connected to
16
+ * - Any referenced server/tool combination has been executed
17
+ * - The server is not in the blocked servers list
18
+ *
19
+ * @throws Error if a referenced server or tool has not been used in the current session,
20
+ * if a blocked server is referenced, or if trying to create a private playbook when disabled
21
+ */
22
+ enforceSavePlaybookPolicy(playbook) {
23
+ if (!Array.isArray(playbook.actions)) {
24
+ throw new Error('Playbook actions must be an array');
25
+ }
26
+ for (const [idx, action] of playbook.actions.entries()) {
27
+ if (!action.call)
28
+ continue;
29
+ // Parse the call string
30
+ // Supported formats:
31
+ // - "mcp_server_id:<server_id>::<tool_name>"
32
+ // - "mcp_server_id:<server_id>"
33
+ // - "playbook_id:<playbook_id>"
34
+ const call = action.call.trim();
35
+ if (call.startsWith('mcp_server_id:')) {
36
+ // Could be with or without tool_name
37
+ // e.g. mcp_server_id:abc123::toolX or mcp_server_id:abc123
38
+ const match = call.match(/^mcp_server_id:([^:]+)(?:::([^:]+))?$/);
39
+ if (!match) {
40
+ throw new Error(`Invalid call format in action ${idx + 1}: "${call}"`);
41
+ }
42
+ const serverId = match[1];
43
+ const toolName = match[2];
44
+ // Check if server is blocked
45
+ if (this.blockedMcpServersSet.has(serverId)) {
46
+ throw new Error(`Playbook action ${idx + 1} references blocked server "${serverId}"`);
47
+ }
48
+ if (toolName) {
49
+ // Must have called this tool on this server
50
+ if (!this.callToolObserver.wasToolCalled(serverId, toolName)) {
51
+ throw new Error(`Playbook action ${idx + 1} references tool "${toolName}" on server "${serverId}" which has not been used in this session.`);
52
+ }
53
+ }
54
+ else {
55
+ // Only server referenced, must have called any tool on this server
56
+ if (!this.callToolObserver.wasServerCalled(serverId)) {
57
+ throw new Error(`Playbook action ${idx + 1} references server "${serverId}" which has not been used in this session.`);
58
+ }
59
+ }
60
+ }
61
+ else if (call.startsWith('playbook_id:')) {
62
+ // For playbook references, we could skip or add logic if needed
63
+ // For now, we do not validate playbook_id usage
64
+ continue;
65
+ }
66
+ else {
67
+ throw new Error(`Playbook action ${idx + 1} has an unrecognized call format: "${call}"`);
68
+ }
69
+ }
70
+ }
71
+ /**
72
+ * Validates if playbook usage logging is allowed based on read-only mode
73
+ * @throws Error if read-only mode is enabled
74
+ */
75
+ enforceLogPlaybookUsagePolicy() {
76
+ if (this.clientContext.permissions.enable_read_only_mode) {
77
+ const promptsCache = Registry.getPromptsCache();
78
+ throw new Error(promptsCache.getPrompt('log_playbook_usage_disabled'));
79
+ }
80
+ }
81
+ }
@@ -0,0 +1,57 @@
1
+ import { ClientContext } from '../clientContext.js';
2
+ import CallToolObserver from './callToolObserver.js';
3
+ import InstallObserver from './installObserver.js';
4
+ import { SavePlaybookParams, SubmitFeedbackParams } from '../../shared/mcpServerTypes.js';
5
+ export declare class PolicyEnforcer {
6
+ private playbookPolicy;
7
+ private feedbackPolicy;
8
+ private serverPolicy;
9
+ private callToolObserver;
10
+ private installObserver;
11
+ constructor();
12
+ /**
13
+ * Initialize the policy enforcer with the client context
14
+ */
15
+ init(clientContext: ClientContext): void;
16
+ /**
17
+ * Enforce playbook policy validation before saving.
18
+ * Throws if the playbook does not pass policy.
19
+ */
20
+ enforceSavePlaybookPolicy(playbook: SavePlaybookParams): void;
21
+ /**
22
+ * Enforce feedback policy validation.
23
+ * Throws if the feedback does not pass policy.
24
+ */
25
+ enforceFeedbackPolicy(feedback: SubmitFeedbackParams): void;
26
+ /**
27
+ * Enforce server call tool policy validation.
28
+ * Throws if attempting to call a tool on a blocked server.
29
+ */
30
+ enforceCallToolPolicy(serverId: string): void;
31
+ /**
32
+ * Enforce server config policy validation.
33
+ * Throws if attempting to use a blocked or disallowed server.
34
+ */
35
+ enforceUseServerPolicy(serverId: string): void;
36
+ /**
37
+ * Enforce playbook usage logging policy validation.
38
+ * Throws if read-only mode is enabled.
39
+ */
40
+ enforceLogPlaybookUsagePolicy(): void;
41
+ /**
42
+ * Filters out blocked servers from a list of objects.
43
+ *
44
+ * @param servers List of objects containing server IDs
45
+ * @param getServerId Function that extracts the server ID from an object
46
+ * @returns Filtered list with blocked servers removed
47
+ */
48
+ filterBlockedMcpServers<T>(servers: T[], getServerId: (item: T) => string): T[];
49
+ /**
50
+ * Get a reference to the CallToolObserver instance.
51
+ */
52
+ getCallToolObserver(): CallToolObserver;
53
+ /**
54
+ * Get a reference to the InstallObserver instance.
55
+ */
56
+ getInstallObserver(): InstallObserver;
57
+ }
@@ -0,0 +1,105 @@
1
+ import { PlaybookPolicy } from './playbookPolicy.js';
2
+ import { FeedbackPolicy } from './feedbackPolicy.js';
3
+ import CallToolObserver from './callToolObserver.js';
4
+ import InstallObserver from './installObserver.js';
5
+ import { ServerPolicy } from './serverPolicy.js';
6
+ export class PolicyEnforcer {
7
+ constructor() {
8
+ this.playbookPolicy = null;
9
+ this.feedbackPolicy = null;
10
+ this.serverPolicy = null;
11
+ this.callToolObserver = null;
12
+ this.installObserver = null;
13
+ }
14
+ /**
15
+ * Initialize the policy enforcer with the client context
16
+ */
17
+ init(clientContext) {
18
+ this.callToolObserver = new CallToolObserver();
19
+ this.installObserver = new InstallObserver();
20
+ this.playbookPolicy = new PlaybookPolicy(clientContext, this.callToolObserver);
21
+ this.feedbackPolicy = new FeedbackPolicy(clientContext, this.callToolObserver, this.installObserver);
22
+ this.serverPolicy = new ServerPolicy(clientContext);
23
+ }
24
+ /**
25
+ * Enforce playbook policy validation before saving.
26
+ * Throws if the playbook does not pass policy.
27
+ */
28
+ enforceSavePlaybookPolicy(playbook) {
29
+ if (!this.playbookPolicy) {
30
+ throw new Error('PolicyEnforcer not initialized');
31
+ }
32
+ this.playbookPolicy.enforceSavePlaybookPolicy(playbook);
33
+ }
34
+ /**
35
+ * Enforce feedback policy validation.
36
+ * Throws if the feedback does not pass policy.
37
+ */
38
+ enforceFeedbackPolicy(feedback) {
39
+ if (!this.feedbackPolicy) {
40
+ throw new Error('PolicyEnforcer not initialized');
41
+ }
42
+ this.feedbackPolicy.enforceFeedbackPolicy(feedback);
43
+ }
44
+ /**
45
+ * Enforce server call tool policy validation.
46
+ * Throws if attempting to call a tool on a blocked server.
47
+ */
48
+ enforceCallToolPolicy(serverId) {
49
+ if (!this.serverPolicy) {
50
+ throw new Error('PolicyEnforcer not initialized');
51
+ }
52
+ this.serverPolicy.enforceCallToolPolicy(serverId);
53
+ }
54
+ /**
55
+ * Enforce server config policy validation.
56
+ * Throws if attempting to use a blocked or disallowed server.
57
+ */
58
+ enforceUseServerPolicy(serverId) {
59
+ if (!this.serverPolicy) {
60
+ throw new Error('PolicyEnforcer not initialized');
61
+ }
62
+ this.serverPolicy.enforceUseServerPolicy(serverId);
63
+ }
64
+ /**
65
+ * Enforce playbook usage logging policy validation.
66
+ * Throws if read-only mode is enabled.
67
+ */
68
+ enforceLogPlaybookUsagePolicy() {
69
+ if (!this.playbookPolicy) {
70
+ throw new Error('PolicyEnforcer not initialized');
71
+ }
72
+ this.playbookPolicy.enforceLogPlaybookUsagePolicy();
73
+ }
74
+ /**
75
+ * Filters out blocked servers from a list of objects.
76
+ *
77
+ * @param servers List of objects containing server IDs
78
+ * @param getServerId Function that extracts the server ID from an object
79
+ * @returns Filtered list with blocked servers removed
80
+ */
81
+ filterBlockedMcpServers(servers, getServerId) {
82
+ if (!this.serverPolicy) {
83
+ throw new Error('PolicyEnforcer not initialized');
84
+ }
85
+ return this.serverPolicy.filterBlockedMcpServers(servers, getServerId);
86
+ }
87
+ /**
88
+ * Get a reference to the CallToolObserver instance.
89
+ */
90
+ getCallToolObserver() {
91
+ if (!this.callToolObserver) {
92
+ throw new Error('PolicyEnforcer not initialized');
93
+ }
94
+ return this.callToolObserver;
95
+ }
96
+ /**
97
+ * Get a reference to the InstallObserver instance.
98
+ */
99
+ getInstallObserver() {
100
+ if (!this.installObserver) {
101
+ throw new Error('PolicyEnforcer not initialized');
102
+ }
103
+ return this.installObserver;
104
+ }
105
+ }
@@ -0,0 +1,39 @@
1
+ import { ClientContext } from '../clientContext.js';
2
+ export declare class ServerPolicy {
3
+ private clientContext;
4
+ private blockedMcpServersSet;
5
+ constructor(clientContext: ClientContext);
6
+ /**
7
+ * Validates that a server is not blocked.
8
+ *
9
+ * @throws Error if attempting to use a blocked server
10
+ */
11
+ enforceBlockedServerPolicy(serverId: string): void;
12
+ /**
13
+ * Validates that a server is allowed.
14
+ *
15
+ * @throws Error if attempting to use a server not in the allowed list
16
+ */
17
+ enforceAllowedServerPolicy(serverId: string): void;
18
+ /**
19
+ * Validates that a server is not blocked before calling a tool on it.
20
+ * Also checks if desktop commander is enabled when calling tools on the desktop commander server.
21
+ *
22
+ * @throws Error if attempting to call a tool on a blocked server or if desktop commander is disabled
23
+ */
24
+ enforceCallToolPolicy(serverId: string): void;
25
+ /**
26
+ * Validates that a server can be used.
27
+ *
28
+ * @throws Error if attempting to use a blocked or disallowed server
29
+ */
30
+ enforceUseServerPolicy(serverId: string): void;
31
+ /**
32
+ * Filters out blocked servers from a list of objects.
33
+ *
34
+ * @param servers List of objects containing server IDs
35
+ * @param getServerId Function that extracts the server ID from an object
36
+ * @returns Filtered list with blocked servers removed
37
+ */
38
+ filterBlockedMcpServers<T>(servers: T[], getServerId: (item: T) => string): T[];
39
+ }
@@ -0,0 +1,61 @@
1
+ export class ServerPolicy {
2
+ constructor(clientContext) {
3
+ this.clientContext = clientContext;
4
+ this.blockedMcpServersSet = new Set(clientContext.flags.blocked_mcp_servers || []);
5
+ }
6
+ /**
7
+ * Validates that a server is not blocked.
8
+ *
9
+ * @throws Error if attempting to use a blocked server
10
+ */
11
+ enforceBlockedServerPolicy(serverId) {
12
+ if (this.blockedMcpServersSet.has(serverId)) {
13
+ throw new Error(`Cannot use blocked server "${serverId}. Questions? Contact support@toolplex.ai"`);
14
+ }
15
+ }
16
+ /**
17
+ * Validates that a server is allowed.
18
+ *
19
+ * @throws Error if attempting to use a server not in the allowed list
20
+ */
21
+ enforceAllowedServerPolicy(serverId) {
22
+ const allowedServers = this.clientContext.permissions.allowed_mcp_servers;
23
+ if (allowedServers && allowedServers.length > 0 && !allowedServers.includes(serverId)) {
24
+ throw new Error(`Server "${serverId}" is not allowed for your account. Please adjust the Allowed MCP Servers permissions on the ToolPlex Dashboard if this is a mistake.`);
25
+ }
26
+ }
27
+ /**
28
+ * Validates that a server is not blocked before calling a tool on it.
29
+ * Also checks if desktop commander is enabled when calling tools on the desktop commander server.
30
+ *
31
+ * @throws Error if attempting to call a tool on a blocked server or if desktop commander is disabled
32
+ */
33
+ enforceCallToolPolicy(serverId) {
34
+ this.enforceBlockedServerPolicy(serverId);
35
+ this.enforceAllowedServerPolicy(serverId);
36
+ // Check if desktop commander is disabled and this is the desktop commander server
37
+ if (!this.clientContext.permissions.use_desktop_commander &&
38
+ serverId === this.clientContext.flags.desktop_commander_server_id) {
39
+ throw new Error('Desktop Commander is disabled for your account');
40
+ }
41
+ }
42
+ /**
43
+ * Validates that a server can be used.
44
+ *
45
+ * @throws Error if attempting to use a blocked or disallowed server
46
+ */
47
+ enforceUseServerPolicy(serverId) {
48
+ this.enforceBlockedServerPolicy(serverId);
49
+ this.enforceAllowedServerPolicy(serverId);
50
+ }
51
+ /**
52
+ * Filters out blocked servers from a list of objects.
53
+ *
54
+ * @param servers List of objects containing server IDs
55
+ * @param getServerId Function that extracts the server ID from an object
56
+ * @returns Filtered list with blocked servers removed
57
+ */
58
+ filterBlockedMcpServers(servers, getServerId) {
59
+ return servers.filter((server) => !this.blockedMcpServersSet.has(getServerId(server)));
60
+ }
61
+ }
@@ -0,0 +1,25 @@
1
+ export declare class PromptsCache {
2
+ private _prompts;
3
+ private _version;
4
+ constructor();
5
+ /**
6
+ * Initialize prompts cache with prompts from init response
7
+ */
8
+ init(prompts: Record<string, string>): void;
9
+ /**
10
+ * Get a specific prompt by key from the cache
11
+ */
12
+ getPrompt(key: string): string;
13
+ /**
14
+ * Get the version of the current prompts
15
+ */
16
+ getVersion(): string;
17
+ /**
18
+ * Check if the cache is initialized
19
+ */
20
+ isInitialized(): boolean;
21
+ /**
22
+ * Reset the cache
23
+ */
24
+ reset(): void;
25
+ }
@@ -0,0 +1,51 @@
1
+ export class PromptsCache {
2
+ constructor() {
3
+ this._prompts = null;
4
+ this._version = null;
5
+ this._prompts = null;
6
+ this._version = null;
7
+ }
8
+ /**
9
+ * Initialize prompts cache with prompts from init response
10
+ */
11
+ init(prompts) {
12
+ // Allow re-init.
13
+ this._prompts = prompts;
14
+ this._version = prompts._version;
15
+ }
16
+ /**
17
+ * Get a specific prompt by key from the cache
18
+ */
19
+ getPrompt(key) {
20
+ if (!this._prompts) {
21
+ throw new Error('PromptsCache not initialized');
22
+ }
23
+ const prompt = this._prompts[key];
24
+ if (!prompt) {
25
+ throw new Error(`Prompt "${key}" not found in cache`);
26
+ }
27
+ return prompt;
28
+ }
29
+ /**
30
+ * Get the version of the current prompts
31
+ */
32
+ getVersion() {
33
+ if (!this._version) {
34
+ throw new Error('PromptsCache not initialized');
35
+ }
36
+ return this._version;
37
+ }
38
+ /**
39
+ * Check if the cache is initialized
40
+ */
41
+ isInitialized() {
42
+ return this._prompts !== null;
43
+ }
44
+ /**
45
+ * Reset the cache
46
+ */
47
+ reset() {
48
+ this._prompts = null;
49
+ this._version = null;
50
+ }
51
+ }
@@ -0,0 +1,34 @@
1
+ import { ClientContext } from './clientContext.js';
2
+ import { ToolplexApiService } from './toolplexApi/service.js';
3
+ import { StdioServerManagerClient } from '../shared/stdioServerManagerClient.js';
4
+ import { TelemetryLogger } from './logging/telemetryLogger.js';
5
+ import { PromptsCache } from './promptsCache.js';
6
+ import { ToolDefinitionsCache } from './toolDefinitionsCache.js';
7
+ import { ServersCache } from './serversCache.js';
8
+ import { PolicyEnforcer } from './policy/policyEnforcer.js';
9
+ /**
10
+ * In-memory global registry for the ToolPlex client.
11
+ * Maintains singleton instances of core services and clients used throughout the application.
12
+ */
13
+ declare class Registry {
14
+ private static _clientContext;
15
+ private static _toolplexApiService;
16
+ private static _serverManagerClients;
17
+ private static _telemetryLogger;
18
+ private static _promptsCache;
19
+ private static _toolDefinitionsCache;
20
+ private static _serversCache;
21
+ private static _policyEnforcer;
22
+ static init(clientContext: ClientContext): Promise<void>;
23
+ static getClientContext(): ClientContext;
24
+ static getToolplexApiService(): ToolplexApiService;
25
+ static getServerManagerClients(): Record<string, StdioServerManagerClient>;
26
+ static setServerManagerClients(clients: Record<string, StdioServerManagerClient>): void;
27
+ static getTelemetryLogger(): TelemetryLogger;
28
+ static getPromptsCache(): PromptsCache;
29
+ static getToolDefinitionsCache(): ToolDefinitionsCache;
30
+ static getServersCache(): ServersCache;
31
+ static getPolicyEnforcer(): PolicyEnforcer;
32
+ static reset(): void;
33
+ }
34
+ export default Registry;
@@ -0,0 +1,109 @@
1
+ import { ToolplexApiService } from './toolplexApi/service.js';
2
+ import { TelemetryLogger } from './logging/telemetryLogger.js';
3
+ import { PromptsCache } from './promptsCache.js';
4
+ import { ToolDefinitionsCache } from './toolDefinitionsCache.js';
5
+ import { ServersCache } from './serversCache.js';
6
+ import { PolicyEnforcer } from './policy/policyEnforcer.js';
7
+ /**
8
+ * In-memory global registry for the ToolPlex client.
9
+ * Maintains singleton instances of core services and clients used throughout the application.
10
+ */
11
+ class Registry {
12
+ static async init(clientContext) {
13
+ if (this._clientContext || this._toolplexApiService || this._serverManagerClients) {
14
+ throw new Error('Registry already initialized');
15
+ }
16
+ this._clientContext = clientContext;
17
+ this._toolplexApiService = new ToolplexApiService(clientContext);
18
+ this._serverManagerClients = {};
19
+ this._telemetryLogger = new TelemetryLogger();
20
+ this._promptsCache = new PromptsCache();
21
+ this._toolDefinitionsCache = new ToolDefinitionsCache();
22
+ this._serversCache = new ServersCache();
23
+ this._policyEnforcer = new PolicyEnforcer();
24
+ // Tool definitions must be initialized early to use tools like initialize_toolplex.
25
+ await this._toolDefinitionsCache.init(this._toolplexApiService, clientContext);
26
+ }
27
+ static getClientContext() {
28
+ if (!this._clientContext) {
29
+ throw new Error('ClientContext not initialized in Registry');
30
+ }
31
+ return this._clientContext;
32
+ }
33
+ static getToolplexApiService() {
34
+ if (!this._toolplexApiService) {
35
+ throw new Error('ToolplexApiService not initialized in Registry');
36
+ }
37
+ return this._toolplexApiService;
38
+ }
39
+ static getServerManagerClients() {
40
+ if (!this._serverManagerClients) {
41
+ throw new Error('ServerManagerClients not initialized in Registry');
42
+ }
43
+ return this._serverManagerClients;
44
+ }
45
+ static setServerManagerClients(clients) {
46
+ if (!this._serverManagerClients) {
47
+ throw new Error('Registry not initialized');
48
+ }
49
+ this._serverManagerClients = clients;
50
+ }
51
+ static getTelemetryLogger() {
52
+ if (!this._telemetryLogger) {
53
+ throw new Error('TelemetryLogger not initialized in Registry');
54
+ }
55
+ return this._telemetryLogger;
56
+ }
57
+ static getPromptsCache() {
58
+ if (!this._promptsCache) {
59
+ throw new Error('PromptsCache not initialized in Registry');
60
+ }
61
+ return this._promptsCache;
62
+ }
63
+ static getToolDefinitionsCache() {
64
+ if (!this._toolDefinitionsCache) {
65
+ throw new Error('ToolDefinitionsCache not initialized in Registry');
66
+ }
67
+ return this._toolDefinitionsCache;
68
+ }
69
+ static getServersCache() {
70
+ if (!this._serversCache) {
71
+ throw new Error('ServersCache not initialized in Registry');
72
+ }
73
+ return this._serversCache;
74
+ }
75
+ static getPolicyEnforcer() {
76
+ if (!this._policyEnforcer) {
77
+ throw new Error('PolicyEnforcer not initialized in Registry');
78
+ }
79
+ return this._policyEnforcer;
80
+ }
81
+ static reset() {
82
+ this._clientContext = null;
83
+ this._toolplexApiService = null;
84
+ this._serverManagerClients = null;
85
+ this._telemetryLogger = null;
86
+ if (this._promptsCache) {
87
+ this._promptsCache.reset();
88
+ this._promptsCache = null;
89
+ }
90
+ if (this._toolDefinitionsCache) {
91
+ this._toolDefinitionsCache.reset();
92
+ this._toolDefinitionsCache = null;
93
+ }
94
+ if (this._serversCache) {
95
+ this._serversCache.reset();
96
+ this._serversCache = null;
97
+ }
98
+ this._policyEnforcer = null;
99
+ }
100
+ }
101
+ Registry._clientContext = null;
102
+ Registry._toolplexApiService = null;
103
+ Registry._serverManagerClients = null;
104
+ Registry._telemetryLogger = null;
105
+ Registry._promptsCache = null;
106
+ Registry._toolDefinitionsCache = null;
107
+ Registry._serversCache = null;
108
+ Registry._policyEnforcer = null;
109
+ export default Registry;
@@ -0,0 +1,53 @@
1
+ import { StdioServerManagerClient } from '../shared/stdioServerManagerClient.js';
2
+ type ServerInfo = {
3
+ server_id: string;
4
+ server_name: string;
5
+ description: string;
6
+ };
7
+ /**
8
+ * An in-memory cache that tracks currently installed servers.
9
+ * Maintains a set of server IDs for quick lookup of installed servers.
10
+ * Can be refreshed by querying server manager clients for their current server lists.
11
+ */
12
+ export declare class ServersCache {
13
+ private _serverIds;
14
+ constructor();
15
+ /**
16
+ * Initialize the cache with a list of servers, e.g. from initialize() succeeded list.
17
+ * Only tracks server IDs.
18
+ */
19
+ init(servers: ServerInfo[]): void;
20
+ /**
21
+ * Update the cache with a new list of servers, e.g. after calling listServersHandler.
22
+ * This does not imply initialization, but is meant to refresh the cache with the latest list.
23
+ * Only tracks server IDs.
24
+ */
25
+ updateServers(servers: ServerInfo[]): void;
26
+ /**
27
+ * Returns true if the server is installed (present in the cache).
28
+ * Throws an error if the cache is not initialized.
29
+ */
30
+ isInstalled(serverId: string): boolean;
31
+ /**
32
+ * Get all cached server IDs.
33
+ */
34
+ getServerIds(): string[];
35
+ /**
36
+ * Refresh the cache by calling list_servers on all server manager clients.
37
+ * This follows the pattern in handleListServers (listServersHandler.ts):
38
+ * - Use sendRequest('list_servers', {}) on each client.
39
+ * - Validate/parse the response with ListServersResultSchema.
40
+ * - Collect all servers from all runtimes.
41
+ * @param serverManagerClients - Record of server manager clients (e.g. from Registry)
42
+ */
43
+ refreshCache(serverManagerClients: Record<string, StdioServerManagerClient>): Promise<void>;
44
+ /**
45
+ * Check if the cache is initialized
46
+ */
47
+ isInitialized(): boolean;
48
+ /**
49
+ * Reset the cache
50
+ */
51
+ reset(): void;
52
+ }
53
+ export {};