forked from apify/apify-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp.server.tool_call_contracts.test.ts
More file actions
1226 lines (1146 loc) · 59.2 KB
/
Copy pathmcp.server.tool_call_contracts.test.ts
File metadata and controls
1226 lines (1146 loc) · 59.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
import type { InitializeRequest } from '@modelcontextprotocol/sdk/types.js';
import { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js';
import { afterEach, describe, expect, it, vi } from 'vitest';
import log from '@apify/log';
import { FAILURE_CATEGORY, HELPER_TOOLS, TOOL_STATUS } from '../../src/const.js';
import * as mcpClient from '../../src/mcp/client.js';
import type { McpClientContext } from '../../src/mcp/client_context.js';
import type { ActorsMcpServer } from '../../src/mcp/server.js';
import type { PaymentProvider } from '../../src/payments/types.js';
import * as telemetry from '../../src/telemetry.js';
import * as callActor from '../../src/tools/actors/call_actor.js';
import {
REPORT_PROBLEM_INVALID_INPUT_NUDGE,
REPORT_PROBLEM_NUDGE,
reportProblem,
} from '../../src/tools/dev/report_problem.js';
import type { ToolEntry, ToolInputSchema } from '../../src/types.js';
import { TOOL_TYPE } from '../../src/types.js';
import { compileSchema } from '../../src/utils/ajv.js';
import * as logging from '../../src/utils/logging.js';
import {
getLegacyServer,
getRequestHandler,
getTaskStore,
makePaymentRequiredError,
makePermissionApprovalError,
makeRecorderTool,
makeThrowingTool,
PERMISSION_HTTP_STATUS,
withServer,
X402_PAYMENT_DATA,
} from './helpers/mcp_server.js';
/**
* Pins the handler-level behavior contracts for the two catch paths (the sync
* `CallToolRequestSchema` catch and the `executeToolAndUpdateTask` catch). Both catches now share
* error classification via `buildToolCallErrorResult`; #658 will still unify the two sinks
* themselves. Failure classes are fabricated by throwing from a fake tool's `call`; both the sync
* path (result shapes) and the task path (terminal status mapping) assert the same source-of-truth
* per class.
*/
/** The wire `content` `buildPaymentRequiredResponse` produces for X402_PAYMENT_DATA. */
const X402_RESPONSE_CONTENT = [
{ type: 'text', text: JSON.stringify(X402_PAYMENT_DATA) },
{ type: 'text', text: 'Payment required to run this Actor or access this resource.' },
];
type FailureClass = {
label: string;
makeError: () => unknown;
taskStatus: 'completed' | 'failed';
/** Exact stored task-store payload — deep-equal pin, so any added/dropped key fails. */
storedResult: Record<string, unknown>;
telemetry: { tool_status: string; failure_category: string; failure_http_status?: number };
/** Failure-class keys expected on top of BASE_TELEMETRY_KEYS (exact-key-set pin). */
telemetryExtraKeys: string[];
};
const FAILURE_CLASSES: FailureClass[] = [
{
label: '402 payment-required',
makeError: () => makePaymentRequiredError(X402_PAYMENT_DATA),
taskStatus: 'completed',
storedResult: { content: X402_RESPONSE_CONTENT, isError: true, structuredContent: X402_PAYMENT_DATA },
telemetry: {
tool_status: TOOL_STATUS.SOFT_FAIL,
failure_category: FAILURE_CATEGORY.INVALID_INPUT,
failure_http_status: 402,
},
telemetryExtraKeys: ['failure_category', 'failure_http_status'],
},
{
label: 'permission-approval',
makeError: makePermissionApprovalError,
taskStatus: 'completed',
storedResult: { content: [{ type: 'text', text: 'needs approval' }], isError: true },
telemetry: {
tool_status: TOOL_STATUS.SOFT_FAIL,
failure_category: FAILURE_CATEGORY.PERMISSION_APPROVAL_REQUIRED,
failure_http_status: PERMISSION_HTTP_STATUS,
},
telemetryExtraKeys: ['failure_category', 'failure_http_status'],
},
{
label: 'generic execution error',
makeError: () => new Error('boom'),
taskStatus: 'failed',
storedResult: {
content: [
{
type: 'text',
text: 'Error calling tool "test-throwing-tool": boom. Verify the tool name and input parameters.',
},
],
isError: true,
internalToolStatus: TOOL_STATUS.FAILED,
},
telemetry: {
tool_status: TOOL_STATUS.FAILED,
failure_category: FAILURE_CATEGORY.INTERNAL_ERROR,
},
telemetryExtraKeys: ['failure_category', 'failure_detail'],
},
];
/**
* Keys `prepareTelemetryData` + the handler `finally` finalization put on every tool-call Segment
* event, sync and task paths alike — pinned empirically. This suite is the only CI guard for these
* shapes (dashboards consume them; the internal repo does not), so the pin is an exact key set: a
* key appearing or disappearing must be a conscious edit here.
*/
const BASE_TELEMETRY_KEYS = [
'app',
'app_version',
'mcp_client_capabilities',
'mcp_client_name',
'mcp_client_version',
'mcp_protocol_version',
'mcp_session_id',
'tool_exec_time_ms',
'tool_name',
'tool_response_content_bytes',
'tool_response_file_bytes',
'tool_response_structured_content_bytes',
'tool_status',
'transport_type',
];
const CLIENT_CAPABILITIES = {
roots: { listChanged: true },
experimental: { feature: { enabled: true } },
};
const INITIALIZE_REQUEST = {
method: 'initialize',
params: {
protocolVersion: '2025-06-18',
clientInfo: { name: 'context-client', version: '1.2.3' },
capabilities: CLIENT_CAPABILITIES,
},
} as InitializeRequest;
function expectClientContextTelemetry(properties: Record<string, unknown>): void {
expect(properties).toMatchObject({
mcp_client_name: 'context-client',
mcp_client_version: '1.2.3',
mcp_protocol_version: '2025-06-18',
mcp_client_capabilities: CLIENT_CAPABILITIES,
});
}
/** Silence the error-path logging the failure branches emit, keeping test output clean. */
function silenceLogs(): void {
vi.spyOn(log, 'error').mockImplementation(() => log);
vi.spyOn(log, 'exception').mockImplementation(() => log);
vi.spyOn(log, 'softFail').mockImplementation(() => log);
vi.spyOn(log, 'warning').mockImplementation(() => log);
}
/** An ACTOR_MCP tool with `taskSupport` forced so it clears the pre-dispatch gate (see the gap test). */
function makeActorMcpTool(): ToolEntry {
return {
type: TOOL_TYPE.ACTOR_MCP,
name: 'test-actor-mcp-tool',
description: 'actor-mcp',
inputSchema: { type: 'object', properties: {} } as ToolInputSchema,
ajvValidate: compileSchema({ type: 'object', properties: {} }),
originToolName: 'origin-tool',
actorId: 'test/actor',
serverId: 'server-id',
serverUrl: 'https://example.invalid/mcp',
execution: { taskSupport: 'optional' },
} as ToolEntry;
}
async function runSync(server: ActorsMcpServer, tool: ToolEntry): Promise<Record<string, unknown>> {
server.upsertTools([tool]);
const handler = getRequestHandler(server, 'tools/call');
return handler(
{ method: 'tools/call', params: { name: tool.name, arguments: {}, _meta: { mcpSessionId: 's1' } } },
{ signal: { aborted: false }, sendNotification: vi.fn() },
);
}
const TERMINAL_STATUSES = new Set(['completed', 'failed', 'cancelled']);
/** Asserts the telemetry `properties` shape `trackToolCall` received for a failure class. */
function expectFailureClassTelemetry(
trackSpy: { mock: { calls: [unknown, unknown, Record<string, unknown>][] } },
fc: FailureClass,
): void {
expect(trackSpy.mock.calls).toHaveLength(1);
const properties = trackSpy.mock.calls[0][2];
expect(Object.keys(properties).sort()).toEqual([...BASE_TELEMETRY_KEYS, ...fc.telemetryExtraKeys].sort());
expect(properties.tool_status).toBe(fc.telemetry.tool_status);
expect(properties.failure_category).toBe(fc.telemetry.failure_category);
if (fc.telemetry.failure_http_status === undefined) {
expect(properties).not.toHaveProperty('failure_http_status');
} else {
expect(properties.failure_http_status).toBe(fc.telemetry.failure_http_status);
}
}
/** Drives a task-mode call, waits for terminal status, and reads back the stored task + result. */
async function runTaskAndReadBack(server: ActorsMcpServer, tool: ToolEntry) {
server.upsertTools([tool]);
const handler = getRequestHandler(server, 'tools/call');
const res = (await handler(
{
method: 'tools/call',
params: { name: tool.name, arguments: {}, _meta: { mcpSessionId: 's1' }, task: { ttl: 60_000 } },
},
{ signal: { aborted: false }, sendNotification: vi.fn() },
)) as { task: { taskId: string } };
const task = await vi.waitFor(async () => {
const current = await getTaskStore(server).getTask(res.task.taskId);
if (!current || !TERMINAL_STATUSES.has(current.status)) {
throw new Error(`Task ${res.task.taskId} did not reach a terminal status`);
}
return current;
});
const result = await getTaskStore(server).getTaskResult(res.task.taskId);
return { task, result: result as Record<string, unknown> };
}
describe('CallToolRequestSchema handler', () => {
afterEach(() => vi.restoreAllMocks());
describe('sync failure result shapes', () => {
it('returns the x402 payment-required response shape for a 402 failure', async () => {
await withServer(async (server) => {
silenceLogs();
const result = await runSync(
server,
makeThrowingTool({ error: makePaymentRequiredError(X402_PAYMENT_DATA) }),
);
expect(result.isError).toBe(true);
// Per the x402 MCP transport spec the payload rides both structuredContent and
// content[0].text as JSON — payment clients parse these; pin both exactly.
expect(result.content).toEqual(X402_RESPONSE_CONTENT);
expect(result.structuredContent).toEqual(X402_PAYMENT_DATA);
// Server-internal telemetry/status must not leak onto the wire.
expect(result.toolTelemetry).toBeUndefined();
expect('internalToolStatus' in result).toBe(false);
});
});
it('returns the message-only payment-required response for a 402 without payment data', async () => {
await withServer(async (server) => {
silenceLogs();
const result = await runSync(server, makeThrowingTool({ error: makePaymentRequiredError() }));
expect(result.isError).toBe(true);
expect(result.content).toEqual([{ type: 'text', text: 'Payment required' }]);
expect(result.structuredContent).toBeUndefined();
expect(result.toolTelemetry).toBeUndefined();
expect('internalToolStatus' in result).toBe(false);
});
});
it('returns the permission-approval response shape for a permission-approval failure', async () => {
await withServer(async (server) => {
silenceLogs();
const result = await runSync(server, makeThrowingTool({ error: makePermissionApprovalError() }));
expect(result.isError).toBe(true);
expect(result.content).toEqual([{ type: 'text', text: 'needs approval' }]);
expect(result.toolTelemetry).toBeUndefined();
expect('internalToolStatus' in result).toBe(false);
});
});
});
it('rejects a task-augmented call to a tool without taskSupport before dispatch', async () => {
await withServer(async (server) => {
silenceLogs();
const { tool, received } = makeRecorderTool('no-task-support-tool');
server.upsertTools([tool]);
// failInvalidParams awaits sendLoggingMessage before throwing McpError; the harness has
// no transport (notification would throw "Not connected"), so stub it to observe the
// real InvalidParams rejection.
vi.spyOn(getLegacyServer(server), 'sendLoggingMessage').mockResolvedValue(undefined);
const handler = getRequestHandler(server, 'tools/call');
await expect(
handler(
{
method: 'tools/call',
params: {
name: 'no-task-support-tool',
arguments: {},
_meta: { mcpSessionId: 's1' },
task: { ttl: 60_000 },
},
},
{ signal: { aborted: false }, sendNotification: vi.fn() },
),
).rejects.toMatchObject({ code: ErrorCode.InvalidParams });
expect(received.called).toBe(false);
});
});
it('rejects arguments failing the input schema as an InvalidParams protocol error', async () => {
// Pins the deliberate divergence from SDK 1.29 defaults: validation failures surface as
// McpError protocol errors, never as isError tool results. The taskSupport-gate test above
// covers the other failInvalidParams instance; this one covers AJV validation.
await withServer(async (server) => {
silenceLogs();
const { tool, received } = makeRecorderTool('strict-schema-tool');
const schema = { type: 'object', properties: { x: { type: 'string' } }, required: ['x'] };
tool.inputSchema = schema as ToolInputSchema;
tool.ajvValidate = compileSchema(schema);
server.upsertTools([tool]);
vi.spyOn(getLegacyServer(server), 'sendLoggingMessage').mockResolvedValue(undefined);
const handler = getRequestHandler(server, 'tools/call');
await expect(
handler(
{
method: 'tools/call',
params: { name: 'strict-schema-tool', arguments: {}, _meta: { mcpSessionId: 's1' } },
},
{ signal: { aborted: false }, sendNotification: vi.fn() },
),
).rejects.toMatchObject({ code: ErrorCode.InvalidParams });
expect(received.called).toBe(false);
});
});
describe('tool-call telemetry properties per failure class', () => {
// Telemetry on: no token + allowUnauthMode so prepareTelemetryData skips the userInfo network
// call (userId null) while trackToolCall still fires. Assert the properties shape per class.
for (const fc of FAILURE_CLASSES) {
it(`emits telemetry properties for a ${fc.label} failure`, async () => {
const trackSpy = vi.spyOn(telemetry, 'trackToolCall').mockImplementation(() => {});
await withServer(
async (server) => {
silenceLogs();
await runSync(server, makeThrowingTool({ error: fc.makeError() }));
},
{ token: undefined, telemetry: { enabled: true }, allowUnauthMode: true },
);
expectFailureClassTelemetry(trackSpy, fc);
});
}
});
it('uses the complete client context for sync-call telemetry', async () => {
const trackSpy = vi.spyOn(telemetry, 'trackToolCall').mockImplementation(() => {});
await withServer(
async (server) => {
await runSync(server, makeRecorderTool('sync-context-tool').tool);
},
{
token: undefined,
telemetry: { enabled: true },
allowUnauthMode: true,
initializeRequestData: INITIALIZE_REQUEST,
},
);
expect(trackSpy.mock.calls).toHaveLength(1);
expectClientContextTelemetry(trackSpy.mock.calls[0][2]);
});
});
describe('CallToolRequestSchema handler — invalid-call rejections (characterization)', () => {
afterEach(() => vi.restoreAllMocks());
it('emits an error-level logging notification before rejecting an invalid tool call', async () => {
// The side-channel v1 uses to notify the client of a rejection before throwing McpError.
// Existing invalid-params tests only stub sendLoggingMessage; none asserts it fired.
await withServer(async (server) => {
silenceLogs();
const sendLogSpy = vi.spyOn(getLegacyServer(server), 'sendLoggingMessage').mockResolvedValue(undefined);
const handler = getRequestHandler(server, 'tools/call');
await expect(
handler(
{
method: 'tools/call',
params: { name: 'does-not-exist', arguments: {}, _meta: { mcpSessionId: 's1' } },
},
{ signal: { aborted: false }, sendNotification: vi.fn() },
),
).rejects.toMatchObject({ code: ErrorCode.InvalidParams });
expect(sendLogSpy).toHaveBeenCalledWith(
expect.objectContaining({ level: 'error', data: expect.stringContaining('was not found') }),
);
});
});
it('rejects a call with no Apify token as an InvalidParams protocol error', async () => {
await withServer(
async (server) => {
silenceLogs();
const { tool, received } = makeRecorderTool('auth-required-tool');
server.upsertTools([tool]);
vi.spyOn(getLegacyServer(server), 'sendLoggingMessage').mockResolvedValue(undefined);
const handler = getRequestHandler(server, 'tools/call');
await expect(
handler(
{
method: 'tools/call',
params: { name: 'auth-required-tool', arguments: {}, _meta: { mcpSessionId: 's1' } },
},
{ signal: { aborted: false }, sendNotification: vi.fn() },
),
).rejects.toMatchObject({
code: ErrorCode.InvalidParams,
message: expect.stringContaining('Apify API token is required'),
});
expect(received.called).toBe(false);
},
{ token: undefined },
);
});
it('rejects a call to an unknown tool name as an InvalidParams protocol error', async () => {
await withServer(async (server) => {
silenceLogs();
vi.spyOn(getLegacyServer(server), 'sendLoggingMessage').mockResolvedValue(undefined);
const handler = getRequestHandler(server, 'tools/call');
await expect(
handler(
{
method: 'tools/call',
params: { name: 'does-not-exist', arguments: {}, _meta: { mcpSessionId: 's1' } },
},
{ signal: { aborted: false }, sendNotification: vi.fn() },
),
).rejects.toMatchObject({
code: ErrorCode.InvalidParams,
message: expect.stringContaining('was not found'),
});
});
});
it('rejects a call with missing arguments as an InvalidParams protocol error', async () => {
await withServer(async (server) => {
silenceLogs();
const { tool, received } = makeRecorderTool('missing-args-tool');
server.upsertTools([tool]);
vi.spyOn(getLegacyServer(server), 'sendLoggingMessage').mockResolvedValue(undefined);
const handler = getRequestHandler(server, 'tools/call');
await expect(
handler(
{ method: 'tools/call', params: { name: 'missing-args-tool', _meta: { mcpSessionId: 's1' } } },
{ signal: { aborted: false }, sendNotification: vi.fn() },
),
).rejects.toMatchObject({
code: ErrorCode.InvalidParams,
message: expect.stringContaining('Missing arguments'),
});
expect(received.called).toBe(false);
});
});
it('reports ABORTED tool status when the request signal is aborted', async () => {
// A throwing tool + aborted signal exercises the EXECUTION arm's abort-preserving branch:
// toolStatus is ABORTED (not re-derived from the error) and rides the wire as toolTelemetry.
await withServer(async (server) => {
silenceLogs();
server.upsertTools([makeThrowingTool({ error: new Error('boom') })]);
const handler = getRequestHandler(server, 'tools/call');
const result = await handler(
{
method: 'tools/call',
params: { name: 'test-throwing-tool', arguments: {}, _meta: { mcpSessionId: 's1' } },
},
{ signal: { aborted: true }, sendNotification: vi.fn() },
);
expect(result.isError).toBe(true);
expect(result.toolTelemetry).toEqual({ toolStatus: TOOL_STATUS.ABORTED });
});
});
});
describe('executeToolAndUpdateTask()', () => {
afterEach(() => vi.restoreAllMocks());
describe('task terminal status per failure class', () => {
for (const fc of FAILURE_CLASSES) {
it(`stores a ${fc.label} failure with terminal status ${fc.taskStatus}`, async () => {
await withServer(async (server) => {
silenceLogs();
const tool = makeThrowingTool({ error: fc.makeError(), taskSupport: 'optional' });
const { task, result } = await runTaskAndReadBack(server, tool);
expect(task.status).toBe(fc.taskStatus);
// Exact-object pin: a payment client fetches this via tasks/result, so the
// payload must not drift, and no internal key (toolTelemetry,
// internalToolStatus on completed classes) may appear.
expect(result).toEqual(fc.storedResult);
});
});
}
});
describe('task-call telemetry properties per failure class', () => {
// Same seam and per-class expectations as the sync block: the task catch assigns
// callDiagnostics from the shared mapper's result via a flat overwrite, unlike the sync
// path's spread-merge onto a pre-existing object, so pin it separately. Telemetry fires once
// via finishTaskTracking; the emitted properties carry no task-specific key (taskId appears
// only in the log line, not in the Segment properties).
for (const fc of FAILURE_CLASSES) {
it(`emits telemetry properties for a ${fc.label} failure in task mode`, async () => {
const trackSpy = vi.spyOn(telemetry, 'trackToolCall').mockImplementation(() => {});
await withServer(
async (server) => {
silenceLogs();
const tool = makeThrowingTool({ error: fc.makeError(), taskSupport: 'optional' });
await runTaskAndReadBack(server, tool);
// The task path stores the terminal result *before* finishTaskTracking fires
// trackToolCall, so terminal status alone does not imply telemetry was emitted.
await vi.waitFor(() => {
if (trackSpy.mock.calls.length === 0) throw new Error('trackToolCall spy was not called');
});
// Let any queued duplicate fire land before the single-call assertion below.
await new Promise((resolve) => {
setImmediate(resolve);
});
await new Promise((resolve) => {
setImmediate(resolve);
});
},
{ token: undefined, telemetry: { enabled: true }, allowUnauthMode: true },
);
expectFailureClassTelemetry(trackSpy, fc);
expect(trackSpy.mock.calls[0][2]).not.toHaveProperty('taskId');
expect(trackSpy.mock.calls[0][2]).not.toHaveProperty('task_id');
});
}
});
it('uses the client context captured when the task is scheduled', async () => {
const trackSpy = vi.spyOn(telemetry, 'trackToolCall').mockImplementation(() => {});
await withServer(
async (server) => {
const { tool } = makeRecorderTool('task-context-tool', { taskSupport: 'optional' });
server.upsertTools([tool]);
const handler = getRequestHandler(server, 'tools/call');
const response = await handler(
{
method: 'tools/call',
params: {
name: tool.name,
arguments: {},
_meta: { mcpSessionId: 's1' },
task: { ttl: 60_000 },
},
},
{ signal: { aborted: false }, sendNotification: vi.fn() },
);
(server as unknown as { _clientContext: McpClientContext })._clientContext = {
protocolVersion: 'changed',
clientInfo: { name: 'changed', version: 'changed' },
capabilities: { changed: true },
};
await vi.waitFor(async () => {
const task = await getTaskStore(server).getTask((response.task as { taskId: string }).taskId);
if (task?.status !== 'completed') throw new Error('task did not complete');
if (trackSpy.mock.calls.length === 0) throw new Error('trackToolCall spy was not called');
});
},
{
token: undefined,
telemetry: { enabled: true },
allowUnauthMode: true,
initializeRequestData: INITIALIZE_REQUEST,
},
);
expect(trackSpy.mock.calls).toHaveLength(1);
expectClientContextTelemetry(trackSpy.mock.calls[0][2]);
});
it('dispatches an ACTOR_MCP tool run as a task and surfaces a connect failure as a completed error result', async () => {
// #1063 CLOSED: executeToolAndUpdateTask now dispatches ACTOR_MCP through the shared switch.
// Stub connectMCPClient to return null — deterministic, no real network attempt — so the
// ACTOR_MCP branch stores its connect-failure soft-fail result — a `completed` task carrying an
// `isError` body (matching the sync path), not the old empty {} masquerading as a success.
await withServer(async (server) => {
silenceLogs();
vi.spyOn(mcpClient, 'connectMCPClient').mockResolvedValue(null);
// The connect-failure branch logs via the transport; the harness has none, so stub it.
vi.spyOn(getLegacyServer(server), 'sendLoggingMessage').mockResolvedValue(undefined);
const { task, result } = await runTaskAndReadBack(server, makeActorMcpTool());
expect(task.status).toBe('completed');
expect(result.isError).toBe(true);
expect((result.content as { text: string }[])[0].text).toContain('Failed to connect to MCP server');
});
});
it('dispatches an ACTOR_MCP tool run as a task and stores the real proxied result', async () => {
// Gap closure, success path: with a reachable proxied tool, the task stores the real callTool
// result as `completed` (not {}), and telemetry fires once with the ACTOR_MCP-derived status.
const trackSpy = vi.spyOn(telemetry, 'trackToolCall').mockImplementation(() => {});
const proxiedResult = { content: [{ type: 'text', text: 'proxied ok' }] };
await withServer(
async (server) => {
silenceLogs();
const stubClient = {
callTool: vi.fn().mockResolvedValue(proxiedResult),
close: vi.fn().mockResolvedValue(undefined),
setNotificationHandler: vi.fn(),
} as unknown as Client;
vi.spyOn(mcpClient, 'connectMCPClient').mockResolvedValue(stubClient);
const { task, result } = await runTaskAndReadBack(server, makeActorMcpTool());
expect(task.status).toBe('completed');
expect(result).toEqual(proxiedResult);
await vi.waitFor(() => {
if (trackSpy.mock.calls.length === 0) throw new Error('trackToolCall spy was not called');
});
},
{ token: undefined, telemetry: { enabled: true }, allowUnauthMode: true },
);
expect(trackSpy.mock.calls).toHaveLength(1);
expect(trackSpy.mock.calls[0][2].tool_status).toBe(TOOL_STATUS.SUCCEEDED);
});
it('suppresses the report-problem nudge for a 402-carrying ACTOR_MCP task failure', async () => {
// Pins the accepted task-mode delta. The ACTOR_MCP dispatch catch now carries
// failure_http_status, so a contained remote 402 reaches withReportProblemNudge with
// failureHttpStatus === 402 and the nudge is suppressed (402 is a billing state, not a defect).
// If the ACTOR_MCP path stopped carrying failure_http_status, this INVALID_INPUT failure would
// instead append REPORT_PROBLEM_INVALID_INPUT_NUDGE and this test would fail.
await withServer(async (server) => {
silenceLogs();
const rejectingClient = {
callTool: vi.fn().mockRejectedValue(new McpError(402 as ErrorCode, 'remote 402')),
close: vi.fn().mockResolvedValue(undefined),
setNotificationHandler: vi.fn(),
} as unknown as Client;
vi.spyOn(mcpClient, 'connectMCPClient').mockResolvedValue(rejectingClient);
// report-problem must be served for the nudge to be a candidate at all (spread the frozen
// tool so server.close() can null its ajvValidate on teardown).
server.upsertTools([{ ...reportProblem }]);
const { task, result } = await runTaskAndReadBack(server, makeActorMcpTool());
expect(task.status).toBe('completed');
expect(result.isError).toBe(true);
const texts = (result.content as { text: string }[]).map((c) => c.text);
expect(texts.some((t) => t.includes('Failed to call MCP tool'))).toBe(true);
expect(texts).not.toContain(REPORT_PROBLEM_NUDGE);
expect(texts).not.toContain(REPORT_PROBLEM_INVALID_INPUT_NUDGE);
});
});
});
describe('ACTOR_MCP remote-McpError containment (sync tools/call catch)', () => {
afterEach(() => vi.restoreAllMocks());
/** A stub MCP client whose `callTool` rejects with `error`; connect/close/notify are no-ops. */
function stubRejectingClient(error: unknown): Client {
return {
callTool: vi.fn().mockRejectedValue(error),
close: vi.fn().mockResolvedValue(undefined),
setNotificationHandler: vi.fn(),
} as unknown as Client;
}
it('contains a remote McpError as a soft-fail result instead of re-throwing on the wire', async () => {
// The ACTOR_MCP inner catch seals the route: without it the McpError would reach the sync
// outer catch, which re-throws every McpError as a JSON-RPC error — so this test fails if the
// inner catch is removed (the handler would reject instead of resolving with a soft-fail body).
await withServer(async (server) => {
silenceLogs();
vi.spyOn(mcpClient, 'connectMCPClient').mockResolvedValue(
stubRejectingClient(new McpError(ErrorCode.InternalError, 'remote boom')),
);
const result = await runSync(server, makeActorMcpTool());
expect(result.isError).toBe(true);
expect((result.content as { text: string }[])[0].text).toContain('Failed to call MCP tool');
});
});
it('contains a remote 402-coded McpError as a soft-fail result, never a thrown payment error', async () => {
// A remote McpError with code 402 would classify as PAYMENT if it reached buildToolCallErrorResult
// (getHttpStatusCode falls through to `.code`). The inner catch keeps it a soft-fail execution
// result and it never surfaces as a thrown payment error — and the telemetry carries
// failure_http_status, which ACTOR_MCP execution failures now report like every other path.
const trackSpy = vi.spyOn(telemetry, 'trackToolCall').mockImplementation(() => {});
await withServer(
async (server) => {
silenceLogs();
vi.spyOn(mcpClient, 'connectMCPClient').mockResolvedValue(
stubRejectingClient(new McpError(402 as ErrorCode, 'remote 402')),
);
const result = await runSync(server, makeActorMcpTool());
expect(result.isError).toBe(true);
expect((result.content as { text: string }[])[0].text).toContain('Failed to call MCP tool');
// Not the x402 payment response shape (no structuredContent payload).
expect(result.structuredContent).toBeUndefined();
},
{ token: undefined, telemetry: { enabled: true }, allowUnauthMode: true },
);
expect(trackSpy.mock.calls).toHaveLength(1);
const properties = trackSpy.mock.calls[0][2];
expect(properties.tool_status).toBe(TOOL_STATUS.SOFT_FAIL);
expect(properties.failure_http_status).toBe(402);
expect(properties.failure_category).toBe(FAILURE_CATEGORY.INVALID_INPUT);
});
it('forwards the abort signal into the remote Actor-MCP callTool options', async () => {
// Without `signal` in callTool options, cancel/disconnect cannot stop the remote call before
// EXTERNAL_TOOL_CALL_TIMEOUT_MSEC — ACTOR/INTERNAL already abort; ACTOR_MCP must match.
await withServer(async (server) => {
silenceLogs();
const callTool = vi.fn().mockResolvedValue({ content: [{ type: 'text', text: 'ok' }] });
const stubClient = {
callTool,
close: vi.fn().mockResolvedValue(undefined),
setNotificationHandler: vi.fn(),
} as unknown as Client;
vi.spyOn(mcpClient, 'connectMCPClient').mockResolvedValue(stubClient);
const controller = new AbortController();
server.upsertTools([makeActorMcpTool()]);
const handler = getRequestHandler(server, 'tools/call');
await handler(
{
method: 'tools/call',
params: { name: 'test-actor-mcp-tool', arguments: {}, _meta: { mcpSessionId: 's1' } },
},
{ signal: controller.signal, sendNotification: vi.fn() },
);
expect(callTool).toHaveBeenCalledTimes(1);
const options = callTool.mock.calls[0][2] as { signal?: AbortSignal; timeout?: number };
expect(options.signal).toBe(controller.signal);
expect(options.timeout).toBeGreaterThan(0);
});
});
it('re-throws an escaped McpError as a JSON-RPC error, not an isError tool result', async () => {
// Escaped McpErrors must remain JSON-RPC errors, including 402-coded errors.
await withServer(async (server) => {
silenceLogs();
const tool = makeThrowingTool({
name: 'mcp-error-tool',
error: new McpError(ErrorCode.InvalidParams, 'protocol boom'),
});
server.upsertTools([tool]);
const handler = getRequestHandler(server, 'tools/call');
await expect(
handler(
{
method: 'tools/call',
params: {
name: 'mcp-error-tool',
arguments: {},
_meta: { mcpSessionId: 's1' },
},
},
{ signal: { aborted: false }, sendNotification: vi.fn() },
),
).rejects.toMatchObject({ code: ErrorCode.InvalidParams });
});
});
it('re-throws a 402-coded McpError as JSON-RPC, never a PAYMENT tool result', async () => {
// A 402-coded McpError must reject as JSON-RPC, not become a payment result.
await withServer(async (server) => {
silenceLogs();
const tool = makeThrowingTool({
name: 'mcp-402-tool',
error: new McpError(402 as ErrorCode, 'remote 402'),
});
server.upsertTools([tool]);
const handler = getRequestHandler(server, 'tools/call');
await expect(
handler(
{
method: 'tools/call',
params: {
name: 'mcp-402-tool',
arguments: {},
_meta: { mcpSessionId: 's1' },
},
},
{ signal: { aborted: false }, sendNotification: vi.fn() },
),
).rejects.toMatchObject({ code: 402 });
});
});
});
describe('CallToolRequestSchema handler — task-augmented pre-flight failures', () => {
afterEach(() => vi.restoreAllMocks());
// x402 payload the payment provider returns; asserted intact in the stored structuredContent.
const X402_PAYLOAD = { x402Version: 1, accepts: [{ scheme: 'exact', resource: 'test' }] };
/** Skyfire-like provider whose getPaymentRequiredData populates the x402 structuredContent. */
function makePaymentProvider(): PaymentProvider {
return {
id: 'skyfire',
allowsUnauthenticated: true,
decorateToolSchema: (tool) => tool,
validatePayment: (args) => (args['skyfire-pay-id'] ? null : 'Missing skyfire-pay-id'),
getPaymentRequiredData: () => X402_PAYLOAD,
getPaymentHeaders: (args): Record<string, string> =>
args['skyfire-pay-id'] ? { 'skyfire-pay-id': args['skyfire-pay-id'] as string } : {},
removePaymentFields: (args) => {
const { 'skyfire-pay-id': _removed, ...rest } = args;
return rest;
},
redactForLogging: (args) => ({ ...(args as Record<string, unknown>), 'skyfire-pay-id': '[REDACTED]' }),
};
}
/** Drives a task-augmented call and returns the CreateTaskResult (already terminal on pre-flight failure). */
async function callTask(
server: ActorsMcpServer,
tool: ToolEntry,
args: Record<string, unknown>,
): Promise<{ task: { taskId: string; status: string } }> {
server.upsertTools([tool]);
const handler = getRequestHandler(server, 'tools/call');
return (await handler(
{
method: 'tools/call',
params: { name: tool.name, arguments: args, _meta: { mcpSessionId: 's1' }, task: { ttl: 60_000 } },
},
{ signal: { aborted: false }, sendNotification: vi.fn() },
)) as { task: { taskId: string; status: string } };
}
/** notifications/tasks/status statuses emitted via server.notification, in order. */
function statusNotificationStatuses(notifySpy: ReturnType<typeof vi.spyOn>): (string | undefined)[] {
return (notifySpy.mock.calls as unknown[][])
.map((c) => c[0] as { method?: string; params?: { status?: string } })
.filter((n) => n.method === 'notifications/tasks/status')
.map((n) => n.params?.status);
}
/** Flush the setImmediate-deferred status notification (emitted after the response). */
async function flushDeferredNotification(): Promise<void> {
await new Promise((resolve) => {
setImmediate(resolve);
});
}
it('resolves a payment pre-flight failure as a terminal completed task, one completed notification', async () => {
await withServer(
async (server) => {
silenceLogs();
const notifySpy = vi.spyOn(getLegacyServer(server), 'notification').mockResolvedValue(undefined);
const { tool, received } = makeRecorderTool('payment-tool', {
paymentRequired: true,
taskSupport: 'optional',
});
const res = await callTask(server, tool, {});
// CreateTaskResult is already terminal — no observable `working` phase.
expect(res.task.status).toBe('completed');
// The tool implementation never ran.
expect(received.called).toBe(false);
// Stored result carries the x402 payload intact.
const stored = (await getTaskStore(server).getTaskResult(res.task.taskId)) as Record<string, unknown>;
expect(stored.isError).toBe(true);
expect(stored.structuredContent).toEqual(X402_PAYLOAD);
// Exactly one status notification, `completed`, emitted after the response.
expect(statusNotificationStatuses(notifySpy)).toEqual([]);
await flushDeferredNotification();
expect(statusNotificationStatuses(notifySpy)).toEqual(['completed']);
},
{ paymentProvider: makePaymentProvider() },
);
});
it('stores a payment pre-flight result deep-equal to the sync (non-task) path result', async () => {
await withServer(
async (server) => {
silenceLogs();
vi.spyOn(getLegacyServer(server), 'notification').mockResolvedValue(undefined);
const handler = getRequestHandler(server, 'tools/call');
// Non-task sync path returns the paymentRequiredResult directly.
const syncTool = makeRecorderTool('payment-tool-sync', { paymentRequired: true }).tool;
server.upsertTools([syncTool]);
const syncResult = await handler(
{
method: 'tools/call',
params: { name: 'payment-tool-sync', arguments: {}, _meta: { mcpSessionId: 's1' } },
},
{ signal: { aborted: false }, sendNotification: vi.fn() },
);
// Task path stores the same failure as a completed result.
const taskTool = makeRecorderTool('payment-tool-task', {
paymentRequired: true,
taskSupport: 'optional',
}).tool;
const res = await callTask(server, taskTool, {});
const stored = await getTaskStore(server).getTaskResult(res.task.taskId);
expect(stored).toEqual(syncResult);
},
{ paymentProvider: makePaymentProvider() },
);
});
it('resolves a standby pre-flight rejection as a terminal completed task, one completed notification', async () => {
await withServer(
async (server) => {
silenceLogs();
const standbyResult = { content: [{ type: 'text', text: 'standby not supported' }], isError: true };
const standbySpy = vi
.spyOn(callActor, 'checkPaymentProviderStandbyConflict')
.mockResolvedValue(standbyResult);
const notifySpy = vi.spyOn(getLegacyServer(server), 'notification').mockResolvedValue(undefined);
const { tool, received } = makeRecorderTool(HELPER_TOOLS.ACTOR_CALL, { taskSupport: 'optional' });
const res = await callTask(server, tool, { actor: 'apify/some-actor' });
expect(res.task.status).toBe('completed');
expect(received.called).toBe(false);
const stored = await getTaskStore(server).getTaskResult(res.task.taskId);
expect(stored).toEqual(standbyResult);
await flushDeferredNotification();
expect(statusNotificationStatuses(notifySpy)).toEqual(['completed']);
// No second, asynchronous re-evaluation of the already-known outcome (criterion 13).
expect(standbySpy).toHaveBeenCalledTimes(1);
},
{ paymentProvider: makePaymentProvider() },
);
});
it('prefers the standby rejection over a payment-required failure when both apply', async () => {
await withServer(
async (server) => {
silenceLogs();
const standbyResult = { content: [{ type: 'text', text: 'standby not supported' }], isError: true };
vi.spyOn(callActor, 'checkPaymentProviderStandbyConflict').mockResolvedValue(standbyResult);
vi.spyOn(getLegacyServer(server), 'notification').mockResolvedValue(undefined);
// paymentRequired + missing skyfire-pay-id would also yield paymentRequiredResult.
const { tool } = makeRecorderTool(HELPER_TOOLS.ACTOR_CALL, {
paymentRequired: true,
taskSupport: 'optional',
});
const res = await callTask(server, tool, { actor: 'apify/some-actor' });
const stored = (await getTaskStore(server).getTaskResult(res.task.taskId)) as Record<string, unknown>;
// Standby result wins — not the x402 payment payload.
expect(stored).toEqual(standbyResult);
expect(stored.structuredContent).toBeUndefined();
},
{ paymentProvider: makePaymentProvider() },
);
});
it('records telemetry once with the sync-path pre-flight properties, no taskId', async () => {
const trackSpy = vi.spyOn(telemetry, 'trackToolCall').mockImplementation(() => {});
await withServer(
async (server) => {
silenceLogs();
vi.spyOn(getLegacyServer(server), 'notification').mockResolvedValue(undefined);
const { tool } = makeRecorderTool('payment-tool', { paymentRequired: true, taskSupport: 'optional' });
await callTask(server, tool, {});
},
{
token: undefined,
telemetry: { enabled: true },
allowUnauthMode: true,
paymentProvider: makePaymentProvider(),
},
);
// Same properties the sync 402 pre-flight path records (FAILURE_CLASSES[0]).
expectFailureClassTelemetry(trackSpy, FAILURE_CLASSES[0]);
expect(trackSpy.mock.calls[0][2]).not.toHaveProperty('taskId');
});
it('records standby telemetry with INVALID_INPUT and no failure_http_status', async () => {
const trackSpy = vi.spyOn(telemetry, 'trackToolCall').mockImplementation(() => {});
await withServer(
async (server) => {
silenceLogs();
const standbyResult = { content: [{ type: 'text', text: 'standby not supported' }], isError: true };
vi.spyOn(callActor, 'checkPaymentProviderStandbyConflict').mockResolvedValue(standbyResult);
vi.spyOn(getLegacyServer(server), 'notification').mockResolvedValue(undefined);
const { tool } = makeRecorderTool(HELPER_TOOLS.ACTOR_CALL, { taskSupport: 'optional' });
await callTask(server, tool, { actor: 'apify/some-actor' });
},
{
token: undefined,
telemetry: { enabled: true },
allowUnauthMode: true,
paymentProvider: makePaymentProvider(),
},
);
expect(trackSpy.mock.calls).toHaveLength(1);
const properties = trackSpy.mock.calls[0][2] as Record<string, unknown>;
expect(properties.tool_status).toBe(TOOL_STATUS.SOFT_FAIL);
expect(properties.failure_category).toBe(FAILURE_CATEGORY.INVALID_INPUT);
// Standby is not a 402 — the sync short-circuit omits failure_http_status and so must this path.
expect(properties).not.toHaveProperty('failure_http_status');
expect(properties).not.toHaveProperty('taskId');
});