langflow icon indicating copy to clipboard operation
langflow copied to clipboard

fix: Convert object strings to object for MCP tools

Open Dishwasha opened this issue 5 months ago • 3 comments

When using an MCP tool directly as a component, rather than via an agent tool call, provided arguments arrive as strings rather than objects so the dynamically generated AnonModel pydantic validation fails. This is observed when using the mongodb-mcp-server with the "find" tool call when setting filter, projection, or sort fields. mongodb-mcp-server exposes the schema for these fields only as {}, since there is no fixed schema for these dynamic objects. The error will look like:

Error in build_output: Invalid input: 3 validation errors for InputSchema filter Input should be a valid dictionary or instance of AnonModel0 [type=model_type, input_value='', input_type=str] ...

Summary by CodeRabbit

  • Bug Fixes
    • Improved automatic parsing of JSON input for model fields.

Dishwasha avatar Nov 13 '25 04:11 Dishwasha

[!IMPORTANT]

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

Adds automatic JSON string parsing for fields annotated as AnonModel within tool_coroutine's argument processing. After merging provided arguments, the code identifies fields with AnonModel annotations and converts string values to objects via JSON decoding. Downstream validation and conversion logic remain unchanged.

Changes

Cohort / File(s) Change Summary
AnonModel JSON Parsing Enhancement
src/lfx/src/lfx/base/mcp/util.py
Adds automatic JSON string-to-object conversion for arguments with AnonModel annotation in tool_coroutine after argument merging

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10–12 minutes

  • Verify JSON decoding logic correctly identifies AnonModel annotated fields
  • Confirm proper handling of edge cases (None values, already-parsed objects, invalid JSON)
  • Check error handling and exception propagation for malformed JSON strings

Pre-merge checks and finishing touches

[!IMPORTANT]

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

❌ Failed checks (2 warnings, 2 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
Test Quality And Coverage ⚠️ Warning Pull request adds JSON parsing logic to tool_coroutine but no test coverage exists for this functionality, and a critical bug in tuple unpacking was identified. Create comprehensive async tests covering valid/invalid JSON parsing, error handling, edge cases, and fix the identified tuple unpacking bug before merging.
Test Coverage For New Implementations ❓ Inconclusive Cannot locate the modified file or test files in the repository to assess test coverage for the MCP utility changes. Verify that the modified file exists at the specified path and search for test files covering the tool_coroutine and JSON parsing functionality.
Test File Naming And Structure ❓ Inconclusive Repository structure could not be verified; test files related to PR changes in lfx/base/mcp/util.py could not be located or accessed. Verify test files exist for the modified code; confirm test naming follows test_*.py pattern and proper pytest structure is used.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically summarizes the main change: converting object strings to objects for MCP tools. It directly reflects the core issue being addressed in the changeset.
Excessive Mock Usage Warning ✅ Passed No test files were modified in this PR. The changes are only to production code in util.py, so mock usage cannot be assessed.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Nov 13 '25 04:11 coderabbitai[bot]

https://github.com/search?q=repo%3Amongodb-js%2Fmongodb-mcp-server%20object(%7B%7D)&type=code reveals the open-ended {} schema for the tool call.

Dishwasha avatar Nov 13 '25 04:11 Dishwasha

It can be related to https://github.com/langflow-ai/langflow/issues/9881 (flow does not transfer to MCP deep dicts)

drfenixion avatar Nov 13 '25 05:11 drfenixion