diff --git a/e2e/specs/mock/bedrock-descriptions.spec.ts b/e2e/specs/mock/bedrock-descriptions.spec.ts new file mode 100644 index 0000000000..d05f01d1a0 --- /dev/null +++ b/e2e/specs/mock/bedrock-descriptions.spec.ts @@ -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((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', + }); + } + } +}); diff --git a/packages/api/src/mcp/description.spec.ts b/packages/api/src/mcp/description.spec.ts new file mode 100644 index 0000000000..0a0b697e11 --- /dev/null +++ b/packages/api/src/mcp/description.spec.ts @@ -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' }, + }); + }, +); diff --git a/packages/api/src/mcp/tools.ts b/packages/api/src/mcp/tools.ts index fb6de6632b..621dbc7ad9 100644 --- a/packages/api/src/mcp/tools.ts +++ b/packages/api/src/mcp/tools.ts @@ -33,6 +33,7 @@ export function createMCPStructuredTool( ): DynamicStructuredTool { 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 }; diff --git a/packages/api/src/tools/definitions.spec.ts b/packages/api/src/tools/definitions.spec.ts index 2746687ca9..6ede0dcfe2 100644 --- a/packages/api/src/tools/definitions.spec.ts +++ b/packages/api/src/tools/definitions.spec.ts @@ -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`); }); }); diff --git a/packages/api/src/tools/definitions.ts b/packages/api/src/tools/definitions.ts index 41b83f8c82..399c95ba42 100644 --- a/packages/api/src/tools/definitions.ts +++ b/packages/api/src/tools/definitions.ts @@ -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,