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
|
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
describe,
it,
expect,
vi,
beforeEach,
afterEach,
Mocked,
} from 'vitest';
import {
ToolRegistry,
DiscoveredTool,
DiscoveredMCPTool,
} from './tool-registry.js';
import { Config } from '../config/config.js';
import { BaseTool, ToolResult } from './tools.js';
import { FunctionDeclaration } from '@google/genai';
import { execSync, spawn } from 'node:child_process'; // Import spawn here
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
// Mock node:child_process
vi.mock('node:child_process', async () => {
const actual = await vi.importActual('node:child_process');
return {
...actual,
execSync: vi.fn(),
spawn: vi.fn(),
};
});
// Mock MCP SDK
vi.mock('@modelcontextprotocol/sdk/client/index.js', () => {
const Client = vi.fn();
Client.prototype.connect = vi.fn();
Client.prototype.listTools = vi.fn();
Client.prototype.callTool = vi.fn();
return { Client };
});
vi.mock('@modelcontextprotocol/sdk/client/stdio.js', () => {
const StdioClientTransport = vi.fn();
StdioClientTransport.prototype.stderr = {
on: vi.fn(),
};
return { StdioClientTransport };
});
class MockTool extends BaseTool<{ param: string }, ToolResult> {
constructor(name = 'mock-tool', description = 'A mock tool') {
super(name, name, description, {
type: 'object',
properties: {
param: { type: 'string' },
},
required: ['param'],
});
}
async execute(params: { param: string }): Promise<ToolResult> {
return {
llmContent: `Executed with ${params.param}`,
returnDisplay: `Executed with ${params.param}`,
};
}
}
describe('ToolRegistry', () => {
let config: Config;
let toolRegistry: ToolRegistry;
beforeEach(() => {
// Provide a mock target directory for Config initialization
const mockTargetDir = '/test/dir';
config = new Config(
'test-api-key',
'test-model',
false, // sandbox
mockTargetDir, // targetDir
false, // debugMode
undefined, // question
false, // fullContext
undefined, // coreTools
undefined, // toolDiscoveryCommand
undefined, // toolCallCommand
undefined, // mcpServerCommand
undefined, // mcpServers
'TestAgent/1.0', // userAgent
);
toolRegistry = new ToolRegistry(config);
vi.spyOn(console, 'warn').mockImplementation(() => {}); // Suppress console.warn
});
afterEach(() => {
vi.restoreAllMocks();
});
describe('registerTool', () => {
it('should register a new tool', () => {
const tool = new MockTool();
toolRegistry.registerTool(tool);
expect(toolRegistry.getTool('mock-tool')).toBe(tool);
});
it('should overwrite an existing tool with the same name and log a warning', () => {
const tool1 = new MockTool('tool1');
const tool2 = new MockTool('tool1'); // Same name
toolRegistry.registerTool(tool1);
toolRegistry.registerTool(tool2);
expect(toolRegistry.getTool('tool1')).toBe(tool2);
expect(console.warn).toHaveBeenCalledWith(
'Tool with name "tool1" is already registered. Overwriting.',
);
});
});
describe('getFunctionDeclarations', () => {
it('should return an empty array if no tools are registered', () => {
expect(toolRegistry.getFunctionDeclarations()).toEqual([]);
});
it('should return function declarations for registered tools', () => {
const tool1 = new MockTool('tool1');
const tool2 = new MockTool('tool2');
toolRegistry.registerTool(tool1);
toolRegistry.registerTool(tool2);
const declarations = toolRegistry.getFunctionDeclarations();
expect(declarations).toHaveLength(2);
expect(declarations.map((d: FunctionDeclaration) => d.name)).toContain(
'tool1',
);
expect(declarations.map((d: FunctionDeclaration) => d.name)).toContain(
'tool2',
);
});
});
describe('getAllTools', () => {
it('should return an empty array if no tools are registered', () => {
expect(toolRegistry.getAllTools()).toEqual([]);
});
it('should return all registered tools', () => {
const tool1 = new MockTool('tool1');
const tool2 = new MockTool('tool2');
toolRegistry.registerTool(tool1);
toolRegistry.registerTool(tool2);
const tools = toolRegistry.getAllTools();
expect(tools).toHaveLength(2);
expect(tools).toContain(tool1);
expect(tools).toContain(tool2);
});
});
describe('getTool', () => {
it('should return undefined if the tool is not found', () => {
expect(toolRegistry.getTool('non-existent-tool')).toBeUndefined();
});
it('should return the tool if found', () => {
const tool = new MockTool();
toolRegistry.registerTool(tool);
expect(toolRegistry.getTool('mock-tool')).toBe(tool);
});
});
// New describe block for coreTools testing
describe('core tool registration based on config.coreTools', () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const MOCK_TOOL_ALPHA_CLASS_NAME = 'MockCoreToolAlpha'; // Class.name
const MOCK_TOOL_ALPHA_STATIC_NAME = 'ToolAlphaFromStatic'; // Tool.Name and registration name
class MockCoreToolAlpha extends BaseTool<any, ToolResult> {
static readonly Name = MOCK_TOOL_ALPHA_STATIC_NAME;
constructor() {
super(
MockCoreToolAlpha.Name,
MockCoreToolAlpha.Name,
'Description for Alpha Tool',
{},
);
}
async execute(_params: any): Promise<ToolResult> {
return { llmContent: 'AlphaExecuted', returnDisplay: 'AlphaExecuted' };
}
}
const MOCK_TOOL_BETA_CLASS_NAME = 'MockCoreToolBeta'; // Class.name
const MOCK_TOOL_BETA_STATIC_NAME = 'ToolBetaFromStatic'; // Tool.Name and registration name
class MockCoreToolBeta extends BaseTool<any, ToolResult> {
static readonly Name = MOCK_TOOL_BETA_STATIC_NAME;
constructor() {
super(
MockCoreToolBeta.Name,
MockCoreToolBeta.Name,
'Description for Beta Tool',
{},
);
}
async execute(_params: any): Promise<ToolResult> {
return { llmContent: 'BetaExecuted', returnDisplay: 'BetaExecuted' };
}
}
const availableCoreToolClasses = [MockCoreToolAlpha, MockCoreToolBeta];
let currentConfig: Config;
let currentToolRegistry: ToolRegistry;
const mockTargetDir = '/test/dir'; // As used in outer scope
// Helper to set up Config, ToolRegistry, and simulate core tool registration
const setupRegistryAndSimulateRegistration = (
coreToolsValueInConfig: string[] | undefined,
) => {
currentConfig = new Config(
'test-api-key',
'test-model',
false, // sandbox
mockTargetDir, // targetDir
false, // debugMode
undefined, // question
false, // fullContext
coreToolsValueInConfig, // coreTools setting being tested
undefined, // toolDiscoveryCommand
undefined, // toolCallCommand
undefined, // mcpServerCommand
undefined, // mcpServers
'TestAgent/1.0', // userAgent
);
// We assume Config has a getter like getCoreTools() or stores it publicly.
// For this test, we'll directly use coreToolsValueInConfig for the simulation logic,
// as that's what Config would provide.
const coreToolsListFromConfig = coreToolsValueInConfig; // Simulating config.getCoreTools()
currentToolRegistry = new ToolRegistry(currentConfig);
// Simulate the external process that registers core tools based on config
if (coreToolsListFromConfig === undefined) {
// If coreTools is undefined, all available core tools are registered
availableCoreToolClasses.forEach((ToolClass) => {
currentToolRegistry.registerTool(new ToolClass());
});
} else {
// If coreTools is an array, register tools if their static Name or class name is in the list
availableCoreToolClasses.forEach((ToolClass) => {
if (
coreToolsListFromConfig.includes(ToolClass.Name) || // Check against static Name
coreToolsListFromConfig.includes(ToolClass.name) // Check against class name
) {
currentToolRegistry.registerTool(new ToolClass());
}
});
}
};
// beforeEach for this nested describe is not strictly needed if setup is per-test,
// but ensure console.warn is mocked if any registration overwrites occur (though unlikely with this setup).
beforeEach(() => {
vi.spyOn(console, 'warn').mockImplementation(() => {});
});
it('should register all core tools if coreTools config is undefined', () => {
setupRegistryAndSimulateRegistration(undefined);
expect(
currentToolRegistry.getTool(MOCK_TOOL_ALPHA_STATIC_NAME),
).toBeInstanceOf(MockCoreToolAlpha);
expect(
currentToolRegistry.getTool(MOCK_TOOL_BETA_STATIC_NAME),
).toBeInstanceOf(MockCoreToolBeta);
expect(currentToolRegistry.getAllTools()).toHaveLength(2);
});
it('should register no core tools if coreTools config is an empty array []', () => {
setupRegistryAndSimulateRegistration([]);
expect(currentToolRegistry.getAllTools()).toHaveLength(0);
expect(
currentToolRegistry.getTool(MOCK_TOOL_ALPHA_STATIC_NAME),
).toBeUndefined();
expect(
currentToolRegistry.getTool(MOCK_TOOL_BETA_STATIC_NAME),
).toBeUndefined();
});
it('should register only tools specified by their static Name (ToolClass.Name) in coreTools config', () => {
setupRegistryAndSimulateRegistration([MOCK_TOOL_ALPHA_STATIC_NAME]); // e.g., ["ToolAlphaFromStatic"]
expect(
currentToolRegistry.getTool(MOCK_TOOL_ALPHA_STATIC_NAME),
).toBeInstanceOf(MockCoreToolAlpha);
expect(
currentToolRegistry.getTool(MOCK_TOOL_BETA_STATIC_NAME),
).toBeUndefined();
expect(currentToolRegistry.getAllTools()).toHaveLength(1);
});
it('should register only tools specified by their class name (ToolClass.name) in coreTools config', () => {
// ToolBeta is registered under MOCK_TOOL_BETA_STATIC_NAME ('ToolBetaFromStatic')
// We configure coreTools with its class name: MOCK_TOOL_BETA_CLASS_NAME ('MockCoreToolBeta')
setupRegistryAndSimulateRegistration([MOCK_TOOL_BETA_CLASS_NAME]);
expect(
currentToolRegistry.getTool(MOCK_TOOL_BETA_STATIC_NAME),
).toBeInstanceOf(MockCoreToolBeta);
expect(
currentToolRegistry.getTool(MOCK_TOOL_ALPHA_STATIC_NAME),
).toBeUndefined();
expect(currentToolRegistry.getAllTools()).toHaveLength(1);
});
it('should register tools if specified by either static Name or class name in a mixed coreTools config', () => {
// Config: ["ToolAlphaFromStatic", "MockCoreToolBeta"]
// ToolAlpha matches by static Name. ToolBeta matches by class name.
setupRegistryAndSimulateRegistration([
MOCK_TOOL_ALPHA_STATIC_NAME, // Matches MockCoreToolAlpha.Name
MOCK_TOOL_BETA_CLASS_NAME, // Matches MockCoreToolBeta.name
]);
expect(
currentToolRegistry.getTool(MOCK_TOOL_ALPHA_STATIC_NAME),
).toBeInstanceOf(MockCoreToolAlpha);
expect(
currentToolRegistry.getTool(MOCK_TOOL_BETA_STATIC_NAME),
).toBeInstanceOf(MockCoreToolBeta); // Registered under its static Name
expect(currentToolRegistry.getAllTools()).toHaveLength(2);
});
});
describe('discoverTools', () => {
let mockConfigGetToolDiscoveryCommand: ReturnType<typeof vi.spyOn>;
let mockConfigGetMcpServers: ReturnType<typeof vi.spyOn>;
let mockConfigGetMcpServerCommand: ReturnType<typeof vi.spyOn>;
let mockExecSync: ReturnType<typeof vi.mocked<typeof execSync>>;
beforeEach(() => {
mockConfigGetToolDiscoveryCommand = vi.spyOn(
config,
'getToolDiscoveryCommand',
);
mockConfigGetMcpServers = vi.spyOn(config, 'getMcpServers');
mockConfigGetMcpServerCommand = vi.spyOn(config, 'getMcpServerCommand');
mockExecSync = vi.mocked(execSync);
// Clear any tools registered by previous tests in this describe block
toolRegistry = new ToolRegistry(config);
});
it('should discover tools using discovery command', () => {
const discoveryCommand = 'my-discovery-command';
mockConfigGetToolDiscoveryCommand.mockReturnValue(discoveryCommand);
const mockToolDeclarations: FunctionDeclaration[] = [
{
name: 'discovered-tool-1',
description: 'A discovered tool',
parameters: { type: 'object', properties: {} } as Record<
string,
unknown
>,
},
];
mockExecSync.mockReturnValue(
Buffer.from(
JSON.stringify([{ function_declarations: mockToolDeclarations }]),
),
);
toolRegistry.discoverTools();
expect(execSync).toHaveBeenCalledWith(discoveryCommand);
const discoveredTool = toolRegistry.getTool('discovered-tool-1');
expect(discoveredTool).toBeInstanceOf(DiscoveredTool);
expect(discoveredTool?.name).toBe('discovered-tool-1');
expect(discoveredTool?.description).toContain('A discovered tool');
expect(discoveredTool?.description).toContain(discoveryCommand);
});
it('should remove previously discovered tools before discovering new ones', () => {
const discoveryCommand = 'my-discovery-command';
mockConfigGetToolDiscoveryCommand.mockReturnValue(discoveryCommand);
mockExecSync.mockReturnValueOnce(
Buffer.from(
JSON.stringify([
{
function_declarations: [
{
name: 'old-discovered-tool',
description: 'old',
parameters: { type: 'object' },
},
],
},
]),
),
);
toolRegistry.discoverTools();
expect(toolRegistry.getTool('old-discovered-tool')).toBeInstanceOf(
DiscoveredTool,
);
mockExecSync.mockReturnValueOnce(
Buffer.from(
JSON.stringify([
{
function_declarations: [
{
name: 'new-discovered-tool',
description: 'new',
parameters: { type: 'object' },
},
],
},
]),
),
);
toolRegistry.discoverTools();
expect(toolRegistry.getTool('old-discovered-tool')).toBeUndefined();
expect(toolRegistry.getTool('new-discovered-tool')).toBeInstanceOf(
DiscoveredTool,
);
});
it('should discover tools using MCP servers defined in getMcpServers and strip schema properties', async () => {
mockConfigGetToolDiscoveryCommand.mockReturnValue(undefined); // No regular discovery
mockConfigGetMcpServerCommand.mockReturnValue(undefined); // No command-based MCP
mockConfigGetMcpServers.mockReturnValue({
'my-mcp-server': {
command: 'mcp-server-cmd',
args: ['--port', '1234'],
},
});
const mockMcpClientInstance = vi.mocked(Client.prototype);
mockMcpClientInstance.listTools.mockResolvedValue({
tools: [
{
name: 'mcp-tool-1',
description: 'An MCP tool',
inputSchema: {
type: 'object',
properties: {
param1: { type: 'string', $schema: 'remove-me' },
param2: {
type: 'object',
additionalProperties: false,
properties: {
nested: { type: 'number' },
},
},
},
additionalProperties: true,
$schema: 'http://json-schema.org/draft-07/schema#',
},
},
],
});
mockMcpClientInstance.connect.mockResolvedValue(undefined);
toolRegistry.discoverTools();
await new Promise((resolve) => setTimeout(resolve, 100)); // Wait for async operations
expect(Client).toHaveBeenCalledTimes(1);
expect(StdioClientTransport).toHaveBeenCalledWith({
command: 'mcp-server-cmd',
args: ['--port', '1234'],
env: expect.any(Object),
stderr: 'pipe',
});
expect(mockMcpClientInstance.connect).toHaveBeenCalled();
expect(mockMcpClientInstance.listTools).toHaveBeenCalled();
const discoveredTool = toolRegistry.getTool('mcp-tool-1');
expect(discoveredTool).toBeInstanceOf(DiscoveredMCPTool);
expect(discoveredTool?.name).toBe('mcp-tool-1');
expect(discoveredTool?.description).toContain('An MCP tool');
expect(discoveredTool?.description).toContain('mcp-tool-1');
// Verify that $schema and additionalProperties are removed
const cleanedSchema = discoveredTool?.schema.parameters;
expect(cleanedSchema).not.toHaveProperty('$schema');
expect(cleanedSchema).not.toHaveProperty('additionalProperties');
expect(cleanedSchema?.properties?.param1).not.toHaveProperty('$schema');
expect(cleanedSchema?.properties?.param2).not.toHaveProperty(
'additionalProperties',
);
expect(
cleanedSchema?.properties?.param2?.properties?.nested,
).not.toHaveProperty('$schema');
expect(
cleanedSchema?.properties?.param2?.properties?.nested,
).not.toHaveProperty('additionalProperties');
});
it('should discover tools using MCP server command from getMcpServerCommand', async () => {
mockConfigGetToolDiscoveryCommand.mockReturnValue(undefined);
mockConfigGetMcpServers.mockReturnValue({}); // No direct MCP servers
mockConfigGetMcpServerCommand.mockReturnValue(
'mcp-server-start-command --param',
);
const mockMcpClientInstance = vi.mocked(Client.prototype);
mockMcpClientInstance.listTools.mockResolvedValue({
tools: [
{
name: 'mcp-tool-cmd',
description: 'An MCP tool from command',
inputSchema: { type: 'object' },
}, // Corrected: Add type: 'object'
],
});
mockMcpClientInstance.connect.mockResolvedValue(undefined);
toolRegistry.discoverTools();
await new Promise((resolve) => setTimeout(resolve, 100));
expect(Client).toHaveBeenCalledTimes(1);
expect(StdioClientTransport).toHaveBeenCalledWith({
command: 'mcp-server-start-command',
args: ['--param'],
env: expect.any(Object),
stderr: 'pipe',
});
expect(mockMcpClientInstance.connect).toHaveBeenCalled();
expect(mockMcpClientInstance.listTools).toHaveBeenCalled();
const discoveredTool = toolRegistry.getTool('mcp-tool-cmd'); // Name is not prefixed if only one MCP server
expect(discoveredTool).toBeInstanceOf(DiscoveredMCPTool);
expect(discoveredTool?.name).toBe('mcp-tool-cmd');
});
it('should handle errors during MCP tool discovery gracefully', async () => {
mockConfigGetToolDiscoveryCommand.mockReturnValue(undefined);
mockConfigGetMcpServers.mockReturnValue({
'failing-mcp': { command: 'fail-cmd' },
});
vi.spyOn(console, 'error').mockImplementation(() => {});
const mockMcpClientInstance = vi.mocked(Client.prototype);
mockMcpClientInstance.connect.mockRejectedValue(
new Error('Connection failed'),
);
// Need to await the async IIFE within discoverTools.
// Since discoverTools itself isn't async, we can't directly await it.
// We'll check the console.error mock.
toolRegistry.discoverTools();
await new Promise((resolve) => setTimeout(resolve, 100)); // Wait for async operations
expect(console.error).toHaveBeenCalledWith(
`failed to start or connect to MCP server 'failing-mcp' ${JSON.stringify({ command: 'fail-cmd' })}; \nError: Connection failed`,
);
expect(toolRegistry.getAllTools()).toHaveLength(0); // No tools should be registered
});
});
});
describe('DiscoveredTool', () => {
let config: Config;
const toolName = 'my-discovered-tool';
const toolDescription = 'Does something cool.';
const toolParamsSchema = {
type: 'object',
properties: { path: { type: 'string' } },
};
let mockSpawnInstance: Partial<ReturnType<typeof spawn>>;
beforeEach(() => {
const mockTargetDir = '/test/dir';
config = new Config(
'test-api-key',
'test-model',
false, // sandbox
mockTargetDir, // targetDir
false, // debugMode
undefined, // question
false, // fullContext
undefined, // coreTools
undefined, // toolDiscoveryCommand
undefined, // toolCallCommand
undefined, // mcpServerCommand
undefined, // mcpServers
'TestAgent/1.0', // userAgent
);
vi.spyOn(config, 'getToolDiscoveryCommand').mockReturnValue(
'discovery-cmd',
);
vi.spyOn(config, 'getToolCallCommand').mockReturnValue('call-cmd');
const mockStdin = {
write: vi.fn(),
end: vi.fn(),
on: vi.fn(),
writable: true,
} as any;
const mockStdout = {
on: vi.fn(),
read: vi.fn(),
readable: true,
} as any;
const mockStderr = {
on: vi.fn(),
read: vi.fn(),
readable: true,
} as any;
mockSpawnInstance = {
stdin: mockStdin,
stdout: mockStdout,
stderr: mockStderr,
on: vi.fn(), // For process events like 'close', 'error'
kill: vi.fn(),
pid: 123,
connected: true,
disconnect: vi.fn(),
ref: vi.fn(),
unref: vi.fn(),
spawnargs: [],
spawnfile: '',
channel: null,
exitCode: null,
signalCode: null,
killed: false,
stdio: [mockStdin, mockStdout, mockStderr, null, null] as any,
};
vi.mocked(spawn).mockReturnValue(mockSpawnInstance as any);
});
afterEach(() => {
vi.restoreAllMocks();
});
it('constructor should set up properties correctly and enhance description', () => {
const tool = new DiscoveredTool(
config,
toolName,
toolDescription,
toolParamsSchema,
);
expect(tool.name).toBe(toolName);
expect(tool.schema.description).toContain(toolDescription);
expect(tool.schema.description).toContain('discovery-cmd');
expect(tool.schema.description).toContain('call-cmd my-discovered-tool');
expect(tool.schema.parameters).toEqual(toolParamsSchema);
});
it('execute should call spawn with correct command and params, and return stdout on success', async () => {
const tool = new DiscoveredTool(
config,
toolName,
toolDescription,
toolParamsSchema,
);
const params = { path: '/foo/bar' };
const expectedOutput = JSON.stringify({ result: 'success' });
// Simulate successful execution
(mockSpawnInstance.stdout!.on as Mocked<any>).mockImplementation(
(event: string, callback: (data: string) => void) => {
if (event === 'data') {
callback(expectedOutput);
}
},
);
(mockSpawnInstance.on as Mocked<any>).mockImplementation(
(
event: string,
callback: (code: number | null, signal: NodeJS.Signals | null) => void,
) => {
if (event === 'close') {
callback(0, null); // Success
}
},
);
const result = await tool.execute(params);
expect(spawn).toHaveBeenCalledWith('call-cmd', [toolName]);
expect(mockSpawnInstance.stdin!.write).toHaveBeenCalledWith(
JSON.stringify(params),
);
expect(mockSpawnInstance.stdin!.end).toHaveBeenCalled();
expect(result.llmContent).toBe(expectedOutput);
expect(result.returnDisplay).toBe(expectedOutput);
});
it('execute should return error details if spawn results in an error', async () => {
const tool = new DiscoveredTool(
config,
toolName,
toolDescription,
toolParamsSchema,
);
const params = { path: '/foo/bar' };
const stderrOutput = 'Something went wrong';
const error = new Error('Spawn error');
// Simulate error during spawn
(mockSpawnInstance.stderr!.on as Mocked<any>).mockImplementation(
(event: string, callback: (data: string) => void) => {
if (event === 'data') {
callback(stderrOutput);
}
},
);
(mockSpawnInstance.on as Mocked<any>).mockImplementation(
(
event: string,
callback:
| ((code: number | null, signal: NodeJS.Signals | null) => void)
| ((error: Error) => void),
) => {
if (event === 'error') {
(callback as (error: Error) => void)(error); // Simulate 'error' event
}
if (event === 'close') {
(
callback as (
code: number | null,
signal: NodeJS.Signals | null,
) => void
)(1, null); // Non-zero exit code
}
},
);
const result = await tool.execute(params);
expect(result.llmContent).toContain(`Stderr: ${stderrOutput}`);
expect(result.llmContent).toContain(`Error: ${error.toString()}`);
expect(result.llmContent).toContain('Exit Code: 1');
expect(result.returnDisplay).toBe(result.llmContent);
});
});
describe('DiscoveredMCPTool', () => {
let mockMcpClient: Client;
const toolName = 'my-mcp-tool';
const toolDescription = 'An MCP-discovered tool.';
const toolInputSchema = {
type: 'object',
properties: { data: { type: 'string' } },
};
beforeEach(() => {
mockMcpClient = new Client({
name: 'test-client',
version: '0.0.0',
}) as Mocked<Client>;
});
afterEach(() => {
vi.restoreAllMocks();
});
it('constructor should set up properties correctly and enhance description', () => {
const tool = new DiscoveredMCPTool(
mockMcpClient,
toolName,
toolDescription,
toolInputSchema,
toolName,
);
expect(tool.name).toBe(toolName);
expect(tool.schema.description).toContain(toolDescription);
expect(tool.schema.description).toContain('tools/call');
expect(tool.schema.description).toContain(toolName);
expect(tool.schema.parameters).toEqual(toolInputSchema);
});
it('execute should call mcpClient.callTool with correct params and return serialized result', async () => {
const tool = new DiscoveredMCPTool(
mockMcpClient,
toolName,
toolDescription,
toolInputSchema,
toolName,
);
const params = { data: 'test_data' };
const mcpResult = { success: true, value: 'processed' };
vi.mocked(mockMcpClient.callTool).mockResolvedValue(mcpResult);
const result = await tool.execute(params);
expect(mockMcpClient.callTool).toHaveBeenCalledWith(
{
name: toolName,
arguments: params,
},
undefined,
{
timeout: 10 * 60 * 1000,
},
);
const expectedOutput = JSON.stringify(mcpResult, null, 2);
expect(result.llmContent).toBe(expectedOutput);
expect(result.returnDisplay).toBe(expectedOutput);
});
});
|