Skip to content

Conversation

@mplacona
Copy link

@mplacona mplacona commented Jan 20, 2026

Disclosure: I worked with Merge on getting this PR together

Problem

The MergeAgentHandlerTool was not correctly handling the Agent Handler MCP protocol's parameter format:

  1. Agent Handler wraps all parameters in an input object
  2. MCP protocol uses inputSchema instead of parameters for tool schemas
  3. Schema parsing didn't handle anyOf nullable type definitions
  4. Pydantic schema exposed the wrapper object instead of actual parameters

This caused tools to fail when called by CrewAI agents.

Solution

Parameter Wrapping

  • Wrap all arguments in an {"input": kwargs} structure before sending to Agent Handler
  • Add backwards compatibility to detect pre-wrapped input and pass through as-is

Schema Parsing

  • Updated to parse inputSchema from MCP responses (not parameters)
  • Automatically unwrap the input object to expose actual tool parameters in Pydantic schema
  • This ensures CrewAI agents see the real parameters (e.g., title, description) instead of just input

Nullable Type Support

  • Added support for anyOf type definitions commonly used for nullable fields
  • Properly marks nullable fields as optional with None default values
  • Correctly infers the primary type from anyOf definitions

Debugging Improvements

  • Added debug logging when inputSchema is missing from responses
  • Better error messages for schema parsing failures

Testing

  • Updated all test mocks to use inputSchema format matching real MCP responses
  • Added tests for schema parsing from inputSchema
  • Added tests for anyOf nullable type handling
  • Added tests for backwards compatibility with pre-wrapped input
  • Verified all tests pass with new parameter wrapping logic

Impact

  • Tools now work correctly with Agent Handler MCP protocol
  • Pydantic schemas correctly represent actual tool parameters
  • CrewAI agents can properly invoke Agent Handler tools
  • Backwards compatible with any existing code that pre-wraps parameters

Note

Medium Risk
Changes alter how tool arguments are wrapped and how schemas are interpreted, which can break existing integrations if expectations differ. Risk is mitigated by added backwards-compat handling and expanded tests for schema/nullable edge cases.

Overview
Fixes MergeAgentHandlerTool to correctly speak the Agent Handler MCP protocol by wrapping tool call arguments under {"input": ...} (with a pass-through for legacy pre-wrapped dict input) and updating request typing.

Updates schema discovery to read MCP inputSchema (instead of parameters), unwraps the top-level input wrapper so CrewAI sees real tool parameters, and adds support for anyOf nullable fields when generating Pydantic args_schema (plus debug logging when inputSchema is missing).

Tests are updated to reflect inputSchema/input wrapping and expanded with coverage for nullable/required/optional combinations and backwards-compat argument wrapping behavior.

Written by Cursor Bugbot for commit e415a87. This will update automatically on new commits. Configure here.

…l correctly

## Problem
The MergeAgentHandlerTool was not correctly handling the Agent Handler MCP protocol's parameter format:
1. Agent Handler wraps all parameters in an `input` object
2. MCP protocol uses `inputSchema` instead of `parameters` for tool schemas
3. Schema parsing didn't handle `anyOf` nullable type definitions
4. Pydantic schema exposed the wrapper object instead of actual parameters

This caused tools to fail when called by CrewAI agents.

## Solution

### Parameter Wrapping
- Wrap all arguments in an `{"input": kwargs}` structure before sending to Agent Handler
- Add backwards compatibility to detect pre-wrapped input and pass through as-is

### Schema Parsing
- Updated to parse `inputSchema` from MCP responses (not `parameters`)
- Automatically unwrap the `input` object to expose actual tool parameters in Pydantic schema
- This ensures CrewAI agents see the real parameters (e.g., `title`, `description`) instead of just `input`

### Nullable Type Support
- Added support for `anyOf` type definitions commonly used for nullable fields
- Properly marks nullable fields as optional with `None` default values
- Correctly infers the primary type from `anyOf` definitions

### Debugging Improvements
- Added debug logging when `inputSchema` is missing from responses
- Better error messages for schema parsing failures

## Testing
- Updated all test mocks to use `inputSchema` format matching real MCP responses
- Added tests for schema parsing from `inputSchema`
- Added tests for `anyOf` nullable type handling
- Added tests for backwards compatibility with pre-wrapped input
- Verified all tests pass with new parameter wrapping logic

## Impact
- Tools now work correctly with Agent Handler MCP protocol
- Pydantic schemas correctly represent actual tool parameters
- CrewAI agents can properly invoke Agent Handler tools
- Backwards compatible with any existing code that pre-wraps parameters
Copy link
Contributor

@gilfeig gilfeig left a comment

Choose a reason for hiding this comment

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

LGTM!

cursor[bot]

This comment was marked as outdated.

Separate nullable type annotation from optional default value in
schema parsing. Previously, required nullable fields were incorrectly
given a None default, allowing callers to omit them. This caused API
errors when backends distinguish between missing fields and explicit
null values.

Changes:
- Split nullable (type accepts None) from optional (has default)
- Required nullable fields now require explicit value (can be None)
- Improve input parameter wrapping check with isinstance guard
- Add comprehensive tests for all field type combinations

All 27 tests pass, confirming fix works without breaking existing
functionality.
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

@mplacona
Copy link
Author

mplacona commented Feb 3, 2026

@lorenzejay could you please help me get this past the finish line? Thanks

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.

3 participants