@apify/actors-mcp-server 0.13.0 → 0.14.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 (75) hide show
  1. package/dist/const.d.ts +6 -5
  2. package/dist/const.d.ts.map +1 -1
  3. package/dist/const.js +8 -7
  4. package/dist/const.js.map +1 -1
  5. package/dist/dev_server.d.ts +18 -6
  6. package/dist/dev_server.d.ts.map +1 -1
  7. package/dist/dev_server.js +141 -16
  8. package/dist/dev_server.js.map +1 -1
  9. package/dist/index.d.ts +2 -1
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +6 -4
  12. package/dist/index.js.map +1 -1
  13. package/dist/mcp/legacy_server.d.ts +75 -0
  14. package/dist/mcp/legacy_server.d.ts.map +1 -0
  15. package/dist/mcp/legacy_server.js +563 -0
  16. package/dist/mcp/legacy_server.js.map +1 -0
  17. package/dist/mcp/server.d.ts +110 -86
  18. package/dist/mcp/server.d.ts.map +1 -1
  19. package/dist/mcp/server.js +275 -670
  20. package/dist/mcp/server.js.map +1 -1
  21. package/dist/mcp/stateless_server.d.ts +58 -0
  22. package/dist/mcp/stateless_server.d.ts.map +1 -0
  23. package/dist/mcp/stateless_server.js +335 -0
  24. package/dist/mcp/stateless_server.js.map +1 -0
  25. package/dist/mcp/task_execution.d.ts +15 -8
  26. package/dist/mcp/task_execution.d.ts.map +1 -1
  27. package/dist/mcp/task_execution.js +31 -24
  28. package/dist/mcp/task_execution.js.map +1 -1
  29. package/dist/mcp/tool_call_engine.d.ts +23 -10
  30. package/dist/mcp/tool_call_engine.d.ts.map +1 -1
  31. package/dist/mcp/tool_call_engine.js +37 -28
  32. package/dist/mcp/tool_call_engine.js.map +1 -1
  33. package/dist/mcp/tool_call_telemetry.d.ts +4 -4
  34. package/dist/mcp/tool_call_telemetry.d.ts.map +1 -1
  35. package/dist/mcp/tool_call_telemetry.js +7 -7
  36. package/dist/mcp/tool_call_telemetry.js.map +1 -1
  37. package/dist/mcp/tool_dispatch.d.ts +19 -15
  38. package/dist/mcp/tool_dispatch.d.ts.map +1 -1
  39. package/dist/mcp/tool_dispatch.js +21 -19
  40. package/dist/mcp/tool_dispatch.js.map +1 -1
  41. package/dist/tools/actors/call_actor.js +3 -3
  42. package/dist/tools/actors/call_actor.js.map +1 -1
  43. package/dist/tools/actors/fetch_actor_details.d.ts.map +1 -1
  44. package/dist/tools/actors/fetch_actor_details.js +6 -6
  45. package/dist/tools/actors/fetch_actor_details.js.map +1 -1
  46. package/dist/tools/actors/search_actors.js +4 -4
  47. package/dist/tools/actors/search_actors.js.map +1 -1
  48. package/dist/tools/dev/report_problem.d.ts.map +1 -1
  49. package/dist/tools/dev/report_problem.js +4 -2
  50. package/dist/tools/dev/report_problem.js.map +1 -1
  51. package/dist/tools/runs/get_actor_run.js +4 -4
  52. package/dist/tools/runs/get_actor_run.js.map +1 -1
  53. package/dist/tools/runs/get_actor_run_log.d.ts.map +1 -1
  54. package/dist/tools/runs/get_actor_run_log.js +4 -2
  55. package/dist/tools/runs/get_actor_run_log.js.map +1 -1
  56. package/dist/tools/storage/get_dataset_items.js +4 -4
  57. package/dist/tools/storage/get_dataset_items.js.map +1 -1
  58. package/dist/tools/storage/storage_helpers.d.ts +1 -1
  59. package/dist/tools/storage/storage_helpers.d.ts.map +1 -1
  60. package/dist/tools/storage/storage_helpers.js +2 -2
  61. package/dist/tools/storage/storage_helpers.js.map +1 -1
  62. package/dist/tools/widgets/search_actors_widget.js +4 -4
  63. package/dist/tools/widgets/search_actors_widget.js.map +1 -1
  64. package/dist/tsconfig.tsbuildinfo +1 -1
  65. package/dist/types.d.ts +16 -20
  66. package/dist/types.d.ts.map +1 -1
  67. package/dist/types.js +2 -2
  68. package/dist/types.js.map +1 -1
  69. package/dist/utils/tool_status.d.ts +6 -0
  70. package/dist/utils/tool_status.d.ts.map +1 -1
  71. package/dist/utils/tool_status.js +9 -2
  72. package/dist/utils/tool_status.js.map +1 -1
  73. package/manifest.json +1 -1
  74. package/package.json +10 -2
  75. package/server.json +1 -1
@@ -2,97 +2,108 @@
2
2
  * Model Context Protocol (MCP) server for Apify Actors
3
3
  */
4
4
 
5
- !function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="24ee8a78-9505-5c66-bc50-80953252f83f")}catch(e){}}();
6
- import { randomUUID } from 'node:crypto';
7
- import { InMemoryTaskStore } from '@modelcontextprotocol/sdk/experimental/tasks/stores/in-memory.js';
8
- import { Server } from '@modelcontextprotocol/sdk/server/index.js';
9
- import { CallToolRequestSchema, CancelTaskRequestSchema, ErrorCode, GetPromptRequestSchema, GetTaskPayloadRequestSchema, GetTaskRequestSchema, InitializeRequestSchema, ListPromptsRequestSchema, ListResourcesRequestSchema, ListResourceTemplatesRequestSchema, ListTasksRequestSchema, ListToolsRequestSchema, McpError, ReadResourceRequestSchema, RELATED_TASK_META_KEY, SetLevelRequestSchema, } from '@modelcontextprotocol/sdk/types.js';
5
+ !function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="529d6806-45aa-580f-86b6-b1a0fe148f0c")}catch(e){}}();
6
+ import { dirname } from 'node:path';
7
+ import { fileURLToPath } from 'node:url';
10
8
  import log from '@apify/log';
11
9
  import { parseBooleanOrNull } from '@apify/utilities';
12
10
  import { ApifyClient } from '../apify_client.js';
13
- import { DEFAULT_TELEMETRY_ENABLED, DEFAULT_TELEMETRY_ENV, FAILURE_CATEGORY, HELPER_TOOLS, TOOL_STATUS, } from '../const.js';
11
+ import { DEFAULT_TELEMETRY_ENABLED, DEFAULT_TELEMETRY_ENV, HELPER_TOOLS } from '../const.js';
14
12
  import { prompts } from '../prompts/index.js';
15
13
  import { createPromptService } from '../prompts/prompt_service.js';
16
14
  import { createResourceService } from '../resources/resource_service.js';
17
15
  import { resolveAvailableWidgets } from '../resources/widgets.js';
18
- import { getServerInfo } from '../server_card.js';
19
16
  import { getTelemetryEnv } from '../telemetry.js';
20
- import { withReportProblemNudge } from '../tools/dev/report_problem.js';
21
17
  import { SERVER_MODE, TOOL_TYPE } from '../types.js';
22
- import { isMcpClientFaultMessage, sanitizeMezmoMessage } from '../utils/logging.js';
23
18
  import { getRequestOriginForClient, isReportProblemBlockedForClient } from '../utils/mcp_clients.js';
24
19
  import { getServerInstructions } from '../utils/server-instructions/index.js';
25
20
  import { parseServerMode, resolveServerMode } from '../utils/server_mode.js';
26
- import { buildActorFields, getToolFullName, getToolPublicFieldOnly } from '../utils/tools.js';
27
21
  import { getActors, getToolsForServerMode, toolNamesToInput } from '../utils/tools_loader.js';
28
22
  import { buildMcpClientContext, isUiSupportedByClient } from './client_context.js';
29
- import { LOG_LEVEL_MAP } from './const.js';
30
- import { InternalError, InvalidParamsError } from './errors.js';
31
- import { emitTaskStatusNotification, executeToolAndUpdateTask } from './task_execution.js';
32
- import { buildPreflightFailureOutcome, classifyToolCallError, executeSyncToolCall, prepareToolCall, } from './tool_call_engine.js';
33
- import { logToolCallAndTelemetry, prepareTelemetryData } from './tool_call_telemetry.js';
34
- import { parseInputParamsFromUrl, storeTaskResultOrSkipIfExpired } from './utils.js';
23
+ import { LegacyMcpServer } from './legacy_server.js';
24
+ import { parseInputParamsFromUrl } from './utils.js';
35
25
  /**
36
- * Legacy protocol boundary: project a service's domain error to its v1 `McpError`, copying `message`
37
- * and `data` unchanged so the wire output (code, message, presence of `data`) is byte-identical.
38
- * Non-domain errors pass through untouched.
26
+ * Stable identity of a fetch input, so reloading the same input replaces its retained source rather
27
+ * than adding another (see {@link ActorsMcpServer.toolSources}). Keys are sorted because callers
28
+ * need not agree on property order. The array replacer filters keys at every depth — safe only
29
+ * while `Input` stays flat; a nested object's keys would be silently dropped and inputs collide.
39
30
  */
40
- function toLegacyMcpError(error) {
41
- if (error instanceof InvalidParamsError)
42
- return new McpError(ErrorCode.InvalidParams, error.message, error.data);
43
- if (error instanceof InternalError)
44
- return new McpError(ErrorCode.InternalError, error.message, error.data);
45
- return error;
31
+ function toolSourceKey(input) {
32
+ return JSON.stringify(input, Object.keys(input).sort());
46
33
  }
47
34
  /**
48
- * Create Apify MCP server
35
+ * Read the widget registry from disk. Mode-agnostic, so a successful read is resolved once and
36
+ * shared (see {@link ActorsMcpServer.resolveWidgetsForMode}). Rejects on a failed scan so the
37
+ * caller can tell that apart from a successful empty registry.
38
+ */
39
+ async function resolveServableWidgets() {
40
+ const resolved = await resolveAvailableWidgets(dirname(fileURLToPath(import.meta.url)));
41
+ const readyWidgets = [];
42
+ const missingWidgets = [];
43
+ for (const [uri, widget] of resolved.entries()) {
44
+ if (widget.exists) {
45
+ readyWidgets.push(widget.name);
46
+ }
47
+ else {
48
+ missingWidgets.push(widget.name);
49
+ log.softFail(`Widget file not found: ${widget.jsPath} (widget: ${uri})`);
50
+ }
51
+ }
52
+ if (readyWidgets.length > 0) {
53
+ log.debug('Ready widgets', { widgets: readyWidgets });
54
+ }
55
+ if (missingWidgets.length > 0) {
56
+ log.softFail('Some widgets are not ready', {
57
+ widgets: missingWidgets,
58
+ note: 'These widgets will not be available. Ensure web/dist files are built and included in deployment.',
59
+ });
60
+ }
61
+ return resolved;
62
+ }
63
+ /**
64
+ * The shared-Apify-behavior facade: owns the tool registry + loaders, server-mode resolution,
65
+ * telemetry config, widgets, prompt/resource services, and token/client resolution. Constructs
66
+ * exactly one {@link LegacyMcpServer} (2025-era adapter) and delegates all v1 protocol work to it.
67
+ * Implements {@link StatelessMcpServerHost} for the 2026-07-28 adapter too, but does not construct
68
+ * it — `createStatelessServer` builds one per request from a snapshot this facade hands out.
49
69
  */
50
70
  export class ActorsMcpServer {
71
+ get clientContext() {
72
+ return this._clientContext;
73
+ }
74
+ get serverMode() {
75
+ return this._serverMode;
76
+ }
77
+ /** The instance's own view: what a stateful connection composes and gates against. */
78
+ get servingContext() {
79
+ return { serverMode: this._serverMode, clientContext: this._clientContext };
80
+ }
51
81
  constructor(options = {}) {
52
82
  var _a;
53
- Object.defineProperty(this, "server", {
54
- enumerable: true,
55
- configurable: true,
56
- writable: true,
57
- value: void 0
58
- });
83
+ /**
84
+ * The resolved tool map the instance's own (stateful) connection serves, composed from
85
+ * `toolSources` once the handshake makes mode and client known. A stateless request never
86
+ * reads it — its snapshot re-composes from the sources ({@link createRequestSnapshot}).
87
+ */
59
88
  Object.defineProperty(this, "tools", {
60
89
  enumerable: true,
61
90
  configurable: true,
62
91
  writable: true,
63
92
  value: void 0
64
93
  });
65
- Object.defineProperty(this, "sigintHandler", {
66
- enumerable: true,
67
- configurable: true,
68
- writable: true,
69
- value: void 0
70
- });
71
- Object.defineProperty(this, "currentLogLevel", {
72
- enumerable: true,
73
- configurable: true,
74
- writable: true,
75
- value: 'info'
76
- });
77
94
  Object.defineProperty(this, "options", {
78
95
  enumerable: true,
79
96
  configurable: true,
80
97
  writable: true,
81
98
  value: void 0
82
99
  });
83
- Object.defineProperty(this, "taskStore", {
84
- enumerable: true,
85
- configurable: true,
86
- writable: true,
87
- value: void 0
88
- });
89
100
  Object.defineProperty(this, "actorStore", {
90
101
  enumerable: true,
91
102
  configurable: true,
92
103
  writable: true,
93
104
  value: void 0
94
105
  });
95
- Object.defineProperty(this, "clientContext", {
106
+ Object.defineProperty(this, "_clientContext", {
96
107
  enumerable: true,
97
108
  configurable: true,
98
109
  writable: true,
@@ -100,10 +111,10 @@ export class ActorsMcpServer {
100
111
  });
101
112
  /**
102
113
  * Resolved server mode. Preliminary value at construction (`'auto'` → `DEFAULT`).
103
- * Finalized inside the `initialize` request handler (see constructor) once the
114
+ * Finalized inside the `initialize` request handler (see {@link applyInitialize}) once the
104
115
  * client's capabilities are known. Effectively set-once per connection.
105
116
  */
106
- Object.defineProperty(this, "serverMode", {
117
+ Object.defineProperty(this, "_serverMode", {
107
118
  enumerable: true,
108
119
  configurable: true,
109
120
  writable: true,
@@ -120,10 +131,9 @@ export class ActorsMcpServer {
120
131
  writable: true,
121
132
  value: void 0
122
133
  });
123
- /** True once the server mode is final: at construction for explicit `default`/`apps`, or after
124
- * the initialize handler resolves `'auto'`. Composing before this in `'auto'` mode would use
125
- * the preliminary DEFAULT mode and produce the wrong (non-widget) tool variants, so composition
126
- * waits for it. Distinct from {@link clientKnown}, which only withholds client-gated tools. */
134
+ /** True once the server mode is final (at construction, or after initialize resolves `'auto'`).
135
+ * Composition waits for it — composing earlier in `'auto'` mode would produce the wrong tool
136
+ * variants. Distinct from {@link clientKnown}, which only withholds client-gated tools. */
127
137
  Object.defineProperty(this, "serverModeResolved", {
128
138
  enumerable: true,
129
139
  configurable: true,
@@ -131,17 +141,30 @@ export class ActorsMcpServer {
131
141
  value: void 0
132
142
  });
133
143
  /**
134
- * Tool sources queued until composition is possible. Enqueued when the mode is not yet resolved
135
- * (`'auto'` before initialize), and re-composed by the initialize flush — which is also when the
136
- * client becomes known, so any client-gated tools withheld by an eager compose are added then.
137
- * We capture the exact actor-tool slice fetched for each request so the flush composes every
138
- * entry against *its own* actor list rather than the accumulated union across unrelated requests.
144
+ * Tool sources queued until composition is possible (`'auto'` mode before initialize),
145
+ * re-composed by the initialize flush once mode and client are known. Each entry keeps the
146
+ * exact actor-tool slice fetched for its input, so the flush composes it against its own list.
147
+ * Keyed like {@link toolSources}: a stateless-only facade never drains this queue, so a reload
148
+ * replaces its entry instead of appending, keeping the queue bounded by distinct inputs.
139
149
  */
140
150
  Object.defineProperty(this, "pendingToolsUntilClientKnown", {
141
151
  enumerable: true,
142
152
  configurable: true,
143
153
  writable: true,
144
- value: []
154
+ value: new Map()
155
+ });
156
+ /**
157
+ * The unresolved inputs `tools` is composed from — not a second tool registry. Retained (never
158
+ * drained) so a stateless request, whose identity arrives per request, can compose its own
159
+ * resolved set from the same inputs without touching `tools`. Keyed by input because nothing
160
+ * drains it: a reload replaces its entry instead of appending, bounding the map by distinct
161
+ * inputs instead of growing per reload for the facade's lifetime.
162
+ */
163
+ Object.defineProperty(this, "toolSources", {
164
+ enumerable: true,
165
+ configurable: true,
166
+ writable: true,
167
+ value: new Map()
145
168
  });
146
169
  // Telemetry configuration (resolved from options and env vars, see setupTelemetry)
147
170
  Object.defineProperty(this, "telemetryEnabled", {
@@ -156,6 +179,19 @@ export class ActorsMcpServer {
156
179
  writable: true,
157
180
  value: void 0
158
181
  });
182
+ // Neutral prompt/resource services; the legacy adapter wires SDK handlers to these.
183
+ Object.defineProperty(this, "promptService", {
184
+ enumerable: true,
185
+ configurable: true,
186
+ writable: true,
187
+ value: void 0
188
+ });
189
+ Object.defineProperty(this, "resourceService", {
190
+ enumerable: true,
191
+ configurable: true,
192
+ writable: true,
193
+ value: void 0
194
+ });
159
195
  // List of widgets that are ready to be served
160
196
  Object.defineProperty(this, "availableWidgets", {
161
197
  enumerable: true,
@@ -163,6 +199,16 @@ export class ActorsMcpServer {
163
199
  writable: true,
164
200
  value: new Map()
165
201
  });
202
+ /**
203
+ * In-flight or successfully settled widget resolution, memoized so the disk scan runs once. A
204
+ * failed attempt is dropped rather than kept (see {@link resolveWidgetsForMode}).
205
+ */
206
+ Object.defineProperty(this, "widgetsResolution", {
207
+ enumerable: true,
208
+ configurable: true,
209
+ writable: true,
210
+ value: void 0
211
+ });
166
212
  /** Set in the initialize handler once client capabilities are known. */
167
213
  Object.defineProperty(this, "clientSupportsUi", {
168
214
  enumerable: true,
@@ -170,18 +216,15 @@ export class ActorsMcpServer {
170
216
  writable: true,
171
217
  value: false
172
218
  });
219
+ // The v1 SDK adapter. Package-private: constructed here and never exposed on the public surface.
220
+ Object.defineProperty(this, "legacyServer", {
221
+ enumerable: true,
222
+ configurable: true,
223
+ writable: true,
224
+ value: void 0
225
+ });
173
226
  this.options = options;
174
- this.clientContext = buildMcpClientContext((_a = options.initializeRequestData) === null || _a === void 0 ? void 0 : _a.params);
175
- // for stdio use in memory task store if not provided, otherwise use provided task store
176
- if (this.options.transportType === 'stdio' && !this.options.taskStore) {
177
- this.taskStore = new InMemoryTaskStore();
178
- }
179
- else if (this.options.taskStore) {
180
- this.taskStore = this.options.taskStore;
181
- }
182
- else {
183
- throw new Error('Task store must be provided for non-stdio transport types');
184
- }
227
+ this._clientContext = buildMcpClientContext((_a = options.initializeRequestData) === null || _a === void 0 ? void 0 : _a.params);
185
228
  this.actorStore = options.actorStore;
186
229
  // Constructor is an ingestion boundary for programmatic callers. Normalize via
187
230
  // parseServerMode so that runtime-invalid values ('openai' alias, stray strings)
@@ -194,44 +237,19 @@ export class ActorsMcpServer {
194
237
  rawServerMode !== undefined ? parseServerMode(rawServerMode) : parseServerMode(legacyUiMode);
195
238
  // Preliminary resolution — re-resolved inside the initialize handler once
196
239
  // client capabilities are known (only for 'auto').
197
- this.serverMode = resolveServerMode(this.serverModeOption, false);
240
+ this._serverMode = resolveServerMode(this.serverModeOption, false);
198
241
  this.serverModeResolved = this.serverModeOption !== 'auto';
199
- const { setupSigintHandler = true } = options;
200
- this.server = new Server(getServerInfo(), {
201
- capabilities: {
202
- tools: {
203
- listChanged: true,
204
- },
205
- // Declare long-running task support
206
- tasks: {
207
- list: {},
208
- cancel: {},
209
- requests: {
210
- tools: {
211
- call: {},
212
- },
213
- },
214
- },
215
- // Declared but unused — some clients (e.g. Claude Desktop) fail without it.
216
- resources: {},
217
- prompts: {},
218
- logging: {},
219
- },
220
- instructions: getServerInstructions(),
221
- });
222
242
  const { telemetryEnabled, telemetryEnv } = this.setupTelemetry();
223
243
  this.telemetryEnabled = telemetryEnabled;
224
244
  this.telemetryEnv = telemetryEnv;
225
- this.setupInitializeHandler();
226
- this.setupLoggingProxy();
227
245
  this.tools = new Map();
228
- this.setupErrorHandling(setupSigintHandler);
229
- this.setupLoggingHandlers();
230
- this.setupToolHandlers();
231
- this.setupPromptHandlers();
232
- // Handle resource requests so clients like Claude Desktop don't fail.
233
- this.setupResourceHandlers();
234
- this.setupTaskHandlers();
246
+ this.promptService = createPromptService(prompts);
247
+ this.resourceService = createResourceService({
248
+ paymentProvider: this.options.paymentProvider,
249
+ getMode: () => this.serverMode,
250
+ getAvailableWidgets: () => this.availableWidgets,
251
+ });
252
+ this.legacyServer = new LegacyMcpServer(this);
235
253
  }
236
254
  /**
237
255
  * Telemetry configuration with precedence: explicit options > env vars > defaults
@@ -255,51 +273,83 @@ export class ActorsMcpServer {
255
273
  return { telemetryEnabled, telemetryEnv };
256
274
  }
257
275
  /**
258
- * Override the SDK's `initialize` request handler to run mode resolution and
259
- * pending-source flush before `InitializeResult` is sent. Delegates boilerplate
260
- * (protocolVersion, capabilities, instructions) to the SDK's captured `_oninitialize`.
261
- *
262
- * Not using `server.oninitialized`: the SDK dispatches notification handlers
263
- * fire-and-forget (separate microtask), so a follow-up `tools/list` can race past them.
264
- * The request handler guarantees tools are final before the response and the first `tools/list`.
276
+ * The shared initialize steps the legacy adapter delegates to before returning
277
+ * `InitializeResult`: refresh client context, capture the raw request for session recovery,
278
+ * resolve `'auto'` mode against client capabilities, flush pending tool sources, resolve
279
+ * widgets. Ordering is load-bearing: mode before compose, compose before instructions, so tool
280
+ * presence reflects the final composed set.
265
281
  */
266
- setupInitializeHandler() {
267
- // Capture the SDK's default initialize handler installed in its constructor.
268
- // Private-field access on the SDK Server — verified against
269
- // @modelcontextprotocol/sdk ^1.25.x (see package.json). On SDK bumps, re-check
270
- // `@modelcontextprotocol/sdk/shared/protocol.js` for a still-named `_oninitialize`;
271
- // if renamed or made non-delegable, rebuild the InitializeResult shape here
272
- // (protocolVersion, serverInfo, capabilities, instructions) instead of delegating.
273
- // The capability-gating unit tests construct a server and act as a canary.
274
- // eslint-disable-next-line no-underscore-dangle
275
- const sdkInitHandler = this.server._oninitialize.bind(this.server);
276
- this.server.setRequestHandler(InitializeRequestSchema, async (request) => {
277
- var _a;
278
- this.clientContext = buildMcpClientContext(request.params);
279
- this.options.initializeRequestData = request;
280
- this.clientSupportsUi = isUiSupportedByClient(this.clientContext);
281
- if (this.serverModeOption === 'auto') {
282
- const resolved = resolveServerMode('auto', this.clientSupportsUi);
283
- if (resolved !== this.serverMode) {
284
- this.serverMode = resolved;
285
- }
286
- this.serverModeResolved = true;
282
+ async applyInitialize(request) {
283
+ var _a;
284
+ this._clientContext = buildMcpClientContext(request.params);
285
+ this.options.initializeRequestData = request;
286
+ this.clientSupportsUi = isUiSupportedByClient(this.clientContext);
287
+ if (this.serverModeOption === 'auto') {
288
+ const resolved = resolveServerMode('auto', this.clientSupportsUi);
289
+ if (resolved !== this._serverMode) {
290
+ this._serverMode = resolved;
287
291
  }
288
- log.info('Resolved server mode for client capabilities', {
289
- serverMode: this.serverMode,
290
- serverModeOption: this.serverModeOption,
291
- clientSupportsUi: this.clientSupportsUi,
292
- capabilities: (_a = request === null || request === void 0 ? void 0 : request.params) === null || _a === void 0 ? void 0 : _a.capabilities,
293
- });
294
- this.composePendingToolsForClient();
295
- await this.resolveWidgets();
296
- const result = await sdkInitHandler(request);
297
- // Tools are final here (composePendingToolsForClient ran above, applying the per-client
298
- // blocklist), so tool presence is the ground truth for whether to advertise
299
- // report-problem in the instructions.
300
- result.instructions = getServerInstructions(this.serverMode, this.tools.has(HELPER_TOOLS.PROBLEM_REPORT));
301
- return result;
292
+ this.serverModeResolved = true;
293
+ }
294
+ log.info('Resolved server mode for client capabilities', {
295
+ serverMode: this.serverMode,
296
+ serverModeOption: this.serverModeOption,
297
+ clientSupportsUi: this.clientSupportsUi,
298
+ capabilities: (_a = request === null || request === void 0 ? void 0 : request.params) === null || _a === void 0 ? void 0 : _a.capabilities,
302
299
  });
300
+ this.composePendingToolsForClient();
301
+ await this.resolveInstanceWidgets();
302
+ }
303
+ /**
304
+ * Server instructions for the current connection: mode plus whether report-problem is loaded.
305
+ * Read by the legacy adapter after `applyInitialize`, when the tool set is final.
306
+ */
307
+ getServerInstructions() {
308
+ return getServerInstructions(this.serverMode, this.tools.has(HELPER_TOOLS.PROBLEM_REPORT));
309
+ }
310
+ /**
311
+ * Instructions for a stateless serving unit. The SDK answers `server/discover` from them before
312
+ * any request's envelope is seen, so they are configuration-level: no report-problem mention
313
+ * (that tool's presence is decided per request) and the configured mode only. Reads
314
+ * `serverModeOption`, never `_serverMode` — one facade serves both eras, and a legacy
315
+ * `initialize` rewrites `_serverMode`, which must not leak into later stateless requests.
316
+ */
317
+ getStatelessServerInstructions() {
318
+ return getServerInstructions(resolveServerMode(this.serverModeOption, false));
319
+ }
320
+ /**
321
+ * Build the read-only view one stateless (2026-07-28) request is served from: mode and tool set
322
+ * resolved against *that request's* declared identity, and a resource service bound to both.
323
+ * Nothing request-specific is written back to the facade, so concurrent requests with different
324
+ * identities cannot contaminate each other.
325
+ */
326
+ async createRequestSnapshot(clientContext) {
327
+ // From the configured option, not `_serverMode` — same reason as
328
+ // {@link getStatelessServerInstructions}.
329
+ const serverMode = resolveServerMode(this.serverModeOption, isUiSupportedByClient(clientContext));
330
+ const view = { serverMode, clientContext };
331
+ // Re-compose from the retained sources, not the live `tools` map (composed for the
332
+ // instance's own view). Directly upserted tools are deliberately left out — carrying them
333
+ // over would re-add tools this view's gating just withheld.
334
+ const tools = new Map();
335
+ for (const source of this.toolSources.values()) {
336
+ for (const tool of this.composeToolsForClient(source, view)) {
337
+ const stored = this.toStoredTool(tool);
338
+ tools.set(stored.name, stored);
339
+ }
340
+ }
341
+ const availableWidgets = await this.resolveWidgetsForMode(serverMode);
342
+ return {
343
+ serverMode,
344
+ clientContext,
345
+ tools,
346
+ resourceService: createResourceService({
347
+ paymentProvider: this.options.paymentProvider,
348
+ getMode: () => serverMode,
349
+ getAvailableWidgets: () => availableWidgets,
350
+ }),
351
+ createApifyClient: (token) => this.createApifyClient(token, clientContext),
352
+ };
303
353
  }
304
354
  /** True once the connecting client is known (set in the initialize handler, or hydrated by a
305
355
  * recovery path). Only client-gated tools wait for this so the per-client blocklist can be
@@ -308,37 +358,34 @@ export class ActorsMcpServer {
308
358
  return this.clientContext != null;
309
359
  }
310
360
  /**
311
- * Compose the tool list for the current connection: resolve mode-specific tools, then drop
312
- * report-problem unless it is currently servable (see {@link isReportProblemServable}).
313
- * report-problem is a default-injected tool (via tools_loader) rather than a category member,
314
- * gated here by servability. Every other tool composes eagerly, so a recovery/rehydration load
315
- * without an initialize still restores them. report-problem is withheld until the client is known
316
- * and re-added by the initialize flush. Used by every input-driven load path and the flush.
317
- * (Actor tools are never gated, so they need no filtering.)
361
+ * Compose one source's tool list against `view`: resolve mode-specific tools, then drop
362
+ * report-problem unless servable for that view ({@link isReportProblemServable}). Load paths
363
+ * and the initialize flush pass the instance's own {@link servingContext};
364
+ * {@link createRequestSnapshot} passes a view derived from one stateless request.
318
365
  */
319
- composeToolsForClient(input, actorTools) {
320
- const tools = getToolsForServerMode(input, actorTools, this.serverMode);
321
- if (this.isReportProblemServable())
366
+ composeToolsForClient(source, view) {
367
+ const tools = getToolsForServerMode(source.input, source.actorTools, view.serverMode);
368
+ if (this.isReportProblemServable(view))
322
369
  return tools;
323
370
  return tools.filter((tool) => tool.name !== HELPER_TOOLS.PROBLEM_REPORT);
324
371
  }
325
372
  /**
326
- * Whether report-problem may be served on this connection right now:
327
- * - Its only function is forwarding submissions via telemetry, so it is never servable when
328
- * telemetry is disabled (it would just fake an acknowledgement into the void).
329
- * - It cannot be judged until the connecting client is known, so it is withheld until then;
330
- * the initialize flush re-composes and adds it if the client allows.
331
- * Every other tool is unconditionally servable, so recovery loads compose them eagerly and they
332
- * survive a load that never sees an initialize.
373
+ * Whether report-problem may be served against `view`. Never without telemetry (submissions
374
+ * would vanish into the void) and never before a client context exists — on a stateful
375
+ * connection the initialize flush re-adds it once the handshake supplies one.
376
+ *
377
+ * The stateless envelope requires protocol and capability metadata but not `clientInfo`. A
378
+ * request declaring no client name matches no blocked substring and is served the tool by
379
+ * policy.
333
380
  */
334
- isReportProblemServable() {
335
- return this.telemetryEnabled && this.clientKnown && !isReportProblemBlockedForClient(this.clientContext);
381
+ isReportProblemServable(view) {
382
+ return (this.telemetryEnabled && view.clientContext != null && !isReportProblemBlockedForClient(view.clientContext));
336
383
  }
337
384
  composePendingToolsForClient() {
338
- if (this.pendingToolsUntilClientKnown.length === 0)
385
+ if (this.pendingToolsUntilClientKnown.size === 0)
339
386
  return;
340
- const tools = this.pendingToolsUntilClientKnown.flatMap(({ input, actorTools }) => this.composeToolsForClient(input, actorTools));
341
- this.pendingToolsUntilClientKnown = [];
387
+ const tools = [...this.pendingToolsUntilClientKnown.values()].flatMap((source) => this.composeToolsForClient(source, this.servingContext));
388
+ this.pendingToolsUntilClientKnown.clear();
342
389
  // Load paths already upserted the client-agnostic tools pre-init; re-upserting is
343
390
  // idempotent, and this pass adds the client-gated tools (e.g. report-problem) now that the
344
391
  // client is known.
@@ -384,25 +431,26 @@ export class ActorsMcpServer {
384
431
  return [...this.listInternalToolNames(), ...this.listActorToolNames(), ...this.listActorMcpServerToolIds()];
385
432
  }
386
433
  /**
387
- * Buffer-or-compose gate shared by the actor-tools loaders. If the server mode isn't resolved
388
- * yet ('auto' before initialize), queue the whole source for `composePendingToolsForClient` and
389
- * (if non-empty) upsert the mode-agnostic actor tools immediately.
390
- * Once the mode is resolved, compose the client-specific set via `composeToolsForClient` (which
391
- * withholds report-problem until the client is known) and upsert it; if the client still isn't
392
- * known, queue the source so the initialize flush re-composes and adds the client-gated tools.
434
+ * Buffer-or-compose gate shared by the actor-tools loaders. Mode not resolved yet: queue the
435
+ * source for the initialize flush, upserting the mode-agnostic actor tools immediately. Mode
436
+ * resolved: compose and upsert now; if the client is still unknown, also queue the source so
437
+ * the flush adds the client-gated tools.
393
438
  */
394
439
  registerFetchedActorTools(input, actorTools) {
440
+ const source = { input, actorTools };
441
+ const key = toolSourceKey(input);
442
+ this.toolSources.set(key, source);
395
443
  if (!this.serverModeResolved) {
396
- this.pendingToolsUntilClientKnown.push({ input, actorTools });
444
+ this.pendingToolsUntilClientKnown.set(key, source);
397
445
  if (actorTools.length > 0)
398
446
  this.upsertTools(actorTools);
399
447
  return;
400
448
  }
401
- const tools = this.composeToolsForClient(input, actorTools);
449
+ const tools = this.composeToolsForClient(source, this.servingContext);
402
450
  if (tools.length > 0)
403
451
  this.upsertTools(tools);
404
452
  if (!this.clientKnown)
405
- this.pendingToolsUntilClientKnown.push({ input, actorTools });
453
+ this.pendingToolsUntilClientKnown.set(key, source);
406
454
  }
407
455
  /**
408
456
  * Loads missing toolNames from a provided list of tool names.
@@ -431,14 +479,9 @@ export class ActorsMcpServer {
431
479
  this.registerFetchedActorTools(input, actorTools);
432
480
  }
433
481
  /**
434
- * Two-phase: getActors (async, client-agnostic Apify fetch) then composeToolsForClient
435
- * (sync compose + servability filter). If the mode isn't resolved yet ('auto' before initialize)
436
- * the whole source is queued for the flush. Otherwise tools compose immediately; client-gated
437
- * tools are withheld until the client is known, and the source is queued so the flush adds them.
438
- *
439
- * Don't move the getActors await into the initialize handler — clients time out
440
- * waiting for InitializeResult. The queue buffers already-fetched data, not network
441
- * work. See #721.
482
+ * Two-phase: getActors (async, client-agnostic fetch) then the buffer-or-compose gate.
483
+ * Don't move the getActors await into the initialize handler — clients time out waiting for
484
+ * InitializeResult; the queue buffers already-fetched data, not network work. See #721.
442
485
  */
443
486
  async loadToolsFromInput(input, apifyClient) {
444
487
  const actorTools = await getActors(input, apifyClient, {
@@ -447,86 +490,22 @@ export class ActorsMcpServer {
447
490
  });
448
491
  this.registerFetchedActorTools(input, actorTools);
449
492
  }
450
- /** Delete tools from the server. */
451
- removeToolsByName(toolNames) {
452
- const removedTools = [];
453
- for (const toolName of toolNames) {
454
- if (this.removeToolByName(toolName)) {
455
- removedTools.push(toolName);
456
- }
457
- }
458
- return removedTools;
459
- }
460
493
  /**
461
- * Upsert new tools.
462
- * @param tools - Array of tool wrappers to add or update
463
- * @returns Array of added/updated tool wrappers
494
+ * Upsert new tools. Writes the shared tool map directly, bypassing the retained load sources,
495
+ * so a tool added only this way reaches no stateless snapshot. Load through `loadToolsFrom*` /
496
+ * `loadToolsByName` instead to serve a tool on both protocol eras.
464
497
  */
465
498
  upsertTools(tools) {
466
- // Client gating (e.g. hiding report-problem from Anthropic surfaces) is applied earlier, in
467
- // composeToolsForClient — the single compose choke point where the client is known. Do not
468
- // filter here: this is a low-level commit point reached before the client is known too.
499
+ // Client gating happens earlier, in composeToolsForClient. Do not filter here: this is a
500
+ // low-level commit point reached before the client is known too.
469
501
  for (const tool of tools) {
470
- const stored = this.options.paymentProvider ? this.options.paymentProvider.decorateToolSchema(tool) : tool;
502
+ const stored = this.toStoredTool(tool);
471
503
  this.tools.set(stored.name, stored);
472
504
  }
473
505
  return tools;
474
506
  }
475
- removeToolByName(toolName) {
476
- if (this.tools.has(toolName)) {
477
- this.tools.delete(toolName);
478
- log.debug('Deleted tool', { toolName });
479
- return true;
480
- }
481
- return false;
482
- }
483
- setupErrorHandling(setupSIGINTHandler = true) {
484
- this.server.onerror = (error) => {
485
- var _a;
486
- // Known client faults are expected noise, not server bugs — softFail so they don't
487
- // flood Mezmo error alerts. The fault patterns live in utils/logging.ts.
488
- const message = (_a = error.message) !== null && _a !== void 0 ? _a : '';
489
- if (isMcpClientFaultMessage(message)) {
490
- // Sanitize the errMessage value to preserve the soft-fail level (Mezmo promotes
491
- // entries whose message contains "error").
492
- log.softFail('MCP client fault, request could not be handled', {
493
- errMessage: sanitizeMezmoMessage(message),
494
- });
495
- }
496
- else {
497
- log.error('[MCP Error]', { error });
498
- }
499
- };
500
- if (setupSIGINTHandler) {
501
- const handler = async () => {
502
- await this.server.close();
503
- process.exit(0);
504
- };
505
- process.once('SIGINT', handler);
506
- this.sigintHandler = handler;
507
- }
508
- }
509
- setupLoggingProxy() {
510
- const originalSendLoggingMessage = this.server.sendLoggingMessage.bind(this.server);
511
- // Filter outgoing log messages below the client's requested level.
512
- this.server.sendLoggingMessage = async (params) => {
513
- var _a, _b;
514
- const messageLevelValue = (_a = LOG_LEVEL_MAP[params.level]) !== null && _a !== void 0 ? _a : -1; // Unknown levels get -1, discard
515
- const currentLevelValue = (_b = LOG_LEVEL_MAP[this.currentLogLevel]) !== null && _b !== void 0 ? _b : LOG_LEVEL_MAP.info; // Default to info if invalid
516
- if (messageLevelValue >= currentLevelValue) {
517
- await originalSendLoggingMessage(params);
518
- }
519
- };
520
- }
521
- setupLoggingHandlers() {
522
- this.server.setRequestHandler(SetLevelRequestSchema, (request) => {
523
- const { level } = request.params;
524
- if (LOG_LEVEL_MAP[level] !== undefined) {
525
- this.currentLogLevel = level;
526
- }
527
- // Sending empty result based on MCP spec
528
- return {};
529
- });
507
+ toStoredTool(tool) {
508
+ return this.options.paymentProvider ? this.options.paymentProvider.decorateToolSchema(tool) : tool;
530
509
  }
531
510
  /**
532
511
  * Token sources in order: per-request `_meta.apifyToken` (stdio inline) > server-instance
@@ -544,429 +523,55 @@ export class ActorsMcpServer {
544
523
  * Still carries the request-origin tag from the client context captured by this point.
545
524
  */
546
525
  resolveApifyClient(params) {
547
- const token = this.resolveApifyToken(params._meta);
548
- return token
549
- ? new ApifyClient({ token, requestOrigin: getRequestOriginForClient(this.clientContext) })
550
- : undefined;
526
+ return this.createApifyClient(this.resolveApifyToken(params._meta), this.clientContext);
551
527
  }
552
- setupResourceHandlers() {
553
- const resourceService = createResourceService({
554
- paymentProvider: this.options.paymentProvider,
555
- getMode: () => this.serverMode,
556
- getAvailableWidgets: () => this.availableWidgets,
557
- });
558
- this.server.setRequestHandler(ListResourcesRequestSchema, async () => {
559
- return await resourceService.listResources();
560
- });
561
- this.server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
562
- try {
563
- return await resourceService.readResource(request.params.uri, this.resolveApifyClient(request.params));
564
- }
565
- catch (error) {
566
- throw toLegacyMcpError(error);
567
- }
568
- });
569
- this.server.setRequestHandler(ListResourceTemplatesRequestSchema, async () => {
570
- return await resourceService.listResourceTemplates();
571
- });
528
+ /** The one place a request-scoped Apify client is constructed, on either protocol era. */
529
+ createApifyClient(token, clientContext) {
530
+ return token ? new ApifyClient({ token, requestOrigin: getRequestOriginForClient(clientContext) }) : undefined;
572
531
  }
573
532
  /**
574
- * Sets up MCP request handlers for prompts. The prompt service throws domain errors; the
575
- * boundary maps them to `McpError` so wire output is unchanged.
533
+ * Widgets servable in `mode`: none outside apps mode, otherwise the disk registry. A successful
534
+ * scan is memoized per facade (a widget file appearing later is not picked up); a failed one is
535
+ * dropped so the next caller retries. Touches no per-connection state, so a per-request caller
536
+ * cannot disturb a concurrent one.
576
537
  */
577
- setupPromptHandlers() {
578
- const promptService = createPromptService(prompts);
579
- this.server.setRequestHandler(ListPromptsRequestSchema, () => promptService.listPrompts());
580
- this.server.setRequestHandler(GetPromptRequestSchema, async (request) => {
581
- try {
582
- return promptService.getPrompt(request.params.name, request.params.arguments);
583
- }
584
- catch (error) {
585
- throw toLegacyMcpError(error);
586
- }
587
- });
588
- }
589
- /**
590
- * Fetches a task by ID, softFail-logging and throwing a client-facing McpError if it doesn't exist.
591
- */
592
- async getTaskOrThrow(taskId, mcpSessionId, logTag) {
593
- const task = await this.taskStore.getTask(taskId, mcpSessionId);
594
- if (!task) {
595
- // Client error (invalid/unknown taskId) — softFail to avoid polluting error logs.
596
- log.softFail(`[${logTag}] Task not found`, { taskId, mcpSessionId, statusCode: 404 });
597
- throw new McpError(ErrorCode.InvalidParams, `Task "${taskId}" not found`);
598
- }
599
- return task;
600
- }
601
- /**
602
- * Sets up MCP request handlers for long-running tasks.
603
- * Each handler reads `_meta.mcpSessionId` (injected at the transport layer) to isolate
604
- * per-session task stores.
605
- */
606
- setupTaskHandlers() {
607
- // List tasks
608
- this.server.setRequestHandler(ListTasksRequestSchema, async (request) => {
609
- var _a;
610
- const params = (request.params || {});
611
- const { cursor } = params;
612
- const mcpSessionId = (_a = params._meta) === null || _a === void 0 ? void 0 : _a.mcpSessionId;
613
- log.debug('[ListTasksRequestSchema] Listing tasks', { mcpSessionId });
614
- const result = await this.taskStore.listTasks(cursor, mcpSessionId);
615
- return { tasks: result.tasks, nextCursor: result.nextCursor };
616
- });
617
- // Get task status
618
- this.server.setRequestHandler(GetTaskRequestSchema, async (request) => {
619
- var _a;
620
- const params = (request.params || {});
621
- const { taskId } = params;
622
- const mcpSessionId = (_a = params._meta) === null || _a === void 0 ? void 0 : _a.mcpSessionId;
623
- log.debug('[GetTaskRequestSchema] Getting task status', { taskId, mcpSessionId });
624
- return await this.getTaskOrThrow(taskId, mcpSessionId, 'GetTaskRequestSchema');
625
- });
626
- // Get task result payload
627
- this.server.setRequestHandler(GetTaskPayloadRequestSchema, async (request) => {
628
- var _a;
629
- const params = (request.params || {});
630
- const { taskId } = params;
631
- const mcpSessionId = (_a = params._meta) === null || _a === void 0 ? void 0 : _a.mcpSessionId;
632
- log.debug('[GetTaskPayloadRequestSchema] Getting task result', { taskId, mcpSessionId });
633
- const task = await this.getTaskOrThrow(taskId, mcpSessionId, 'GetTaskPayloadRequestSchema');
634
- if (task.status !== 'completed' && task.status !== 'failed') {
635
- throw new McpError(ErrorCode.InvalidParams, `Task "${taskId}" is not completed yet. Current status: ${task.status}`);
636
- }
637
- const result = await this.taskStore.getTaskResult(taskId, mcpSessionId);
638
- // taskId is not in the result body — _meta.related-task lets clients correlate them
639
- return {
640
- ...result,
641
- _meta: {
642
- ...result._meta,
643
- [RELATED_TASK_META_KEY]: { taskId },
644
- },
645
- };
646
- });
647
- // Cancel task
648
- this.server.setRequestHandler(CancelTaskRequestSchema, async (request) => {
649
- var _a;
650
- const params = (request.params || {});
651
- const { taskId } = params;
652
- const mcpSessionId = (_a = params._meta) === null || _a === void 0 ? void 0 : _a.mcpSessionId;
653
- log.debug('[CancelTaskRequestSchema] Cancelling task', { taskId, mcpSessionId });
654
- const task = await this.getTaskOrThrow(taskId, mcpSessionId, 'CancelTaskRequestSchema');
655
- if (task.status === 'completed' || task.status === 'failed' || task.status === 'cancelled') {
656
- // Client error (cancel on terminal task) — softFail to avoid polluting error logs.
657
- log.softFail('[CancelTaskRequestSchema] Task already in terminal state', {
658
- taskId,
659
- mcpSessionId,
660
- status: task.status,
661
- statusCode: 409,
662
- });
663
- throw new McpError(ErrorCode.InvalidParams, `Cannot cancel task "${taskId}" with status "${task.status}"`);
664
- }
665
- await this.taskStore.updateTaskStatus(taskId, 'cancelled', 'Cancelled by client', mcpSessionId);
666
- const updatedTask = await this.taskStore.getTask(taskId, mcpSessionId);
667
- log.debug('[CancelTaskRequestSchema] Task cancelled successfully', { taskId, mcpSessionId });
668
- await emitTaskStatusNotification(taskId, mcpSessionId, this.taskStore, this.server);
669
- return updatedTask;
670
- });
671
- }
672
- setupToolHandlers() {
673
- // Handles the request to list tools.
674
- this.server.setRequestHandler(ListToolsRequestSchema, async () => {
675
- const tools = Array.from(this.tools.values()).map((tool) => getToolPublicFieldOnly(tool, {
676
- mode: this.serverMode,
677
- filterWidgetMeta: true,
678
- }));
679
- return { tools };
680
- });
681
- /**
682
- * Handles the request to call a tool. `extra` carries request-scoped helpers such as
683
- * `sendNotification`. Throws {@link McpError} to mirror the MCP SDK's McpServer error codes.
684
- */
685
- this.server.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
686
- var _a, _b, _c;
687
- const params = request.params;
688
- // Keep telemetry on the decoded arguments.
689
- // eslint-disable-next-line prefer-const
690
- let { name, arguments: args, _meta: meta } = params;
691
- const progressToken = meta === null || meta === void 0 ? void 0 : meta.progressToken;
692
- const apifyToken = this.resolveApifyToken(meta);
693
- // Injected upstream; required for long-running tasks — the task store keys on it and
694
- // there is no other channel to pass it.
695
- const mcpSessionId = meta === null || meta === void 0 ? void 0 : meta.mcpSessionId;
696
- if (!mcpSessionId) {
697
- log.error('MCP Session ID is missing in tool call request. This should never happen.');
698
- throw new Error('MCP Session ID is required for tool calls');
699
- }
700
- const startTime = Date.now();
701
- let toolStatus = TOOL_STATUS.SUCCEEDED;
702
- let callDiagnostics = {};
703
- let shouldTrackTelemetry = true;
704
- let resolvedToolName = name;
705
- // Set only on the pre-flight task path — the one task-mode flow whose telemetry rides
706
- // this handler's `finally` — so its `Tool call completed` log line keeps the taskId the
707
- // async path logs via finishTaskTracking.
708
- let preflightTaskId;
709
- // The nudge must be included in the measured result.
710
- let toolResult = null;
711
- // Keep actor context available to the outer catch.
712
- let actorName;
713
- let actorId;
714
- const { clientContext } = this;
715
- // Start with the raw name so early failures still have telemetry.
716
- const { telemetryData, userId } = await prepareTelemetryData({
717
- toolName: name,
718
- mcpSessionId,
719
- apifyToken,
720
- apifyMcpServer: this,
721
- clientContext,
722
- });
723
- try {
724
- const prepared = await prepareToolCall({
725
- apifyMcpServer: this,
726
- apifyToken,
727
- name,
728
- args,
729
- meta,
730
- requestHeaders: (_a = extra.requestInfo) === null || _a === void 0 ? void 0 : _a.headers,
731
- isTaskRequest: Boolean(request.params.task),
732
- mcpSessionId,
733
- telemetryData,
734
- clientContext,
735
- extra,
736
- });
737
- if ('result' in prepared) {
738
- // The engine already classified this post-resolution failure.
739
- resolvedToolName = prepared.resolvedToolName;
740
- args = prepared.decodedArgs;
741
- toolStatus = prepared.toolStatus;
742
- callDiagnostics = prepared.callDiagnostics;
743
- toolResult = prepared.result;
744
- return prepared.result;
745
- }
746
- if ('message' in prepared) {
747
- // Reproduce v1's invalid-params tail and preserve telemetry fields.
748
- resolvedToolName = (_b = prepared.resolvedToolName) !== null && _b !== void 0 ? _b : resolvedToolName;
749
- if (prepared.decodedArgs)
750
- args = prepared.decodedArgs;
751
- toolStatus = prepared.toolStatus;
752
- callDiagnostics = prepared.callDiagnostics;
753
- log.softFail(prepared.message, {
754
- mcpSessionId,
755
- failureCategory: prepared.callDiagnostics.failure_category,
756
- actorName: prepared.callDiagnostics.actor_name,
757
- validationKeyword: prepared.callDiagnostics.validation_keyword,
758
- validationPath: prepared.callDiagnostics.validation_path,
759
- validationMissingProperty: prepared.callDiagnostics.validation_missing_property,
760
- validationAdditionalProperty: prepared.callDiagnostics.validation_additional_property,
761
- ...prepared.logFields,
762
- });
763
- await this.server.sendLoggingMessage({ level: 'error', data: prepared.message });
764
- throw new McpError(ErrorCode.InvalidParams, prepared.message);
765
- }
766
- const { tool, toolArgs, logSafeArgs, apifyClient, standbyRejection, paymentRequiredResult } = prepared;
767
- actorName = prepared.actorName;
768
- actorId = prepared.actorId;
769
- resolvedToolName = getToolFullName(tool);
770
- // Telemetry uses the decoded arguments.
771
- args = prepared.decodedArgs;
772
- // TODO: we should split this huge method into smaller parts as it is slowly getting out of hand
773
- // Handle long-running task request
774
- if (request.params.task) {
775
- const task = await this.taskStore.createTask({
776
- ttl: request.params.task.ttl,
777
- }, `call-tool-${name}-${randomUUID()}`, request, mcpSessionId);
778
- log.debug('Created task for tool execution', {
779
- taskId: task.taskId,
780
- toolName: tool.name,
781
- mcpSessionId,
782
- });
783
- // Pre-flight failure is already known — the outcome needs no work. Resolve the
784
- // task synchronously: store the failure as the terminal `completed` result and emit
785
- // exactly one `completed` status notification (no `updateTaskStatus('working')`, so no
786
- // `working` notification). Standby rejection wins over the generic payment-required
787
- // short-circuit, matching the sync path's precedence. Telemetry rides the handler's
788
- // outer `finally` (shouldTrackTelemetry stays true), firing once with the sync-path
789
- // properties plus the taskId on the log line.
790
- const preflightResult = standbyRejection !== null && standbyRejection !== void 0 ? standbyRejection : paymentRequiredResult;
791
- if (preflightResult) {
792
- const outcome = buildPreflightFailureOutcome(standbyRejection, paymentRequiredResult, actorName, actorId);
793
- toolStatus = outcome.toolStatus;
794
- callDiagnostics = outcome.callDiagnostics;
795
- preflightTaskId = task.taskId;
796
- try {
797
- await storeTaskResultOrSkipIfExpired(this.taskStore, tool.name, task.taskId, 'completed', outcome.result, mcpSessionId);
798
- }
799
- catch (error) {
800
- // A store failure (not expiry) would otherwise fall through to the generic
801
- // catch and return a task-less tool result the client rejects as a
802
- // CreateTaskResult parse error; surface it as a protocol error instead.
803
- // The pre-flight outcome left toolStatus=SOFT_FAIL, but a genuine store
804
- // outage is a hard failure — correct it before throwing so the handler
805
- // `finally` logs FAILED/INTERNAL_ERROR, not the stale pre-flight SOFT_FAIL.
806
- toolStatus = TOOL_STATUS.FAILED;
807
- callDiagnostics = {
808
- failure_category: FAILURE_CATEGORY.INTERNAL_ERROR,
809
- ...buildActorFields(actorName, actorId),
810
- };
811
- throw new McpError(ErrorCode.InternalError, `Failed to store the pre-flight result for task "${task.taskId}": ${error instanceof Error ? error.message : String(error)}`);
812
- }
813
- // Defer so the client sees the CreateTaskResult (and learns the taskId) before
814
- // the terminal status notification — the async path's post-response ordering.
815
- // emitTaskStatusNotification never throws and no-ops if the task expired.
816
- setImmediate(() => {
817
- void emitTaskStatusNotification(task.taskId, mcpSessionId, this.taskStore, this.server);
818
- });
819
- // Measure the nudged result without changing the stored result.
820
- toolResult = withReportProblemNudge({
821
- result: outcome.result,
822
- tools: this.tools,
823
- failingToolName: resolvedToolName,
824
- failureCategory: callDiagnostics.failure_category,
825
- failureHttpStatus: callDiagnostics.failure_http_status,
826
- });
827
- // createTask returned status `working`; synthesize the terminal status instead of
828
- // re-fetching — if the task expired before the result store (the one case
829
- // storeTaskResultOrSkipIfExpired tolerates), a re-fetch would come back empty and a
830
- // `working` fallback would contradict the tasks/get 404 the client sees next.
831
- return { task: { ...task, status: 'completed' } };
832
- }
833
- // Execute the tool asynchronously and update task status
834
- setImmediate(() => {
835
- executeToolAndUpdateTask({
836
- taskId: task.taskId,
837
- tool,
838
- toolArgs,
839
- logSafeArgs,
840
- apifyClient,
841
- apifyToken,
842
- progressToken,
843
- extra,
844
- mcpSessionId,
845
- actorName,
846
- actorId,
847
- apifyMcpServer: this,
848
- clientContext,
849
- }).catch((error) =>
850
- // Benign task-expiry is handled in-method (see the catch block and
851
- // storeTaskResultOrSkipIfExpired); anything reaching here is genuinely unexpected.
852
- log.error('executeToolAndUpdateTask failed unexpectedly', { taskId: task.taskId, error }));
853
- });
854
- // Return the task immediately; execution continues asynchronously
855
- shouldTrackTelemetry = false;
856
- return { task };
857
- }
858
- // Sync path: run the shared dispatch tail and project its neutral outcome by identity.
859
- const outcome = await executeSyncToolCall(prepared, {
860
- apifyMcpServer: this,
861
- apifyToken,
862
- toolName: name,
863
- mcpSessionId,
864
- progressToken,
865
- extra,
866
- });
867
- toolStatus = outcome.toolStatus;
868
- callDiagnostics = outcome.callDiagnostics;
869
- toolResult = outcome.result;
870
- return outcome.result;
871
- }
872
- catch (error) {
873
- // Match v1: classify task-creation failures, but re-throw protocol errors as JSON-RPC.
874
- if (error instanceof McpError) {
875
- throw error;
876
- }
877
- const outcome = classifyToolCallError(error, {
878
- apifyMcpServer: this,
879
- toolName: name,
880
- failingToolName: resolvedToolName,
881
- actorName,
882
- actorId,
883
- isAborted: Boolean((_c = extra.signal) === null || _c === void 0 ? void 0 : _c.aborted),
884
- mcpSessionId,
885
- });
886
- toolStatus = outcome.toolStatus;
887
- callDiagnostics = outcome.callDiagnostics;
888
- toolResult = outcome.result;
889
- return outcome.result;
890
- }
891
- finally {
892
- if (shouldTrackTelemetry) {
893
- logToolCallAndTelemetry({
894
- toolName: resolvedToolName,
895
- mcpSessionId,
896
- toolStatus,
897
- startTime,
898
- telemetryData,
899
- userId,
900
- callDiagnostics,
901
- args,
902
- result: toolResult,
903
- taskId: preflightTaskId,
904
- apifyMcpServer: this,
905
- });
906
- }
907
- }
908
- });
909
- }
910
- /**
911
- * Resolves widgets and determines which ones are ready to be served.
912
- */
913
- async resolveWidgets() {
914
- if (this.serverMode !== SERVER_MODE.APPS) {
915
- return;
916
- }
917
- try {
918
- const { fileURLToPath } = await import('node:url');
919
- const path = await import('node:path');
920
- const filename = fileURLToPath(import.meta.url);
921
- const dirName = path.dirname(filename);
922
- const resolved = await resolveAvailableWidgets(dirName);
923
- this.availableWidgets = resolved;
924
- const readyWidgets = [];
925
- const missingWidgets = [];
926
- for (const [uri, widget] of resolved.entries()) {
927
- if (widget.exists) {
928
- readyWidgets.push(widget.name);
929
- }
930
- else {
931
- missingWidgets.push(widget.name);
932
- log.softFail(`Widget file not found: ${widget.jsPath} (widget: ${uri})`);
933
- }
934
- }
935
- if (readyWidgets.length > 0) {
936
- log.debug('Ready widgets', { widgets: readyWidgets });
937
- }
938
- if (missingWidgets.length > 0) {
939
- log.softFail('Some widgets are not ready', {
940
- widgets: missingWidgets,
941
- note: 'These widgets will not be available. Ensure web/dist files are built and included in deployment.',
942
- });
943
- }
538
+ async resolveWidgetsForMode(mode) {
539
+ var _a;
540
+ if (mode !== SERVER_MODE.APPS) {
541
+ return new Map();
944
542
  }
945
- catch (error) {
543
+ // Catch on the shared attempt, not per awaiter: N callers awaiting one rejected scan would
544
+ // otherwise report one root cause N times. Dropping the memo lets the next caller re-run it.
545
+ const resolution = ((_a = this.widgetsResolution) !== null && _a !== void 0 ? _a : (this.widgetsResolution = resolveServableWidgets().catch((error) => {
546
+ this.widgetsResolution = undefined;
946
547
  const errorMessage = error instanceof Error ? error.message : String(error);
947
548
  log.softFail(`Failed to resolve widgets: ${errorMessage}`);
948
549
  // Continue without widgets
949
- }
550
+ return new Map();
551
+ })));
552
+ return await resolution;
553
+ }
554
+ /**
555
+ * Resolve the instance's own widget map for this connection's mode. The only writer of that
556
+ * field — per-request callers take the map `resolveWidgetsForMode` returns instead.
557
+ */
558
+ async resolveInstanceWidgets() {
559
+ this.availableWidgets = await this.resolveWidgetsForMode(this.serverMode);
950
560
  }
951
561
  async connect(transport) {
952
- await this.resolveWidgets();
953
- await this.server.connect(transport);
562
+ await this.resolveInstanceWidgets();
563
+ await this.legacyServer.connect(transport);
954
564
  }
955
565
  async close() {
956
- if (this.sigintHandler) {
957
- process.removeListener('SIGINT', this.sigintHandler);
958
- this.sigintHandler = undefined;
959
- }
960
- // Clear all tools and null their compiled schemas.
961
- for (const tool of this.tools.values()) {
962
- if (tool.ajvValidate && typeof tool.ajvValidate === 'function') {
963
- tool.ajvValidate = null;
964
- }
965
- }
566
+ // Transport/server down first, then everything this facade retains: the composed map plus
567
+ // both source maps, each holding every fetched `ToolEntry` with its compiled AJV validator.
568
+ // Close is the release point — a long-lived host churning sessions must not accumulate them
569
+ // — so a stateless snapshot taken after close composes no tools.
570
+ await this.legacyServer.close();
966
571
  this.tools.clear();
967
- // Closing the server also removes its event handlers.
968
- await this.server.close();
572
+ this.toolSources.clear();
573
+ this.pendingToolsUntilClientKnown.clear();
969
574
  }
970
575
  }
971
576
  //# sourceMappingURL=server.js.map
972
- //# debugId=24ee8a78-9505-5c66-bc50-80953252f83f
577
+ //# debugId=529d6806-45aa-580f-86b6-b1a0fe148f0c