-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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]
There was a problem hiding this 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"]:
Hey @markindanila Lookin good! I'd say we need as many tests as we can on this. |
@ogabrielluiz Hi! |
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:$defs
field of Pydantic JSON schema$defs
field of Pydantic JSON schemaWith this PR all types of BaseModel are in general supported including Enum attributes with these exceptions:
dict[<type1>, <type2>]
are not parsed deeper than justdict
sincedict keys
typing cannot be inherited from JSON Pydantic schemastring
: