Skip to content

Commit 0df8ebf

Browse files
committed
fix: ensure consistent output for structured content in tool conversion
1 parent e5c83b5 commit 0df8ebf

File tree

2 files changed

+16
-34
lines changed

2 files changed

+16
-34
lines changed

langchain_mcp_adapters/tools.py

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@
3131
NonTextContent: TypeAlias = ImageContent | EmbeddedResource | TextResourceContents
3232
MAX_ITERATIONS = 1000
3333

34-
# Define at module level
35-
JSONValue: TypeAlias = (
36-
str | int | float | bool | None |
37-
dict[str, "JSONValue"] | list["JSONValue"]
38-
)
34+
3935

4036

4137
def _convert_call_tool_result(
@@ -68,10 +64,6 @@ def _convert_call_tool_result(
6864
raise ToolException(tool_content)
6965

7066
artifact = _build_artifact(non_text_contents, call_tool_result)
71-
structured = getattr(call_tool_result, "structuredContent", None)
72-
73-
if structured is not None:
74-
return _add_structured_content_to_result(tool_content, structured, artifact)
7567

7668
return tool_content, (artifact if artifact else None)
7769

@@ -118,22 +110,6 @@ def _build_artifact(
118110
return artifact
119111

120112

121-
def _add_structured_content_to_result(
122-
tool_content: str | list[str],
123-
structured: JSONValue,
124-
artifact: dict[str, Any],
125-
) -> tuple[list[Any], dict[str, Any] | None]:
126-
"""Add structured content to result as JSON block."""
127-
content_blocks: list[Any] = []
128-
if isinstance(tool_content, str):
129-
if tool_content:
130-
content_blocks.append(tool_content)
131-
else:
132-
content_blocks.extend(tool_content)
133-
content_blocks.append({"type": "json", "structured": structured})
134-
return content_blocks, (artifact if artifact else None)
135-
136-
137113
async def _list_all_tools(session: ClientSession) -> list[MCPTool]:
138114
"""List all available tools from an MCP session with pagination support.
139115

tests/test_tools.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -548,15 +548,14 @@ def test_convert_with_structured_content():
548548
)
549549
result.structuredContent = structured_data
550550

551-
content_blocks, artifact = _convert_call_tool_result(result)
551+
text_content, artifact = _convert_call_tool_result(result)
552552

553-
assert content_blocks[0] == "Search completed"
554-
assert content_blocks[1] == {"type": "json", "structured": structured_data}
553+
assert text_content == "Search completed"
555554
assert artifact["structuredContent"] == structured_data
556555

557556

558557
def test_convert_structured_content_includes_json_block():
559-
"""Test that structuredContent is included as JSON block in content."""
558+
"""Test that structuredContent is included in artifact only."""
560559
structured_data = {"result": "success"}
561560

562561
result = CallToolResult(
@@ -565,11 +564,11 @@ def test_convert_structured_content_includes_json_block():
565564
)
566565
result.structuredContent = structured_data
567566

568-
content_blocks, artifact = _convert_call_tool_result(result)
567+
content, artifact = _convert_call_tool_result(result)
569568

570-
assert isinstance(content_blocks, list)
571-
assert content_blocks[0] == "Done"
572-
assert content_blocks[1] == {"type": "json", "structured": structured_data}
569+
# Content stays simple - just the text
570+
assert content == "Done"
571+
# Structured data goes in artifact
573572
assert artifact["structuredContent"] == structured_data
574573

575574

@@ -580,7 +579,14 @@ def test_convert_with_structured_content_only():
580579
result = CallToolResult(content=[], isError=False)
581580
result.structuredContent = structured_data
582581

583-
content_blocks, artifact = _convert_call_tool_result(result)
582+
content, artifact = _convert_call_tool_result(result)
584583

584+
<<<<<<< HEAD
585585
assert content_blocks == [{"type": "json", "structured": structured_data}]
586586
assert artifact["structuredContent"] == structured_data
587+
=======
588+
# Empty text content returns empty string
589+
assert content == ""
590+
# Structured data goes in artifact
591+
assert artifact["structuredContent"] == structured_data
592+
>>>>>>> 81f6de3 (fix: ensure consistent output for structured content in tool conversion)

0 commit comments

Comments
 (0)