From c74e4b1f7d4511b7aa99650693260bb2557416f6 Mon Sep 17 00:00:00 2001 From: Luigi Pederzani Date: Sun, 5 Oct 2025 11:57:30 +0200 Subject: [PATCH] return tool result directly --- src/adapters/langchain_adapter.ts | 48 +------------------------------ 1 file changed, 1 insertion(+), 47 deletions(-) diff --git a/src/adapters/langchain_adapter.ts b/src/adapters/langchain_adapter.ts index cec04b0d..4dadc2c2 100644 --- a/src/adapters/langchain_adapter.ts +++ b/src/adapters/langchain_adapter.ts @@ -2,10 +2,7 @@ import type { JSONSchema } from '@dmitryrechkin/json-schema-to-zod' import type { StructuredToolInterface } from '@langchain/core/tools' import type { CallToolResult, - EmbeddedResource, - ImageContent, Tool as MCPTool, - TextContent, } from '@modelcontextprotocol/sdk/types.js' import type { ZodTypeAny } from 'zod' import type { BaseConnector } from '../connectors/base.js' @@ -26,49 +23,6 @@ function schemaToZod(schema: unknown): ZodTypeAny { } } -function parseMcpToolResult(toolResult: CallToolResult): string { - if (toolResult.isError) { - throw new Error(`Tool execution failed: ${toolResult.content}`) - } - if (!toolResult.content || toolResult.content.length === 0) { - throw new Error('Tool execution returned no content') - } - - let decoded = '' - for (const item of toolResult.content) { - switch (item.type) { - case 'text': { - decoded += (item as TextContent).text - break - } - case 'image': { - decoded += (item as ImageContent).data - break - } - case 'resource': { - const res = (item as EmbeddedResource).resource - if (res?.text !== undefined) { - decoded += res.text - } - else if (res?.blob !== undefined) { - // eslint-disable-next-line node/prefer-global/buffer - decoded += res.blob instanceof Uint8Array || res.blob instanceof Buffer - // eslint-disable-next-line node/prefer-global/buffer - ? Buffer.from(res.blob).toString('base64') - : String(res.blob) - } - else { - throw new Error(`Unexpected resource type: ${res?.type}`) - } - break - } - default: - throw new Error(`Unexpected content type: ${(item as any).type}`) - } - } - return decoded -} - export class LangChainAdapter extends BaseAdapter { constructor(disallowedTools: string[] = []) { super(disallowedTools) @@ -99,7 +53,7 @@ export class LangChainAdapter extends BaseAdapter { logger.debug(`MCP tool \"${mcpTool.name}\" received input: ${JSON.stringify(input)}`) try { const result: CallToolResult = await connector.callTool(mcpTool.name, input) - return parseMcpToolResult(result) + return JSON.stringify(result) } catch (err: any) { logger.error(`Error executing MCP tool: ${err.message}`)