Skip to content

Conversation

@willkill07
Copy link
Member

@willkill07 willkill07 commented Oct 31, 2025

Parameters with default values were incorrectly marked as required in MCP tool schemas. Now checking for PydanticUndefined and properly extracting default values from Pydantic fields to correctly identify optional parameters.

This PR also adds comprehensive unit tests for the tool schema conversion

  • I am familiar with the Contributing Guidelines.
  • We require that all contributors "sign-off" on their commits. This certifies that the contribution is your original work, or you have rights to submit it under the same license, or a compatible license.
    • Any contribution which contains commits that are not Signed-Off will not be accepted.
  • When the PR is ready for review, new or existing tests cover these changes.
  • When the PR is ready for review, the documentation is up to date with these changes.
  • New Features

    • Improved detection and handling of optional parameters and default/default-factory values in tool wrapper generation, with unified validation and consistent invocation behavior for workflows and regular functions.
    • Ensures parameter descriptions, order, and type annotations are preserved in generated wrappers.
  • Tests

    • Added comprehensive tests covering optional/default combinations, wrapper signatures, execution paths, and observability propagation.

Summary by CodeRabbit

Release Notes

  • New Features

    • Added improved support for optional parameters in MCP tools with enhanced default value handling.
  • Bug Fixes

    • Fixed inconsistent behavior with nested schema validation and default factory application in tool parameters.
  • Tests

    • Added comprehensive test coverage for optional field handling and parameter validation scenarios.

…cp serve" (NVIDIA#1126)

Parameters with default values were incorrectly marked as required in MCP tool schemas. Now checking for PydanticUndefined and properly extracting default values from Pydantic fields to correctly identify optional parameters.

This PR also adds comprehensive unit tests for the tool schema conversion

- I am familiar with the [Contributing Guidelines](https://github.com/NVIDIA/NeMo-Agent-Toolkit/blob/develop/docs/source/resources/contributing.md).
- We require that all contributors "sign-off" on their commits. This certifies that the contribution is your original work, or you have rights to submit it under the same license, or a compatible license.
  - Any contribution which contains commits that are not Signed-Off will not be accepted.
- When the PR is ready for review, new or existing tests cover these changes.
- When the PR is ready for review, the documentation is up to date with these changes.

* **New Features**
  * Improved detection and handling of optional parameters and default/default-factory values in tool wrapper generation, with unified validation and consistent invocation behavior for workflows and regular functions.
  * Ensures parameter descriptions, order, and type annotations are preserved in generated wrappers.

* **Tests**
  * Added comprehensive tests covering optional/default combinations, wrapper signatures, execution paths, and observability propagation.

Authors:
  - Anuradha Karuppiah (https://github.com/AnuradhaKaruppiah)

Approvers:
  - Yuchen Zhang (https://github.com/yczhang-nv)

URL: NVIDIA#1126

Signed-off-by: Will Killian <[email protected]>
@willkill07 willkill07 self-assigned this Oct 31, 2025
@willkill07 willkill07 requested a review from a team as a code owner October 31, 2025 15:21
@willkill07 willkill07 added bug Something isn't working non-breaking Non-breaking change labels Oct 31, 2025
@coderabbitai
Copy link

coderabbitai bot commented Oct 31, 2025

Walkthrough

The changes refactor how optional Pydantic fields are handled in MCP tool conversion. A new is_field_optional() helper function detects optional fields and their defaults. The validation path is unified to use Pydantic's model_validate() instead of ad-hoc reconstruction. Validated model instances or dumps are now passed directly to workflow runners and underlying functions.

Changes

Cohort / File(s) Summary
Production refactoring of optional field handling
src/nat/front_ends/mcp/tool_converter.py
Introduces is_field_optional(field: FieldInfo) helper to detect optional vs. required fields and extract default values (including a _USE_PYDANTIC_DEFAULT sentinel for factory defaults). Extends imports to include Any, FieldInfo, and PydanticUndefined. Integrates the helper into parameter construction and replaces ad-hoc nested-schema handling with uniform Pydantic model_validate() flow. Updates workflow and non-workflow call paths to pass validated model instances or dumps instead of raw kwargs reconstruction.
Comprehensive test coverage for field handling and validation
tests/nat/front_ends/mcp/test_tool_converter.py
Adds new mock schema models covering all-required, mixed, all-optional, and optional-with-none Pydantic field configurations. Introduces tests for is_field_optional() utility with various default scenarios (defaults, factories, None, zero). Adds parameter schema validation tests across different model configurations and extended integration tests covering observability context propagation, wrapper execution, error handling, and optional parameter omission/provision scenarios. Imports and uses _USE_PYDANTIC_DEFAULT sentinel.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Sentinel value handling: Pay close attention to how _USE_PYDANTIC_DEFAULT is created, propagated through kwargs cleanup, and interpreted by Pydantic during model_validate()
  • Validation orchestration: Review the refactored path ensuring that model_validate() correctly applies defaults and factory functions, and that edge cases with nested schemas are handled properly
  • Workflow vs. non-workflow paths: Verify that both branches (workflow context present vs. absent) correctly pass model instances or dumps to their respective callees
  • Optional field edge cases: Confirm that the detection logic correctly distinguishes between required fields, optional with explicit defaults, optional with factories, and optional with None as default

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "Fixes to detect optional parameters in tool conversion used by "nat mcp serve"" is fully related to the main changes in the changeset. The title accurately describes the core improvement: enhanced detection of optional parameters in MCP tool schema conversion, which directly aligns with the implementation of is_field_optional(), improved parameter handling for Pydantic fields, and better default value extraction. The title uses imperative mood ("Fixes"), is descriptive, and clearly conveys the purpose of the changes. The title exceeds the stated guideline of 72 characters by 6 characters (78 total), but this minor overage does not materially impact clarity or conciseness given the tilde () indicates an approximate threshold.
Docstring Coverage ✅ Passed Docstring coverage is 96.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

@willkill07
Copy link
Member Author

/merge

@rapids-bot rapids-bot bot merged commit 5382798 into NVIDIA:release/1.3 Oct 31, 2025
16 of 17 checks passed
Copy link

@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: 0

🧹 Nitpick comments (2)
tests/nat/front_ends/mcp/test_tool_converter.py (2)

547-553: Tighten order assertion

This test claims to guard parameter ordering but only checks membership, so it still passes if the parameters are shuffled. Please assert the exact sequence, e.g., assert param_names == ["required_str", "required_int", "optional_str", "optional_int", "optional_list"].


625-631: Verify propagated defaults

We only check that acall_invoke ran once, which wouldn't fail if defaults failed to propagate. Please assert the actual call signature (e.g., via assert_called_once_with(...)) so we prove the factory/default values were forwarded correctly.

📜 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 3a179be and 21ea957.

📒 Files selected for processing (2)
  • src/nat/front_ends/mcp/tool_converter.py (4 hunks)
  • tests/nat/front_ends/mcp/test_tool_converter.py (6 hunks)
🧰 Additional context used
📓 Path-based instructions (8)
**/*.py

📄 CodeRabbit inference engine (.cursor/rules/general.mdc)

**/*.py: In code comments use the abbreviations: nat (API namespace/CLI), nvidia-nat (package), NAT (env var prefixes); never use these abbreviations in documentation
Follow PEP 20 and PEP 8 for Python style
Run yapf with column_limit=120; yapf is used for formatting (run second)
Indent with 4 spaces (no tabs) and end each file with a single trailing newline
Use ruff (ruff check --fix) as a linter (not formatter) per pyproject.toml; fix warnings unless explicitly ignored
Respect Python naming schemes: snake_case for functions/variables, PascalCase for classes, UPPER_CASE for constants
When re-raising exceptions, use bare raise to preserve stack trace; log with logger.error(), not logger.exception()
When catching and logging without re-raising, use logger.exception() to capture full stack trace
Provide Google-style docstrings for every public module, class, function, and CLI command
Docstring first line must be a concise description ending with a period
Validate and sanitize all user input, especially in web or CLI interfaces
Prefer httpx with SSL verification enabled by default and follow OWASP Top-10 recommendations
Use async/await for I/O-bound work (HTTP, DB, file I/O)
Cache expensive computations with functools.lru_cache or an external cache when appropriate
Leverage NumPy vectorized operations when beneficial and feasible

Files:

  • tests/nat/front_ends/mcp/test_tool_converter.py
  • src/nat/front_ends/mcp/tool_converter.py
{tests/**/*.py,examples/*/tests/**/*.py}

📄 CodeRabbit inference engine (.cursor/rules/general.mdc)

Unit tests live in tests/ or examples/*/tests and use markers defined in pyproject.toml (e.g., integration)

Files:

  • tests/nat/front_ends/mcp/test_tool_converter.py
{tests/**/*.py,examples/*/tests/**/*.py,packages/*/tests/**/*.py}

📄 CodeRabbit inference engine (.cursor/rules/general.mdc)

{tests/**/*.py,examples/*/tests/**/*.py,packages/*/tests/**/*.py}: Use pytest with pytest-asyncio for async code
Test functions should be named with test_ prefix using snake_case
Extract frequently repeated test code into pytest fixtures
Pytest fixtures should define the name argument in @pytest.fixture and the function should be prefixed fixture_ using snake_case
Mock external services with pytest_httpserver or unittest.mock; avoid live endpoints
Mark slow tests with @pytest.mark.slow to skip in default test suite
Mark integration tests requiring external services with @pytest.mark.integration and follow nat-tests guidelines

Files:

  • tests/nat/front_ends/mcp/test_tool_converter.py
{**/*.py,**/*.sh,**/*.md,**/*.toml,**/*.y?(a)ml,**/*.json,**/*.txt,**/*.ini,**/*.cfg,**/*.ipynb}

📄 CodeRabbit inference engine (.cursor/rules/general.mdc)

{**/*.py,**/*.sh,**/*.md,**/*.toml,**/*.y?(a)ml,**/*.json,**/*.txt,**/*.ini,**/*.cfg,**/*.ipynb}: Every file must start with the standard SPDX Apache-2.0 header
Confirm copyright years are up to date when a file is changed
All source files must include the SPDX Apache-2.0 header template (copy from an existing file)

Files:

  • tests/nat/front_ends/mcp/test_tool_converter.py
  • src/nat/front_ends/mcp/tool_converter.py
**/*

⚙️ CodeRabbit configuration file

**/*: # Code Review Instructions

  • Ensure the code follows best practices and coding standards. - For Python code, follow
    PEP 20 and
    PEP 8 for style guidelines.
  • Check for security vulnerabilities and potential issues. - Python methods should use type hints for all parameters and return values.
    Example:
    def my_function(param1: int, param2: str) -> bool:
        pass
  • For Python exception handling, ensure proper stack trace preservation:
    • When re-raising exceptions: use bare raise statements to maintain the original stack trace,
      and use logger.error() (not logger.exception()) to avoid duplicate stack trace output.
    • When catching and logging exceptions without re-raising: always use logger.exception()
      to capture the full stack trace information.

Documentation Review Instructions - Verify that documentation and comments are clear and comprehensive. - Verify that the documentation doesn't contain any TODOs, FIXMEs or placeholder text like "lorem ipsum". - Verify that the documentation doesn't contain any offensive or outdated terms. - Verify that documentation and comments are free of spelling mistakes, ensure the documentation doesn't contain any

words listed in the ci/vale/styles/config/vocabularies/nat/reject.txt file, words that might appear to be
spelling mistakes but are listed in the ci/vale/styles/config/vocabularies/nat/accept.txt file are OK.

Misc. - All code (except .mdc files that contain Cursor rules) should be licensed under the Apache License 2.0,

and should contain an Apache License 2.0 header comment at the top of each file.

  • Confirm that copyright years are up-to date whenever a file is changed.

Files:

  • tests/nat/front_ends/mcp/test_tool_converter.py
  • src/nat/front_ends/mcp/tool_converter.py
tests/**/*.py

⚙️ CodeRabbit configuration file

tests/**/*.py: - Ensure that tests are comprehensive, cover edge cases, and validate the functionality of the code. - Test functions should be named using the test_ prefix, using snake_case. - Any frequently repeated code should be extracted into pytest fixtures. - Pytest fixtures should define the name argument when applying the pytest.fixture decorator. The fixture
function being decorated should be named using the fixture_ prefix, using snake_case. Example:
@pytest.fixture(name="my_fixture")
def fixture_my_fixture():
pass

Files:

  • tests/nat/front_ends/mcp/test_tool_converter.py
{src/**/*.py,packages/*/src/**/*.py}

📄 CodeRabbit inference engine (.cursor/rules/general.mdc)

{src/**/*.py,packages/*/src/**/*.py}: All importable Python code must live under src/ or packages//src/
All public APIs must have Python 3.11+ type hints on parameters and return values
Prefer typing/collections.abc abstractions (e.g., Sequence over list)
Use typing.Annotated for units or metadata when useful
Treat pyright warnings as errors during development

Files:

  • src/nat/front_ends/mcp/tool_converter.py
src/nat/**/*

📄 CodeRabbit inference engine (.cursor/rules/general.mdc)

src/nat/**/* contains core functionality; changes should prioritize backward compatibility

Files:

  • src/nat/front_ends/mcp/tool_converter.py

⚙️ CodeRabbit configuration file

This directory contains the core functionality of the toolkit. Changes should prioritize backward compatibility.

Files:

  • src/nat/front_ends/mcp/tool_converter.py
🧬 Code graph analysis (1)
tests/nat/front_ends/mcp/test_tool_converter.py (1)
src/nat/front_ends/mcp/tool_converter.py (1)
  • is_field_optional (41-70)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working non-breaking Non-breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants