@johnhenry/wsh 0.34.0 → 0.34.2
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.
- package/README.md +3 -1
- package/package.json +1 -1
- package/src/index.d.ts +1 -1
- package/src/mcp-bridge.mjs +45 -9
package/README.md
CHANGED
|
@@ -618,7 +618,9 @@ const pty = client.getSession(client.listSessions()[0].channelId); // live sessi
|
|
|
618
618
|
```
|
|
619
619
|
|
|
620
620
|
An aborted upload writes nothing (the server writes the file only when the last chunk arrives). An aborted `callTool`
|
|
621
|
-
also sends `McpCancel` to a host advertising `mcp-cancel`, so the tool handler's `ctx.signal` fires.
|
|
621
|
+
also sends `McpCancel` to a host advertising `mcp-cancel`, so the tool handler's `ctx.signal` fires. The Rust `wsh-server`
|
|
622
|
+
advertises it from `rust-v0.6.0`: it drops the call's task and kills the tool's process group, and offers CLI tools
|
|
623
|
+
from `[[mcp.tools]]` entries in its `config.toml`.
|
|
622
624
|
|
|
623
625
|
### MCP tools
|
|
624
626
|
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
package/src/mcp-bridge.mjs
CHANGED
|
@@ -6,13 +6,32 @@
|
|
|
6
6
|
* remote server. Tool specs are cached after discovery for efficient reuse.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import { MSG, mcpDiscover, mcpCall } from './messages.mjs';
|
|
9
|
+
import { MSG, mcpDiscover, mcpCall, mcpCancel } from './messages.mjs';
|
|
10
10
|
import { waitForControlMessage } from './control-listener.mjs';
|
|
11
|
-
import { MCP_CALL_ID_FEATURE } from './client.mjs';
|
|
11
|
+
import { MCP_CALL_ID_FEATURE, MCP_CANCEL_FEATURE } from './client.mjs';
|
|
12
12
|
|
|
13
13
|
/** Default timeout for MCP operations (15 seconds). */
|
|
14
14
|
const MCP_TIMEOUT_MS = 15_000;
|
|
15
15
|
|
|
16
|
+
function abortError(signal) {
|
|
17
|
+
const e = new DOMException('The operation was aborted', 'AbortError');
|
|
18
|
+
if (signal.reason !== undefined) e.cause = signal.reason;
|
|
19
|
+
return e;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Settle with `promise`, or reject with an AbortError the moment `signal` fires. */
|
|
23
|
+
function raceAbort(signal, promise) {
|
|
24
|
+
if (signal.aborted) return Promise.reject(abortError(signal));
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
const onAbort = () => reject(abortError(signal));
|
|
27
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
28
|
+
promise.then(
|
|
29
|
+
(v) => { signal.removeEventListener('abort', onAbort); resolve(v); },
|
|
30
|
+
(e) => { signal.removeEventListener('abort', onAbort); reject(e); },
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
16
35
|
export class WshMcpBridge {
|
|
17
36
|
/** @type {object} WshClient reference. */
|
|
18
37
|
#client;
|
|
@@ -107,9 +126,12 @@ export class WshMcpBridge {
|
|
|
107
126
|
* @param {object} [args={}] - Arguments to pass to the tool
|
|
108
127
|
* @param {object} [opts]
|
|
109
128
|
* @param {number} [opts.timeout] - Timeout in ms (default 15s)
|
|
129
|
+
* @param {AbortSignal} [opts.signal] - Abort the call. Rejects with an `AbortError`; against a host advertising
|
|
130
|
+
* `mcp-cancel` the tool is told to stop too. An already-aborted signal sends nothing.
|
|
110
131
|
* @returns {Promise<{ success: boolean, output: *, error?: string }>}
|
|
111
132
|
*/
|
|
112
|
-
async call(toolName, args = {}, { timeout = MCP_TIMEOUT_MS } = {}) {
|
|
133
|
+
async call(toolName, args = {}, { timeout = MCP_TIMEOUT_MS, signal } = {}) {
|
|
134
|
+
if (signal?.aborted) throw abortError(signal);
|
|
113
135
|
if (!toolName || typeof toolName !== 'string') {
|
|
114
136
|
throw new Error('toolName is required');
|
|
115
137
|
}
|
|
@@ -121,14 +143,16 @@ export class WshMcpBridge {
|
|
|
121
143
|
);
|
|
122
144
|
}
|
|
123
145
|
|
|
124
|
-
if (this.#correlates()) return this.#callOnce(toolName, args, timeout);
|
|
146
|
+
if (this.#correlates()) return this.#callOnce(toolName, args, timeout, signal);
|
|
125
147
|
|
|
148
|
+
// Uncorrelated: an aborted caller stops waiting, but keeps its queue slot until the reply (or timeout) so the
|
|
149
|
+
// late reply cannot be handed to the next call.
|
|
126
150
|
const run = this.#queue.then(
|
|
127
|
-
() => this.#callOnce(toolName, args, timeout),
|
|
128
|
-
() => this.#callOnce(toolName, args, timeout),
|
|
151
|
+
() => (signal?.aborted ? Promise.reject(abortError(signal)) : this.#callOnce(toolName, args, timeout)),
|
|
152
|
+
() => (signal?.aborted ? Promise.reject(abortError(signal)) : this.#callOnce(toolName, args, timeout)),
|
|
129
153
|
);
|
|
130
154
|
this.#queue = run.then(() => undefined, () => undefined);
|
|
131
|
-
return run;
|
|
155
|
+
return signal ? raceAbort(signal, run) : run;
|
|
132
156
|
}
|
|
133
157
|
|
|
134
158
|
/** True when the connected server echoes call_id. @private */
|
|
@@ -137,7 +161,7 @@ export class WshMcpBridge {
|
|
|
137
161
|
}
|
|
138
162
|
|
|
139
163
|
/** @private */
|
|
140
|
-
async #callOnce(toolName, args, timeout) {
|
|
164
|
+
async #callOnce(toolName, args, timeout, signal) {
|
|
141
165
|
const callId = this.#correlates()
|
|
142
166
|
? `bridge-${++this.#callCounter}-${Date.now()}`
|
|
143
167
|
: undefined;
|
|
@@ -146,7 +170,7 @@ export class WshMcpBridge {
|
|
|
146
170
|
mcpCall({ tool: toolName, arguments: args, callId })
|
|
147
171
|
);
|
|
148
172
|
|
|
149
|
-
const
|
|
173
|
+
const waiting = this._waitForMessage(
|
|
150
174
|
(msg) => {
|
|
151
175
|
// ERROR carries no call_id and cannot be attributed to one call;
|
|
152
176
|
// it is taken by whichever call is waiting.
|
|
@@ -157,6 +181,18 @@ export class WshMcpBridge {
|
|
|
157
181
|
},
|
|
158
182
|
timeout
|
|
159
183
|
);
|
|
184
|
+
let response;
|
|
185
|
+
try {
|
|
186
|
+
response = signal ? await raceAbort(signal, waiting) : await waiting;
|
|
187
|
+
} catch (e) {
|
|
188
|
+
if (signal?.aborted && e?.name === 'AbortError') {
|
|
189
|
+
waiting.catch(() => {}); // its own timeout still fires; nobody is listening
|
|
190
|
+
if (callId !== undefined && this.#client?.hasFeature?.(MCP_CANCEL_FEATURE) === true) {
|
|
191
|
+
Promise.resolve(this.#client.sendControl(mcpCancel({ callId }))).catch(() => {});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
throw e;
|
|
195
|
+
}
|
|
160
196
|
|
|
161
197
|
if (response.type === MSG.ERROR) {
|
|
162
198
|
return {
|