|
| 1 | +from unittest.mock import AsyncMock, MagicMock |
| 2 | + |
| 3 | +import pytest |
| 4 | +from langchain_core.messages import ToolMessage |
| 5 | +from langchain_core.tools import BaseTool, ToolException |
| 6 | +from mcp.types import ( |
| 7 | + CallToolResult, |
| 8 | + EmbeddedResource, |
| 9 | + ImageContent, |
| 10 | + TextContent, |
| 11 | + TextResourceContents, |
| 12 | +) |
| 13 | +from mcp.types import Tool as MCPTool |
| 14 | + |
| 15 | +from langchain_mcp_adapters.tools import ( |
| 16 | + _convert_call_tool_result, |
| 17 | + convert_mcp_tool_to_langchain_tool, |
| 18 | + load_mcp_tools, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def test_convert_single_text_content(): |
| 23 | + # Test with a single text content |
| 24 | + result = CallToolResult( |
| 25 | + content=[TextContent(type="text", text="test result")], |
| 26 | + isError=False, |
| 27 | + ) |
| 28 | + |
| 29 | + text_content, non_text_content = _convert_call_tool_result(result) |
| 30 | + |
| 31 | + assert text_content == "test result" |
| 32 | + assert non_text_content is None |
| 33 | + |
| 34 | + |
| 35 | +def test_convert_multiple_text_contents(): |
| 36 | + # Test with multiple text contents |
| 37 | + result = CallToolResult( |
| 38 | + content=[ |
| 39 | + TextContent(type="text", text="result 1"), |
| 40 | + TextContent(type="text", text="result 2"), |
| 41 | + ], |
| 42 | + isError=False, |
| 43 | + ) |
| 44 | + |
| 45 | + text_content, non_text_content = _convert_call_tool_result(result) |
| 46 | + |
| 47 | + assert text_content == ["result 1", "result 2"] |
| 48 | + assert non_text_content is None |
| 49 | + |
| 50 | + |
| 51 | +def test_convert_with_non_text_content(): |
| 52 | + # Test with non-text content |
| 53 | + image_content = ImageContent(type="image", mimeType="image/png", data="base64data") |
| 54 | + resource_content = EmbeddedResource( |
| 55 | + type="resource", |
| 56 | + resource=TextResourceContents(uri="resource://test", mimeType="text/plain", text="hi"), |
| 57 | + ) |
| 58 | + |
| 59 | + result = CallToolResult( |
| 60 | + content=[ |
| 61 | + TextContent(type="text", text="text result"), |
| 62 | + image_content, |
| 63 | + resource_content, |
| 64 | + ], |
| 65 | + isError=False, |
| 66 | + ) |
| 67 | + |
| 68 | + text_content, non_text_content = _convert_call_tool_result(result) |
| 69 | + |
| 70 | + assert text_content == "text result" |
| 71 | + assert non_text_content == [image_content, resource_content] |
| 72 | + |
| 73 | + |
| 74 | +def test_convert_with_error(): |
| 75 | + # Test with error |
| 76 | + result = CallToolResult( |
| 77 | + content=[TextContent(type="text", text="error message")], |
| 78 | + isError=True, |
| 79 | + ) |
| 80 | + |
| 81 | + with pytest.raises(ToolException) as exc_info: |
| 82 | + _convert_call_tool_result(result) |
| 83 | + |
| 84 | + assert str(exc_info.value) == "error message" |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.asyncio |
| 88 | +async def test_convert_mcp_tool_to_langchain_tool(): |
| 89 | + tool_input_schema = { |
| 90 | + "properties": { |
| 91 | + "param1": {"title": "Param1", "type": "string"}, |
| 92 | + "param2": {"title": "Param2", "type": "integer"}, |
| 93 | + }, |
| 94 | + "required": ["param1", "param2"], |
| 95 | + "title": "ToolSchema", |
| 96 | + "type": "object", |
| 97 | + } |
| 98 | + # Mock session and MCP tool |
| 99 | + session = AsyncMock() |
| 100 | + session.call_tool.return_value = CallToolResult( |
| 101 | + content=[TextContent(type="text", text="tool result")], |
| 102 | + isError=False, |
| 103 | + ) |
| 104 | + |
| 105 | + mcp_tool = MCPTool( |
| 106 | + name="test_tool", |
| 107 | + description="Test tool description", |
| 108 | + inputSchema=tool_input_schema, |
| 109 | + ) |
| 110 | + |
| 111 | + # Convert MCP tool to LangChain tool |
| 112 | + lc_tool = convert_mcp_tool_to_langchain_tool(session, mcp_tool) |
| 113 | + |
| 114 | + # Verify the converted tool |
| 115 | + assert lc_tool.name == "test_tool" |
| 116 | + assert lc_tool.description == "Test tool description" |
| 117 | + assert lc_tool.args_schema == tool_input_schema |
| 118 | + |
| 119 | + # Test calling the tool |
| 120 | + result = await lc_tool.ainvoke( |
| 121 | + {"args": {"param1": "test", "param2": 42}, "id": "1", "type": "tool_call"} |
| 122 | + ) |
| 123 | + |
| 124 | + # Verify session.call_tool was called with correct arguments |
| 125 | + session.call_tool.assert_called_once_with("test_tool", {"param1": "test", "param2": 42}) |
| 126 | + |
| 127 | + # Verify result |
| 128 | + assert result == ToolMessage(content="tool result", name="test_tool", tool_call_id="1") |
| 129 | + |
| 130 | + |
| 131 | +@pytest.mark.asyncio |
| 132 | +async def test_load_mcp_tools(): |
| 133 | + tool_input_schema = { |
| 134 | + "properties": { |
| 135 | + "param1": {"title": "Param1", "type": "string"}, |
| 136 | + "param2": {"title": "Param2", "type": "integer"}, |
| 137 | + }, |
| 138 | + "required": ["param1", "param2"], |
| 139 | + "title": "ToolSchema", |
| 140 | + "type": "object", |
| 141 | + } |
| 142 | + # Mock session and list_tools response |
| 143 | + session = AsyncMock() |
| 144 | + mcp_tools = [ |
| 145 | + MCPTool( |
| 146 | + name="tool1", |
| 147 | + description="Tool 1 description", |
| 148 | + inputSchema=tool_input_schema, |
| 149 | + ), |
| 150 | + MCPTool( |
| 151 | + name="tool2", |
| 152 | + description="Tool 2 description", |
| 153 | + inputSchema=tool_input_schema, |
| 154 | + ), |
| 155 | + ] |
| 156 | + session.list_tools.return_value = MagicMock(tools=mcp_tools) |
| 157 | + |
| 158 | + # Mock call_tool to return different results for different tools |
| 159 | + async def mock_call_tool(tool_name, arguments): |
| 160 | + if tool_name == "tool1": |
| 161 | + return CallToolResult( |
| 162 | + content=[TextContent(type="text", text=f"tool1 result with {arguments}")], |
| 163 | + isError=False, |
| 164 | + ) |
| 165 | + else: |
| 166 | + return CallToolResult( |
| 167 | + content=[TextContent(type="text", text=f"tool2 result with {arguments}")], |
| 168 | + isError=False, |
| 169 | + ) |
| 170 | + |
| 171 | + session.call_tool.side_effect = mock_call_tool |
| 172 | + |
| 173 | + # Load MCP tools |
| 174 | + tools = await load_mcp_tools(session) |
| 175 | + |
| 176 | + # Verify the tools |
| 177 | + assert len(tools) == 2 |
| 178 | + assert all(isinstance(tool, BaseTool) for tool in tools) |
| 179 | + assert tools[0].name == "tool1" |
| 180 | + assert tools[1].name == "tool2" |
| 181 | + |
| 182 | + # Test calling the first tool |
| 183 | + result1 = await tools[0].ainvoke( |
| 184 | + {"args": {"param1": "test1", "param2": 1}, "id": "1", "type": "tool_call"} |
| 185 | + ) |
| 186 | + assert result1 == ToolMessage( |
| 187 | + content="tool1 result with {'param1': 'test1', 'param2': 1}", name="tool1", tool_call_id="1" |
| 188 | + ) |
| 189 | + |
| 190 | + # Test calling the second tool |
| 191 | + result2 = await tools[1].ainvoke( |
| 192 | + {"args": {"param1": "test2", "param2": 2}, "id": "2", "type": "tool_call"} |
| 193 | + ) |
| 194 | + assert result2 == ToolMessage( |
| 195 | + content="tool2 result with {'param1': 'test2', 'param2': 2}", name="tool2", tool_call_id="2" |
| 196 | + ) |
0 commit comments