Skip to content

Fix: Add JSON string deserialization support for metadata parameter#85

Open
MyWWWChat wants to merge 2 commits intoqdrant:masterfrom
MyWWWChat:fix/metadata-json-deserialization
Open

Fix: Add JSON string deserialization support for metadata parameter#85
MyWWWChat wants to merge 2 commits intoqdrant:masterfrom
MyWWWChat:fix/metadata-json-deserialization

Conversation

@MyWWWChat
Copy link
Copy Markdown

Problem

The MCP server's metadata parameter schema doesn't match its implementation, causing validation errors when Claude (and other MCP clients) send metadata as JSON strings.

Current Issue:

  • Schema: Only accepts object | null for metadata
  • Implementation: Supports dict | str | None (can parse JSON strings)
  • Result: Input validation error: '{"code": "..."}' is not valid under any of the given schemas

Solution

This PR fixes the schema-implementation mismatch by:

  1. Updated type annotation from Metadata | None to Union[Metadata, str, None]
  2. Added JSON deserialization logic with proper error handling
  3. Added validation to ensure metadata is a dictionary after deserialization
  4. Added comprehensive test suite with 13 test cases covering all scenarios

Technical Changes

src/mcp_server_qdrant/mcp_server.py

  • Changed metadata type annotation to include str type
  • Added JSON deserialization for string metadata (lines 121-128)
  • Added type validation after deserialization (lines 131-132)
  • Handles edge cases: "null", "none", empty strings, whitespace

src/mcp_server_qdrant/qdrant.py

  • Added fallback to 'text' field when 'document' is missing in search results
  • Prevents KeyError when retrieving search results with different payload structures

tests/test_metadata_deserialization.py

  • New comprehensive test suite with 13 test cases
  • Tests JSON strings, dictionaries, None, edge cases
  • Tests error handling for invalid JSON and non-dict types

Testing

All tests pass:

  • ✅ 13 new tests for metadata deserialization
  • ✅ 37 existing tests continue to pass
  • ✅ Backward compatibility maintained

Impact

This fix resolves the metadata validation error that prevents Claude and other MCP clients from using the metadata parameter as documented. The change is backward compatible - existing code using dict or None continues to work.

Related Issues

Fixes the error: Input validation error: '{"code": "def test(): pass"}' is not valid under any of the given schemas

This enables proper usage as described in TOOL_STORE_DESCRIPTION:

"Store code snippets with descriptions. The 'information' parameter should contain a natural language description of what the code does, while the actual code should be included in the 'metadata' parameter as a 'code' property."

claude added 2 commits July 11, 2025 06:32
- Support metadata as JSON string, dict, or None
- Add automatic deserialization of JSON strings with proper error handling
- Handle edge cases (null, none, empty strings, whitespace)
- Add comprehensive test suite with 13 test cases covering all scenarios
- Maintain backward compatibility with existing dict/None metadata
- Fix MCP interface issue where Claude serializes parameters to strings

Resolves metadata validation error: 'Input validation error: ... is not valid under any of the given schemas'

Technical changes:
- Updated Union type annotation to include str in metadata parameter
- Added JSON deserialization logic in store() function
- Added validation for non-dict types after deserialization
- Added comprehensive test coverage for all edge cases
- All existing tests (37 total) continue to pass
- Add fallback to 'text' field when 'document' is not present
- Ensures compatibility with different payload structures
- Prevents KeyError when retrieving search results
@shigomany
Copy link
Copy Markdown

This PR fixes a critical schema-implementation mismatch that currently breaks the qdrant-store tool for all Claude (and other MCP) users.

The Problem: The MCP server's metadata parameter schema doesn't match its implementation. The schema only accepts object | null, but the implementation actually supports dict | str | None (JSON strings). This causes validation errors when Claude sends metadata as JSON strings:

Input validation error: '{"code": "..."}' is not valid under any of the given schemas
Why This Matters: According to the TOOL_STORE_DESCRIPTION documentation, code should be stored with:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants