@superdurable/dex-agents 0.4.0 → 0.6.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 (144) hide show
  1. package/README.md +616 -55
  2. package/dist/src/agent-approval-display.d.ts +135 -0
  3. package/dist/src/agent-approval-display.js +536 -0
  4. package/dist/src/agent-approval-display.js.map +1 -0
  5. package/dist/src/agent-context-compaction.d.ts +1 -1
  6. package/dist/src/agent-framework-runner.d.ts +160 -12
  7. package/dist/src/agent-framework-runner.js +4 -0
  8. package/dist/src/agent-framework-runner.js.map +1 -1
  9. package/dist/src/agent-local-directory-media-store.d.ts +111 -0
  10. package/dist/src/agent-local-directory-media-store.js +417 -0
  11. package/dist/src/agent-local-directory-media-store.js.map +1 -0
  12. package/dist/src/agent-media.d.ts +279 -0
  13. package/dist/src/agent-media.js +457 -0
  14. package/dist/src/agent-media.js.map +1 -0
  15. package/dist/src/agent-session-codecs.d.ts +5 -1
  16. package/dist/src/agent-session-codecs.js +76 -4
  17. package/dist/src/agent-session-codecs.js.map +1 -1
  18. package/dist/src/agent-session-failure.d.ts +4 -2
  19. package/dist/src/agent-session-failure.js +6 -2
  20. package/dist/src/agent-session-failure.js.map +1 -1
  21. package/dist/src/agent-session-flow-graph.d.ts +46 -0
  22. package/dist/src/agent-session-flow-graph.js +51 -0
  23. package/dist/src/agent-session-flow-graph.js.map +1 -1
  24. package/dist/src/agent-session-flow.d.ts +28 -3
  25. package/dist/src/agent-session-flow.js +21 -1
  26. package/dist/src/agent-session-flow.js.map +1 -1
  27. package/dist/src/agent-session-persistence.d.ts +33 -0
  28. package/dist/src/agent-session-persistence.js +22 -1
  29. package/dist/src/agent-session-persistence.js.map +1 -1
  30. package/dist/src/agent-session-start.d.ts +1 -1
  31. package/dist/src/agent-session-start.js +2 -2
  32. package/dist/src/agent-session-start.js.map +1 -1
  33. package/dist/src/agent-session-steps.d.ts +2 -0
  34. package/dist/src/agent-session-steps.js +74 -5
  35. package/dist/src/agent-session-steps.js.map +1 -1
  36. package/dist/src/agent-session-summary.d.ts +138 -0
  37. package/dist/src/agent-session-summary.js +151 -0
  38. package/dist/src/agent-session-summary.js.map +1 -0
  39. package/dist/src/agent-session-tracer.js +1 -1
  40. package/dist/src/agent-session-tracer.js.map +1 -1
  41. package/dist/src/agent-session-transitions.d.ts +11 -3
  42. package/dist/src/agent-session-transitions.js +58 -6
  43. package/dist/src/agent-session-transitions.js.map +1 -1
  44. package/dist/src/agent-session-values.d.ts +179 -2
  45. package/dist/src/agent-session-values.js.map +1 -1
  46. package/dist/src/agent-session-wait-steps.js +14 -2
  47. package/dist/src/agent-session-wait-steps.js.map +1 -1
  48. package/dist/src/ai-sdk/ai-sdk-model-client.d.ts +15 -3
  49. package/dist/src/ai-sdk/ai-sdk-model-client.js +36 -7
  50. package/dist/src/ai-sdk/ai-sdk-model-client.js.map +1 -1
  51. package/dist/src/ai-sdk/index.d.ts +1 -1
  52. package/dist/src/ai-sdk/index.js +1 -1
  53. package/dist/src/ai-sdk/index.js.map +1 -1
  54. package/dist/src/{stepwise → builtin-loop}/agent-definition.d.ts +120 -5
  55. package/dist/src/builtin-loop/agent-definition.js.map +1 -0
  56. package/dist/src/builtin-loop/builtin-agent-session-flow.d.ts +77 -0
  57. package/dist/src/{stepwise/stepwise-agent-session-flow.js → builtin-loop/builtin-agent-session-flow.js} +19 -9
  58. package/dist/src/builtin-loop/builtin-agent-session-flow.js.map +1 -0
  59. package/dist/src/{stepwise/stepwise-agent-runner.d.ts → builtin-loop/builtin-loop-agent-runner.d.ts} +72 -14
  60. package/dist/src/{stepwise/stepwise-agent-runner.js → builtin-loop/builtin-loop-agent-runner.js} +284 -41
  61. package/dist/src/builtin-loop/builtin-loop-agent-runner.js.map +1 -0
  62. package/dist/src/{stepwise/stepwise-input-items.d.ts → builtin-loop/builtin-loop-input-items.d.ts} +7 -3
  63. package/dist/src/{stepwise/stepwise-input-items.js → builtin-loop/builtin-loop-input-items.js} +57 -10
  64. package/dist/src/builtin-loop/builtin-loop-input-items.js.map +1 -0
  65. package/dist/src/builtin-loop/function-tool.d.ts +282 -0
  66. package/dist/src/{stepwise → builtin-loop}/function-tool.js +63 -3
  67. package/dist/src/builtin-loop/function-tool.js.map +1 -0
  68. package/dist/src/builtin-loop/index.d.ts +8 -0
  69. package/dist/src/builtin-loop/index.js +13 -0
  70. package/dist/src/builtin-loop/index.js.map +1 -0
  71. package/dist/src/builtin-loop/json-schema-validation.js.map +1 -0
  72. package/dist/src/{stepwise → builtin-loop}/model-client.d.ts +36 -7
  73. package/dist/src/builtin-loop/model-client.js.map +1 -0
  74. package/dist/src/{stepwise → builtin-loop}/scripted-model-client.d.ts +8 -5
  75. package/dist/src/{stepwise → builtin-loop}/scripted-model-client.js +8 -5
  76. package/dist/src/builtin-loop/scripted-model-client.js.map +1 -0
  77. package/dist/src/{stepwise → builtin-loop}/tool-source.d.ts +2 -2
  78. package/dist/src/builtin-loop/tool-source.js.map +1 -0
  79. package/dist/src/connectors/connector-input-schema.js +1 -1
  80. package/dist/src/connectors/connector-input-schema.js.map +1 -1
  81. package/dist/src/connectors/connector-step-context.d.ts +19 -2
  82. package/dist/src/connectors/connector-step-context.js +61 -5
  83. package/dist/src/connectors/connector-step-context.js.map +1 -1
  84. package/dist/src/connectors/connector-tool-source.d.ts +63 -14
  85. package/dist/src/connectors/connector-tool-source.js +124 -25
  86. package/dist/src/connectors/connector-tool-source.js.map +1 -1
  87. package/dist/src/index.d.ts +8 -4
  88. package/dist/src/index.js +5 -1
  89. package/dist/src/index.js.map +1 -1
  90. package/dist/src/mcp/index.d.ts +1 -0
  91. package/dist/src/mcp/index.js +1 -0
  92. package/dist/src/mcp/index.js.map +1 -1
  93. package/dist/src/mcp/mcp-dex-conventions.d.ts +57 -0
  94. package/dist/src/mcp/mcp-dex-conventions.js +90 -0
  95. package/dist/src/mcp/mcp-dex-conventions.js.map +1 -0
  96. package/dist/src/mcp/mcp-image-content.d.ts +20 -0
  97. package/dist/src/mcp/mcp-image-content.js +82 -0
  98. package/dist/src/mcp/mcp-image-content.js.map +1 -0
  99. package/dist/src/mcp/mcp-tool-source.d.ts +205 -18
  100. package/dist/src/mcp/mcp-tool-source.js +289 -47
  101. package/dist/src/mcp/mcp-tool-source.js.map +1 -1
  102. package/dist/src/s3/index.d.ts +1 -0
  103. package/dist/src/s3/index.js +9 -0
  104. package/dist/src/s3/index.js.map +1 -0
  105. package/dist/src/s3/s3-media-store.d.ts +138 -0
  106. package/dist/src/s3/s3-media-store.js +255 -0
  107. package/dist/src/s3/s3-media-store.js.map +1 -0
  108. package/dist/src/stepwise/index.d.ts +41 -8
  109. package/dist/src/stepwise/index.js +35 -5
  110. package/dist/src/stepwise/index.js.map +1 -1
  111. package/dist/src/testing/agent-session-test-client.d.ts +6 -3
  112. package/dist/src/testing/agent-session-test-client.js +10 -3
  113. package/dist/src/testing/agent-session-test-client.js.map +1 -1
  114. package/dist/src/testing/private-dex-engine.d.ts +8 -0
  115. package/dist/src/testing/private-dex-engine.js +17 -1
  116. package/dist/src/testing/private-dex-engine.js.map +1 -1
  117. package/dist/src/testing/scripted-model-client.d.ts +1 -1
  118. package/dist/src/testing/scripted-model-client.js +2 -2
  119. package/dist/src/testing/scripted-model-client.js.map +1 -1
  120. package/dist/src/tool-source-router.d.ts +3 -3
  121. package/dist/src/tool-source-router.js +2 -2
  122. package/dist/src/ui-stream/agent-event-json.d.ts +2 -1
  123. package/dist/src/ui-stream/agent-event-json.js +3 -1
  124. package/dist/src/ui-stream/agent-event-json.js.map +1 -1
  125. package/dist/src/ui-stream/agent-ui-messages.d.ts +11 -3
  126. package/dist/src/ui-stream/agent-ui-messages.js +29 -5
  127. package/dist/src/ui-stream/agent-ui-messages.js.map +1 -1
  128. package/package.json +17 -4
  129. package/dist/src/stepwise/agent-definition.js.map +0 -1
  130. package/dist/src/stepwise/function-tool.d.ts +0 -130
  131. package/dist/src/stepwise/function-tool.js.map +0 -1
  132. package/dist/src/stepwise/json-schema-validation.js.map +0 -1
  133. package/dist/src/stepwise/model-client.js.map +0 -1
  134. package/dist/src/stepwise/scripted-model-client.js.map +0 -1
  135. package/dist/src/stepwise/stepwise-agent-runner.js.map +0 -1
  136. package/dist/src/stepwise/stepwise-agent-session-flow.d.ts +0 -35
  137. package/dist/src/stepwise/stepwise-agent-session-flow.js.map +0 -1
  138. package/dist/src/stepwise/stepwise-input-items.js.map +0 -1
  139. package/dist/src/stepwise/tool-source.js.map +0 -1
  140. /package/dist/src/{stepwise → builtin-loop}/agent-definition.js +0 -0
  141. /package/dist/src/{stepwise → builtin-loop}/json-schema-validation.d.ts +0 -0
  142. /package/dist/src/{stepwise → builtin-loop}/json-schema-validation.js +0 -0
  143. /package/dist/src/{stepwise → builtin-loop}/model-client.js +0 -0
  144. /package/dist/src/{stepwise → builtin-loop}/tool-source.js +0 -0
@@ -0,0 +1,82 @@
1
+ // Copyright (c) 2026 Super Durable, Inc.
2
+ //
3
+ // Licensed under the Sustainable Use License 1.0.
4
+ // You may not use this file except in compliance with the License.
5
+ // See the LICENSE file in the repository root.
6
+ //
7
+ // SPDX-License-Identifier: LicenseRef-Sustainable-Use-1.0
8
+ import { ToolExecutionError } from "../agent-framework-runner.js";
9
+ import { ImageValidationError, maxImagesPerMessage } from "../agent-media.js";
10
+ import { serializeToolOutputJson } from "./mcp-resource-and-prompt-tools.js";
11
+ // Canonical padded base64, the same check as the Python core's.
12
+ const base64Pattern = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
13
+ /** Reports whether a result carries at least one `image` content block. */
14
+ export function hasImageContent(result) {
15
+ return result.content.some((block) => block.type === "image");
16
+ }
17
+ /**
18
+ * Renders a failed result's text with `[image <n> omitted]` for each image block.
19
+ *
20
+ * The text becomes the record's `error`, so image bytes never reach session state.
21
+ */
22
+ export function renderToolFailureText(result) {
23
+ let imageCount = 0;
24
+ return result.content
25
+ .map((block) => {
26
+ if (block.type === "text") {
27
+ return block.text;
28
+ }
29
+ if (block.type === "image") {
30
+ imageCount += 1;
31
+ return `[image ${imageCount} omitted]`;
32
+ }
33
+ return serializeToolOutputJson(block);
34
+ })
35
+ .join("\n");
36
+ }
37
+ /**
38
+ * Uploads a successful result's image blocks and returns the tool output with their references.
39
+ *
40
+ * Each image block becomes `[image <n>]` in the output text, `n` counting from 1 in `images` order. The caller
41
+ * passes only a result with image blocks.
42
+ * @throws {ToolExecutionError} When the result has more than {@link maxImagesPerMessage} images, or an image is not
43
+ * valid base64 or not an image the store accepts.
44
+ */
45
+ export async function storeToolResultImages(result, store, toolName) {
46
+ const imageCount = result.content.filter((block) => block.type === "image").length;
47
+ if (imageCount > maxImagesPerMessage) {
48
+ throw new ToolExecutionError(`MCP tool ${toolName} returned ${imageCount} images; a tool result carries at most ${maxImagesPerMessage}`);
49
+ }
50
+ const references = [];
51
+ const parts = [];
52
+ for (const block of result.content) {
53
+ if (block.type === "text") {
54
+ parts.push(block.text);
55
+ }
56
+ else if (block.type === "image") {
57
+ references.push(await storeImage(block, store, toolName, references.length + 1));
58
+ parts.push(`[image ${references.length}]`);
59
+ }
60
+ else {
61
+ parts.push(serializeToolOutputJson(block));
62
+ }
63
+ }
64
+ return { output: parts.join("\n"), images: references };
65
+ }
66
+ async function storeImage(block, store, toolName, imageNumber) {
67
+ const data = typeof block.data === "string" && base64Pattern.test(block.data) ? Buffer.from(block.data, "base64") : undefined;
68
+ // Re-encoding refuses unused trailing bits, so one image has one accepted text.
69
+ if (data === undefined || data.toString("base64") !== block.data) {
70
+ throw new ToolExecutionError(`MCP tool ${toolName} returned image ${imageNumber}, which is not valid base64`);
71
+ }
72
+ try {
73
+ return await store.putImage(new Uint8Array(data));
74
+ }
75
+ catch (error) {
76
+ if (error instanceof ImageValidationError) {
77
+ throw new ToolExecutionError(`MCP tool ${toolName} returned image ${imageNumber}, which is not a supported image: ${error.message}`);
78
+ }
79
+ throw error;
80
+ }
81
+ }
82
+ //# sourceMappingURL=mcp-image-content.js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C,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACnH,gFAAgF;IAChF,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,KAAK,KAAK,CAAC,IAAI,EAAE,CAAC;QACjE,MAAM,IAAI,kBAAkB,CAAC,YAAY,QAAQ,mBAAmB,WAAW,6BAA6B,CAAC,CAAC;IAChH,CAAC;IACD,IAAI,CAAC;QACH,OAAO,MAAM,KAAK,CAAC,QAAQ,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC;IACpD,CAAC;IAAC,OAAO,KAAc,EAAE,CAAC;QACxB,IAAI,KAAK,YAAY,oBAAoB,EAAE,CAAC;YAC1C,MAAM,IAAI,kBAAkB,CAC1B,YAAY,QAAQ,mBAAmB,WAAW,qCAAqC,KAAK,CAAC,OAAO,EAAE,CACvG,CAAC;QACJ,CAAC;QACD,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -1,9 +1,16 @@
1
- import { type CallToolResult } from "@modelcontextprotocol/sdk/types.js";
1
+ import { McpError, type CallToolResult, type Tool } from "@modelcontextprotocol/sdk/types.js";
2
2
  import { type AgentEventWriter, type AgentTurnContext, type DeferredToolCall, type DeferredToolExecution, type McpToolAnnotations, type ToolExecutionResult, type ToolPolicy, type ToolTaskCancellation, type ToolTaskPoll, type ToolTaskPollOutcome, type ToolTaskStarted } from "../agent-framework-runner.js";
3
3
  import type { ToolEffect } from "../agent-session-values.js";
4
- import type { SourceToolDefinition } from "../stepwise/tool-source.js";
4
+ import type { SourceToolDefinition } from "../builtin-loop/tool-source.js";
5
+ import type { MediaStore } from "../agent-media.js";
5
6
  import { type McpServerConfig } from "./mcp-server-config.js";
6
- /** `_meta` key that carries the session's idempotency key on every `tools/call`. */
7
+ /**
8
+ * `_meta` key that carries the session's idempotency key on every `tools/call`.
9
+ *
10
+ * The key is `<flowId>/<turnId>/<toolCallId>`: the same on every attempt of one tool call, and unique per
11
+ * call. A server that follows the Dex MCP conventions derives its own call ID from it, so a retried attempt
12
+ * is recognized as the same call.
13
+ */
7
14
  export declare const mcpIdempotencyKeyMetaKey = "io.superdurable.dex/idempotencyKey";
8
15
  /** `AgentEvents` type for an MCP `notifications/progress` of a running tool call. */
9
16
  export declare const mcpToolProgressEventType = "mcp.tool_progress";
@@ -19,7 +26,7 @@ export declare const defaultMcpTaskTtlMs = 86400000;
19
26
  export declare const defaultMcpConnectionTimeoutMs = 30000;
20
27
  /** Default number of connection attempts before a request reports the server unreachable. */
21
28
  export declare const defaultMcpConnectionAttempts = 3;
22
- /** Default {@link McpToolSourceOptions.sourceName}, which stored Stepwise calls are routed by. */
29
+ /** Default {@link McpToolSourceOptions.sourceName}, which stored built-in loop calls are routed by. */
23
30
  export declare const defaultMcpSourceName = "mcp";
24
31
  /** Default age in milliseconds after which `listTools` lists a server again, in the background: 5 minutes. */
25
32
  export declare const defaultMcpListingTtlMs = 300000;
@@ -50,10 +57,26 @@ export interface McpTool {
50
57
  /** The server's behavior hints, or `undefined` when it sent none. */
51
58
  readonly annotations: McpToolAnnotations | undefined;
52
59
  /**
53
- * Effect from {@link toolEffectFromMcpToolAnnotations} when the server configuration trusts the
54
- * annotations, else `undefined`. An explicit {@link ToolPolicy} overrides it.
60
+ * The effect the server declares when its configuration trusts it, else `undefined`: {@link dexEffect}
61
+ * when the tool declares one, otherwise the effect from {@link toolEffectFromMcpToolAnnotations}. An
62
+ * explicit {@link ToolPolicy} overrides it, except for a tool with a {@link dexEffect}, where
63
+ * {@link McpToolSource.deferredToolCall} lets the policy raise the effect but never lower it.
55
64
  */
56
65
  readonly frameworkEffect: ToolEffect | undefined;
66
+ /**
67
+ * The effect the tool declares under `mcpToolEffectMetaKey` in its definition's `_meta`, when the server
68
+ * configuration trusts the server, else `undefined`. A tool with a `dexEffect` follows the Dex MCP
69
+ * conventions in the package README: a session {@link ToolPolicy} may raise its effect or its approval but
70
+ * never lowers its effect.
71
+ * @since 0.5.0
72
+ */
73
+ readonly dexEffect?: ToolEffect | undefined;
74
+ /**
75
+ * Whether the tool declares `mcpToolApprovalMetaKey` together with a {@link dexEffect}. Always `false` or
76
+ * absent without a {@link dexEffect}.
77
+ * @since 0.5.0
78
+ */
79
+ readonly isApprovalRecommended?: boolean;
57
80
  /**
58
81
  * The tool's MCP `execution.taskSupport`. A `required` tool is called as a task; see
59
82
  * {@link McpToolSource} for who polls it.
@@ -96,7 +119,7 @@ export interface McpToolSourceOptions {
96
119
  * Whether {@link McpToolSource.listTools} also offers `mcp_list_resources_and_prompts`, `mcp_read_resource`,
97
120
  * and `mcp_get_prompt`, after the servers' tools, when at least one server is configured. Their requests are
98
121
  * bounded by `connectionTimeoutMs`. Defaults to `false`. Enable it on one source of an agent only: two sources
99
- * with it list the same names, which the Stepwise runner refuses. See {@link McpToolSource}.
122
+ * with it list the same names, which the built-in loop runner refuses. See {@link McpToolSource}.
100
123
  */
101
124
  readonly resourceAndPromptToolsEnabled?: boolean;
102
125
  /**
@@ -110,11 +133,37 @@ export interface McpToolSourceOptions {
110
133
  */
111
134
  readonly listingFailureBackoffMs?: number;
112
135
  /**
113
- * The source's stable name in a Stepwise agent's `toolSources`, which stored calls are routed by. Defaults to
136
+ * The source's stable name in a built-in loop agent's `toolSources`, which stored calls are routed by. Defaults to
114
137
  * {@link defaultMcpSourceName}, `"mcp"`, so adding or removing a server keeps it. An agent with two `McpToolSource`
115
138
  * instances must name at least one, because names must be unique.
116
139
  */
117
140
  readonly sourceName?: string;
141
+ /**
142
+ * The store for the images of tool results. With a store, each `image` content block of a successful result is
143
+ * uploaded inside the tool Step and returned in {@link ToolExecutionResult.images}; the output text shows it as
144
+ * `[image <n>]`, counting from 1. More than `maxImagesPerMessage` images, invalid base64, or bytes the store
145
+ * refuses throw {@link ToolExecutionError}. Omitted, image blocks stay JSON in the output text. Pass the store the
146
+ * session's runner reads images from.
147
+ * @since 0.5.0
148
+ */
149
+ readonly mediaStore?: MediaStore;
150
+ /**
151
+ * Writes one warning of the source, such as a server left out of the listing, a tool left out for a duplicate
152
+ * name, a retried call, or a failed `tasks/cancel`. The source keeps working after each warning. A listing that
153
+ * keeps failing warns again on each retry, so an application with a terminal can send the warnings to a log file
154
+ * instead. Defaults to `console.warn`.
155
+ *
156
+ * The callback must not throw. If it does, the source catches the error, writes the warning and the error to
157
+ * `console.warn`, and keeps working, so a full disk does not fail `listTools()` or a tool call.
158
+ *
159
+ * ```ts
160
+ * const source = new McpToolSource(servers, {
161
+ * logWarning: (message) => appendFileSync("agent.log", `${message}\n`),
162
+ * });
163
+ * ```
164
+ * @since 0.6.0
165
+ */
166
+ readonly logWarning?: (message: string) => void;
118
167
  }
119
168
  /** Per-call options of {@link McpToolSource.callTool}. */
120
169
  export interface McpToolCallOptions {
@@ -132,6 +181,13 @@ export interface McpToolCallOptions {
132
181
  readonly saveHeartbeatCheckpoint?: (checkpoint: string) => Promise<void>;
133
182
  /** Aborts the requests, for example the tool Step's cancellation signal. */
134
183
  readonly signal?: AbortSignal;
184
+ /**
185
+ * More `_meta` entries to send with the `tools/call` request, such as the identity the caller expects the tool
186
+ * to have. The {@link mcpIdempotencyKeyMetaKey} entry from {@link idempotencyKey} wins over an entry with the
187
+ * same key. Defaults to none.
188
+ * @since 0.5.0
189
+ */
190
+ readonly requestMeta?: Readonly<Record<string, unknown>>;
135
191
  }
136
192
  /**
137
193
  * A `tools/call` that reached an MCP server but returned no result.
@@ -165,13 +221,49 @@ export declare class McpToolCallInterruptedError extends Error {
165
221
  export declare class McpToolListingError extends Error {
166
222
  readonly serverName: string;
167
223
  readonly detail: string;
224
+ /**
225
+ * The HTTP status a Streamable HTTP server answered to the failed listing, such as 401 or 403 for denied access.
226
+ * It is `undefined` for a stdio server, or when no HTTP response came, such as for a refused connection or a
227
+ * timeout.
228
+ *
229
+ * @since 0.6.0
230
+ */
231
+ readonly httpStatus: number | undefined;
168
232
  /**
169
233
  * Creates the error.
170
234
  * @param serverName - Configured name of the last MCP server that failed.
171
235
  * @param detail - What went wrong with that server.
236
+ * @param httpStatus - The HTTP status the server answered, when it answered one. Since 0.6.0.
172
237
  */
173
- constructor(serverName: string, detail: string);
238
+ constructor(serverName: string, detail: string, httpStatus?: number);
174
239
  }
240
+ export type ToolCallOutcome = {
241
+ readonly kind: "succeeded";
242
+ readonly output: string;
243
+ readonly result?: CallToolResult;
244
+ } | {
245
+ readonly kind: "failed";
246
+ readonly message: string;
247
+ } | {
248
+ readonly kind: "outcome_unknown";
249
+ readonly message: string;
250
+ } | {
251
+ readonly kind: "interrupted";
252
+ readonly detail: string;
253
+ } | {
254
+ readonly kind: "server_unreachable";
255
+ readonly detail: string;
256
+ } | {
257
+ readonly kind: "task_lost";
258
+ readonly detail: string;
259
+ } | {
260
+ readonly kind: "task_started";
261
+ readonly taskId: string;
262
+ readonly pollIntervalSeconds: number;
263
+ } | {
264
+ readonly kind: "task_pending";
265
+ readonly pollIntervalSeconds: number;
266
+ };
175
267
  declare const nodeInspectSymbol: unique symbol;
176
268
  /**
177
269
  * Offers the tools of configured MCP servers to an agent, one Dex Step per call.
@@ -189,6 +281,14 @@ declare const nodeInspectSymbol: unique symbol;
189
281
  * the session's `defaultToolEffect`, `unclassified` by default. Annotations are trusted by default. A
190
282
  * {@link ToolPolicy} in `AgentSessionOptions.toolPolicies` keyed by {@link McpTool.name} wins.
191
283
  *
284
+ * A server that follows the Dex MCP conventions declares each tool's effect and approval in its definition's
285
+ * `_meta` instead, under `mcpToolEffectMetaKey` and `mcpToolApprovalMetaKey`, as a connector manifest does for
286
+ * `ConnectorToolSource`. A session {@link ToolPolicy} for such a tool may keep or raise its safety but never
287
+ * lowers it: the call gets the more conservative of the two effects, and its writes run in the model's order.
288
+ * A result or JSON-RPC error
289
+ * whose `_meta` reports `mcpToolOutcomeUnknown` records `outcome_unknown` without a retry, and
290
+ * `mcpToolOutcomeRejected` records `failed`. See the package README, "Dex MCP conventions".
291
+ *
192
292
  * The source uses `@modelcontextprotocol/sdk` 1.x, which speaks MCP up to 2025-11-25: every request
193
293
  * opens a new connection, performs the `initialize` handshake, ends the HTTP protocol session, and
194
294
  * closes it, so no protocol session outlives a request. Every `tools/call` carries the session's
@@ -197,11 +297,19 @@ declare const nodeInspectSymbol: unique symbol;
197
297
  * Results map onto the session contract:
198
298
  * - A result becomes the tool output: text blocks joined with newlines, other blocks and structured
199
299
  * content as JSON.
300
+ * - With {@link McpToolSourceOptions.mediaStore}, a successful result's `image` blocks are uploaded and returned
301
+ * as {@link ToolExecutionResult.images}, and the text shows `[image <n>]` in their place. More than
302
+ * `maxImagesPerMessage` images, invalid base64, or bytes the store refuses throw {@link ToolExecutionError}.
303
+ * - A failed result's text, with or without a media store, shows `[image <n> omitted]` for each `image` block, so
304
+ * no image bytes reach the recorded error.
200
305
  * - An `isError` result, a JSON-RPC error response other than an internal error, and a failed,
201
306
  * cancelled, or `input_required` task throw {@link ToolExecutionError}, so the session records
202
307
  * `failed` and the model sees the text. The 2025-11-25 tasks deliver input requests as server requests
203
308
  * that must be answered on a live connection, so no durable question can carry them; the source cancels
204
309
  * an `input_required` task.
310
+ * - A result or JSON-RPC error whose `_meta` reports the Dex outcome `unknown` throws
311
+ * {@link ToolOutcomeUnknownError}, so the session records `outcome_unknown` and never sends the call again.
312
+ * The outcome `rejected` throws {@link ToolExecutionError}. Either wins over `isError` and the error code.
205
313
  * - No result after the request was sent throws {@link McpToolCallInterruptedError}; the tool Step's
206
314
  * effect decides whether it is re-issued.
207
315
  * - A call that was never sent, because its server is not configured or cannot be reached, throws
@@ -279,6 +387,7 @@ export declare class McpToolSource {
279
387
  private readonly listingTtlMs;
280
388
  private readonly listingFailureBackoffMs;
281
389
  private readonly configuredSourceName;
390
+ private readonly mediaStore;
282
391
  private tools;
283
392
  private readonly resourceAndPromptTools;
284
393
  /**
@@ -308,7 +417,7 @@ export declare class McpToolSource {
308
417
  readonly refresh?: boolean;
309
418
  }): Promise<readonly McpTool[]>;
310
419
  /**
311
- * The name a Stepwise agent's `toolSources` routes stored calls by: `sourceName` as passed, or
420
+ * The name a built-in loop agent's `toolSources` routes stored calls by: `sourceName` as passed, or
312
421
  * {@link defaultMcpSourceName}, `"mcp"`.
313
422
  */
314
423
  get sourceName(): string;
@@ -328,7 +437,7 @@ export declare class McpToolSource {
328
437
  */
329
438
  cachedTools(): readonly McpTool[];
330
439
  /**
331
- * Returns the tool names this source lists whatever its servers offer, so the Stepwise runner refuses a name
440
+ * Returns the tool names this source lists whatever its servers offer, so the built-in loop runner refuses a name
332
441
  * collision when it is constructed instead of on every model call.
333
442
  * @returns The resource and prompt tool names when they are enabled, else none.
334
443
  */
@@ -341,29 +450,71 @@ export declare class McpToolSource {
341
450
  */
342
451
  tool(toolName: string): McpTool;
343
452
  /**
344
- * Returns a {@link ToolPolicy} for every listed tool whose annotations declare an effect.
453
+ * Returns a {@link ToolPolicy} for every listed tool whose server declares an effect.
345
454
  *
346
- * Use it with a framework adapter that offers the MCP tools as its own tools: pass it in the
347
- * adapter's `toolPolicies` and spread your own policies after it, so yours win.
455
+ * A tool with a {@link McpTool.dexEffect} gets that effect and requires approval exactly when
456
+ * {@link McpTool.isApprovalRecommended}. To give a framework adapter these policies together with your own,
457
+ * use {@link mergeToolPolicies}. Do not spread your policies after these: yours would then replace a
458
+ * declaration and could lower its effect.
348
459
  * @example
349
460
  * ```ts
350
461
  * await tools.listTools();
351
- * const toolPolicies = { ...tools.toolPolicies(), files__delete: { effect: "non_idempotent_write" } };
462
+ * const declaredPolicies = tools.toolPolicies();
352
463
  * ```
353
- * @returns Policies by {@link McpTool.name}, with the effect's default approval.
464
+ * @returns Policies by {@link McpTool.name}: the declared approval for a tool with a
465
+ * {@link McpTool.dexEffect}, else the effect's default approval.
354
466
  * @throws {RangeError} When {@link listTools} has not run.
355
467
  */
356
468
  toolPolicies(): Record<string, ToolPolicy>;
469
+ /**
470
+ * Returns the application's tool policies merged with what the listed servers declare.
471
+ *
472
+ * Use it with a framework adapter that offers the MCP tools as its own tools: pass the result as the
473
+ * adapter's `toolPolicies`, so every call gets the policy {@link deferredToolCall} would give it.
474
+ *
475
+ * For a tool with a {@link McpTool.dexEffect}, your policy may keep or raise its safety but never lowers
476
+ * it. The tool gets the more conservative of `dexEffect` and your policy's effect, ranked `read`,
477
+ * `idempotent_write`, `non_idempotent_write`, then `unclassified`. Your `requiresApproval`, when set, wins,
478
+ * so `false` turns off a recommended approval. When you leave it out, the tool requires approval when
479
+ * {@link McpTool.isApprovalRecommended} is `true` or your policy's effect requires approval by default.
480
+ *
481
+ * For any other tool, your policy wins over its annotation effect, as before. Policies for tools that no
482
+ * listed server offers pass through unchanged.
483
+ * @example
484
+ * ```ts
485
+ * await tools.listTools();
486
+ * const toolPolicies = tools.mergeToolPolicies({
487
+ * files__delete: { effect: "non_idempotent_write" },
488
+ * lookup_order: { effect: "read" },
489
+ * });
490
+ * ```
491
+ * @param applicationPolicies - Your policies by tool name, keyed by {@link McpTool.name} for MCP tools.
492
+ * @returns A new record with every entry of `applicationPolicies` and of {@link toolPolicies}. A tool with
493
+ * a {@link McpTool.dexEffect} gets the combined policy, with `requiresApproval` set.
494
+ * @throws {RangeError} When {@link listTools} has not run.
495
+ * @since 0.5.0
496
+ */
497
+ mergeToolPolicies(applicationPolicies: Readonly<Record<string, ToolPolicy>>): Record<string, ToolPolicy>;
357
498
  /**
358
499
  * Builds the {@link DeferredToolCall} for one model call of a listed MCP tool.
359
500
  *
360
501
  * The policy comes from `turn.resolveToolPolicy` with the tool's annotation effect, so an explicit
361
502
  * {@link ToolPolicy} wins. The call's `toolSourceReference` names the server, the tool's MCP name, and
362
503
  * its task support.
504
+ *
505
+ * For a tool with a {@link McpTool.dexEffect}, the session's {@link ToolPolicy} for the tool may keep or
506
+ * raise its safety but never lowers it. The call gets the more conservative of `dexEffect` and the policy's
507
+ * effect, ranked `read`, `idempotent_write`, `non_idempotent_write`, then `unclassified`. It waits for
508
+ * approval when the policy sets `requiresApproval`. When the policy leaves `requiresApproval` out, the call
509
+ * waits when {@link McpTool.isApprovalRecommended} is `true` or the policy's effect requires approval by
510
+ * default. Without a policy, it waits exactly when {@link McpTool.isApprovalRecommended} is `true`. A write
511
+ * runs in the model's order after the turn's earlier writes (`DeferredToolCall.ordered`). Changed in
512
+ * 0.5.0: for a tool with a `dexEffect`, a {@link ToolPolicy} may raise the effect or the approval but never
513
+ * lowers the effect.
363
514
  * @param turn - The `RunModel` context.
364
515
  * @param call - Framework tool call ID, the {@link McpTool.name} the model called, the arguments as JSON
365
516
  * text, and optionally `listedTool`, the {@link McpTool} the listing offered for this model call, as the
366
- * Stepwise runner passes it, so a refresh that finished meanwhile cannot change or remove it. Without it, the
517
+ * built-in loop runner passes it, so a refresh that finished meanwhile cannot change or remove it. Without it, the
367
518
  * tool is looked up in the current listing.
368
519
  * @returns The call to put in `ToolCallsRequested.toolCalls`.
369
520
  * @throws {RangeError} When no `listedTool` is given and {@link listTools} has not run or the tool is unknown.
@@ -402,13 +553,42 @@ export declare class McpToolSource {
402
553
  * A Streamable HTTP server without protocol sessions that answers with a task gets {@link ToolTaskStarted}.
403
554
  * The source saves the task ID as the heartbeat checkpoint first; a retried attempt that reads it returns
404
555
  * the same task without calling the tool again.
556
+ *
557
+ * Changed in 0.5.0: takes `options`, whose `requestMeta` adds entries to the `tools/call` request's `_meta`.
405
558
  * @param call - The approved call from the tool Step.
559
+ * @param options - `requestMeta`: more `_meta` entries for the `tools/call` request, as in
560
+ * {@link McpToolCallOptions.requestMeta}. The idempotency key entry wins on a name clash. Defaults to none.
406
561
  * @returns The tool output the model sees, or {@link ToolTaskStarted} for a task that `PollToolTask` polls.
407
562
  * @throws {ToolExecutionError} When the arguments are not a JSON object, the tool failed, the server
408
563
  * answered with an error or asked for input, or the call was never sent.
409
564
  * @throws {McpToolCallInterruptedError} When the call was sent and no result came back.
410
565
  */
411
- executeDeferredToolCall(call: DeferredToolExecution): Promise<ToolExecutionResult | ToolTaskStarted>;
566
+ executeDeferredToolCall(call: DeferredToolExecution, options?: {
567
+ readonly requestMeta?: Readonly<Record<string, unknown>>;
568
+ }): Promise<ToolExecutionResult | ToolTaskStarted>;
569
+ /**
570
+ * Returns whether {@link executeDeferredToolCall} will resume a task that an earlier attempt of the same call
571
+ * started and can never send a new `tools/call` for this call.
572
+ *
573
+ * It is `true` only when the call's heartbeat checkpoint names a task of the same server and tool that the
574
+ * session polls, and the session still polls tool tasks (`call.supportsToolTasks`). Then the call returns that
575
+ * task at once, without contacting the server. In every other case, including a task polled inside the tool
576
+ * Step, such as one bound to an MCP protocol session, the call may send `tools/call`: a task the server no longer
577
+ * knows is called again. Use it to skip a check that only guards a new send, such as checking a tool's current
578
+ * listing, so a catalog that is down or changed does not stop the recovery of a task already running.
579
+ *
580
+ * @example
581
+ * ```ts
582
+ * if (!tools.willResumeToolTaskWithoutSending(call)) {
583
+ * await checkCurrentListing(call.toolCall.toolName);
584
+ * }
585
+ * return tools.executeDeferredToolCall(call);
586
+ * ```
587
+ * @param call - The tool Step's call, with this source's own `toolSourceReference`.
588
+ * @returns `true` when the call resumes a session-polled task and sends nothing; otherwise `false`.
589
+ * @since 0.5.0
590
+ */
591
+ willResumeToolTaskWithoutSending(call: DeferredToolExecution): boolean;
412
592
  /**
413
593
  * Polls one MCP task that {@link executeDeferredToolCall} started, once.
414
594
  *
@@ -420,6 +600,8 @@ export declare class McpToolSource {
420
600
  * server's `pollInterval` to at most a minute.
421
601
  * @throws {ToolExecutionError} When the task failed, was cancelled, returned an `isError` result, or asked
422
602
  * for input.
603
+ * @throws {ToolOutcomeUnknownError} When the task's result reported the Dex outcome `unknown`; the session
604
+ * records `outcome_unknown`.
423
605
  * @throws {McpToolCallInterruptedError} When the server could not be reached, no longer knows the task, or
424
606
  * answered a poll with an error. The task may still run, so `PollToolTask` retries and then records the
425
607
  * call by its effect.
@@ -458,6 +640,7 @@ export declare class McpToolSource {
458
640
  private runResourceAndPromptTool;
459
641
  private callLimits;
460
642
  private resolveTarget;
643
+ private toolExecutionResult;
461
644
  private callTarget;
462
645
  private targetFromReference;
463
646
  private targetFromListing;
@@ -465,6 +648,10 @@ export declare class McpToolSource {
465
648
  private combineServerTools;
466
649
  private listServerTools;
467
650
  }
651
+ export declare function mcpToolFromListing(server: McpServerConfig, tool: Tool): McpTool;
652
+ export declare function outcomeFromToolResult(result: CallToolResult): ToolCallOutcome;
653
+ /** Maps a JSON-RPC error answer; `undefined` leaves the outcome open, so the effect class decides. */
654
+ export declare function outcomeFromMcpError(error: McpError): ToolCallOutcome | undefined;
468
655
  /** Joins text blocks; other content blocks and structured content render as `serializeToolOutputJson` text. */
469
656
  export declare function renderToolResultText(result: CallToolResult): string;
470
657
  export {};