Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

bhosmer-ant
Copy link
Contributor

@bhosmer-ant bhosmer-ant commented Jul 10, 2025

Fix BaseModel method name conflicts in func_metadata when function parameters have names that conflict with BaseModel methods.

Motivation and Context

When function parameters have names that conflict with BaseModel methods (e.g., 'model_dump', 'dict', 'json', 'validate'), Pydantic v2 raises warnings about shadowing parent attributes. This prevents users from creating MCP tools with parameter names that match BaseModel method names.

How Has This Been Tested?

  • new tests in test_func_metadata.py:
    • test_basemodel_reserved_names: Verifies schema generation with conflicting parameter names
    • test_basemodel_reserved_names_validation: Tests validation and function calling with reserved names
    • test_basemodel_reserved_names_with_json_preparsing: Tests JSON pre-parsing with reserved names

Breaking Changes

None. This change is fully backward compatible. The aliasing is transparent to users - they continue to use the original parameter names in their function signatures and when calling the functions.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

The solution uses Pydantic's alias system to map conflicting parameter names to prefixed internal fields (field_ prefix) while preserving the original names in the external API. The implementation:

  • Dynamically detects BaseModel method conflicts using hasattr and callable checks
  • Only applies aliases to parameters that actually conflict
  • Updates model_dump_one_level and pre_parse_json to handle aliases correctly
  • Uses by_alias=True in schema generation to ensure correct parameter names in the API

When function parameters have names that conflict with BaseModel methods
(e.g., 'model_dump', 'dict', 'json'), Pydantic v2 warns about shadowing
parent attributes. This fix uses aliases to map conflicting parameter
names to prefixed internal fields while preserving the original names
in the external API.

- Dynamically detect BaseModel method conflicts using hasattr/callable
- Apply aliases only to conflicting parameters
- Update model_dump_one_level and pre_parse_json to handle aliases
- Add by_alias=True to schema generation for correct parameter names
- Add tests for functions with reserved parameter names
Copy link
Contributor

@ihrpr ihrpr left a comment

Choose a reason for hiding this comment

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

LGTM

@ihrpr ihrpr merged commit 49991fd into main Jul 14, 2025
13 checks passed
@ihrpr ihrpr deleted the basil/fastmcp_reserved_param_names branch July 14, 2025 07:29
saqadri pushed a commit to saqadri/stdio-fixes that referenced this pull request Aug 6, 2025
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.

2 participants