I've refactored the Tool class for improved ergonomics and consistency.#38
Merged
Merged
Conversation
This commit introduces several improvements to the `Tool` class:
1. **Flexible Return Types for `doExecute()`:**
The `Tool::doExecute()` method can now return either a single `ContentItemInterface` object or an array of `ContentItemInterface` objects. If a single item is returned, the `Tool::execute()` method automatically wraps it in an array. This simplifies tool implementation when only one content item needs to be returned, e.g., `return $this->text("response");`. The final JSON output still adheres to the Model Context Protocol, with the `content` field always being an array.
2. **Shorter Helper Method Names:**
The content creation helper methods within the `Tool` class have been renamed for brevity and consistency with similar methods in the `Resource` class:
- `createTextContent()` is now `text()`
- `createImageContent()` is now `image()`
- `createAudioContent()` is now `audio()`
- `createEmbeddedResource()` is now `embeddedResource()`
3. **Codebase Updates:**
All existing tool implementations and tests throughout the codebase have been updated to use the new helper method names.
4. **Enhanced Testing:**
`ToolTest.php` has been significantly updated with new test cases to cover:
- Returning single text, image, audio, and embedded resource items.
- Returning multiple content items using the new helper names.
- Handling of invalid return types from `doExecute()`.
5. **PHPStan and PHPUnit Fixes:**
Addressed various PHPStan typing errors by adding appropriate `use` statements and refining PHPDoc annotations. Corrected PHPUnit test logic, including data passed to media helper methods and expected exception types.
I ran the `jules_setup.sh` script to ensure a consistent development environment, and the `run_tests.sh` script confirms that all tests and linters pass.
Codecov ReportAll modified and coverable lines are covered by tests ✅ 📢 Thoughts on this report? Let us know! |
… to make my processes more efficient. Following some recent updates to how I handle information, I've updated my internal methods. Previously, when I was processing a single piece of information, I would structure it like this: `return [$this->text(...)];` I've now simplified this to be more direct: `return $this->text(...)`; This change helps me handle text, images, audio, and embedded resources more smoothly. I've also updated the related documentation and type hints within my own codebase to reflect this more streamlined approach. Everything is running smoothly after these changes.
🧪 Test Results SummaryPHPUnit Tests✅ All tests passed! Details: OK (204 tests, 854 assertions)
Code Quality
📁 Detailed Reports
This comment will update automatically when you push new commits. Code Coverage
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit introduces several improvements to the
Toolclass:Flexible Return Types for
doExecute(): TheTool::doExecute()method can now return either a singleContentItemInterfaceobject or an array ofContentItemInterfaceobjects. If a single item is returned, theTool::execute()method automatically wraps it in an array. This simplifies tool implementation when only one content item needs to be returned, e.g.,return $this->text("response");. The final JSON output still adheres to the Model Context Protocol, with thecontentfield always being an array.Shorter Helper Method Names: The content creation helper methods within the
Toolclass have been renamed for brevity and consistency with similar methods in theResourceclass:createTextContent()is nowtext()createImageContent()is nowimage()createAudioContent()is nowaudio()createEmbeddedResource()is nowembeddedResource()Codebase Updates: All existing tool implementations and tests throughout the codebase have been updated to use the new helper method names.
Enhanced Testing:
ToolTest.phphas been significantly updated with new test cases to cover:doExecute().PHPStan and PHPUnit Fixes: Addressed various PHPStan typing errors by adding appropriate
usestatements and refining PHPDoc annotations. Corrected PHPUnit test logic, including data passed to media helper methods and expected exception types.I ran the
jules_setup.shscript to ensure a consistent development environment, and therun_tests.shscript confirms that all tests and linters pass.