mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-19 22:43:38 +00:00
🪨 fix: Supply Nonempty MCP Tool Descriptions for Bedrock
Supply nonempty MCP tool descriptions for Bedrock while preserving existing descriptions and approval identities. Fixes #18013
This commit is contained in:
parent
ddbff5ac81
commit
24da3c92ed
5 changed files with 175 additions and 7 deletions
98
e2e/specs/mock/bedrock-descriptions.spec.ts
Normal file
98
e2e/specs/mock/bedrock-descriptions.spec.ts
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
import http from 'node:http';
|
||||
import { once } from 'node:events';
|
||||
import { expect, test } from '@playwright/test';
|
||||
import { ChatBedrockConverse } from '@langchain/aws';
|
||||
import { NodeHttpHandler } from '@smithy/node-http-handler';
|
||||
import { Providers, createSchemaOnlyTools } from '@librechat/agents';
|
||||
import { BedrockRuntimeClient } from '@aws-sdk/client-bedrock-runtime';
|
||||
import { createMCPStructuredTool, loadToolDefinitions } from '@librechat/api';
|
||||
import type { StructuredToolInterface } from '@librechat/agents/langchain/tools';
|
||||
|
||||
/** Capture the actual SDK HTTP body without credentials or a remote provider. */
|
||||
async function captureRequest(tools: StructuredToolInterface[]) {
|
||||
let body = '';
|
||||
const server = http.createServer(async (request, response) => {
|
||||
for await (const chunk of request) {
|
||||
body += chunk.toString();
|
||||
}
|
||||
response.setHeader('Content-Type', 'application/json');
|
||||
response.end(
|
||||
JSON.stringify({
|
||||
output: { message: { role: 'assistant', content: [{ text: 'OK' }] } },
|
||||
stopReason: 'end_turn',
|
||||
usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
|
||||
metrics: { latencyMs: 1 },
|
||||
}),
|
||||
);
|
||||
});
|
||||
server.listen(0, '127.0.0.1');
|
||||
await once(server, 'listening');
|
||||
const address = server.address();
|
||||
if (address == null || typeof address === 'string') {
|
||||
throw new Error('Expected a local HTTP port');
|
||||
}
|
||||
const client = new BedrockRuntimeClient({
|
||||
region: 'us-east-1',
|
||||
endpoint: `http://127.0.0.1:${address.port}`,
|
||||
requestHandler: new NodeHttpHandler(),
|
||||
credentials: { accessKeyId: 'test', secretAccessKey: 'test' },
|
||||
});
|
||||
try {
|
||||
const model = new ChatBedrockConverse({
|
||||
model: 'global.openai.gpt-6.1-sol',
|
||||
region: 'us-east-1',
|
||||
client,
|
||||
});
|
||||
await model.invoke('Hello', { tools });
|
||||
return JSON.parse(body) as {
|
||||
toolConfig: { tools: { toolSpec: { name: string; description: string } }[] };
|
||||
};
|
||||
} finally {
|
||||
client.destroy();
|
||||
await new Promise<void>((resolve, reject) =>
|
||||
server.close((error) => (error ? reject(error) : resolve())),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
test('runtime MCP tools have valid descriptions @scenario:runtime-mcp-tools-without-descriptions-send-nonempty-bedrock-descriptions', async () => {
|
||||
const tools = ['', 'Search the web'].map((description, index) =>
|
||||
createMCPStructuredTool(async () => 'result', {
|
||||
name: `search_${index}`,
|
||||
description,
|
||||
schema: { type: 'object', properties: {} },
|
||||
}),
|
||||
);
|
||||
const body = await captureRequest(tools);
|
||||
expect(body.toolConfig.tools.map(({ toolSpec }) => toolSpec.description)).toEqual([
|
||||
'search_0 tool',
|
||||
'Search the web',
|
||||
]);
|
||||
});
|
||||
|
||||
test('named and wildcard definitions have valid descriptions @scenario:named-and-wildcard-mcp-definitions-without-descriptions-send-nonempty-bedrock-descriptions', async () => {
|
||||
for (const selection of ['sys__all__sys_mcp_gateway', 'search_mcp_gateway']) {
|
||||
for (const description of [undefined, '', 'Search the web']) {
|
||||
const result = await loadToolDefinitions(
|
||||
{ userId: 'user', agentId: 'agent', tools: [selection], provider: Providers.BEDROCK },
|
||||
{
|
||||
isBuiltInTool: () => false,
|
||||
getOrFetchMCPServerTools: async () => ({
|
||||
search_mcp_gateway: {
|
||||
function: {
|
||||
name: 'search_mcp_gateway',
|
||||
description,
|
||||
parameters: { type: 'object', properties: {} },
|
||||
},
|
||||
},
|
||||
}),
|
||||
},
|
||||
);
|
||||
const body = await captureRequest(createSchemaOnlyTools(result.toolDefinitions));
|
||||
expect(body.toolConfig.tools[0].toolSpec).toMatchObject({
|
||||
name: 'search_mcp_gateway',
|
||||
description: description || 'search_mcp_gateway tool',
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
23
packages/api/src/mcp/description.spec.ts
Normal file
23
packages/api/src/mcp/description.spec.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import { z } from 'zod';
|
||||
import { ChatBedrockConverse } from '@langchain/aws';
|
||||
import { createMCPStructuredTool } from './tools';
|
||||
|
||||
it.each(['', 'Search the web'])(
|
||||
'preserves valid descriptions or supplies a fallback for runtime MCP tools (%p)',
|
||||
(description) => {
|
||||
const tool = createMCPStructuredTool(async () => 'result', {
|
||||
name: 'web_search',
|
||||
description,
|
||||
schema: z.object({}),
|
||||
});
|
||||
const model = new ChatBedrockConverse({
|
||||
model: 'amazon.nova-pro-v1:0',
|
||||
region: 'us-east-1',
|
||||
credentials: { accessKeyId: 'test', secretAccessKey: 'test' },
|
||||
});
|
||||
const params = model.invocationParams({ tools: [tool] });
|
||||
expect(params.toolConfig?.tools?.[0]).toMatchObject({
|
||||
toolSpec: { name: 'web_search', description: description || 'web_search tool' },
|
||||
});
|
||||
},
|
||||
);
|
||||
|
|
@ -33,6 +33,7 @@ export function createMCPStructuredTool(
|
|||
): DynamicStructuredTool<unknown> {
|
||||
const tool = new DynamicStructuredTool({
|
||||
...fields,
|
||||
description: fields.description || `${fields.name} tool`,
|
||||
func: async (input, runManager, config) => {
|
||||
const invocation = config as typeof config & {
|
||||
toolCall?: { id?: string };
|
||||
|
|
|
|||
|
|
@ -1,4 +1,10 @@
|
|||
import { Providers, WebSearchToolDefinition, GitHubCompareToolDefinition } from '@librechat/agents';
|
||||
import { ChatBedrockConverse } from '@langchain/aws';
|
||||
import {
|
||||
Providers,
|
||||
createSchemaOnlyTools,
|
||||
WebSearchToolDefinition,
|
||||
GitHubCompareToolDefinition,
|
||||
} from '@librechat/agents';
|
||||
import type {
|
||||
LoadToolDefinitionsParams,
|
||||
LoadToolDefinitionsResult,
|
||||
|
|
@ -1057,7 +1063,47 @@ describe('definitions.ts', () => {
|
|||
expect((toolDef as { serverName?: string }).serverName).toBe('Connector__Company');
|
||||
});
|
||||
|
||||
it('should convert empty MCP tool descriptions to undefined', async () => {
|
||||
it.each(['sys__all__sys_mcp_gateway', 'web_search_mcp_gateway'])(
|
||||
'sends valid Bedrock tool descriptions for %s',
|
||||
async (selection) => {
|
||||
const toolName = 'web_search_mcp_gateway';
|
||||
for (const description of [undefined, '', 'Search the web']) {
|
||||
mockGetOrFetchMCPServerTools.mockResolvedValue({
|
||||
[toolName]: {
|
||||
function: {
|
||||
name: toolName,
|
||||
description,
|
||||
parameters: { type: 'object', properties: {} },
|
||||
},
|
||||
},
|
||||
});
|
||||
const result = await loadToolDefinitions(
|
||||
{ userId: 'user', agentId: 'agent', tools: [selection], provider: Providers.BEDROCK },
|
||||
{
|
||||
getOrFetchMCPServerTools: mockGetOrFetchMCPServerTools,
|
||||
isBuiltInTool: mockIsBuiltInTool,
|
||||
},
|
||||
);
|
||||
const model = new ChatBedrockConverse({
|
||||
model: 'global.openai.gpt-6.1-sol',
|
||||
region: 'us-east-1',
|
||||
credentials: { accessKeyId: 'test', secretAccessKey: 'test' },
|
||||
});
|
||||
const params = model.invocationParams({
|
||||
tools: createSchemaOnlyTools(result.toolDefinitions),
|
||||
});
|
||||
expect(params.toolConfig?.tools?.[0]).toEqual({
|
||||
toolSpec: {
|
||||
name: toolName,
|
||||
description: description || `${toolName} tool`,
|
||||
inputSchema: { json: { type: 'object', properties: {} } },
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
it('should supply fallback descriptions for empty MCP tool descriptions', async () => {
|
||||
const mockServerTools = {
|
||||
no_desc_tool_mcp_asana: {
|
||||
function: {
|
||||
|
|
@ -1092,14 +1138,14 @@ describe('definitions.ts', () => {
|
|||
|
||||
const noDef = result.toolDefinitions.find((d) => d.name === 'no_desc_tool_mcp_asana');
|
||||
expect(noDef).toBeDefined();
|
||||
expect(noDef?.description).toBeUndefined();
|
||||
expect(noDef?.description).toBe('no_desc_tool_mcp_asana tool');
|
||||
|
||||
const hasDef = result.toolDefinitions.find((d) => d.name === 'has_desc_tool_mcp_asana');
|
||||
expect(hasDef).toBeDefined();
|
||||
expect(hasDef?.description).toBe('List tasks');
|
||||
});
|
||||
|
||||
it('should convert empty description to undefined for directly named MCP tool', async () => {
|
||||
it('should supply a fallback description for a directly named MCP tool', async () => {
|
||||
const toolName = 'no_desc_tool_mcp_asana';
|
||||
mockGetOrFetchMCPServerTools.mockResolvedValue({
|
||||
[toolName]: {
|
||||
|
|
@ -1126,7 +1172,7 @@ describe('definitions.ts', () => {
|
|||
|
||||
const def = result.toolDefinitions.find((d) => d.name === toolName);
|
||||
expect(def).toBeDefined();
|
||||
expect(def?.description).toBeUndefined();
|
||||
expect(def?.description).toBe(`${toolName} tool`);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -454,7 +454,7 @@ export async function loadToolDefinitions(
|
|||
bindToolApprovalIdentity(
|
||||
{
|
||||
name: actualToolName,
|
||||
description: toolDef.function.description || undefined,
|
||||
description: toolDef.function.description || `${actualToolName} tool`,
|
||||
parameters: buildMcpParameters(toolDef.function.parameters),
|
||||
serverName,
|
||||
serverToolName: toolDef.serverToolName,
|
||||
|
|
@ -484,7 +484,7 @@ export async function loadToolDefinitions(
|
|||
bindToolApprovalIdentity(
|
||||
{
|
||||
name: toolName,
|
||||
description: toolMatch.def.function.description || undefined,
|
||||
description: toolMatch.def.function.description || `${toolName} tool`,
|
||||
parameters: buildMcpParameters(toolMatch.def.function.parameters),
|
||||
serverName,
|
||||
serverToolName: toolMatch.def.serverToolName,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue