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

What’s changed

  • MCP request/params handling: Tool call arguments are now wrapped as {"input": kwargs} and passed via tools/call params.arguments; pre-wrapped input dicts are preserved for backward compatibility.
  • Schema parsing update: Switched from parameters to MCP’s inputSchema, unwrapping the top-level input object to expose real fields (e.g., title, description) in args_schema. Added support for anyOf (nullable) types, correct Python type mapping, required vs optional handling, and None defaults where appropriate.
  • Robustness & DX: Added debug logging when inputSchema is missing; minor typing fixes (type: ignore), removed unused BaseModel import; from_tool_pack now uses a temporary args_schema model.
  • Tests: Updated mocks to inputSchema; adjusted request assertions to expect wrapped arguments.input; added comprehensive tests for schema unwrapping, nullable handling, required/optional semantics, backward compatibility, and edge cases (non-dict input, numeric input).

Written by Cursor Bugbot for commit f9c9239. 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.

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