Skip to content

Commit 81f6de3

Browse files
committed
fix: ensure consistent output for structured content in tool conversion
1 parent 7b27808 commit 81f6de3

File tree

2 files changed

+12
-35
lines changed

2 files changed

+12
-35
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: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -466,15 +466,14 @@ def test_convert_with_structured_content():
466466
)
467467
result.structuredContent = structured_data
468468

469-
content_blocks, artifact = _convert_call_tool_result(result)
469+
text_content, artifact = _convert_call_tool_result(result)
470470

471-
assert content_blocks[0] == "Search completed"
472-
assert content_blocks[1] == {"type": "json", "structured": structured_data}
471+
assert text_content == "Search completed"
473472
assert artifact["structuredContent"] == structured_data
474473

475474

476475
def test_convert_structured_content_includes_json_block():
477-
"""Test that structuredContent is included as JSON block in content."""
476+
"""Test that structuredContent is included in artifact only."""
478477
structured_data = {"result": "success"}
479478

480479
result = CallToolResult(
@@ -483,11 +482,11 @@ def test_convert_structured_content_includes_json_block():
483482
)
484483
result.structuredContent = structured_data
485484

486-
content_blocks, artifact = _convert_call_tool_result(result)
485+
content, artifact = _convert_call_tool_result(result)
487486

488-
assert isinstance(content_blocks, list)
489-
assert content_blocks[0] == "Done"
490-
assert content_blocks[1] == {"type": "json", "structured": structured_data}
487+
# Content stays simple - just the text
488+
assert content == "Done"
489+
# Structured data goes in artifact
491490
assert artifact["structuredContent"] == structured_data
492491

493492

@@ -498,7 +497,9 @@ def test_convert_with_structured_content_only():
498497
result = CallToolResult(content=[], isError=False)
499498
result.structuredContent = structured_data
500499

501-
content_blocks, artifact = _convert_call_tool_result(result)
500+
content, artifact = _convert_call_tool_result(result)
502501

503-
assert content_blocks == [{"type": "json", "structured": structured_data}]
502+
# Empty text content returns empty string
503+
assert content == ""
504+
# Structured data goes in artifact
504505
assert artifact["structuredContent"] == structured_data

0 commit comments

Comments
 (0)