Skip to content

Whatsmeow api compatibility and mcp serialization - #301

Open
SKNOTFOUND248 wants to merge 2 commits into
lharries:mainfrom
SKNOTFOUND248:whatsmeow-api-compatibility-and-mcp-serialization
Open

Whatsmeow api compatibility and mcp serialization#301
SKNOTFOUND248 wants to merge 2 commits into
lharries:mainfrom
SKNOTFOUND248:whatsmeow-api-compatibility-and-mcp-serialization

Conversation

@SKNOTFOUND248

Copy link
Copy Markdown

Title:

Fix FastMCP tool serialization by converting dataclass responses to dictionaries

Description:

This pull request fixes a serialization issue in the Python MCP server where several WhatsApp tools failed during response validation.

The tool functions in main.py were annotated to return List[Dict[str, Any]], while the underlying helper functions in whatsapp.py returned dataclass instances such as Contact, Chat, and Message. Although the data was successfully retrieved, FastMCP (via Pydantic) expected standard Python dictionaries and raised validation errors when attempting to serialize the dataclass objects.

To resolve this, I introduced a recursive _to_dict() helper that converts dataclass instances into dictionaries while also handling nested dataclasses, lists, dictionaries, and datetime objects by converting timestamps to ISO 8601 strings. All MCP tool handlers now serialize their responses before returning them, ensuring that the output matches the declared return types.

This change restores compatibility with FastMCP's validation and serialization pipeline without modifying the underlying data models or business logic.

Benefits:

  • Fixes Pydantic validation errors caused by dataclass responses.
  • Ensures MCP tools return JSON-serializable dictionaries.
  • Preserves the existing dataclass-based implementation in whatsapp.py.
  • Improves compatibility with FastMCP and MCP clients.
  • No changes to the public tool interface or existing functionality.

Tested:

  • Contact search
  • Chat search
  • Message retrieval
  • MCP tool response serialization
  • FastMCP response validation

All affected tools now return correctly serialized responses without validation errors.

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.

1 participant