fix(mcp): coerce string-encoded metadata, types, and tags in tool calls#869
Open
octo-patch wants to merge 1 commit intovectorize-io:mainfrom
Open
Conversation
MCP tool bridges (Claude Code, Cursor, etc.) sometimes serialize JSON arrays and objects as strings during transport, causing Pydantic to reject the input with a validation error. The agent then retries with the same broken format, wasting tokens and silently losing memories. Add defensive coercion at two layers: HTTP API (http.py): - RecallRequest: field_validator on types and tags (mode="before") that parses JSON-string arrays back into lists - MemoryItem: field_validator on metadata (mode="before") that parses JSON-string objects back into dicts (tags coercion was already present) MCP tools (mcp_tools.py): - build_content_dict: coerce metadata from JSON string to dict, matching the existing tags coercion in the same function - both recall() variants: coerce types and tags from JSON strings before passing to the memory engine All coercions are backward-compatible — correctly-typed inputs pass through unchanged. Fixes vectorize-io#849
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.
Fixes #849
Problem
MCP tool bridges (Claude Code, Cursor, and others) sometimes serialize JSON arrays and objects as strings during transport. This causes Pydantic validation failures on
retainandrecallcalls:The agent retries with the same broken format, wasting tokens and silently failing to store memories.
Solution
Add defensive coercion at two layers — mirroring the pattern already used for
MemoryItem.tags(added in #682):HTTP API (
http.py):RecallRequest:field_validatorontypesandtags(mode="before") that parses JSON-string arrays back into listsMemoryItem:field_validatoronmetadata(mode="before") that parses JSON-string objects back into dictsMCP tools (
mcp_tools.py):build_content_dict: coercemetadatafrom JSON string to dict, matching the existingtagscoercion in the same functionrecall()variants (with and withoutbank_idparam): coercetypesandtagsfrom JSON strings before passing to the memory engineAll coercions are backward-compatible — correctly-typed native arrays/objects pass through unchanged.
Testing
The existing coercion pattern for
tagsinMemoryItem(from #682) already has test coverage. The new validators follow the identical logic and can be validated by sending a JSON-string value to the affected fields.