Skip to content
This repository was archived by the owner on May 21, 2026. It is now read-only.
Merged
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
48 changes: 1 addition & 47 deletions src/adapters/langchain_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<StructuredToolInterface> {
constructor(disallowedTools: string[] = []) {
super(disallowedTools)
Expand Down Expand Up @@ -99,7 +53,7 @@ export class LangChainAdapter extends BaseAdapter<StructuredToolInterface> {
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}`)
Expand Down