Skip to content

Conversation

@Zsailer
Copy link
Contributor

@Zsailer Zsailer commented Sep 11, 2025

This PR adds automatic conversion of JSON string arguments to dictionary objects for MCP tools that expect dict parameters.

Summary

  • Automatically converts JSON strings to dicts for dict-typed parameters
  • Supports all dict annotation styles: dict, Optional[dict], Union[dict, None], dict | None, Dict[str, Any]
  • Handles both async and sync functions
  • Includes comprehensive error handling for invalid JSON
  • Updates tool schemas to accept both objects and JSON strings
  • Always enabled for maximum MCP client compatibility

Testing

  • Added 11 comprehensive unit tests covering all scenarios
  • All existing tests continue to pass (44/44)
  • Tests cover edge cases, error handling, and complex nested structures

- Add _auto_convert_json_args wrapper to automatically parse JSON strings to dicts
- Modify function annotations to accept Union[dict, str] for dict parameters
- Update tool schemas to accept JSON strings for dict-typed parameters
- Add auto_convert_json_args configuration option (default: True)
- Remove enable_debug_logging trait in favor of standard logging levels
- Improve error handling with better exception chaining
- Remove configurable auto_convert_json_args trait from MCPServer
- JSON argument conversion is now always applied to all registered tools
- This ensures consistent behavior and compatibility with all MCP clients
- Update docstrings to reflect that conversion is always enabled
- Fix ruff linting issues (imports, type annotations, unnecessary else)
- Add TestJSONArgumentConversion class with 9 comprehensive tests
- Test basic dict conversion, Optional/Union types, typed dicts
- Test error handling for invalid JSON strings
- Test async and sync function wrappers
- Test complex nested JSON structures
- Test annotation modification behavior
- Test that non-dict parameters remain unchanged

- Add TestJSONSchemaModification class with schema tests
- Test tool registration with schema modification
- Test multiple dict parameters in single function

- Fix union type handling for Python 3.10+ syntax (dict | None)
- Support both typing.Union and new UnionType for compatibility
- Update schema modification to handle both union syntaxes

All 44 tests passing, including 11 new JSON conversion tests
- Rename functions for better clarity:
  * _auto_convert_json_args → _wrap_with_json_conversion
  * _modify_schema_for_json_string_support → _update_schema_for_json_args

- Create shared helper _is_dict_compatible_annotation() to consolidate
  duplicate union type detection logic across functions

- Simplify union type handling with unified approach for both
  typing.Union and Python 3.10+ UnionType syntax

- Rename internal helper _modify_annotation_for_string_support
  to _add_string_to_annotation for clarity

- Reduce code duplication by ~15 lines while maintaining all functionality
- All 44 tests still pass, including 11 JSON conversion tests
- Format code according to project style guidelines
- All tests continue to pass after formatting
@Zsailer Zsailer merged commit 549f183 into jupyter-ai-contrib:main Sep 11, 2025
9 checks passed
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.

1 participant