@librechat/agents 3.0.67 → 3.0.69
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/dist/cjs/common/enum.cjs +2 -0
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/main.cjs +7 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/tools/ToolSearch.cjs +202 -8
- package/dist/cjs/tools/ToolSearch.cjs.map +1 -1
- package/dist/esm/common/enum.mjs +2 -0
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/main.mjs +1 -1
- package/dist/esm/tools/ToolSearch.mjs +196 -9
- package/dist/esm/tools/ToolSearch.mjs.map +1 -1
- package/dist/types/common/enum.d.ts +3 -1
- package/dist/types/tools/ToolSearch.d.ts +52 -2
- package/dist/types/types/tools.d.ts +3 -1
- package/package.json +1 -1
- package/src/common/enum.ts +2 -0
- package/src/tools/ToolSearch.ts +248 -9
- package/src/tools/__tests__/ToolSearch.test.ts +384 -2
- package/src/types/tools.ts +3 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/tools/__tests__/ToolSearch.test.ts
|
|
2
2
|
/**
|
|
3
|
-
* Unit tests for Tool Search
|
|
3
|
+
* Unit tests for Tool Search.
|
|
4
4
|
* Tests helper functions and sanitization logic without hitting the API.
|
|
5
5
|
*/
|
|
6
6
|
import { describe, it, expect } from '@jest/globals';
|
|
@@ -11,8 +11,15 @@ import {
|
|
|
11
11
|
countNestedGroups,
|
|
12
12
|
hasNestedQuantifiers,
|
|
13
13
|
performLocalSearch,
|
|
14
|
+
extractMcpServerName,
|
|
15
|
+
isFromMcpServer,
|
|
16
|
+
isFromAnyMcpServer,
|
|
17
|
+
normalizeServerFilter,
|
|
18
|
+
getAvailableMcpServers,
|
|
19
|
+
getBaseToolName,
|
|
20
|
+
formatServerListing,
|
|
14
21
|
} from '../ToolSearch';
|
|
15
|
-
import type { ToolMetadata } from '@/types';
|
|
22
|
+
import type { ToolMetadata, LCToolRegistry } from '@/types';
|
|
16
23
|
|
|
17
24
|
describe('ToolSearch', () => {
|
|
18
25
|
describe('escapeRegexSpecialChars', () => {
|
|
@@ -438,4 +445,379 @@ describe('ToolSearch', () => {
|
|
|
438
445
|
expect(result.tool_references[0].snippet.length).toBeGreaterThan(0);
|
|
439
446
|
});
|
|
440
447
|
});
|
|
448
|
+
|
|
449
|
+
describe('extractMcpServerName', () => {
|
|
450
|
+
it('extracts server name from MCP tool name', () => {
|
|
451
|
+
expect(extractMcpServerName('get_weather_mcp_weather-server')).toBe(
|
|
452
|
+
'weather-server'
|
|
453
|
+
);
|
|
454
|
+
expect(extractMcpServerName('send_email_mcp_gmail')).toBe('gmail');
|
|
455
|
+
expect(extractMcpServerName('query_database_mcp_postgres-mcp')).toBe(
|
|
456
|
+
'postgres-mcp'
|
|
457
|
+
);
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
it('returns undefined for non-MCP tools', () => {
|
|
461
|
+
expect(extractMcpServerName('get_weather')).toBeUndefined();
|
|
462
|
+
expect(extractMcpServerName('send_email')).toBeUndefined();
|
|
463
|
+
expect(extractMcpServerName('regular_tool_name')).toBeUndefined();
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
it('handles edge cases', () => {
|
|
467
|
+
expect(extractMcpServerName('_mcp_server')).toBe('server');
|
|
468
|
+
expect(extractMcpServerName('tool_mcp_')).toBe('');
|
|
469
|
+
});
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
describe('getBaseToolName', () => {
|
|
473
|
+
it('extracts base name from MCP tool name', () => {
|
|
474
|
+
expect(getBaseToolName('get_weather_mcp_weather-server')).toBe(
|
|
475
|
+
'get_weather'
|
|
476
|
+
);
|
|
477
|
+
expect(getBaseToolName('send_email_mcp_gmail')).toBe('send_email');
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
it('returns full name for non-MCP tools', () => {
|
|
481
|
+
expect(getBaseToolName('get_weather')).toBe('get_weather');
|
|
482
|
+
expect(getBaseToolName('regular_tool')).toBe('regular_tool');
|
|
483
|
+
});
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
describe('isFromMcpServer', () => {
|
|
487
|
+
it('returns true for matching MCP server', () => {
|
|
488
|
+
expect(
|
|
489
|
+
isFromMcpServer('get_weather_mcp_weather-server', 'weather-server')
|
|
490
|
+
).toBe(true);
|
|
491
|
+
expect(isFromMcpServer('send_email_mcp_gmail', 'gmail')).toBe(true);
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it('returns false for non-matching MCP server', () => {
|
|
495
|
+
expect(
|
|
496
|
+
isFromMcpServer('get_weather_mcp_weather-server', 'other-server')
|
|
497
|
+
).toBe(false);
|
|
498
|
+
expect(isFromMcpServer('send_email_mcp_gmail', 'outlook')).toBe(false);
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
it('returns false for non-MCP tools', () => {
|
|
502
|
+
expect(isFromMcpServer('get_weather', 'weather-server')).toBe(false);
|
|
503
|
+
expect(isFromMcpServer('regular_tool', 'any-server')).toBe(false);
|
|
504
|
+
});
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
describe('isFromAnyMcpServer', () => {
|
|
508
|
+
it('returns true if tool is from any of the specified servers', () => {
|
|
509
|
+
expect(
|
|
510
|
+
isFromAnyMcpServer('get_weather_mcp_weather-api', [
|
|
511
|
+
'weather-api',
|
|
512
|
+
'gmail',
|
|
513
|
+
])
|
|
514
|
+
).toBe(true);
|
|
515
|
+
expect(
|
|
516
|
+
isFromAnyMcpServer('send_email_mcp_gmail', ['weather-api', 'gmail'])
|
|
517
|
+
).toBe(true);
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
it('returns false if tool is not from any specified server', () => {
|
|
521
|
+
expect(
|
|
522
|
+
isFromAnyMcpServer('get_weather_mcp_weather-api', ['gmail', 'slack'])
|
|
523
|
+
).toBe(false);
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
it('returns false for non-MCP tools', () => {
|
|
527
|
+
expect(isFromAnyMcpServer('regular_tool', ['weather-api', 'gmail'])).toBe(
|
|
528
|
+
false
|
|
529
|
+
);
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
it('returns false for empty server list', () => {
|
|
533
|
+
expect(isFromAnyMcpServer('get_weather_mcp_weather-api', [])).toBe(false);
|
|
534
|
+
});
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
describe('normalizeServerFilter', () => {
|
|
538
|
+
it('converts string to single-element array', () => {
|
|
539
|
+
expect(normalizeServerFilter('gmail')).toEqual(['gmail']);
|
|
540
|
+
});
|
|
541
|
+
|
|
542
|
+
it('passes through arrays unchanged', () => {
|
|
543
|
+
expect(normalizeServerFilter(['gmail', 'slack'])).toEqual([
|
|
544
|
+
'gmail',
|
|
545
|
+
'slack',
|
|
546
|
+
]);
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
it('returns empty array for undefined', () => {
|
|
550
|
+
expect(normalizeServerFilter(undefined)).toEqual([]);
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it('returns empty array for empty string', () => {
|
|
554
|
+
expect(normalizeServerFilter('')).toEqual([]);
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
it('filters out empty strings from arrays', () => {
|
|
558
|
+
expect(normalizeServerFilter(['gmail', '', 'slack'])).toEqual([
|
|
559
|
+
'gmail',
|
|
560
|
+
'slack',
|
|
561
|
+
]);
|
|
562
|
+
});
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
describe('getAvailableMcpServers', () => {
|
|
566
|
+
const createRegistry = (): LCToolRegistry => {
|
|
567
|
+
const registry: LCToolRegistry = new Map();
|
|
568
|
+
registry.set('get_weather_mcp_weather-api', {
|
|
569
|
+
name: 'get_weather_mcp_weather-api',
|
|
570
|
+
description: 'Get weather',
|
|
571
|
+
defer_loading: true,
|
|
572
|
+
});
|
|
573
|
+
registry.set('get_forecast_mcp_weather-api', {
|
|
574
|
+
name: 'get_forecast_mcp_weather-api',
|
|
575
|
+
description: 'Get forecast',
|
|
576
|
+
defer_loading: true,
|
|
577
|
+
});
|
|
578
|
+
registry.set('send_email_mcp_gmail', {
|
|
579
|
+
name: 'send_email_mcp_gmail',
|
|
580
|
+
description: 'Send email',
|
|
581
|
+
defer_loading: true,
|
|
582
|
+
});
|
|
583
|
+
registry.set('read_inbox_mcp_gmail', {
|
|
584
|
+
name: 'read_inbox_mcp_gmail',
|
|
585
|
+
description: 'Read inbox',
|
|
586
|
+
defer_loading: true,
|
|
587
|
+
});
|
|
588
|
+
registry.set('post_message_mcp_slack', {
|
|
589
|
+
name: 'post_message_mcp_slack',
|
|
590
|
+
description: 'Post to Slack',
|
|
591
|
+
defer_loading: true,
|
|
592
|
+
});
|
|
593
|
+
registry.set('regular_tool', {
|
|
594
|
+
name: 'regular_tool',
|
|
595
|
+
description: 'Not an MCP tool',
|
|
596
|
+
defer_loading: true,
|
|
597
|
+
});
|
|
598
|
+
registry.set('non_deferred_mcp_special', {
|
|
599
|
+
name: 'non_deferred_mcp_special',
|
|
600
|
+
description: 'Not deferred',
|
|
601
|
+
defer_loading: false,
|
|
602
|
+
});
|
|
603
|
+
return registry;
|
|
604
|
+
};
|
|
605
|
+
|
|
606
|
+
it('extracts unique server names from registry', () => {
|
|
607
|
+
const registry = createRegistry();
|
|
608
|
+
const servers = getAvailableMcpServers(registry, true);
|
|
609
|
+
|
|
610
|
+
expect(servers).toEqual(['gmail', 'slack', 'weather-api']);
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
it('returns servers sorted alphabetically', () => {
|
|
614
|
+
const registry = createRegistry();
|
|
615
|
+
const servers = getAvailableMcpServers(registry, true);
|
|
616
|
+
|
|
617
|
+
expect(servers).toEqual([...servers].sort());
|
|
618
|
+
});
|
|
619
|
+
|
|
620
|
+
it('excludes non-MCP tools', () => {
|
|
621
|
+
const registry = createRegistry();
|
|
622
|
+
const servers = getAvailableMcpServers(registry, true);
|
|
623
|
+
|
|
624
|
+
expect(servers).not.toContain('regular_tool');
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
it('respects onlyDeferred flag', () => {
|
|
628
|
+
const registry = createRegistry();
|
|
629
|
+
|
|
630
|
+
const deferredOnly = getAvailableMcpServers(registry, true);
|
|
631
|
+
expect(deferredOnly).not.toContain('special');
|
|
632
|
+
|
|
633
|
+
const allTools = getAvailableMcpServers(registry, false);
|
|
634
|
+
expect(allTools).toContain('special');
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
it('returns empty array for undefined registry', () => {
|
|
638
|
+
expect(getAvailableMcpServers(undefined, true)).toEqual([]);
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
it('returns empty array for registry with no MCP tools', () => {
|
|
642
|
+
const registry: LCToolRegistry = new Map();
|
|
643
|
+
registry.set('tool1', {
|
|
644
|
+
name: 'tool1',
|
|
645
|
+
description: 'Regular tool',
|
|
646
|
+
defer_loading: true,
|
|
647
|
+
});
|
|
648
|
+
|
|
649
|
+
expect(getAvailableMcpServers(registry, true)).toEqual([]);
|
|
650
|
+
});
|
|
651
|
+
});
|
|
652
|
+
|
|
653
|
+
describe('performLocalSearch with MCP tools', () => {
|
|
654
|
+
const mcpTools: ToolMetadata[] = [
|
|
655
|
+
{
|
|
656
|
+
name: 'get_weather_mcp_weather-server',
|
|
657
|
+
description: 'Get weather from MCP server',
|
|
658
|
+
parameters: undefined,
|
|
659
|
+
},
|
|
660
|
+
{
|
|
661
|
+
name: 'get_forecast_mcp_weather-server',
|
|
662
|
+
description: 'Get forecast from MCP server',
|
|
663
|
+
parameters: undefined,
|
|
664
|
+
},
|
|
665
|
+
{
|
|
666
|
+
name: 'send_email_mcp_gmail',
|
|
667
|
+
description: 'Send email via Gmail MCP',
|
|
668
|
+
parameters: undefined,
|
|
669
|
+
},
|
|
670
|
+
{
|
|
671
|
+
name: 'read_inbox_mcp_gmail',
|
|
672
|
+
description: 'Read inbox via Gmail MCP',
|
|
673
|
+
parameters: undefined,
|
|
674
|
+
},
|
|
675
|
+
{
|
|
676
|
+
name: 'get_weather',
|
|
677
|
+
description: 'Regular weather tool (not MCP)',
|
|
678
|
+
parameters: undefined,
|
|
679
|
+
},
|
|
680
|
+
];
|
|
681
|
+
|
|
682
|
+
it('searches across all tools including MCP tools', () => {
|
|
683
|
+
const result = performLocalSearch(
|
|
684
|
+
mcpTools,
|
|
685
|
+
'weather',
|
|
686
|
+
['name', 'description'],
|
|
687
|
+
10
|
|
688
|
+
);
|
|
689
|
+
|
|
690
|
+
expect(result.tool_references.length).toBe(3);
|
|
691
|
+
expect(result.tool_references.map((r) => r.tool_name)).toContain(
|
|
692
|
+
'get_weather_mcp_weather-server'
|
|
693
|
+
);
|
|
694
|
+
expect(result.tool_references.map((r) => r.tool_name)).toContain(
|
|
695
|
+
'get_weather'
|
|
696
|
+
);
|
|
697
|
+
});
|
|
698
|
+
|
|
699
|
+
it('finds MCP tools by searching the full name including server suffix', () => {
|
|
700
|
+
const result = performLocalSearch(mcpTools, 'gmail', ['name'], 10);
|
|
701
|
+
|
|
702
|
+
expect(result.tool_references.length).toBe(2);
|
|
703
|
+
expect(result.tool_references.map((r) => r.tool_name)).toContain(
|
|
704
|
+
'send_email_mcp_gmail'
|
|
705
|
+
);
|
|
706
|
+
expect(result.tool_references.map((r) => r.tool_name)).toContain(
|
|
707
|
+
'read_inbox_mcp_gmail'
|
|
708
|
+
);
|
|
709
|
+
});
|
|
710
|
+
|
|
711
|
+
it('can search for tools by MCP delimiter', () => {
|
|
712
|
+
const result = performLocalSearch(mcpTools, '_mcp_', ['name'], 10);
|
|
713
|
+
|
|
714
|
+
expect(result.tool_references.length).toBe(4);
|
|
715
|
+
expect(result.tool_references.map((r) => r.tool_name)).not.toContain(
|
|
716
|
+
'get_weather'
|
|
717
|
+
);
|
|
718
|
+
});
|
|
719
|
+
});
|
|
720
|
+
|
|
721
|
+
describe('formatServerListing', () => {
|
|
722
|
+
const serverTools: ToolMetadata[] = [
|
|
723
|
+
{
|
|
724
|
+
name: 'get_weather_mcp_weather-api',
|
|
725
|
+
description: 'Get current weather conditions for a location',
|
|
726
|
+
parameters: undefined,
|
|
727
|
+
},
|
|
728
|
+
{
|
|
729
|
+
name: 'get_forecast_mcp_weather-api',
|
|
730
|
+
description: 'Get weather forecast for the next 7 days',
|
|
731
|
+
parameters: undefined,
|
|
732
|
+
},
|
|
733
|
+
];
|
|
734
|
+
|
|
735
|
+
it('formats server listing with tool names and descriptions', () => {
|
|
736
|
+
const result = formatServerListing(serverTools, 'weather-api');
|
|
737
|
+
|
|
738
|
+
expect(result).toContain('Tools from MCP server: weather-api');
|
|
739
|
+
expect(result).toContain('2 tool(s)');
|
|
740
|
+
expect(result).toContain('get_weather');
|
|
741
|
+
expect(result).toContain('get_forecast');
|
|
742
|
+
expect(result).toContain('preview only');
|
|
743
|
+
});
|
|
744
|
+
|
|
745
|
+
it('includes hint to search for specific tool to load it', () => {
|
|
746
|
+
const result = formatServerListing(serverTools, 'weather-api');
|
|
747
|
+
|
|
748
|
+
expect(result).toContain('To use a tool, search for it by name');
|
|
749
|
+
});
|
|
750
|
+
|
|
751
|
+
it('uses base tool name (without MCP suffix) in display', () => {
|
|
752
|
+
const result = formatServerListing(serverTools, 'weather-api');
|
|
753
|
+
|
|
754
|
+
expect(result).toContain('**get_weather**');
|
|
755
|
+
expect(result).not.toContain('**get_weather_mcp_weather-api**');
|
|
756
|
+
});
|
|
757
|
+
|
|
758
|
+
it('handles empty tools array', () => {
|
|
759
|
+
const result = formatServerListing([], 'empty-server');
|
|
760
|
+
|
|
761
|
+
expect(result).toContain('No tools found');
|
|
762
|
+
expect(result).toContain('empty-server');
|
|
763
|
+
});
|
|
764
|
+
|
|
765
|
+
it('truncates long descriptions', () => {
|
|
766
|
+
const toolsWithLongDesc: ToolMetadata[] = [
|
|
767
|
+
{
|
|
768
|
+
name: 'long_tool_mcp_server',
|
|
769
|
+
description:
|
|
770
|
+
'This is a very long description that exceeds 80 characters and should be truncated to keep the listing compact and readable.',
|
|
771
|
+
parameters: undefined,
|
|
772
|
+
},
|
|
773
|
+
];
|
|
774
|
+
|
|
775
|
+
const result = formatServerListing(toolsWithLongDesc, 'server');
|
|
776
|
+
|
|
777
|
+
expect(result).toContain('...');
|
|
778
|
+
expect(result.length).toBeLessThan(
|
|
779
|
+
toolsWithLongDesc[0].description.length + 200
|
|
780
|
+
);
|
|
781
|
+
});
|
|
782
|
+
|
|
783
|
+
it('handles multiple servers with grouped output', () => {
|
|
784
|
+
const multiServerTools: ToolMetadata[] = [
|
|
785
|
+
{
|
|
786
|
+
name: 'get_weather_mcp_weather-api',
|
|
787
|
+
description: 'Get weather',
|
|
788
|
+
parameters: undefined,
|
|
789
|
+
},
|
|
790
|
+
{
|
|
791
|
+
name: 'send_email_mcp_gmail',
|
|
792
|
+
description: 'Send email',
|
|
793
|
+
parameters: undefined,
|
|
794
|
+
},
|
|
795
|
+
{
|
|
796
|
+
name: 'read_inbox_mcp_gmail',
|
|
797
|
+
description: 'Read inbox',
|
|
798
|
+
parameters: undefined,
|
|
799
|
+
},
|
|
800
|
+
];
|
|
801
|
+
|
|
802
|
+
const result = formatServerListing(multiServerTools, [
|
|
803
|
+
'weather-api',
|
|
804
|
+
'gmail',
|
|
805
|
+
]);
|
|
806
|
+
|
|
807
|
+
expect(result).toContain('Tools from MCP servers: weather-api, gmail');
|
|
808
|
+
expect(result).toContain('3 tool(s)');
|
|
809
|
+
expect(result).toContain('### weather-api');
|
|
810
|
+
expect(result).toContain('### gmail');
|
|
811
|
+
expect(result).toContain('get_weather');
|
|
812
|
+
expect(result).toContain('send_email');
|
|
813
|
+
expect(result).toContain('read_inbox');
|
|
814
|
+
});
|
|
815
|
+
|
|
816
|
+
it('accepts single server as array', () => {
|
|
817
|
+
const result = formatServerListing(serverTools, ['weather-api']);
|
|
818
|
+
|
|
819
|
+
expect(result).toContain('Tools from MCP server: weather-api');
|
|
820
|
+
expect(result).not.toContain('###');
|
|
821
|
+
});
|
|
822
|
+
});
|
|
441
823
|
});
|
package/src/types/tools.ts
CHANGED
|
@@ -131,7 +131,7 @@ export type ProgrammaticCache = { toolMap: ToolMap; toolDefs: LCTool[] };
|
|
|
131
131
|
/** Search mode: code_interpreter uses external sandbox, local uses safe substring matching */
|
|
132
132
|
export type ToolSearchMode = 'code_interpreter' | 'local';
|
|
133
133
|
|
|
134
|
-
/** Parameters for creating a Tool Search
|
|
134
|
+
/** Parameters for creating a Tool Search tool */
|
|
135
135
|
export type ToolSearchParams = {
|
|
136
136
|
apiKey?: string;
|
|
137
137
|
toolRegistry?: LCToolRegistry;
|
|
@@ -139,6 +139,8 @@ export type ToolSearchParams = {
|
|
|
139
139
|
baseUrl?: string;
|
|
140
140
|
/** Search mode: 'code_interpreter' (default) uses sandbox for regex, 'local' uses safe substring matching */
|
|
141
141
|
mode?: ToolSearchMode;
|
|
142
|
+
/** Filter tools to only those from specific MCP server(s). Can be a single name or array of names. */
|
|
143
|
+
mcpServer?: string | string[];
|
|
142
144
|
[key: string]: unknown;
|
|
143
145
|
};
|
|
144
146
|
|