|
11 | 11 | ImageContent,
|
12 | 12 | TextContent,
|
13 | 13 | TextResourceContents,
|
| 14 | + ToolAnnotations, |
14 | 15 | )
|
15 | 16 | from mcp.types import Tool as MCPTool
|
16 | 17 | from pydantic import BaseModel
|
@@ -468,3 +469,71 @@ def custom_httpx_client_factory(
|
468 | 469 | # Expected to fail since server doesn't have SSE endpoint,
|
469 | 470 | # but the important thing is that httpx_client_factory was passed correctly
|
470 | 471 | pass
|
| 472 | + |
| 473 | + |
| 474 | +@pytest.mark.asyncio |
| 475 | +async def test_convert_mcp_tool_metadata_variants(): |
| 476 | + """Verify metadata merging rules in convert_mcp_tool_to_langchain_tool.""" |
| 477 | + tool_input_schema = { |
| 478 | + "properties": {}, |
| 479 | + "required": [], |
| 480 | + "title": "EmptySchema", |
| 481 | + "type": "object", |
| 482 | + } |
| 483 | + |
| 484 | + session = AsyncMock() |
| 485 | + session.call_tool.return_value = CallToolResult( |
| 486 | + content=[TextContent(type="text", text="ok")], isError=False |
| 487 | + ) |
| 488 | + |
| 489 | + mcp_tool_none = MCPTool( |
| 490 | + name="t_none", |
| 491 | + description="", |
| 492 | + inputSchema=tool_input_schema, |
| 493 | + ) |
| 494 | + lc_tool_none = convert_mcp_tool_to_langchain_tool(session, mcp_tool_none) |
| 495 | + assert lc_tool_none.metadata is None |
| 496 | + |
| 497 | + mcp_tool_ann = MCPTool( |
| 498 | + name="t_ann", |
| 499 | + description="", |
| 500 | + inputSchema=tool_input_schema, |
| 501 | + annotations=ToolAnnotations( |
| 502 | + title="Title", readOnlyHint=True, idempotentHint=False |
| 503 | + ), |
| 504 | + ) |
| 505 | + lc_tool_ann = convert_mcp_tool_to_langchain_tool(session, mcp_tool_ann) |
| 506 | + assert lc_tool_ann.metadata == { |
| 507 | + "title": "Title", |
| 508 | + "readOnlyHint": True, |
| 509 | + "idempotentHint": False, |
| 510 | + "destructiveHint": None, |
| 511 | + "openWorldHint": None, |
| 512 | + } |
| 513 | + |
| 514 | + mcp_tool_meta = MCPTool( |
| 515 | + name="t_meta", |
| 516 | + description="", |
| 517 | + inputSchema=tool_input_schema, |
| 518 | + meta={"source": "unit-test", "version": 1}, |
| 519 | + ) |
| 520 | + lc_tool_meta = convert_mcp_tool_to_langchain_tool(session, mcp_tool_meta) |
| 521 | + assert lc_tool_meta.metadata == {"_meta": {"source": "unit-test", "version": 1}} |
| 522 | + |
| 523 | + mcp_tool_both = MCPTool( |
| 524 | + name="t_both", |
| 525 | + description="", |
| 526 | + inputSchema=tool_input_schema, |
| 527 | + annotations=ToolAnnotations(title="Both"), |
| 528 | + meta={"flag": True}, |
| 529 | + ) |
| 530 | + |
| 531 | + lc_tool_both = convert_mcp_tool_to_langchain_tool(session, mcp_tool_both) |
| 532 | + assert lc_tool_both.metadata == { |
| 533 | + "title": "Both", |
| 534 | + "readOnlyHint": None, |
| 535 | + "idempotentHint": None, |
| 536 | + "destructiveHint": None, |
| 537 | + "openWorldHint": None, |
| 538 | + "_meta": {"flag": True}, |
| 539 | + } |
0 commit comments