Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions libs/langchain-mcp-adapters/__tests__/tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,56 @@ describe("Simplified Tool Adapter Tests", () => {
});
});

test("should load tool with no input parameters", async () => {
// Set up mock response
mockClient.listTools.mockReturnValueOnce(
Promise.resolve({
tools: [
{
name: "weather",
description: "Get the current weather",
inputSchema: {
type: "object",
},
},
],
})
);

mockClient.callTool.mockImplementation((params) => {
return Promise.resolve({
content: [
{
type: "text",
text: `It is currently 70 degrees and cloudy.`,
},
],
});
});

// Load tools
const tools = await loadMcpTools(
"mockServer(should load tool with no input parameters)",
mockClient as Client
);

// Verify results
expect(tools.length).toBe(1);
expect(tools[0].name).toBe("weather");

const weatherTool = tools[0];

// should invoke the tool when input is valid
await expect(weatherTool.invoke({})).resolves.toEqual(
"It is currently 70 degrees and cloudy."
);

expect(mockClient.callTool).toHaveBeenCalledWith({
arguments: undefined,
name: "weather",
});
});

test("should handle empty tool list", async () => {
// Set up mock response
mockClient.listTools.mockReturnValueOnce(
Expand Down
37 changes: 25 additions & 12 deletions libs/langchain-mcp-adapters/src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
Tool as MCPTool,
} from "@modelcontextprotocol/sdk/types.js";
import {
DynamicTool,
DynamicStructuredTool,
type DynamicStructuredToolInput,
type StructuredToolInterface,
Expand Down Expand Up @@ -264,18 +265,30 @@ export async function loadMcpTools(
.filter((tool: MCPTool) => !!tool.name)
.map(async (tool: MCPTool) => {
try {
const dst = new DynamicStructuredTool({
name: `${toolNamePrefix}${tool.name}`,
description: tool.description || "",
schema: tool.inputSchema,
responseFormat: "content_and_artifact",
func: _callTool.bind(
null,
serverName,
tool.name,
client
) as DynamicStructuredToolInput["func"],
});
const dst = tool.inputSchema?.properties
? new DynamicStructuredTool({
name: `${toolNamePrefix}${tool.name}`,
description: tool.description || "",
schema: tool.inputSchema,
responseFormat: "content_and_artifact",
func: _callTool.bind(
null,
serverName,
tool.name,
client
) as DynamicStructuredToolInput["func"],
})
: new DynamicTool({
name: `${toolNamePrefix}${tool.name}`,
description: tool.description || "",
responseFormat: "content_and_artifact",
func: _callTool.bind(
null,
serverName,
tool.name,
client
) as DynamicStructuredToolInput["func"],
});
getDebugLog()(`INFO: Successfully loaded tool: ${dst.name}`);
return dst;
} catch (error) {
Expand Down