Skip to content

Commit 085a20c

Browse files
authored
return tool result directly (#52)
1 parent bfcd180 commit 085a20c

File tree

1 file changed

+1
-47
lines changed

1 file changed

+1
-47
lines changed

src/adapters/langchain_adapter.ts

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ import type { JSONSchema } from '@dmitryrechkin/json-schema-to-zod'
22
import type { StructuredToolInterface } from '@langchain/core/tools'
33
import type {
44
CallToolResult,
5-
EmbeddedResource,
6-
ImageContent,
75
Tool as MCPTool,
8-
TextContent,
96
} from '@modelcontextprotocol/sdk/types.js'
107
import type { ZodTypeAny } from 'zod'
118
import type { BaseConnector } from '../connectors/base.js'
@@ -26,49 +23,6 @@ function schemaToZod(schema: unknown): ZodTypeAny {
2623
}
2724
}
2825

29-
function parseMcpToolResult(toolResult: CallToolResult): string {
30-
if (toolResult.isError) {
31-
throw new Error(`Tool execution failed: ${toolResult.content}`)
32-
}
33-
if (!toolResult.content || toolResult.content.length === 0) {
34-
throw new Error('Tool execution returned no content')
35-
}
36-
37-
let decoded = ''
38-
for (const item of toolResult.content) {
39-
switch (item.type) {
40-
case 'text': {
41-
decoded += (item as TextContent).text
42-
break
43-
}
44-
case 'image': {
45-
decoded += (item as ImageContent).data
46-
break
47-
}
48-
case 'resource': {
49-
const res = (item as EmbeddedResource).resource
50-
if (res?.text !== undefined) {
51-
decoded += res.text
52-
}
53-
else if (res?.blob !== undefined) {
54-
// eslint-disable-next-line node/prefer-global/buffer
55-
decoded += res.blob instanceof Uint8Array || res.blob instanceof Buffer
56-
// eslint-disable-next-line node/prefer-global/buffer
57-
? Buffer.from(res.blob).toString('base64')
58-
: String(res.blob)
59-
}
60-
else {
61-
throw new Error(`Unexpected resource type: ${res?.type}`)
62-
}
63-
break
64-
}
65-
default:
66-
throw new Error(`Unexpected content type: ${(item as any).type}`)
67-
}
68-
}
69-
return decoded
70-
}
71-
7226
export class LangChainAdapter extends BaseAdapter<StructuredToolInterface> {
7327
constructor(disallowedTools: string[] = []) {
7428
super(disallowedTools)
@@ -99,7 +53,7 @@ export class LangChainAdapter extends BaseAdapter<StructuredToolInterface> {
9953
logger.debug(`MCP tool \"${mcpTool.name}\" received input: ${JSON.stringify(input)}`)
10054
try {
10155
const result: CallToolResult = await connector.callTool(mcpTool.name, input)
102-
return parseMcpToolResult(result)
56+
return JSON.stringify(result)
10357
}
10458
catch (err: any) {
10559
logger.error(`Error executing MCP tool: ${err.message}`)

0 commit comments

Comments
 (0)