Skip to content

Conversation

@Dishwasha
Copy link

@Dishwasha Dishwasha commented Nov 13, 2025

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.

@github-actions github-actions bot added the community Pull Request from an external contributor label Nov 13, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 13, 2025

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.

@Dishwasha
Copy link
Author

@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Nov 13, 2025
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Nov 13, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 586c79d and 3a17981.

📒 Files selected for processing (1)
  • src/lfx/src/lfx/base/mcp/util.py (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (24)
  • GitHub Check: Run Frontend Tests / Playwright Tests - Shard 13/13
  • GitHub Check: Run Frontend Tests / Playwright Tests - Shard 9/13
  • GitHub Check: Run Frontend Tests / Playwright Tests - Shard 12/13
  • GitHub Check: Run Frontend Tests / Playwright Tests - Shard 11/13
  • GitHub Check: Run Frontend Tests / Playwright Tests - Shard 7/13
  • GitHub Check: Run Frontend Tests / Playwright Tests - Shard 10/13
  • GitHub Check: Run Frontend Tests / Playwright Tests - Shard 5/13
  • GitHub Check: Run Frontend Tests / Playwright Tests - Shard 4/13
  • GitHub Check: Run Frontend Tests / Playwright Tests - Shard 6/13
  • GitHub Check: Run Frontend Tests / Playwright Tests - Shard 8/13
  • GitHub Check: Run Frontend Tests / Playwright Tests - Shard 1/13
  • GitHub Check: Run Frontend Tests / Playwright Tests - Shard 3/13
  • GitHub Check: Run Frontend Tests / Playwright Tests - Shard 2/13
  • GitHub Check: Run Backend Tests / Unit Tests - Python 3.10 - Group 5
  • GitHub Check: Run Backend Tests / Unit Tests - Python 3.10 - Group 4
  • GitHub Check: Lint Backend / Run Mypy (3.11)
  • GitHub Check: Run Backend Tests / Unit Tests - Python 3.10 - Group 3
  • GitHub Check: Run Backend Tests / Unit Tests - Python 3.10 - Group 1
  • GitHub Check: Run Backend Tests / Unit Tests - Python 3.10 - Group 2
  • GitHub Check: Run Backend Tests / LFX Tests - Python 3.10
  • GitHub Check: Lint Backend / Run Mypy (3.12)
  • GitHub Check: Run Backend Tests / Integration Tests - Python 3.10
  • GitHub Check: Run Frontend Unit Tests / Frontend Jest Unit Tests
  • GitHub Check: Test Starter Templates

Comment on lines 319 to 321
for arg in provided_args.items():
if "AnonModel" in str(arg_schema.model_fields[arg].annotation) and isinstance(provided_args[arg], str):
provided_args[arg] = json.loads(provided_args[arg])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical bug: incorrect loop variable unpacking causes runtime failure.

Line 319 iterates over provided_args.items(), which returns (key, value) tuples. However, lines 320-321 treat arg as if it's the key string, when it's actually a tuple. This will cause a TypeError or KeyError immediately when the code executes.

Additionally, there's no error handling for json.loads(), which can raise JSONDecodeError on invalid JSON input.

Apply this diff to fix the bug and add error handling:

-        for arg in provided_args.items():
-            if "AnonModel" in str(arg_schema.model_fields[arg].annotation) and isinstance(provided_args[arg], str):
-                provided_args[arg] = json.loads(provided_args[arg])
+        for arg_name, arg_value in provided_args.items():
+            # Skip if field doesn't exist in schema
+            if arg_name not in arg_schema.model_fields:
+                continue
+            # Parse JSON strings for AnonModel fields
+            if isinstance(arg_value, str) and "AnonModel" in str(arg_schema.model_fields[arg_name].annotation):
+                try:
+                    provided_args[arg_name] = json.loads(arg_value)
+                except json.JSONDecodeError as e:
+                    # Let validation handle the error with better context
+                    logger.warning(f"Failed to parse JSON for field '{arg_name}': {e}")

Key fixes:

  1. Unpack tuple into arg_name and arg_value
  2. Check field existence before accessing model_fields
  3. Reorder checks: isinstance before field access for efficiency
  4. Add try-except for JSONDecodeError with logging
🤖 Prompt for AI Agents
In src/lfx/src/lfx/base/mcp/util.py around lines 319 to 321, the loop
incorrectly iterates over provided_args.items() as a single variable causing
tuple misuse; change the loop to unpack each item into arg_name and arg_value,
first check isinstance(arg_value, str) before touching arg_schema.model_fields,
verify arg_name exists in arg_schema.model_fields before using it, and if the
field annotation string contains "AnonModel" attempt to json.loads(arg_value)
inside a try/except catching json.JSONDecodeError and log the error (and leave
the original value unchanged on failure).

@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Nov 13, 2025
@Dishwasha Dishwasha force-pushed the fix-string-objects branch 2 times, most recently from db1625c to 2d413da Compare November 13, 2025 05:08
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Nov 13, 2025
@drfenixion
Copy link

It can be related to #9881 (flow does not transfer to MCP deep dicts)

@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Nov 13, 2025
@github-actions github-actions bot added the bug Something isn't working label Nov 13, 2025
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Nov 14, 2025
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Nov 19, 2025
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] ...`
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Nov 24, 2025
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Nov 24, 2025
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Nov 24, 2025
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Nov 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working community Pull Request from an external contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants