fix: handle Pydantic TextContent objects in MCP response parser#14
Merged
fix: handle Pydantic TextContent objects in MCP response parser#14
Conversation
The MCP SDK returns Pydantic TextContent objects, not plain dicts. The response_parser.py was using .get() method which only works on dicts, causing AttributeError: 'TextContent' object has no attribute 'get'. This change adds a helper function that handles both dict and Pydantic object access patterns, ensuring compatibility with both the MCP SDK's actual return types and the test suite's mock dicts. Tests added for: - Pydantic TextContent objects - Mixed dict and Pydantic content - Empty Pydantic text content handling Fixes regression introduced in v1.0.3 where parsing logic was added that assumed dict objects. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This refactor addresses the architectural issue identified in code review: the response parser was handling both parsing AND protocol translation, violating single responsibility principle. Changes: 1. Added _serialize_mcp_content() to MCPAdapter to convert Pydantic objects to dicts at the protocol boundary 2. Simplified parse_mcp_content() to only handle plain dicts 3. Removed get_field() helper function (no longer needed) 4. Removed Pydantic mock tests from response_parser tests 5. Added serialization tests to protocol adapter tests Benefits: - Clean separation: adapters translate, parsers parse - Simpler type signatures: list[dict[str, Any]] instead of | Any - Tests use plain dicts (no MCP SDK dependency in parser tests) - More obvious code (removed clever helper function) - Follows adapter pattern correctly The fix maintains backward compatibility while establishing proper architectural boundaries for future MCP content types (images, resources). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Mypy now correctly infers types after hasattr and callable checks, making the type: ignore[attr-defined] comment unnecessary. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes AttributeError in v1.0.3 where response parser couldn't handle MCP SDK's Pydantic
TextContentobjects, and refactors the architecture to establish proper separation of concerns.Problem
The MCP SDK returns Pydantic objects (e.g.,
TextContent), not plain dicts. In v1.0.3, response parsing logic was added that useditem.get("type")anditem.get("text"), which only works on dicts:This was a regression - v1.0.2 worked correctly.
Root Cause
The MCP adapter was passing MCP SDK-specific Pydantic objects directly to the generic response parser, violating protocol boundary separation. The parser had to handle both parsing AND protocol translation.
Solution
Architectural fix with proper separation of concerns:
mcp.py): Added_serialize_mcp_content()to convert Pydantic objects to plain dicts at the protocol boundaryresponse_parser.py): Simplified to only handle plain dicts, removed dual-mode helper functionBenefits
list[dict[str, Any]](no more| Any)Commits
get_field()helper (f29179b)Tests
Response Parser Tests:
Protocol Adapter Tests:
All tests passing (15/15 for affected modules).
Test plan
🤖 Generated with Claude Code