@apify/actors-mcp-server 0.11.6-beta.9 → 0.11.7-beta.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.
- package/README.md +4 -0
- package/dist/const.d.ts +10 -0
- package/dist/const.d.ts.map +1 -1
- package/dist/const.js +10 -0
- package/dist/const.js.map +1 -1
- package/dist/index_internals.d.ts +1 -0
- package/dist/index_internals.d.ts.map +1 -1
- package/dist/mcp/server.d.ts +55 -20
- package/dist/mcp/server.d.ts.map +1 -1
- package/dist/mcp/server.js +205 -85
- package/dist/mcp/server.js.map +1 -1
- package/dist/resources/widgets.d.ts +5 -0
- package/dist/resources/widgets.d.ts.map +1 -1
- package/dist/resources/widgets.js +11 -0
- package/dist/resources/widgets.js.map +1 -1
- package/dist/telemetry.d.ts +14 -1
- package/dist/telemetry.d.ts.map +1 -1
- package/dist/telemetry.js +41 -0
- package/dist/telemetry.js.map +1 -1
- package/dist/tools/actors/actor_executor.d.ts.map +1 -1
- package/dist/tools/actors/actor_executor.js +2 -3
- package/dist/tools/actors/actor_executor.js.map +1 -1
- package/dist/tools/actors/actor_run_response.d.ts +11 -9
- package/dist/tools/actors/actor_run_response.d.ts.map +1 -1
- package/dist/tools/actors/actor_run_response.js +33 -24
- package/dist/tools/actors/actor_run_response.js.map +1 -1
- package/dist/tools/actors/call_actor.js +2 -2
- package/dist/tools/actors/call_actor.js.map +1 -1
- package/dist/tools/dev/report_problem.d.ts +40 -0
- package/dist/tools/dev/report_problem.d.ts.map +1 -0
- package/dist/tools/dev/report_problem.js +112 -0
- package/dist/tools/dev/report_problem.js.map +1 -0
- package/dist/tools/registry.d.ts +1 -1
- package/dist/tools/registry.d.ts.map +1 -1
- package/dist/tools/registry.js +2 -1
- package/dist/tools/registry.js.map +1 -1
- package/dist/tools/runs/get_actor_run.d.ts +7 -4
- package/dist/tools/runs/get_actor_run.d.ts.map +1 -1
- package/dist/tools/runs/get_actor_run.js +21 -19
- package/dist/tools/runs/get_actor_run.js.map +1 -1
- package/dist/tools/widgets/call_actor_widget.js +2 -2
- package/dist/tools/widgets/call_actor_widget.js.map +1 -1
- package/dist/tools/widgets/get_actor_run_widget.js +2 -2
- package/dist/tools/widgets/get_actor_run_widget.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types.d.ts +11 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/mcp_clients.d.ts +10 -0
- package/dist/utils/mcp_clients.d.ts.map +1 -0
- package/dist/utils/mcp_clients.js +14 -0
- package/dist/utils/mcp_clients.js.map +1 -0
- package/dist/utils/server-instructions/index.d.ts +5 -2
- package/dist/utils/server-instructions/index.d.ts.map +1 -1
- package/dist/utils/server-instructions/index.js +10 -3
- package/dist/utils/server-instructions/index.js.map +1 -1
- package/dist/utils/tool_status.d.ts +10 -0
- package/dist/utils/tool_status.d.ts.map +1 -1
- package/dist/utils/tool_status.js +10 -0
- package/dist/utils/tool_status.js.map +1 -1
- package/dist/utils/tools_loader.d.ts.map +1 -1
- package/dist/utils/tools_loader.js +6 -0
- package/dist/utils/tools_loader.js.map +1 -1
- package/manifest.json +1 -1
- package/package.json +1 -1
- package/server.json +1 -1
package/dist/mcp/server.js
CHANGED
|
@@ -19,21 +19,23 @@ import { prompts } from '../prompts/index.js';
|
|
|
19
19
|
import { createResourceService } from '../resources/resource_service.js';
|
|
20
20
|
import { resolveAvailableWidgets } from '../resources/widgets.js';
|
|
21
21
|
import { getServerInfo } from '../server_card.js';
|
|
22
|
-
import { getTelemetryEnv, trackToolCall } from '../telemetry.js';
|
|
22
|
+
import { buildReportedProblemProperties, getTelemetryEnv, trackReportedProblem, trackToolCall } from '../telemetry.js';
|
|
23
23
|
import { decodeDotPropertyNames } from '../tools/actor_input_schema.js';
|
|
24
24
|
import { legacyToolNameToNew } from '../tools/actor_tool_naming.js';
|
|
25
25
|
import { actorExecutor } from '../tools/actors/actor_executor.js';
|
|
26
26
|
import { buildPermissionApprovalResponse, checkPaymentProviderStandbyConflict } from '../tools/actors/call_actor.js';
|
|
27
|
+
import { appendReportProblemNudge } from '../tools/dev/report_problem.js';
|
|
27
28
|
import { getActorsAsTools } from '../tools/index.js';
|
|
28
29
|
import { SERVER_MODE, TOOL_TYPE } from '../types.js';
|
|
29
30
|
import { isPermissionApprovalError, remoteMcpFailureDetail } from '../utils/apify_errors.js';
|
|
30
31
|
import { getHttpStatusCode, isMcpClientFaultMessage, logHttpError, sanitizeMezmoMessage } from '../utils/logging.js';
|
|
31
32
|
import { computeToolResponseBytes, getToolCallErrorUserText, respondErrorNoTelemetry, respondOk, } from '../utils/mcp.js';
|
|
33
|
+
import { isReportProblemBlockedForClient } from '../utils/mcp_clients.js';
|
|
32
34
|
import { buildPaymentRequiredResponse, isX402PaymentRequiredError } from '../utils/payment_errors.js';
|
|
33
35
|
import { createProgressTracker } from '../utils/progress.js';
|
|
34
36
|
import { getServerInstructions } from '../utils/server-instructions/index.js';
|
|
35
37
|
import { parseServerMode, resolveServerMode } from '../utils/server_mode.js';
|
|
36
|
-
import { classifyFailureCategory, deriveResourceIds, extractAjvErrorDetails,
|
|
38
|
+
import { applyToolTelemetry, classifyFailureCategory, deriveResourceIds, extractAjvErrorDetails, getToolStatusFromError, } from '../utils/tool_status.js';
|
|
37
39
|
import { buildActorFields, extractActorId, extractActorName, getToolFullName, getToolPublicFieldOnly, } from '../utils/tools.js';
|
|
38
40
|
import { getActors, getToolsForServerMode, toolNamesToInput } from '../utils/tools_loader.js';
|
|
39
41
|
import { getUserInfoCached } from '../utils/userid_cache.js';
|
|
@@ -81,6 +83,24 @@ export async function emitTaskStatusNotification(taskId, mcpSessionId, taskStore
|
|
|
81
83
|
// Silent fail — notifications are advisory
|
|
82
84
|
}
|
|
83
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Shared diagnostics for a pre-flight failure (standby-provider conflict or missing/invalid payment
|
|
88
|
+
* signature) — a call outcome already known before any Actor runs. Standby rejection wins over the
|
|
89
|
+
* generic payment-required failure so the agent gets the precise reason instead of a generic 402.
|
|
90
|
+
* Pure: callers own their own post-handling (return the result directly, or store + notify + synthesize
|
|
91
|
+
* a terminal task) — call this only once `standbyRejection ?? paymentRequiredResult` is already truthy.
|
|
92
|
+
*/
|
|
93
|
+
function buildPreflightFailureOutcome(standbyRejection, paymentRequiredResult, actorName, actorId) {
|
|
94
|
+
return {
|
|
95
|
+
toolStatus: TOOL_STATUS.SOFT_FAIL,
|
|
96
|
+
callDiagnostics: {
|
|
97
|
+
failure_category: FAILURE_CATEGORY.INVALID_INPUT,
|
|
98
|
+
...(standbyRejection ? {} : { failure_http_status: 402 }),
|
|
99
|
+
...buildActorFields(actorName, actorId),
|
|
100
|
+
},
|
|
101
|
+
result: (standbyRejection !== null && standbyRejection !== void 0 ? standbyRejection : paymentRequiredResult),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
84
104
|
/**
|
|
85
105
|
* Create Apify MCP server
|
|
86
106
|
*/
|
|
@@ -156,7 +176,10 @@ export class ActorsMcpServer {
|
|
|
156
176
|
writable: true,
|
|
157
177
|
value: void 0
|
|
158
178
|
});
|
|
159
|
-
/** True once mode is final
|
|
179
|
+
/** True once the server mode is final: at construction for explicit `default`/`apps`, or after
|
|
180
|
+
* the initialize handler resolves `'auto'`. Composing before this in `'auto'` mode would use
|
|
181
|
+
* the preliminary DEFAULT mode and produce the wrong (non-widget) tool variants, so composition
|
|
182
|
+
* waits for it. Distinct from {@link clientKnown}, which only withholds client-gated tools. */
|
|
160
183
|
Object.defineProperty(this, "serverModeResolved", {
|
|
161
184
|
enumerable: true,
|
|
162
185
|
configurable: true,
|
|
@@ -164,12 +187,13 @@ export class ActorsMcpServer {
|
|
|
164
187
|
value: void 0
|
|
165
188
|
});
|
|
166
189
|
/**
|
|
167
|
-
* Tool
|
|
168
|
-
* (
|
|
169
|
-
*
|
|
170
|
-
*
|
|
190
|
+
* Tool sources queued until composition is possible. Enqueued when the mode is not yet resolved
|
|
191
|
+
* (`'auto'` before initialize), and re-composed by the initialize flush — which is also when the
|
|
192
|
+
* client becomes known, so any client-gated tools withheld by an eager compose are added then.
|
|
193
|
+
* We capture the exact actor-tool slice fetched for each request so the flush composes every
|
|
194
|
+
* entry against *its own* actor list rather than the accumulated union across unrelated requests.
|
|
171
195
|
*/
|
|
172
|
-
Object.defineProperty(this, "
|
|
196
|
+
Object.defineProperty(this, "pendingToolsUntilClientKnown", {
|
|
173
197
|
enumerable: true,
|
|
174
198
|
configurable: true,
|
|
175
199
|
writable: true,
|
|
@@ -314,6 +338,8 @@ export class ActorsMcpServer {
|
|
|
314
338
|
}
|
|
315
339
|
this.serverModeResolved = true;
|
|
316
340
|
}
|
|
341
|
+
// Setting this makes `clientKnown` true, so the queued compose below (and any later
|
|
342
|
+
// load) resolves the tool set for this client and applies the per-client blocklist.
|
|
317
343
|
this.options.initializeRequestData = request;
|
|
318
344
|
log.info('Resolved server mode for client capabilities', {
|
|
319
345
|
serverMode: this.serverMode,
|
|
@@ -321,23 +347,74 @@ export class ActorsMcpServer {
|
|
|
321
347
|
clientSupportsUi: this.clientSupportsUi,
|
|
322
348
|
capabilities: (_a = request === null || request === void 0 ? void 0 : request.params) === null || _a === void 0 ? void 0 : _a.capabilities,
|
|
323
349
|
});
|
|
324
|
-
this.
|
|
350
|
+
this.composePendingToolsForClient();
|
|
325
351
|
await this.resolveWidgets();
|
|
326
352
|
const result = await sdkInitHandler(request);
|
|
327
|
-
|
|
353
|
+
// Tools are final here (composePendingToolsForClient ran above, applying the per-client
|
|
354
|
+
// blocklist), so tool presence is the ground truth for whether to advertise
|
|
355
|
+
// report-problem in the instructions.
|
|
356
|
+
result.instructions = getServerInstructions(this.serverMode, this.tools.has(HELPER_TOOLS.PROBLEM_REPORT));
|
|
328
357
|
return result;
|
|
329
358
|
});
|
|
330
359
|
}
|
|
331
|
-
|
|
332
|
-
|
|
360
|
+
/** True once the connecting client is known (set in the initialize handler, or hydrated by a
|
|
361
|
+
* recovery path). Only client-gated tools wait for this so the per-client blocklist can be
|
|
362
|
+
* applied; client-agnostic tools compose regardless. */
|
|
363
|
+
get clientKnown() {
|
|
364
|
+
return this.options.initializeRequestData != null;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Compose the tool list for the current connection: resolve mode-specific tools, then drop
|
|
368
|
+
* report-problem unless it is currently servable (see {@link isReportProblemServable}).
|
|
369
|
+
* report-problem is a default-injected tool (via tools_loader) rather than a category member,
|
|
370
|
+
* gated here by servability. Every other tool composes eagerly, so a recovery/rehydration load
|
|
371
|
+
* without an initialize still restores them. report-problem is withheld until the client is known
|
|
372
|
+
* and re-added by the initialize flush. Used by every input-driven load path and the flush.
|
|
373
|
+
* (loadActorsAsTools upserts actor tools directly; actor tools are never gated, so they need no
|
|
374
|
+
* filtering.)
|
|
375
|
+
*/
|
|
376
|
+
composeToolsForClient(input, actorTools) {
|
|
377
|
+
const tools = getToolsForServerMode(input, actorTools, this.serverMode);
|
|
378
|
+
if (this.isReportProblemServable())
|
|
379
|
+
return tools;
|
|
380
|
+
return tools.filter((tool) => tool.name !== HELPER_TOOLS.PROBLEM_REPORT);
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Whether report-problem may be served on this connection right now:
|
|
384
|
+
* - Its only function is forwarding submissions via telemetry, so it is never servable when
|
|
385
|
+
* telemetry is disabled (it would just fake an acknowledgement into the void).
|
|
386
|
+
* - It cannot be judged until the connecting client is known, so it is withheld until then;
|
|
387
|
+
* the initialize flush re-composes and adds it if the client allows.
|
|
388
|
+
* Every other tool is unconditionally servable, so recovery loads compose them eagerly and they
|
|
389
|
+
* survive a load that never sees an initialize.
|
|
390
|
+
*/
|
|
391
|
+
isReportProblemServable() {
|
|
392
|
+
return (!!this.telemetryEnabled &&
|
|
393
|
+
this.clientKnown &&
|
|
394
|
+
!isReportProblemBlockedForClient(this.options.initializeRequestData));
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Append the report-problem nudge to a failed tool result. Thin wrapper over
|
|
398
|
+
* {@link appendReportProblemNudge} that fills in whether report-problem is served on this
|
|
399
|
+
* connection; suppression by category/402 is handled inside the helper.
|
|
400
|
+
*/
|
|
401
|
+
withReportProblemNudge(result, failingToolName, failureCategory, failureHttpStatus) {
|
|
402
|
+
return appendReportProblemNudge(result, {
|
|
403
|
+
failingToolName,
|
|
404
|
+
available: this.tools.has(HELPER_TOOLS.PROBLEM_REPORT),
|
|
405
|
+
failureCategory,
|
|
406
|
+
failureHttpStatus,
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
composePendingToolsForClient() {
|
|
410
|
+
if (this.pendingToolsUntilClientKnown.length === 0)
|
|
333
411
|
return;
|
|
334
|
-
const tools = this.
|
|
335
|
-
this.
|
|
336
|
-
// Notify after the flush so shared-state handlers (e.g. Redis recovery) see
|
|
337
|
-
//
|
|
338
|
-
//
|
|
339
|
-
//
|
|
340
|
-
// don't notify at all — this call reconciles both paths to the complete set.
|
|
412
|
+
const tools = this.pendingToolsUntilClientKnown.flatMap(({ input, actorTools }) => this.composeToolsForClient(input, actorTools));
|
|
413
|
+
this.pendingToolsUntilClientKnown = [];
|
|
414
|
+
// Notify after the flush so shared-state handlers (e.g. Redis recovery) see the final tool
|
|
415
|
+
// set. Load paths already upserted the client-agnostic tools pre-init; re-upserting is
|
|
416
|
+
// idempotent, and this pass adds the client-gated tools (e.g. report-problem) now that the
|
|
417
|
+
// client is known, reconciling shared state to the complete set.
|
|
341
418
|
if (tools.length > 0)
|
|
342
419
|
this.upsertTools(tools, true);
|
|
343
420
|
}
|
|
@@ -409,28 +486,29 @@ export class ActorsMcpServer {
|
|
|
409
486
|
return [...this.listInternalToolNames(), ...this.listActorToolNames(), ...this.listActorMcpServerToolIds()];
|
|
410
487
|
}
|
|
411
488
|
/**
|
|
412
|
-
* Buffer-or-compose gate shared by the actor-tools loaders
|
|
413
|
-
*
|
|
414
|
-
* (if non-empty) upsert
|
|
415
|
-
* compose the
|
|
416
|
-
*
|
|
489
|
+
* Buffer-or-compose gate shared by the actor-tools loaders. If the server mode isn't resolved
|
|
490
|
+
* yet ('auto' before initialize), queue the whole source for `composePendingToolsForClient` and
|
|
491
|
+
* (if non-empty) upsert the mode-agnostic actor tools immediately with the given `shouldNotify`.
|
|
492
|
+
* Once the mode is resolved, compose the client-specific set via `composeToolsForClient` (which
|
|
493
|
+
* withholds report-problem until the client is known) and upsert it; if the client still isn't
|
|
494
|
+
* known, queue the source so the initialize flush re-composes and adds the client-gated tools.
|
|
417
495
|
*
|
|
418
|
-
* Callers pass different `shouldNotify` values: `loadToolsByName` forwards
|
|
419
|
-
*
|
|
420
|
-
*
|
|
421
|
-
* post-initialize reconcile. See `updateToolsAfterServerModeResolved` for the full
|
|
422
|
-
* rationale.
|
|
496
|
+
* Callers pass different `shouldNotify` values: `loadToolsByName` forwards `actorTools.length > 0`
|
|
497
|
+
* (notify only when actor tools were fetched), while `loadToolsFromUrl` and `loadToolsFromInput`
|
|
498
|
+
* pass `false` and defer to the post-initialize reconcile. See `composePendingToolsForClient`.
|
|
423
499
|
*/
|
|
424
500
|
registerFetchedActorTools(input, actorTools, shouldNotify) {
|
|
425
501
|
if (!this.serverModeResolved) {
|
|
426
|
-
this.
|
|
502
|
+
this.pendingToolsUntilClientKnown.push({ input, actorTools });
|
|
427
503
|
if (actorTools.length > 0)
|
|
428
504
|
this.upsertTools(actorTools, shouldNotify);
|
|
429
505
|
return;
|
|
430
506
|
}
|
|
431
|
-
const tools =
|
|
507
|
+
const tools = this.composeToolsForClient(input, actorTools);
|
|
432
508
|
if (tools.length > 0)
|
|
433
509
|
this.upsertTools(tools, shouldNotify);
|
|
510
|
+
if (!this.clientKnown)
|
|
511
|
+
this.pendingToolsUntilClientKnown.push({ input, actorTools });
|
|
434
512
|
}
|
|
435
513
|
/**
|
|
436
514
|
* Loads missing toolNames from a provided list of tool names.
|
|
@@ -477,9 +555,10 @@ export class ActorsMcpServer {
|
|
|
477
555
|
this.registerFetchedActorTools(input, actorTools, false);
|
|
478
556
|
}
|
|
479
557
|
/**
|
|
480
|
-
* Two-phase: getActors (async,
|
|
481
|
-
* (sync
|
|
482
|
-
* the
|
|
558
|
+
* Two-phase: getActors (async, client-agnostic Apify fetch) then composeToolsForClient
|
|
559
|
+
* (sync compose + servability filter). If the mode isn't resolved yet ('auto' before initialize)
|
|
560
|
+
* the whole source is queued for the flush. Otherwise tools compose immediately; client-gated
|
|
561
|
+
* tools are withheld until the client is known, and the source is queued so the flush adds them.
|
|
483
562
|
*
|
|
484
563
|
* Don't move the getActors await into the initialize handler — clients time out
|
|
485
564
|
* waiting for InitializeResult. The queue buffers already-fetched data, not network
|
|
@@ -514,6 +593,9 @@ export class ActorsMcpServer {
|
|
|
514
593
|
* @returns Array of added/updated tool wrappers
|
|
515
594
|
*/
|
|
516
595
|
upsertTools(tools, shouldNotifyToolsChangedHandler = false) {
|
|
596
|
+
// Client gating (e.g. hiding report-problem from Anthropic surfaces) is applied earlier, in
|
|
597
|
+
// composeToolsForClient — the single compose choke point where the client is known. Do not
|
|
598
|
+
// filter here: this is a low-level commit point reached before the client is known too.
|
|
517
599
|
for (const tool of tools) {
|
|
518
600
|
const stored = this.options.paymentProvider ? this.options.paymentProvider.decorateToolSchema(tool) : tool;
|
|
519
601
|
this.tools.set(stored.name, stored);
|
|
@@ -765,11 +847,19 @@ export class ActorsMcpServer {
|
|
|
765
847
|
let callDiagnostics = {};
|
|
766
848
|
let shouldTrackTelemetry = true;
|
|
767
849
|
let resolvedToolName = name;
|
|
850
|
+
// Set only on the pre-flight task path — the one task-mode flow whose telemetry rides
|
|
851
|
+
// this handler's `finally` — so its `Tool call completed` log line keeps the taskId the
|
|
852
|
+
// async path logs via finishTaskTracking.
|
|
853
|
+
let preflightTaskId;
|
|
768
854
|
// Captured by `captureResult` so the `finally` block can measure response size for telemetry.
|
|
769
855
|
let toolResult = null;
|
|
770
856
|
const captureResult = (r) => {
|
|
771
|
-
|
|
772
|
-
|
|
857
|
+
// On a failed result, nudge the agent to report the blocker via report-problem at the
|
|
858
|
+
// moment it decides what to do next. Gated on the tool actually being served (see
|
|
859
|
+
// isReportProblemServable), so clients where it is blocklisted or telemetry is off never see it.
|
|
860
|
+
const augmented = this.withReportProblemNudge(r, resolvedToolName, callDiagnostics.failure_category, callDiagnostics.failure_http_status);
|
|
861
|
+
toolResult = augmented;
|
|
862
|
+
return augmented;
|
|
773
863
|
};
|
|
774
864
|
const failInvalidParams = async (message, details, logFields) => {
|
|
775
865
|
toolStatus = TOOL_STATUS.SOFT_FAIL;
|
|
@@ -911,6 +1001,50 @@ export class ActorsMcpServer {
|
|
|
911
1001
|
toolName: tool.name,
|
|
912
1002
|
mcpSessionId,
|
|
913
1003
|
});
|
|
1004
|
+
// Pre-flight failure is already known — the outcome needs no work. Resolve the
|
|
1005
|
+
// task synchronously: store the failure as the terminal `completed` result and emit
|
|
1006
|
+
// exactly one `completed` status notification (no `updateTaskStatus('working')`, so no
|
|
1007
|
+
// `working` notification). Standby rejection wins over the generic payment-required
|
|
1008
|
+
// short-circuit, matching the sync path's precedence. Telemetry rides the handler's
|
|
1009
|
+
// outer `finally` (shouldTrackTelemetry stays true), firing once with the sync-path
|
|
1010
|
+
// properties plus the taskId on the log line.
|
|
1011
|
+
const preflightResult = standbyRejection !== null && standbyRejection !== void 0 ? standbyRejection : paymentRequiredResult;
|
|
1012
|
+
if (preflightResult) {
|
|
1013
|
+
const outcome = buildPreflightFailureOutcome(standbyRejection, paymentRequiredResult, actorName, actorId);
|
|
1014
|
+
toolStatus = outcome.toolStatus;
|
|
1015
|
+
callDiagnostics = outcome.callDiagnostics;
|
|
1016
|
+
preflightTaskId = task.taskId;
|
|
1017
|
+
try {
|
|
1018
|
+
await storeTaskResultOrSkipIfExpired(this.taskStore, tool.name, task.taskId, 'completed', outcome.result, mcpSessionId);
|
|
1019
|
+
}
|
|
1020
|
+
catch (error) {
|
|
1021
|
+
// A store failure (not expiry) would otherwise fall through to the generic
|
|
1022
|
+
// catch and return a task-less tool result the client rejects as a
|
|
1023
|
+
// CreateTaskResult parse error; surface it as a protocol error instead.
|
|
1024
|
+
// The pre-flight outcome left toolStatus=SOFT_FAIL, but a genuine store
|
|
1025
|
+
// outage is a hard failure — correct it before throwing so the handler
|
|
1026
|
+
// `finally` logs FAILED/INTERNAL_ERROR (the outer McpError re-throw preserves
|
|
1027
|
+
// these), not the stale pre-flight SOFT_FAIL.
|
|
1028
|
+
toolStatus = TOOL_STATUS.FAILED;
|
|
1029
|
+
callDiagnostics = {
|
|
1030
|
+
failure_category: FAILURE_CATEGORY.INTERNAL_ERROR,
|
|
1031
|
+
...buildActorFields(actorName, actorId),
|
|
1032
|
+
};
|
|
1033
|
+
throw new McpError(ErrorCode.InternalError, `Failed to store the pre-flight result for task "${task.taskId}": ${error instanceof Error ? error.message : String(error)}`);
|
|
1034
|
+
}
|
|
1035
|
+
// Defer so the client sees the CreateTaskResult (and learns the taskId) before
|
|
1036
|
+
// the terminal status notification — the async path's post-response ordering.
|
|
1037
|
+
// emitTaskStatusNotification never throws and no-ops if the task expired.
|
|
1038
|
+
setImmediate(() => {
|
|
1039
|
+
void emitTaskStatusNotification(task.taskId, mcpSessionId, this.taskStore, this.server);
|
|
1040
|
+
});
|
|
1041
|
+
captureResult(outcome.result);
|
|
1042
|
+
// createTask returned status `working`; synthesize the terminal status instead of
|
|
1043
|
+
// re-fetching — if the task expired before the result store (the one case
|
|
1044
|
+
// storeTaskResultOrSkipIfExpired tolerates), a re-fetch would come back empty and a
|
|
1045
|
+
// `working` fallback would contradict the tasks/get 404 the client sees next.
|
|
1046
|
+
return { task: { ...task, status: 'completed' } };
|
|
1047
|
+
}
|
|
914
1048
|
// Execute the tool asynchronously and update task status
|
|
915
1049
|
setImmediate(() => {
|
|
916
1050
|
this.executeToolAndUpdateTask({
|
|
@@ -918,8 +1052,6 @@ export class ActorsMcpServer {
|
|
|
918
1052
|
tool,
|
|
919
1053
|
toolArgs: toolArgs,
|
|
920
1054
|
logSafeArgs,
|
|
921
|
-
standbyRejection,
|
|
922
|
-
paymentRequiredResult,
|
|
923
1055
|
apifyClient: apifyClient,
|
|
924
1056
|
apifyToken,
|
|
925
1057
|
progressToken,
|
|
@@ -936,25 +1068,14 @@ export class ActorsMcpServer {
|
|
|
936
1068
|
shouldTrackTelemetry = false;
|
|
937
1069
|
return { task };
|
|
938
1070
|
}
|
|
939
|
-
// Sync path:
|
|
940
|
-
//
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
return captureResult(standbyRejection);
|
|
948
|
-
}
|
|
949
|
-
// Check payment validation (already computed by prepareToolCallContext)
|
|
950
|
-
if (paymentRequiredResult) {
|
|
951
|
-
toolStatus = TOOL_STATUS.SOFT_FAIL;
|
|
952
|
-
callDiagnostics = {
|
|
953
|
-
failure_category: FAILURE_CATEGORY.INVALID_INPUT,
|
|
954
|
-
failure_http_status: 402,
|
|
955
|
-
...buildActorFields(actorName, actorId),
|
|
956
|
-
};
|
|
957
|
-
return captureResult(paymentRequiredResult);
|
|
1071
|
+
// Sync path: short-circuit on either pre-flight failure. buildPreflightFailureOutcome
|
|
1072
|
+
// encodes the precedence — standby rejection wins over the generic payment-required
|
|
1073
|
+
// 402, so the agent gets the precise reason.
|
|
1074
|
+
if (standbyRejection || paymentRequiredResult) {
|
|
1075
|
+
const outcome = buildPreflightFailureOutcome(standbyRejection, paymentRequiredResult, actorName, actorId);
|
|
1076
|
+
toolStatus = outcome.toolStatus;
|
|
1077
|
+
callDiagnostics = outcome.callDiagnostics;
|
|
1078
|
+
return captureResult(outcome.result);
|
|
958
1079
|
}
|
|
959
1080
|
// Handle internal tool
|
|
960
1081
|
if (tool.type === TOOL_TYPE.INTERNAL) {
|
|
@@ -977,9 +1098,7 @@ export class ActorsMcpServer {
|
|
|
977
1098
|
mcpSessionId,
|
|
978
1099
|
}));
|
|
979
1100
|
// Extract diagnostics and strip internal fields from res before returning to client.
|
|
980
|
-
|
|
981
|
-
toolStatus = diag.toolStatus;
|
|
982
|
-
callDiagnostics = { ...callDiagnostics, ...diag.callDiagnostics };
|
|
1101
|
+
({ toolStatus, callDiagnostics } = applyToolTelemetry(res, actorName, actorId, callDiagnostics));
|
|
983
1102
|
return captureResult(res);
|
|
984
1103
|
}
|
|
985
1104
|
finally {
|
|
@@ -1090,6 +1209,10 @@ export class ActorsMcpServer {
|
|
|
1090
1209
|
// https://modelcontextprotocol.io/specification/2025-06-18/basic/utilities/cancellation#behavior-requirements
|
|
1091
1210
|
return captureResult({});
|
|
1092
1211
|
}
|
|
1212
|
+
// Mirror the INTERNAL branch: read the telemetry the executor embedded on error
|
|
1213
|
+
// results (e.g. respondUserError → SOFT_FAIL/INVALID_INPUT), strip it from the wire,
|
|
1214
|
+
// and set failure_category so the report-problem nudge picks the softer variant.
|
|
1215
|
+
({ toolStatus, callDiagnostics } = applyToolTelemetry(executorResult, actorName, actorId, callDiagnostics));
|
|
1093
1216
|
return captureResult(executorResult);
|
|
1094
1217
|
}
|
|
1095
1218
|
finally {
|
|
@@ -1179,6 +1302,7 @@ export class ActorsMcpServer {
|
|
|
1179
1302
|
callDiagnostics,
|
|
1180
1303
|
args,
|
|
1181
1304
|
result: toolResult,
|
|
1305
|
+
taskId: preflightTaskId,
|
|
1182
1306
|
});
|
|
1183
1307
|
}
|
|
1184
1308
|
}
|
|
@@ -1236,6 +1360,13 @@ export class ActorsMcpServer {
|
|
|
1236
1360
|
...deriveResourceIds(params.args, params.result),
|
|
1237
1361
|
};
|
|
1238
1362
|
trackToolCall(params.userId, this.telemetryEnv, finalizedTelemetryData);
|
|
1363
|
+
// A successful report-problem call also emits a dedicated feedback event carrying the
|
|
1364
|
+
// submission. A downstream Segment destination fans it out to Slack/GitHub.
|
|
1365
|
+
if (params.toolName === HELPER_TOOLS.PROBLEM_REPORT &&
|
|
1366
|
+
params.toolStatus === TOOL_STATUS.SUCCEEDED &&
|
|
1367
|
+
params.args) {
|
|
1368
|
+
trackReportedProblem(params.userId, this.telemetryEnv, buildReportedProblemProperties(finalizedTelemetryData, params.args));
|
|
1369
|
+
}
|
|
1239
1370
|
}
|
|
1240
1371
|
}
|
|
1241
1372
|
// TODO: this function quite duplicates the main tool call login the CallToolRequestSchema handler, we should refactor
|
|
@@ -1252,7 +1383,7 @@ export class ActorsMcpServer {
|
|
|
1252
1383
|
* @param params.mcpSessionId - MCP session ID for telemetry
|
|
1253
1384
|
*/
|
|
1254
1385
|
async executeToolAndUpdateTask(params) {
|
|
1255
|
-
const { taskId, tool, toolArgs, logSafeArgs,
|
|
1386
|
+
const { taskId, tool, toolArgs, logSafeArgs, apifyClient, apifyToken, progressToken, extra, mcpSessionId, actorName, actorId, } = params;
|
|
1256
1387
|
let toolStatus = TOOL_STATUS.SUCCEEDED;
|
|
1257
1388
|
// Always populate actor fields so they're tracked on both success and failure paths.
|
|
1258
1389
|
let callDiagnostics = { ...buildActorFields(actorName, actorId) };
|
|
@@ -1324,25 +1455,6 @@ export class ActorsMcpServer {
|
|
|
1324
1455
|
await emitTaskStatusNotification(taskId, mcpSessionId, this.taskStore, this.server);
|
|
1325
1456
|
// Execute the tool and get the result
|
|
1326
1457
|
let result = {};
|
|
1327
|
-
// Mirror the sync-path ordering: standby rejection wins over the generic 402 so the
|
|
1328
|
-
// stored task result carries the precise reason the agent should act on.
|
|
1329
|
-
if (standbyRejection) {
|
|
1330
|
-
toolStatus = TOOL_STATUS.SOFT_FAIL;
|
|
1331
|
-
callDiagnostics = {
|
|
1332
|
-
failure_category: FAILURE_CATEGORY.INVALID_INPUT,
|
|
1333
|
-
...buildActorFields(actorName, actorId),
|
|
1334
|
-
};
|
|
1335
|
-
result = standbyRejection;
|
|
1336
|
-
}
|
|
1337
|
-
else if (paymentRequiredResult) {
|
|
1338
|
-
toolStatus = TOOL_STATUS.SOFT_FAIL;
|
|
1339
|
-
callDiagnostics = {
|
|
1340
|
-
failure_category: FAILURE_CATEGORY.INVALID_INPUT,
|
|
1341
|
-
failure_http_status: 402,
|
|
1342
|
-
...buildActorFields(actorName, actorId),
|
|
1343
|
-
};
|
|
1344
|
-
result = paymentRequiredResult;
|
|
1345
|
-
}
|
|
1346
1458
|
// Callback to propagate Actor run statusMessage into the task store and emit a push notification.
|
|
1347
1459
|
// TODO(TC-3): cancel arriving while this is scheduled throws cancelled → working;
|
|
1348
1460
|
// currently swallowed by progress.ts's tick catch — guard or catch explicitly.
|
|
@@ -1371,9 +1483,7 @@ export class ActorsMcpServer {
|
|
|
1371
1483
|
mcpSessionId,
|
|
1372
1484
|
taskMode: true,
|
|
1373
1485
|
}));
|
|
1374
|
-
|
|
1375
|
-
toolStatus = diag.toolStatus;
|
|
1376
|
-
callDiagnostics = { ...callDiagnostics, ...diag.callDiagnostics };
|
|
1486
|
+
({ toolStatus, callDiagnostics } = applyToolTelemetry(res, actorName, actorId, callDiagnostics));
|
|
1377
1487
|
result = res;
|
|
1378
1488
|
}
|
|
1379
1489
|
finally {
|
|
@@ -1411,6 +1521,9 @@ export class ActorsMcpServer {
|
|
|
1411
1521
|
result = {};
|
|
1412
1522
|
}
|
|
1413
1523
|
else {
|
|
1524
|
+
// Mirror the INTERNAL task branch: extract embedded telemetry so failure_category
|
|
1525
|
+
// drives the success-path nudge below and toolTelemetry is stripped from the wire.
|
|
1526
|
+
({ toolStatus, callDiagnostics } = applyToolTelemetry(executorResult, actorName, actorId, callDiagnostics));
|
|
1414
1527
|
result = executorResult;
|
|
1415
1528
|
}
|
|
1416
1529
|
}
|
|
@@ -1423,6 +1536,9 @@ export class ActorsMcpServer {
|
|
|
1423
1536
|
// Check if task was cancelled before storing result
|
|
1424
1537
|
if (await skipIfTaskCancelled(', skipping result storage', toolStatus))
|
|
1425
1538
|
return;
|
|
1539
|
+
// On a failed result, nudge the agent to report the blocker via report-problem (mirrors the
|
|
1540
|
+
// synchronous CallTool path, which task-mode calls like call-actor bypass).
|
|
1541
|
+
result = this.withReportProblemNudge(result, tool.name, callDiagnostics.failure_category, callDiagnostics.failure_http_status);
|
|
1426
1542
|
// Store the result in the task store
|
|
1427
1543
|
log.debug('[executeToolAndUpdateTask] Storing completed result', {
|
|
1428
1544
|
taskId,
|
|
@@ -1535,7 +1651,11 @@ export class ActorsMcpServer {
|
|
|
1535
1651
|
taskId,
|
|
1536
1652
|
mcpSessionId,
|
|
1537
1653
|
});
|
|
1538
|
-
|
|
1654
|
+
// Nudge on a genuinely-failed task result. INTERNAL_ERROR and an unknown category get the
|
|
1655
|
+
// full nudge; a genuine INVALID_INPUT gets the softer nudge; payment (402) is suppressed via
|
|
1656
|
+
// the HTTP status and AUTH / PERMISSION_APPROVAL_REQUIRED via NON_NUDGE_FAILURE_CATEGORIES.
|
|
1657
|
+
// The 402 branch above returns before reaching here, so this only sees non-payment failures.
|
|
1658
|
+
const failedResult = this.withReportProblemNudge({
|
|
1539
1659
|
content: [
|
|
1540
1660
|
{
|
|
1541
1661
|
type: 'text',
|
|
@@ -1544,7 +1664,7 @@ export class ActorsMcpServer {
|
|
|
1544
1664
|
],
|
|
1545
1665
|
isError: true,
|
|
1546
1666
|
internalToolStatus: toolStatus,
|
|
1547
|
-
};
|
|
1667
|
+
}, tool.name, callDiagnostics.failure_category, callDiagnostics.failure_http_status);
|
|
1548
1668
|
await storeTaskResultOrSkipIfExpired(this.taskStore, tool.name, taskId, 'failed', failedResult, mcpSessionId);
|
|
1549
1669
|
await emitTaskStatusNotification(taskId, mcpSessionId, this.taskStore, this.server);
|
|
1550
1670
|
finishTaskTracking(toolStatus, callDiagnostics, failedResult);
|