@mantyx/sdk 2026.11.14 → 2026.11.15

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/CHANGELOG.md CHANGED
@@ -9,9 +9,13 @@ and new releases use calendar versions (`YYYY.M.RELEASE`). Historical versions r
9
9
 
10
10
  - `defineLocalMcp({ ..., toolFilter: [...] })` restricts a local MCP server to the listed tools, as `mantyxMcp` already allows. Unlisted tools are left out of the catalog sent to MANTYX, and the SDK refuses to forward a call to one. The filter stays in the client.
11
11
 
12
+ - `defineLocalMcp({ ..., beforeCall })` runs a hook before every MCP `tools/call`. It gets the unprefixed tool name and a copy of the arguments, and a returned string refuses the call: the string goes back to the model as the tool's error and the server is never called. It may be async.
13
+ - `defineLocalMcp({ ..., fetch })` sets the `fetch` every request of a Streamable HTTP MCP session goes through, including the SSE GET and the closing DELETE, for per-request signing such as AWS SigV4.
14
+
12
15
  ### Fixed
13
16
 
14
17
  - A local MCP connection is now closed when `tools/list` fails during discovery.
18
+ - Closing a Streamable HTTP local MCP connection now ends the server-side session (`DELETE`), as the Python SDK does.
15
19
 
16
20
  ### Changed
17
21
 
package/README.md CHANGED
@@ -215,6 +215,8 @@ await client.runAgent({
215
215
  url: "http://localhost:8080/mcp",
216
216
  headers: { Authorization: `Bearer ${process.env.FS_TOKEN}` },
217
217
  toolFilter: ["read_file", "list_dir"], // optional allow-list, enforced by the SDK
218
+ // beforeCall: (tool, args) => refusal string or undefined, // optional call check
219
+ // fetch: signingFetch, // optional, e.g. per-request SigV4 signing
218
220
  }),
219
221
 
220
222
  // (b) stdio MCP server — the SDK spawns the process for you.
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  MantyxError,
3
3
  MantyxRunError
4
- } from "./chunk-DBMT5UZ2.js";
4
+ } from "./chunk-FWRGNJUW.js";
5
5
 
6
6
  // src/a2a-server.ts
7
7
  import {
@@ -170,8 +170,18 @@ function localMcpToolFilter(toolFilter) {
170
170
  }
171
171
  return toolFilter.length > 0 ? { toolFilter: [...toolFilter] } : {};
172
172
  }
173
+ function localMcpBeforeCall(beforeCall) {
174
+ if (beforeCall === void 0) return {};
175
+ if (typeof beforeCall !== "function") {
176
+ throw new Error("defineLocalMcp: beforeCall must be a function");
177
+ }
178
+ return { beforeCall };
179
+ }
173
180
  function defineLocalMcp(opts) {
174
181
  assertToolName(opts.name);
182
+ if (opts.fetch !== void 0 && typeof opts.fetch !== "function") {
183
+ throw new Error("defineLocalMcp: fetch must be a function");
184
+ }
175
185
  const hasHttp = typeof opts.url === "string" && opts.url.length > 0;
176
186
  const hasStdio = typeof opts.command === "string" && opts.command.length > 0;
177
187
  if (hasHttp && hasStdio) {
@@ -184,6 +194,9 @@ function defineLocalMcp(opts) {
184
194
  "defineLocalMcp: one of `url` (Streamable HTTP) or `command` (stdio) is required"
185
195
  );
186
196
  }
197
+ if (opts.fetch !== void 0 && !hasHttp) {
198
+ throw new Error("defineLocalMcp: `fetch` applies to the Streamable HTTP transport only");
199
+ }
187
200
  if (hasHttp) {
188
201
  const url = opts.url;
189
202
  return {
@@ -191,10 +204,12 @@ function defineLocalMcp(opts) {
191
204
  name: opts.name,
192
205
  http: {
193
206
  url,
194
- ...opts.headers ? { headers: { ...opts.headers } } : {}
207
+ ...opts.headers ? { headers: { ...opts.headers } } : {},
208
+ ...opts.fetch ? { fetch: opts.fetch } : {}
195
209
  },
196
210
  stdio: void 0,
197
211
  ...localMcpToolFilter(opts.toolFilter),
212
+ ...localMcpBeforeCall(opts.beforeCall),
198
213
  ...toolGrouping(opts)
199
214
  };
200
215
  }
@@ -210,6 +225,7 @@ function defineLocalMcp(opts) {
210
225
  ...opts.cwd ? { cwd: opts.cwd } : {}
211
226
  },
212
227
  ...localMcpToolFilter(opts.toolFilter),
228
+ ...localMcpBeforeCall(opts.beforeCall),
213
229
  ...toolGrouping(opts)
214
230
  };
215
231
  }
@@ -548,7 +564,8 @@ async function resolveMcp(t) {
548
564
  if (t.http) {
549
565
  const { StreamableHTTPClientTransport } = await import("@modelcontextprotocol/sdk/client/streamableHttp.js");
550
566
  const httpTransport = new StreamableHTTPClientTransport(new URL(t.http.url), {
551
- requestInit: t.http.headers ? { headers: t.http.headers } : {}
567
+ requestInit: t.http.headers ? { headers: t.http.headers } : {},
568
+ ...t.http.fetch ? { fetch: t.http.fetch } : {}
552
569
  });
553
570
  transport = httpTransport;
554
571
  connect = (c) => c.connect(httpTransport);
@@ -577,6 +594,11 @@ async function resolveMcp(t) {
577
594
  );
578
595
  }
579
596
  const close = async () => {
597
+ try {
598
+ const t2 = transport;
599
+ if (t2.terminateSession) await t2.terminateSession();
600
+ } catch {
601
+ }
580
602
  try {
581
603
  await client.close();
582
604
  } catch {
@@ -613,6 +635,20 @@ async function resolveMcp(t) {
613
635
  };
614
636
  return true;
615
637
  }
638
+ async function mcpCallRefusal(server, toolName, args) {
639
+ if (server.toolFilter && !server.toolFilter.includes(toolName)) {
640
+ return `MCP tool ${JSON.stringify(toolName)} is not in the toolFilter of defineLocalMcp(${JSON.stringify(server.name)})`;
641
+ }
642
+ if (!server.beforeCall) return void 0;
643
+ const out = await server.beforeCall(toolName, structuredClone(args));
644
+ if (out === void 0 || out === null) return void 0;
645
+ if (typeof out !== "string") {
646
+ throw new Error(
647
+ `defineLocalMcp(${JSON.stringify(server.name)}): beforeCall must return a string, undefined or null`
648
+ );
649
+ }
650
+ return out || `defineLocalMcp(${JSON.stringify(server.name)}): beforeCall refused the call`;
651
+ }
616
652
  async function closeMcpRefs(tools) {
617
653
  if (!tools || tools.length === 0) return;
618
654
  const closes = [];
@@ -1094,6 +1130,11 @@ var MantyxClient = class {
1094
1130
  return;
1095
1131
  }
1096
1132
  const upstreamName = mcpToolName.startsWith(`${serverName}_`) ? mcpToolName.slice(serverName.length + 1) : mcpToolName;
1133
+ const refusal = await mcpCallRefusal(server, upstreamName, ev.args ?? {});
1134
+ if (refusal !== void 0) {
1135
+ await this.postToolResult(runId, ev.toolUseId, { error: refusal });
1136
+ return;
1137
+ }
1097
1138
  out = await callMcpTool(server, upstreamName, ev.args ?? {});
1098
1139
  } else {
1099
1140
  const handler = handlers.localTools.get(ev.name);
package/dist/index.cjs CHANGED
@@ -241,8 +241,18 @@ function localMcpToolFilter(toolFilter) {
241
241
  }
242
242
  return toolFilter.length > 0 ? { toolFilter: [...toolFilter] } : {};
243
243
  }
244
+ function localMcpBeforeCall(beforeCall) {
245
+ if (beforeCall === void 0) return {};
246
+ if (typeof beforeCall !== "function") {
247
+ throw new Error("defineLocalMcp: beforeCall must be a function");
248
+ }
249
+ return { beforeCall };
250
+ }
244
251
  function defineLocalMcp(opts) {
245
252
  assertToolName(opts.name);
253
+ if (opts.fetch !== void 0 && typeof opts.fetch !== "function") {
254
+ throw new Error("defineLocalMcp: fetch must be a function");
255
+ }
246
256
  const hasHttp = typeof opts.url === "string" && opts.url.length > 0;
247
257
  const hasStdio = typeof opts.command === "string" && opts.command.length > 0;
248
258
  if (hasHttp && hasStdio) {
@@ -255,6 +265,9 @@ function defineLocalMcp(opts) {
255
265
  "defineLocalMcp: one of `url` (Streamable HTTP) or `command` (stdio) is required"
256
266
  );
257
267
  }
268
+ if (opts.fetch !== void 0 && !hasHttp) {
269
+ throw new Error("defineLocalMcp: `fetch` applies to the Streamable HTTP transport only");
270
+ }
258
271
  if (hasHttp) {
259
272
  const url = opts.url;
260
273
  return {
@@ -262,10 +275,12 @@ function defineLocalMcp(opts) {
262
275
  name: opts.name,
263
276
  http: {
264
277
  url,
265
- ...opts.headers ? { headers: { ...opts.headers } } : {}
278
+ ...opts.headers ? { headers: { ...opts.headers } } : {},
279
+ ...opts.fetch ? { fetch: opts.fetch } : {}
266
280
  },
267
281
  stdio: void 0,
268
282
  ...localMcpToolFilter(opts.toolFilter),
283
+ ...localMcpBeforeCall(opts.beforeCall),
269
284
  ...toolGrouping(opts)
270
285
  };
271
286
  }
@@ -281,6 +296,7 @@ function defineLocalMcp(opts) {
281
296
  ...opts.cwd ? { cwd: opts.cwd } : {}
282
297
  },
283
298
  ...localMcpToolFilter(opts.toolFilter),
299
+ ...localMcpBeforeCall(opts.beforeCall),
284
300
  ...toolGrouping(opts)
285
301
  };
286
302
  }
@@ -404,7 +420,8 @@ async function resolveMcp(t) {
404
420
  if (t.http) {
405
421
  const { StreamableHTTPClientTransport } = await import("@modelcontextprotocol/sdk/client/streamableHttp.js");
406
422
  const httpTransport = new StreamableHTTPClientTransport(new URL(t.http.url), {
407
- requestInit: t.http.headers ? { headers: t.http.headers } : {}
423
+ requestInit: t.http.headers ? { headers: t.http.headers } : {},
424
+ ...t.http.fetch ? { fetch: t.http.fetch } : {}
408
425
  });
409
426
  transport = httpTransport;
410
427
  connect = (c) => c.connect(httpTransport);
@@ -433,6 +450,11 @@ async function resolveMcp(t) {
433
450
  );
434
451
  }
435
452
  const close = async () => {
453
+ try {
454
+ const t2 = transport;
455
+ if (t2.terminateSession) await t2.terminateSession();
456
+ } catch {
457
+ }
436
458
  try {
437
459
  await client.close();
438
460
  } catch {
@@ -469,6 +491,20 @@ async function resolveMcp(t) {
469
491
  };
470
492
  return true;
471
493
  }
494
+ async function mcpCallRefusal(server, toolName, args) {
495
+ if (server.toolFilter && !server.toolFilter.includes(toolName)) {
496
+ return `MCP tool ${JSON.stringify(toolName)} is not in the toolFilter of defineLocalMcp(${JSON.stringify(server.name)})`;
497
+ }
498
+ if (!server.beforeCall) return void 0;
499
+ const out = await server.beforeCall(toolName, structuredClone(args));
500
+ if (out === void 0 || out === null) return void 0;
501
+ if (typeof out !== "string") {
502
+ throw new Error(
503
+ `defineLocalMcp(${JSON.stringify(server.name)}): beforeCall must return a string, undefined or null`
504
+ );
505
+ }
506
+ return out || `defineLocalMcp(${JSON.stringify(server.name)}): beforeCall refused the call`;
507
+ }
472
508
  async function closeMcpRefs(tools) {
473
509
  if (!tools || tools.length === 0) return;
474
510
  const closes = [];
@@ -1165,6 +1201,11 @@ var MantyxClient = class {
1165
1201
  return;
1166
1202
  }
1167
1203
  const upstreamName = mcpToolName.startsWith(`${serverName}_`) ? mcpToolName.slice(serverName.length + 1) : mcpToolName;
1204
+ const refusal = await mcpCallRefusal(server, upstreamName, ev.args ?? {});
1205
+ if (refusal !== void 0) {
1206
+ await this.postToolResult(runId, ev.toolUseId, { error: refusal });
1207
+ return;
1208
+ }
1168
1209
  out = await callMcpTool(server, upstreamName, ev.args ?? {});
1169
1210
  } else {
1170
1211
  const handler = handlers.localTools.get(ev.name);
@@ -2873,7 +2914,7 @@ async function runComputerUse(opts) {
2873
2914
  }
2874
2915
 
2875
2916
  // src/version.ts
2876
- var SDK_VERSION = "2026.11.14";
2917
+ var SDK_VERSION = "2026.11.15";
2877
2918
  // Annotate the CommonJS export names for ESM import in node:
2878
2919
  0 && (module.exports = {
2879
2920
  AgentSession,
package/dist/index.d.ts CHANGED
@@ -37,7 +37,7 @@ export type { DecisionContent, DecisionQuestion, DecisionRequest, DecisionAnswer
37
37
  export { MantyxClient, AgentSession, DEFAULT_BASE_URL, parseRunOutput, taskPlanFromEventData, inputFileAttachment, inputFileUrlAttachment, } from "./client.js";
38
38
  export type { MantyxClientOptions, ModelInfo, ModelCatalog, AgentSpecBase, RunSpec, SessionSpec, ToolNamespace, OutputSchema, OutputSchemaValue, LoopDetection, ToolBudget, ToolBudgets, Supervisor, SupervisorAction, SupervisorPhase, ReasoningTrigger, Subagents, SubagentSelf, SubagentDefinition, SubagentStatus, PlanOptions, PlanMode, PlanSpec, TaskPlan, TaskPlanStep, TaskPlanStepStatus, TaskPlanTransition, RunResult, RunTokenUsage, RunModelInfo, RunEvent, RunEventBase, AssistantDeltaEvent, ThinkingDeltaEvent, AssistantMessageEvent, ModelFallbackEvent, UserMessageEvent, InputFileAttachment, InputFileUrlAttachment, MessageAttachment, InputFileAttachmentMetadata, InputFileUrlAttachmentMetadata, AttachmentMetadata, ConversationMessage, ToolInteraction, ServerToolResultEvent, LocalToolCallEvent, LocalToolResultInEvent, LoopDetectedEvent, ToolBudgetExceededEvent, SupervisorEvent, SubagentStartedEvent, SubagentResultEvent, TaskPlanEvent, ResultEvent, ErrorEvent, CancelledEvent, SessionInfo, SessionSummary, SessionListResult, SessionEventsPage, RunStatus, RunInfo, RunSummary, RunListResult, ListRunsOptions, LocalHandlers, RunFeedbackVerdict, TurnInstructionsAuthority, RunFeedbackInput, RunFeedbackResult, } from "./client.js";
39
39
  export { defineLocalTool, mantyxA2A, defineLocalA2A, mantyxMcp, defineLocalMcp, isLocalTool, isLocalA2ATool, isLocalMcpServer, } from "./tools.js";
40
- export type { LocalTool, LocalToolResult, LocalToolOutput, ToolResultFile, ToolRef, ZodLikeObject, DefineLocalToolOptions, ReasoningLevel, A2AToolRef, LocalA2ATool, McpToolRef, LocalMcpServer, LocalMcpHttpTransport, LocalMcpStdioTransport, MantyxA2AOptions, DefineLocalA2AOptions, MantyxMcpOptions, DefineLocalMcpOptions, ToolGrouping, ToolGroupingOptions, } from "./tools.js";
40
+ export type { LocalTool, LocalToolResult, LocalToolOutput, ToolResultFile, ToolRef, ZodLikeObject, DefineLocalToolOptions, ReasoningLevel, A2AToolRef, LocalA2ATool, McpToolRef, LocalMcpServer, LocalMcpHttpTransport, LocalMcpStdioTransport, McpBeforeCall, McpFetch, MantyxA2AOptions, DefineLocalA2AOptions, MantyxMcpOptions, DefineLocalMcpOptions, ToolGrouping, ToolGroupingOptions, } from "./tools.js";
41
41
  export { MantyxError, MantyxAuthError, MantyxNetworkError, MantyxParseError, MantyxScopeError, MantyxToolError, MantyxRunError, } from "./errors.js";
42
42
  export type { MantyxRunErrorInit, MantyxRunErrorTokens, MantyxRunErrorModel, } from "./errors.js";
43
43
  export type { OutputSchemaEnforcement, StructuredOutputEnforcementMechanism, StructuredOutputInfo, } from "./structured-output.js";
package/dist/index.js CHANGED
@@ -24,7 +24,7 @@ import {
24
24
  taskPlanFromEventData,
25
25
  toToolParametersWire,
26
26
  zodToJsonSchema
27
- } from "./chunk-DBMT5UZ2.js";
27
+ } from "./chunk-FWRGNJUW.js";
28
28
 
29
29
  // src/oauth.ts
30
30
  var DEFAULT_OAUTH_BASE_URL = "https://app.mantyx.io";
@@ -601,7 +601,7 @@ async function runComputerUse(opts) {
601
601
  }
602
602
 
603
603
  // src/version.ts
604
- var SDK_VERSION = "2026.11.14";
604
+ var SDK_VERSION = "2026.11.15";
605
605
  export {
606
606
  AgentSession,
607
607
  COMPUTER_USE_ACTIONS,
@@ -24,6 +24,12 @@ export declare function resolveLocalRefs(tools: ReadonlyArray<ToolRef> | undefin
24
24
  /** MCP servers opened in *this* call — the run driver closes these on completion. */
25
25
  newlyOpenedMcp: LocalMcpServer[];
26
26
  }>;
27
+ /**
28
+ * Why a `tools/call` must not be sent (the {@link LocalMcpServer.toolFilter},
29
+ * then the user's `beforeCall` hook), or `undefined` to send it. A returned
30
+ * string goes back to the model as the tool's error.
31
+ */
32
+ export declare function mcpCallRefusal(server: LocalMcpServer, toolName: string, args: Record<string, unknown>): Promise<string | undefined>;
27
33
  /**
28
34
  * Close every MCP transport on the given refs (best-effort, idempotent).
29
35
  * Safe to call multiple times — the resolved cache is cleared after the
package/dist/tools.d.ts CHANGED
@@ -300,7 +300,19 @@ export interface LocalMcpHttpTransport {
300
300
  url: string;
301
301
  /** HTTP headers sent on every MCP request (typically `Authorization`). */
302
302
  headers?: Record<string, string>;
303
+ /** `fetch` used for every MCP request; see {@link DefineLocalMcpOptions.fetch}. */
304
+ fetch?: McpFetch;
303
305
  }
306
+ /** `fetch`-compatible function the local MCP HTTP transport sends requests with. */
307
+ export type McpFetch = (url: string | URL, init?: RequestInit) => Promise<Response>;
308
+ /**
309
+ * Hook run before every local MCP `tools/call`. Receives the tool name as
310
+ * the MCP server knows it (unprefixed) and a copy of the arguments. Return
311
+ * `undefined` / `null` to send the call, or a string to refuse it: the
312
+ * string goes back to the model as the tool's error and the server is
313
+ * never called.
314
+ */
315
+ export type McpBeforeCall = (toolName: string, args: Record<string, unknown>) => string | null | undefined | Promise<string | null | undefined>;
304
316
  /**
305
317
  * stdio transport spec for {@link defineLocalMcp}. The SDK spawns the
306
318
  * specified executable with the given arguments and speaks JSON-RPC over
@@ -343,6 +355,8 @@ export interface LocalMcpServer extends ToolGrouping {
343
355
  * refused without reaching the server. Unset exposes every tool.
344
356
  */
345
357
  readonly toolFilter?: string[];
358
+ /** Client-only hook run before every `tools/call` (never sent to MANTYX). */
359
+ readonly beforeCall?: McpBeforeCall;
346
360
  /**
347
361
  * Internal cache of the resolved MCP client + catalog. Populated lazily
348
362
  * by the run driver on the first run; not part of the user contract.
@@ -427,6 +441,23 @@ export interface DefineLocalMcpOptions extends ToolGroupingOptions {
427
441
  * away from the model.
428
442
  */
429
443
  toolFilter?: string[];
444
+ /**
445
+ * Runs before every `tools/call`, after {@link toolFilter}, with the tool
446
+ * name as the MCP server knows it (unprefixed) and a copy of the model's
447
+ * arguments. Return `undefined` / `null` to send the call, or a string to
448
+ * refuse it: the string goes back to the model as the tool's error and
449
+ * the server is never called. A thrown error is reported the same way.
450
+ * May be async. Stays in your process.
451
+ */
452
+ beforeCall?: McpBeforeCall;
453
+ /**
454
+ * HTTP transport only: the `fetch` the MCP transport sends every request
455
+ * with (the `tools/call` POSTs, the SSE GET and the closing DELETE). Wrap
456
+ * the global `fetch` to sign each request, for example with AWS SigV4
457
+ * over method, URL, body and timestamp. Static {@link headers} are
458
+ * already set on `init.headers` when it is called. Stays in your process.
459
+ */
460
+ fetch?: McpFetch;
430
461
  }
431
462
  export declare function defineLocalMcp(opts: DefineLocalMcpOptions): LocalMcpServer;
432
463
  export type ToolRef = LocalTool | A2AToolRef | LocalA2ATool | McpToolRef | LocalMcpServer;
package/dist/version.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * Release version — synced from sdk/VERSION (`pnpm run sync-version`).
3
3
  */
4
- export declare const SDK_VERSION = "2026.11.14";
4
+ export declare const SDK_VERSION = "2026.11.15";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mantyx/sdk",
3
- "version": "2026.11.14",
3
+ "version": "2026.11.15",
4
4
  "description": "MANTYX as a hosted agent runtime: define agents inline, mix server-resolved MCP and A2A tools with locally-executed tools, run them remotely.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",