Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/tools/ToolSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ function getDeferredToolsListing(
return '';
}

const toolsByServer: Record<string, string[]> = {};
const toolsByServer: Record<string, string[]> = Object.create(null);

for (const lcTool of toolRegistry.values()) {
if (onlyDeferred && lcTool.defer_loading !== true) {
Expand Down Expand Up @@ -971,7 +971,7 @@ function formatServerListing(
const toolsByServer: Record<
string,
Array<{ name: string; description: string }>
> = {};
> = Object.create(null);
for (const tool of tools) {
const server = extractMcpServerName(tool.name) ?? 'unknown';
if (!(server in toolsByServer)) {
Expand Down
88 changes: 88 additions & 0 deletions src/tools/__tests__/ToolSearch.server-key.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { describe, expect, it } from '@jest/globals';
import type { LCToolRegistry, ToolMetadata } from '@/types';
import {
createToolSearch,
formatServerListing,
getDeferredToolsListing,
} from '../ToolSearch';

function registry(server: string): LCToolRegistry {
return new Map([
[
'weather_mcp_weather',
{
name: 'weather_mcp_weather',
description: 'Read the weather forecast',
defer_loading: true,
},
],
[
`status_mcp_${server}`,
{
name: `status_mcp_${server}`,
description: 'Read service status',
defer_loading: true,
},
],
]);
}

function metadata(server: string): ToolMetadata[] {
return Array.from(registry(server).values(), (tool) => ({
name: tool.name,
description: tool.description ?? '',
}));
}

async function expectWeatherSearch(server: string): Promise<void> {
const search = createToolSearch({
mode: 'local',
toolRegistry: registry(server),
});
const result = await search.invoke({
query: 'weather',
mcp_server: 'weather',
});
expect(JSON.parse(result)).toMatchObject({
found: 1,
tools: [{ name: 'weather_mcp_weather', score: 1 }],
});
}

describe('MCP server grouping accepts ordinary string identifiers', () => {
it('keeps an ordinary two-server listing', () => {
const listing = getDeferredToolsListing(registry('status'), true);
expect(listing).toContain('weather: weather');
expect(listing).toContain('status: status');
});

it('searches the healthy server in the ordinary control', async () => {
await expectWeatherSearch('status');
});

describe.each(['constructor', 'toString', 'hasOwnProperty'])(
'server %s',
(server) => {
it('can build the initial deferred-tool listing', () => {
const listing = getDeferredToolsListing(registry(server), true);
expect(listing).toContain(`${server}: status`);
expect(listing).toContain('weather: weather');
});

it('can format a server preview', () => {
const result = formatServerListing(metadata(server), [server, 'weather']);
expect(JSON.parse(result)).toMatchObject({
total_tools: 2,
tools_by_server: {
[server]: [{ name: `status_mcp_${server}` }],
weather: [{ name: 'weather_mcp_weather' }],
},
});
});

it('does not prevent ranked search for another server', async () => {
await expectWeatherSearch(server);
});
}
);
});