Skip to content

fix: Add pydantic complex types handling for MCP tools #7527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

markindanila
Copy link

@markindanila markindanila commented Apr 9, 2025

Currently MCP tools handle just simple types, which leads to agent not being able to provide the correct input for the complex tool when asked a question.

With this fix it's able to handle nested structures and Enums - see the example output of agent asked to show inputSchema

UPD:
list of changes in base.mcp.utils.create_input_schema_from_json_schema function:

  • add Enums definitions parsing from $defs field of Pydantic JSON schema
  • add BaseModel definitions recursive parsing from $defs field of Pydantic JSON schema
  • add inheritance of Pydantic metadata fields

With this PR all types of BaseModel are in general supported including Enum attributes with these exceptions:

  • attributes of type dict[<type1>, <type2>] are not parsed deeper than just dict since dict keys typing cannot be inherited from JSON Pydantic schema
  • circular attribute references cannot be resolved properly, e.g. in this case both classes will most likely fallback to string:
class A(BaseModel):
    a: B

class B(BaseModel):
    b: A

image

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels Apr 9, 2025
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Apr 9, 2025
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Apr 9, 2025
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Apr 9, 2025
@ogabrielluiz ogabrielluiz requested review from phact and Copilot April 9, 2025 15:20
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

src/backend/base/langflow/base/mcp/util.py:105

  • Consider adding error handling to check that 'ref_name' exists in enum_defs before attempting to return it, to avoid a potential KeyError if an undefined reference is encountered.
return enum_defs[ref_name]

@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Apr 9, 2025
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Apr 9, 2025
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Apr 11, 2025
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Apr 11, 2025
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Apr 11, 2025
@markindanila markindanila requested a review from Copilot April 11, 2025 11:36
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (3)

src/backend/base/langflow/base/mcp/util.py:109

  • Ensure that enum values are strings before applying re.sub (e.g., by converting v to str) to avoid potential type errors.
enum_members = {re.sub(r"\W|^(?=\d)", "_", v): v for v in enum_def["enum"]}

src/backend/base/langflow/base/mcp/util.py:167

  • [nitpick] The use of tuple[*item_types] may lead to compatibility issues with Python's generics syntax; consider using a supported syntax such as Tuple[...], ensuring compatibility with your target Python version.
return tuple[*item_types]  # type: ignore # noqa: PGH003

src/backend/base/langflow/base/mcp/util.py:188

  • The updated check for required fields relies on a field-level 'required' key, but JSON schema normally specifies required fields at the schema level; consider reintroducing the schema-level required check to improve accuracy.
if "default" in field_def or not field_metadata["required"]:

@ogabrielluiz
Copy link
Contributor

Hey @markindanila

Lookin good!

I'd say we need as many tests as we can on this.

@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Apr 11, 2025
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Apr 11, 2025
@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Apr 11, 2025
@markindanila
Copy link
Author

@ogabrielluiz Hi!
I've added the test here, not sure tho how to make the CI run it, but it works locally

@github-actions github-actions bot added bug Something isn't working and removed bug Something isn't working labels Apr 11, 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 size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants