-
Notifications
You must be signed in to change notification settings - Fork 5.8k
fix: update MergeAgentHandlerTool to rightly handle MCP protocol #4255
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
…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
gilfeig
left a comment
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.
LGTM!
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.
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.
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.
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:
inputobjectinputSchemainstead ofparametersfor tool schemasanyOfnullable type definitionsThis caused tools to fail when called by CrewAI agents.
Solution
Parameter Wrapping
{"input": kwargs}structure before sending to Agent HandlerSchema Parsing
inputSchemafrom MCP responses (notparameters)inputobject to expose actual tool parameters in Pydantic schematitle,description) instead of justinputNullable Type Support
anyOftype definitions commonly used for nullable fieldsNonedefault valuesanyOfdefinitionsDebugging Improvements
inputSchemais missing from responsesTesting
inputSchemaformat matching real MCP responsesinputSchemaanyOfnullable type handlingImpact
Note
What’s changed
{"input": kwargs}and passed viatools/callparams.arguments; pre-wrappedinputdicts are preserved for backward compatibility.parametersto MCP’sinputSchema, unwrapping the top-levelinputobject to expose real fields (e.g.,title,description) inargs_schema. Added support foranyOf(nullable) types, correct Python type mapping, required vs optional handling, andNonedefaults where appropriate.inputSchemais missing; minor typing fixes (type: ignore), removed unusedBaseModelimport;from_tool_packnow uses a temporaryargs_schemamodel.inputSchema; adjusted request assertions to expect wrappedarguments.input; added comprehensive tests for schema unwrapping, nullable handling, required/optional semantics, backward compatibility, and edge cases (non-dictinput, numericinput).Written by Cursor Bugbot for commit f9c9239. This will update automatically on new commits. Configure here.