@aria-cli/tools 1.0.12 → 1.0.14

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 (233) hide show
  1. package/dist/index.js +378 -70
  2. package/dist/network-runtime/index.js +8 -12
  3. package/dist-cjs/index.js +400 -435
  4. package/dist-cjs/network-runtime/index.js +8 -172
  5. package/package.json +8 -6
  6. package/dist/.tsbuildinfo +0 -1
  7. package/dist/ask-user-interaction.js +0 -22
  8. package/dist/cache/web-cache.js +0 -66
  9. package/dist/definitions/arion.js +0 -104
  10. package/dist/definitions/browser/browser.js +0 -418
  11. package/dist/definitions/browser/index.js +0 -4
  12. package/dist/definitions/browser/pw-downloads.js +0 -114
  13. package/dist/definitions/browser/pw-interactions.js +0 -199
  14. package/dist/definitions/browser/pw-responses.js +0 -76
  15. package/dist/definitions/browser/pw-session.js +0 -310
  16. package/dist/definitions/browser/pw-shared.js +0 -66
  17. package/dist/definitions/browser/pw-snapshot.js +0 -301
  18. package/dist/definitions/browser/pw-state.js +0 -62
  19. package/dist/definitions/browser/types.js +0 -4
  20. package/dist/definitions/code-intelligence.js +0 -470
  21. package/dist/definitions/core.js +0 -109
  22. package/dist/definitions/delegation.js +0 -512
  23. package/dist/definitions/deploy.js +0 -65
  24. package/dist/definitions/filesystem.js +0 -196
  25. package/dist/definitions/frg.js +0 -63
  26. package/dist/definitions/index.js +0 -20
  27. package/dist/definitions/memory.js +0 -123
  28. package/dist/definitions/messaging.js +0 -625
  29. package/dist/definitions/meta.js +0 -349
  30. package/dist/definitions/network.js +0 -159
  31. package/dist/definitions/outlook.js +0 -277
  32. package/dist/definitions/patch/apply-patch.js +0 -184
  33. package/dist/definitions/patch/fuzzy-match.js +0 -166
  34. package/dist/definitions/patch/index.js +0 -1
  35. package/dist/definitions/patch/patch-parser.js +0 -207
  36. package/dist/definitions/patch/sandbox-paths.js +0 -105
  37. package/dist/definitions/process/index.js +0 -4
  38. package/dist/definitions/process/process-registry.js +0 -213
  39. package/dist/definitions/process/process.js +0 -386
  40. package/dist/definitions/process/pty-keys.js +0 -254
  41. package/dist/definitions/process/session-slug.js +0 -142
  42. package/dist/definitions/quip.js +0 -195
  43. package/dist/definitions/search.js +0 -60
  44. package/dist/definitions/session-history.js +0 -69
  45. package/dist/definitions/shell.js +0 -181
  46. package/dist/definitions/slack.js +0 -180
  47. package/dist/definitions/web.js +0 -109
  48. package/dist/executors/apply-patch.js +0 -901
  49. package/dist/executors/arion.js +0 -119
  50. package/dist/executors/code-intelligence.js +0 -882
  51. package/dist/executors/deploy.js +0 -848
  52. package/dist/executors/filesystem.js +0 -1122
  53. package/dist/executors/frg-freshness.js +0 -576
  54. package/dist/executors/frg.js +0 -298
  55. package/dist/executors/index.js +0 -46
  56. package/dist/executors/learning-meta.js +0 -1146
  57. package/dist/executors/lsp-client.js +0 -296
  58. package/dist/executors/memory.js +0 -750
  59. package/dist/executors/meta.js +0 -220
  60. package/dist/executors/process-registry.js +0 -465
  61. package/dist/executors/pty-session-store.js +0 -30
  62. package/dist/executors/pty.js +0 -271
  63. package/dist/executors/restart.js +0 -119
  64. package/dist/executors/search-freshness.js +0 -195
  65. package/dist/executors/search-types.js +0 -52
  66. package/dist/executors/search.js +0 -66
  67. package/dist/executors/self-diagnose.js +0 -398
  68. package/dist/executors/session-history.js +0 -283
  69. package/dist/executors/shell-safety.js +0 -473
  70. package/dist/executors/shell.js +0 -954
  71. package/dist/executors/utils.js +0 -33
  72. package/dist/executors/web.js +0 -542
  73. package/dist/extraction/content-extraction.js +0 -235
  74. package/dist/extraction/index.js +0 -4
  75. package/dist/headless-control-contract.js +0 -967
  76. package/dist/local-control-http-auth.js +0 -2
  77. package/dist/mcp/client.js +0 -181
  78. package/dist/mcp/connection.js +0 -480
  79. package/dist/mcp/index.js +0 -10
  80. package/dist/mcp/jsonrpc.js +0 -144
  81. package/dist/mcp/types.js +0 -7
  82. package/dist/network-control-adapter.js +0 -72
  83. package/dist/network-runtime/address-types.js +0 -165
  84. package/dist/network-runtime/db-owner-fencing.js +0 -69
  85. package/dist/network-runtime/delivery-receipts.js +0 -267
  86. package/dist/network-runtime/direct-endpoint-authority.js +0 -25
  87. package/dist/network-runtime/local-control-contract.js +0 -627
  88. package/dist/network-runtime/node-store-contract.js +0 -34
  89. package/dist/network-runtime/pair-route-contract.js +0 -77
  90. package/dist/network-runtime/peer-capabilities.js +0 -28
  91. package/dist/network-runtime/peer-principal-ref.js +0 -12
  92. package/dist/network-runtime/peer-state-machine.js +0 -121
  93. package/dist/network-runtime/protocol-schemas.js +0 -205
  94. package/dist/network-runtime/runtime-bootstrap-contract.js +0 -60
  95. package/dist/outlook/desktop-session.js +0 -279
  96. package/dist/policy.js +0 -149
  97. package/dist/providers/brave.js +0 -62
  98. package/dist/providers/duckduckgo.js +0 -176
  99. package/dist/providers/exa.js +0 -63
  100. package/dist/providers/firecrawl.js +0 -55
  101. package/dist/providers/index.js +0 -7
  102. package/dist/providers/jina.js +0 -49
  103. package/dist/providers/router.js +0 -96
  104. package/dist/providers/search-provider.js +0 -32
  105. package/dist/providers/tavily.js +0 -54
  106. package/dist/quip/desktop-session.js +0 -317
  107. package/dist/registry/index.js +0 -1
  108. package/dist/registry/registry.js +0 -756
  109. package/dist/runtime-socket-local-control-client.js +0 -330
  110. package/dist/security/dns-normalization.js +0 -19
  111. package/dist/security/dns-pinning.js +0 -123
  112. package/dist/security/external-content.js +0 -91
  113. package/dist/security/ssrf.js +0 -181
  114. package/dist/slack/desktop-session.js +0 -324
  115. package/dist/tool-factory.js +0 -47
  116. package/dist/types.js +0 -7
  117. package/dist/utils/retry.js +0 -132
  118. package/dist/utils/safe-parse-json.js +0 -160
  119. package/dist/utils/url.js +0 -19
  120. package/dist-cjs/.tsbuildinfo +0 -1
  121. package/dist-cjs/ask-user-interaction.js +0 -27
  122. package/dist-cjs/cache/web-cache.js +0 -70
  123. package/dist-cjs/definitions/arion.js +0 -107
  124. package/dist-cjs/definitions/browser/browser.js +0 -421
  125. package/dist-cjs/definitions/browser/index.js +0 -8
  126. package/dist-cjs/definitions/browser/pw-downloads.js +0 -117
  127. package/dist-cjs/definitions/browser/pw-interactions.js +0 -213
  128. package/dist-cjs/definitions/browser/pw-responses.js +0 -84
  129. package/dist-cjs/definitions/browser/pw-session.js +0 -326
  130. package/dist-cjs/definitions/browser/pw-shared.js +0 -72
  131. package/dist-cjs/definitions/browser/pw-snapshot.js +0 -307
  132. package/dist-cjs/definitions/browser/pw-state.js +0 -70
  133. package/dist-cjs/definitions/browser/types.js +0 -5
  134. package/dist-cjs/definitions/code-intelligence.js +0 -473
  135. package/dist-cjs/definitions/core.js +0 -133
  136. package/dist-cjs/definitions/delegation.js +0 -515
  137. package/dist-cjs/definitions/deploy.js +0 -68
  138. package/dist-cjs/definitions/filesystem.js +0 -199
  139. package/dist-cjs/definitions/frg.js +0 -66
  140. package/dist-cjs/definitions/index.js +0 -43
  141. package/dist-cjs/definitions/memory.js +0 -126
  142. package/dist-cjs/definitions/messaging.js +0 -631
  143. package/dist-cjs/definitions/meta.js +0 -352
  144. package/dist-cjs/definitions/network.js +0 -162
  145. package/dist-cjs/definitions/outlook.js +0 -280
  146. package/dist-cjs/definitions/patch/apply-patch.js +0 -191
  147. package/dist-cjs/definitions/patch/fuzzy-match.js +0 -172
  148. package/dist-cjs/definitions/patch/index.js +0 -5
  149. package/dist-cjs/definitions/patch/patch-parser.js +0 -215
  150. package/dist-cjs/definitions/patch/sandbox-paths.js +0 -113
  151. package/dist-cjs/definitions/process/index.js +0 -8
  152. package/dist-cjs/definitions/process/process-registry.js +0 -231
  153. package/dist-cjs/definitions/process/process.js +0 -389
  154. package/dist-cjs/definitions/process/pty-keys.js +0 -259
  155. package/dist-cjs/definitions/process/session-slug.js +0 -145
  156. package/dist-cjs/definitions/quip.js +0 -198
  157. package/dist-cjs/definitions/search.js +0 -63
  158. package/dist-cjs/definitions/session-history.js +0 -72
  159. package/dist-cjs/definitions/shell.js +0 -184
  160. package/dist-cjs/definitions/slack.js +0 -183
  161. package/dist-cjs/definitions/web.js +0 -112
  162. package/dist-cjs/executors/apply-patch.js +0 -938
  163. package/dist-cjs/executors/arion.js +0 -125
  164. package/dist-cjs/executors/code-intelligence.js +0 -925
  165. package/dist-cjs/executors/deploy.js +0 -869
  166. package/dist-cjs/executors/filesystem.js +0 -1167
  167. package/dist-cjs/executors/frg-freshness.js +0 -627
  168. package/dist-cjs/executors/frg.js +0 -334
  169. package/dist-cjs/executors/index.js +0 -143
  170. package/dist-cjs/executors/learning-meta.js +0 -1165
  171. package/dist-cjs/executors/lsp-client.js +0 -310
  172. package/dist-cjs/executors/memory.js +0 -796
  173. package/dist-cjs/executors/meta.js +0 -226
  174. package/dist-cjs/executors/process-registry.js +0 -469
  175. package/dist-cjs/executors/pty-session-store.js +0 -34
  176. package/dist-cjs/executors/pty.js +0 -312
  177. package/dist-cjs/executors/restart.js +0 -155
  178. package/dist-cjs/executors/search-freshness.js +0 -234
  179. package/dist-cjs/executors/search-types.js +0 -56
  180. package/dist-cjs/executors/search.js +0 -102
  181. package/dist-cjs/executors/self-diagnose.js +0 -434
  182. package/dist-cjs/executors/session-history.js +0 -320
  183. package/dist-cjs/executors/shell-safety.js +0 -478
  184. package/dist-cjs/executors/shell.js +0 -1001
  185. package/dist-cjs/executors/utils.js +0 -73
  186. package/dist-cjs/executors/web.js +0 -547
  187. package/dist-cjs/extraction/content-extraction.js +0 -243
  188. package/dist-cjs/extraction/index.js +0 -8
  189. package/dist-cjs/headless-control-contract.js +0 -972
  190. package/dist-cjs/local-control-http-auth.js +0 -5
  191. package/dist-cjs/mcp/client.js +0 -185
  192. package/dist-cjs/mcp/connection.js +0 -484
  193. package/dist-cjs/mcp/index.js +0 -30
  194. package/dist-cjs/mcp/jsonrpc.js +0 -148
  195. package/dist-cjs/mcp/types.js +0 -8
  196. package/dist-cjs/network-control-adapter.js +0 -77
  197. package/dist-cjs/network-runtime/address-types.js +0 -168
  198. package/dist-cjs/network-runtime/db-owner-fencing.js +0 -76
  199. package/dist-cjs/network-runtime/delivery-receipts.js +0 -276
  200. package/dist-cjs/network-runtime/direct-endpoint-authority.js +0 -29
  201. package/dist-cjs/network-runtime/local-control-contract.js +0 -633
  202. package/dist-cjs/network-runtime/node-store-contract.js +0 -38
  203. package/dist-cjs/network-runtime/pair-route-contract.js +0 -80
  204. package/dist-cjs/network-runtime/peer-capabilities.js +0 -37
  205. package/dist-cjs/network-runtime/peer-principal-ref.js +0 -15
  206. package/dist-cjs/network-runtime/peer-state-machine.js +0 -129
  207. package/dist-cjs/network-runtime/protocol-schemas.js +0 -212
  208. package/dist-cjs/network-runtime/runtime-bootstrap-contract.js +0 -63
  209. package/dist-cjs/outlook/desktop-session.js +0 -318
  210. package/dist-cjs/policy.js +0 -155
  211. package/dist-cjs/providers/brave.js +0 -66
  212. package/dist-cjs/providers/duckduckgo.js +0 -180
  213. package/dist-cjs/providers/exa.js +0 -67
  214. package/dist-cjs/providers/firecrawl.js +0 -59
  215. package/dist-cjs/providers/index.js +0 -17
  216. package/dist-cjs/providers/jina.js +0 -53
  217. package/dist-cjs/providers/router.js +0 -100
  218. package/dist-cjs/providers/search-provider.js +0 -36
  219. package/dist-cjs/providers/tavily.js +0 -58
  220. package/dist-cjs/quip/desktop-session.js +0 -353
  221. package/dist-cjs/registry/index.js +0 -6
  222. package/dist-cjs/registry/registry.js +0 -761
  223. package/dist-cjs/runtime-socket-local-control-client.js +0 -367
  224. package/dist-cjs/security/dns-normalization.js +0 -22
  225. package/dist-cjs/security/dns-pinning.js +0 -160
  226. package/dist-cjs/security/external-content.js +0 -95
  227. package/dist-cjs/security/ssrf.js +0 -221
  228. package/dist-cjs/slack/desktop-session.js +0 -366
  229. package/dist-cjs/tool-factory.js +0 -50
  230. package/dist-cjs/types.js +0 -8
  231. package/dist-cjs/utils/retry.js +0 -169
  232. package/dist-cjs/utils/safe-parse-json.js +0 -164
  233. package/dist-cjs/utils/url.js +0 -23
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LOCAL_HTTP_CLIENT_PROOF_HEADER = exports.LOCAL_HTTP_CLIENT_ID_HEADER = void 0;
4
- exports.LOCAL_HTTP_CLIENT_ID_HEADER = "x-aria-local-client-id";
5
- exports.LOCAL_HTTP_CLIENT_PROOF_HEADER = "x-aria-local-client-proof";
@@ -1,185 +0,0 @@
1
- "use strict";
2
- /**
3
- * MCP Multi-Server Client
4
- *
5
- * Manages connections to multiple MCP servers and aggregates their capabilities.
6
- */
7
- Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.MCPClient = void 0;
9
- const events_1 = require("events");
10
- const connection_js_1 = require("./connection.js");
11
- class MCPClient extends events_1.EventEmitter {
12
- servers = new Map();
13
- serverConfigs = new Map();
14
- reconnectTimers = new Map();
15
- logger;
16
- constructor(options) {
17
- super();
18
- this.logger = options?.logger ?? console;
19
- }
20
- /**
21
- * Connect to an MCP server
22
- */
23
- async connect(config) {
24
- if (this.servers.has(config.name)) {
25
- throw new Error(`Server already connected: ${config.name}`);
26
- }
27
- const connection = new connection_js_1.MCPServerConnection(config);
28
- await connection.initialize();
29
- // Forward events
30
- connection.on("tools/list_changed", () => this.emit("toolsChanged", config.name));
31
- connection.on("resources/list_changed", () => this.emit("resourcesChanged", config.name));
32
- connection.on("prompts/list_changed", () => this.emit("promptsChanged", config.name));
33
- connection.on("exit", (code) => {
34
- this.servers.delete(config.name);
35
- this.emit("serverExit", config.name, code);
36
- // Attempt auto-reconnect on unexpected exit
37
- if (code !== 0 && this.serverConfigs.has(config.name)) {
38
- this.reconnect(config.name, config).catch((err) => {
39
- this.logger.warn("[MCPClient] Reconnect failed:", err?.message);
40
- });
41
- }
42
- });
43
- this.servers.set(config.name, connection);
44
- this.serverConfigs.set(config.name, config);
45
- return connection;
46
- }
47
- /**
48
- * Disconnect from a specific server
49
- */
50
- async disconnect(name) {
51
- this.serverConfigs.delete(name);
52
- // Cancel any pending reconnect timer for this server
53
- const timer = this.reconnectTimers.get(name);
54
- if (timer !== undefined) {
55
- clearTimeout(timer);
56
- this.reconnectTimers.delete(name);
57
- }
58
- const conn = this.servers.get(name);
59
- if (conn) {
60
- this.servers.delete(name);
61
- conn.removeAllListeners();
62
- await conn.shutdown();
63
- }
64
- }
65
- /**
66
- * Attempt to reconnect to a server with exponential backoff
67
- */
68
- async reconnect(serverName, config, attempt = 0) {
69
- const maxAttempts = 3;
70
- const delay = Math.min(1000 * Math.pow(2, attempt), 30000);
71
- if (attempt >= maxAttempts)
72
- return;
73
- await new Promise((resolve) => {
74
- const timer = setTimeout(resolve, delay);
75
- this.reconnectTimers.set(serverName, timer);
76
- });
77
- this.reconnectTimers.delete(serverName);
78
- // Bail out if server was disconnected while we were waiting
79
- if (!this.serverConfigs.has(serverName))
80
- return;
81
- // Guard against double-reconnect race: if another reconnect already
82
- // re-established the connection, skip this attempt.
83
- if (this.servers.has(serverName))
84
- return;
85
- try {
86
- await this.connect(config);
87
- }
88
- catch {
89
- await this.reconnect(serverName, config, attempt + 1);
90
- }
91
- }
92
- /**
93
- * Disconnect from all servers
94
- */
95
- async disconnectAll() {
96
- // Collect names from both active connections and pending reconnects
97
- const names = new Set([...this.servers.keys(), ...this.reconnectTimers.keys()]);
98
- await Promise.all([...names].map((name) => this.disconnect(name)));
99
- }
100
- /**
101
- * Get connected server names
102
- */
103
- getConnectedServers() {
104
- return [...this.servers.keys()];
105
- }
106
- // ========== Tools ==========
107
- /**
108
- * List all tools from all connected servers
109
- */
110
- async listAllTools() {
111
- const results = await Promise.all([...this.servers.entries()].map(async ([name, conn]) => {
112
- try {
113
- const tools = await conn.listTools();
114
- return tools.map((t) => ({ ...t, server: name }));
115
- }
116
- catch {
117
- return [];
118
- }
119
- }));
120
- return results.flat();
121
- }
122
- /**
123
- * Call a tool on a specific server
124
- */
125
- async callTool(server, tool, args, abortSignal) {
126
- const conn = this.servers.get(server);
127
- if (!conn) {
128
- return { success: false, message: `Server not connected: ${server}` };
129
- }
130
- return conn.callTool(tool, args, abortSignal);
131
- }
132
- // ========== Resources ==========
133
- /**
134
- * List all resources from all connected servers
135
- */
136
- async listAllResources() {
137
- const results = await Promise.all([...this.servers.entries()].map(async ([name, conn]) => {
138
- try {
139
- const resources = await conn.listResources();
140
- return resources.map((r) => ({ ...r, server: name }));
141
- }
142
- catch {
143
- return [];
144
- }
145
- }));
146
- return results.flat();
147
- }
148
- /**
149
- * Read a resource from a specific server
150
- */
151
- async readResource(server, uri) {
152
- const conn = this.servers.get(server);
153
- if (!conn) {
154
- throw new Error(`Server not connected: ${server}`);
155
- }
156
- return conn.readResource(uri);
157
- }
158
- // ========== Prompts ==========
159
- /**
160
- * List all prompts from all connected servers
161
- */
162
- async listAllPrompts() {
163
- const results = await Promise.all([...this.servers.entries()].map(async ([name, conn]) => {
164
- try {
165
- const prompts = await conn.listPrompts();
166
- return prompts.map((p) => ({ ...p, server: name }));
167
- }
168
- catch {
169
- return [];
170
- }
171
- }));
172
- return results.flat();
173
- }
174
- /**
175
- * Get a prompt from a specific server
176
- */
177
- async getPrompt(server, name, args) {
178
- const conn = this.servers.get(server);
179
- if (!conn) {
180
- throw new Error(`Server not connected: ${server}`);
181
- }
182
- return conn.getPrompt(name, args);
183
- }
184
- }
185
- exports.MCPClient = MCPClient;
@@ -1,484 +0,0 @@
1
- "use strict";
2
- /**
3
- * MCP Server Connection
4
- *
5
- * Manages lifecycle and protocol methods for a single MCP server.
6
- */
7
- Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.MCPServerConnection = void 0;
9
- const child_process_1 = require("child_process");
10
- const events_1 = require("events");
11
- const stream_1 = require("stream");
12
- const jsonrpc_js_1 = require("./jsonrpc.js");
13
- function sanitizeGitEnv(env = process.env) {
14
- const sanitized = {};
15
- for (const [key, value] of Object.entries(env)) {
16
- if (value === undefined)
17
- continue;
18
- if (key.toUpperCase().startsWith("GIT_"))
19
- continue;
20
- sanitized[key] = value;
21
- }
22
- return sanitized;
23
- }
24
- class MCPServerConnection extends events_1.EventEmitter {
25
- config;
26
- process;
27
- transport = "stdio";
28
- rpc;
29
- capabilities = {};
30
- _initialized = false;
31
- sseAbortController;
32
- sseStreamPromise;
33
- ssePostUrl;
34
- sseSessionId;
35
- sseOutput;
36
- // Auto-reconnect state
37
- _disconnecting = false;
38
- _reconnecting = false;
39
- _reconnectAttempts = 0;
40
- static RECONNECT_BASE_DELAY_MS = 1000;
41
- static RECONNECT_MAX_DELAY_MS = 30000;
42
- static RECONNECT_MAX_ATTEMPTS = 5;
43
- constructor(config) {
44
- super();
45
- this.config = config;
46
- this.rpc = new jsonrpc_js_1.JSONRPCClient();
47
- }
48
- get name() {
49
- return this.config.name;
50
- }
51
- get initialized() {
52
- return this._initialized;
53
- }
54
- /**
55
- * Initialize connection to MCP server
56
- */
57
- async initialize() {
58
- // Reset disconnecting flag on fresh initialization so auto-reconnect is active.
59
- // During reconnect, attemptReconnect() checks _disconnecting before each attempt.
60
- if (!this._reconnecting) {
61
- this._disconnecting = false;
62
- this._reconnectAttempts = 0;
63
- }
64
- this.transport = this.config.transport ?? "stdio";
65
- try {
66
- if (this.transport === "stdio") {
67
- await this.initializeStdioTransport();
68
- }
69
- else {
70
- await this.initializeSSETransport();
71
- }
72
- // Forward notifications
73
- this.rpc.on("notifications/tools/list_changed", () => this.emit("tools/list_changed"));
74
- this.rpc.on("notifications/resources/list_changed", () => this.emit("resources/list_changed"));
75
- this.rpc.on("notifications/prompts/list_changed", () => this.emit("prompts/list_changed"));
76
- // MCP initialize handshake
77
- const result = await this.rpc.request("initialize", {
78
- protocolVersion: "2024-11-05",
79
- capabilities: {
80
- roots: { listChanged: true },
81
- },
82
- clientInfo: {
83
- name: "aria-cli",
84
- version: "1.0.0",
85
- },
86
- });
87
- this.capabilities = result.capabilities || {};
88
- this._initialized = true;
89
- // Send initialized notification
90
- this.rpc.notify("notifications/initialized", {});
91
- return this.capabilities;
92
- }
93
- catch (error) {
94
- await this.cleanupAfterInitializeFailure();
95
- throw error;
96
- }
97
- }
98
- /**
99
- * Attempt to reconnect with exponential backoff.
100
- * Called when the transport drops unexpectedly (not via explicit shutdown).
101
- */
102
- async attemptReconnect() {
103
- if (this._disconnecting || this._reconnecting) {
104
- return;
105
- }
106
- this._reconnecting = true;
107
- while (this._reconnectAttempts < MCPServerConnection.RECONNECT_MAX_ATTEMPTS &&
108
- !this._disconnecting) {
109
- this._reconnectAttempts++;
110
- const delay = Math.min(MCPServerConnection.RECONNECT_BASE_DELAY_MS * Math.pow(2, this._reconnectAttempts - 1), MCPServerConnection.RECONNECT_MAX_DELAY_MS);
111
- this.emit("log", {
112
- level: "info",
113
- message: `Reconnect attempt ${this._reconnectAttempts}/${MCPServerConnection.RECONNECT_MAX_ATTEMPTS} in ${delay}ms`,
114
- });
115
- await new Promise((resolve) => setTimeout(resolve, delay));
116
- if (this._disconnecting) {
117
- break;
118
- }
119
- try {
120
- // Clean up stale RPC state before reconnecting
121
- this.rpc.close();
122
- this.rpc = new jsonrpc_js_1.JSONRPCClient();
123
- await this.initialize();
124
- // Success — reset reconnect state
125
- this._reconnectAttempts = 0;
126
- this._reconnecting = false;
127
- this.emit("reconnected");
128
- return;
129
- }
130
- catch (error) {
131
- this.emit("log", {
132
- level: "error",
133
- message: `Reconnect attempt ${this._reconnectAttempts} failed: ${error.message}`,
134
- });
135
- }
136
- }
137
- // All attempts exhausted or disconnecting flag was set
138
- this._reconnecting = false;
139
- if (!this._disconnecting) {
140
- this._initialized = false;
141
- this.emit("error", new Error(`Failed to reconnect after ${MCPServerConnection.RECONNECT_MAX_ATTEMPTS} attempts`));
142
- }
143
- }
144
- async initializeStdioTransport() {
145
- if (!this.config.command) {
146
- throw new Error("Command required for stdio transport");
147
- }
148
- // Spawn server process
149
- this.process = (0, child_process_1.spawn)(this.config.command, this.config.args || [], {
150
- env: { ...sanitizeGitEnv(), ...sanitizeGitEnv(this.config.env ?? {}) },
151
- stdio: ["pipe", "pipe", "pipe"],
152
- });
153
- if (!this.process.stdin || !this.process.stdout) {
154
- throw new Error("Failed to create stdio streams");
155
- }
156
- this.rpc.connect(this.process.stdin, this.process.stdout);
157
- // Handle stderr for logging
158
- this.process.stderr?.on("data", (data) => {
159
- this.emit("log", { level: "error", message: data.toString() });
160
- });
161
- this.process.on("exit", (code) => {
162
- this._initialized = false;
163
- this.rpc.close();
164
- this.emit("exit", code);
165
- // Attempt auto-reconnect on unexpected exit
166
- if (!this._disconnecting) {
167
- void this.attemptReconnect();
168
- }
169
- });
170
- }
171
- async initializeSSETransport() {
172
- const url = this.config.url;
173
- if (!url) {
174
- throw new Error("URL required for sse transport");
175
- }
176
- const stdin = new stream_1.Writable({
177
- write: (chunk, _encoding, callback) => {
178
- const payload = chunk.toString();
179
- this.postSSEMessages(payload)
180
- .then(() => callback())
181
- .catch((err) => callback(err));
182
- },
183
- });
184
- const stdout = new stream_1.PassThrough();
185
- this.sseOutput = stdout;
186
- this.rpc.connect(stdin, stdout);
187
- this.sseAbortController = new AbortController();
188
- const response = await fetch(url, {
189
- method: "GET",
190
- headers: { Accept: "text/event-stream" },
191
- signal: this.sseAbortController.signal,
192
- });
193
- if (!response.ok || !response.body) {
194
- const errorText = await response.text();
195
- throw new Error(`Failed to connect SSE transport: ${response.status} ${response.statusText} ${errorText}`);
196
- }
197
- this.sseSessionId = response.headers.get("mcp-session-id") ?? undefined;
198
- this.ssePostUrl = url;
199
- this.sseStreamPromise = this.consumeSSE(response.body, url);
200
- }
201
- async postSSEMessages(payload) {
202
- const trimmed = payload.trim();
203
- if (!trimmed) {
204
- return;
205
- }
206
- const endpoint = this.ssePostUrl || this.config.url;
207
- if (!endpoint) {
208
- throw new Error("SSE transport endpoint is not configured");
209
- }
210
- const messages = trimmed
211
- .split("\n")
212
- .map((line) => line.trim())
213
- .filter((line) => line.length > 0);
214
- for (const message of messages) {
215
- const response = await fetch(endpoint, {
216
- method: "POST",
217
- headers: {
218
- "Content-Type": "application/json",
219
- ...(this.sseSessionId ? { "mcp-session-id": this.sseSessionId } : {}),
220
- },
221
- body: message,
222
- });
223
- if (!response.ok) {
224
- const errorText = await response.text();
225
- throw new Error(`SSE transport POST failed: ${response.status} ${response.statusText} ${errorText}`);
226
- }
227
- const responseText = (await response.text()).trim();
228
- if (!responseText || !this.sseOutput) {
229
- continue;
230
- }
231
- // Some servers return direct JSON-RPC responses on POST while others
232
- // publish responses only via SSE. Forward direct JSON replies into RPC.
233
- for (const line of responseText.split("\n")) {
234
- const jsonLine = line.trim();
235
- if (!jsonLine) {
236
- continue;
237
- }
238
- if (jsonLine.startsWith("{") && jsonLine.endsWith("}")) {
239
- this.sseOutput.write(`${jsonLine}\n`);
240
- }
241
- }
242
- }
243
- }
244
- async consumeSSE(body, baseUrl) {
245
- const reader = body.getReader();
246
- const decoder = new TextDecoder();
247
- let buffer = "";
248
- let eventType = "";
249
- let dataLines = [];
250
- const dispatch = () => {
251
- if (dataLines.length === 0) {
252
- eventType = "";
253
- return;
254
- }
255
- const data = dataLines.join("\n").trim();
256
- const event = eventType || "message";
257
- eventType = "";
258
- dataLines = [];
259
- if (!data) {
260
- return;
261
- }
262
- if (event === "endpoint") {
263
- this.ssePostUrl = new URL(data, baseUrl).toString();
264
- return;
265
- }
266
- if (!this.sseOutput) {
267
- return;
268
- }
269
- if (data.startsWith("{") && data.endsWith("}")) {
270
- this.sseOutput.write(`${data}\n`);
271
- }
272
- };
273
- try {
274
- while (true) {
275
- const { done, value } = await reader.read();
276
- if (done) {
277
- dispatch();
278
- break;
279
- }
280
- buffer += decoder.decode(value, { stream: true });
281
- const lines = buffer.split(/\r?\n/);
282
- buffer = lines.pop() || "";
283
- for (const line of lines) {
284
- if (line === "") {
285
- dispatch();
286
- continue;
287
- }
288
- if (line.startsWith("event:")) {
289
- eventType = line.slice(6).trim();
290
- continue;
291
- }
292
- if (line.startsWith("data:")) {
293
- dataLines.push(line.slice(5).trim());
294
- }
295
- }
296
- }
297
- }
298
- catch (error) {
299
- // Ignore abort errors from normal shutdown.
300
- if (!(error instanceof DOMException && error.name === "AbortError")) {
301
- this.emit("log", {
302
- level: "error",
303
- message: `SSE stream error: ${error.message}`,
304
- });
305
- }
306
- }
307
- finally {
308
- if (this._initialized) {
309
- this._initialized = false;
310
- this.rpc.close();
311
- this.emit("exit", 0);
312
- // Attempt auto-reconnect on unexpected SSE stream end
313
- if (!this._disconnecting) {
314
- void this.attemptReconnect();
315
- }
316
- }
317
- }
318
- }
319
- async cleanupAfterInitializeFailure() {
320
- if (this.transport === "stdio") {
321
- if (this.process && !this.process.killed) {
322
- try {
323
- this.process.kill("SIGTERM");
324
- }
325
- catch {
326
- // Ignore kill failures during cleanup.
327
- }
328
- }
329
- this.process = undefined;
330
- }
331
- else {
332
- this.sseAbortController?.abort();
333
- try {
334
- await this.sseStreamPromise;
335
- }
336
- catch {
337
- // Ignore stream errors during cleanup.
338
- }
339
- this.sseOutput?.end();
340
- this.sseOutput = undefined;
341
- }
342
- this.rpc.close();
343
- }
344
- /**
345
- * Gracefully shutdown the connection
346
- */
347
- async shutdown() {
348
- this._disconnecting = true;
349
- if (!this._initialized)
350
- return;
351
- this._initialized = false;
352
- if (this.transport === "sse") {
353
- try {
354
- await this.rpc.request("shutdown", {});
355
- this.rpc.notify("notifications/exit", {});
356
- }
357
- catch {
358
- // Best effort
359
- }
360
- this.sseAbortController?.abort();
361
- try {
362
- await this.sseStreamPromise;
363
- }
364
- catch {
365
- // Ignore stream shutdown errors
366
- }
367
- this.sseOutput?.end();
368
- this.sseOutput = undefined;
369
- this.rpc.close();
370
- return;
371
- }
372
- if (this.process) {
373
- // Send MCP shutdown/exit protocol before killing
374
- try {
375
- await this.rpc.request("shutdown", {});
376
- this.rpc.notify("notifications/exit", {});
377
- // Give process a moment to clean up
378
- await new Promise((resolve) => setTimeout(resolve, 500));
379
- }
380
- catch {
381
- // Process may already be dead, proceed with kill
382
- }
383
- this.process.kill("SIGTERM");
384
- // Await process exit before resolving
385
- await new Promise((resolve) => {
386
- // Escalate to SIGKILL if process doesn't exit within 5s
387
- const killTimeout = setTimeout(() => {
388
- try {
389
- this.process?.kill("SIGKILL");
390
- }
391
- catch {
392
- /* already dead */
393
- }
394
- }, 5000);
395
- this.process.once("exit", () => {
396
- clearTimeout(killTimeout);
397
- this.rpc.close();
398
- resolve();
399
- });
400
- });
401
- }
402
- else {
403
- this.rpc.close();
404
- }
405
- }
406
- // ========== Tools ==========
407
- async listTools() {
408
- const result = await this.rpc.request("tools/list", {});
409
- return result.tools || [];
410
- }
411
- async callTool(name, args, abortSignal) {
412
- // Early abort check before making the RPC call
413
- if (abortSignal?.aborted) {
414
- return { success: false, message: "Tool execution cancelled by user." };
415
- }
416
- try {
417
- // Race the RPC call against the abort signal so user cancel
418
- // doesn't block waiting for a slow MCP server response.
419
- const rpcPromise = this.rpc.request("tools/call", {
420
- name,
421
- arguments: args,
422
- });
423
- let result;
424
- if (abortSignal) {
425
- result = await new Promise((resolve, reject) => {
426
- const onAbort = () => reject(new DOMException("Aborted", "AbortError"));
427
- abortSignal.addEventListener("abort", onAbort, { once: true });
428
- rpcPromise.then((r) => {
429
- abortSignal.removeEventListener("abort", onAbort);
430
- resolve(r);
431
- }, (e) => {
432
- abortSignal.removeEventListener("abort", onAbort);
433
- reject(e);
434
- });
435
- });
436
- }
437
- else {
438
- result = await rpcPromise;
439
- }
440
- const text = result.content
441
- ?.filter((c) => c.type === "text")
442
- .map((c) => c.text)
443
- .join("\n") || "";
444
- return {
445
- success: !result.isError,
446
- message: text,
447
- data: result.content,
448
- };
449
- }
450
- catch (error) {
451
- if (error instanceof DOMException && error.name === "AbortError") {
452
- return { success: false, message: "Tool execution cancelled by user." };
453
- }
454
- return {
455
- success: false,
456
- message: error.message,
457
- };
458
- }
459
- }
460
- // ========== Resources ==========
461
- async listResources() {
462
- const result = await this.rpc.request("resources/list", {});
463
- return result.resources || [];
464
- }
465
- async readResource(uri) {
466
- const result = await this.rpc.request("resources/read", {
467
- uri,
468
- });
469
- return result.contents || [];
470
- }
471
- // ========== Prompts ==========
472
- async listPrompts() {
473
- const result = await this.rpc.request("prompts/list", {});
474
- return result.prompts || [];
475
- }
476
- async getPrompt(name, args) {
477
- const result = await this.rpc.request("prompts/get", {
478
- name,
479
- arguments: args,
480
- });
481
- return result.messages || [];
482
- }
483
- }
484
- exports.MCPServerConnection = MCPServerConnection;