Skip to content

Conversation

@james2037
Copy link
Owner

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.

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
Copy link

codecov bot commented Jun 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

google-labs-jules bot and others added 2 commits June 4, 2025 19:17
… 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.
@github-actions
Copy link

github-actions bot commented Jun 4, 2025

🧪 Test Results Summary

PHPUnit Tests

✅ All tests passed!

Details: OK (204 tests, 854 assertions)

  • Total Tests: 204
  • Total Assertions: 854

Code Quality

  • PHPCS: ✅ No issues
  • PHPStan: ✅ No errors found

📁 Detailed Reports

  • Coverage Report: Download the test-outputs artifact for detailed HTML coverage report
  • Test Results: Check the "Tests" tab above for detailed test results
  • Raw Logs: View the "Actions" tab for complete output logs

This comment will update automatically when you push new commits.

Code Coverage

Code Coverage

Package Line Rate Health
Capability/CapabilityInterface.php 0%
Capability/ResourcesCapability.php 100%
Capability/ToolsCapability.php 100%
Exception/InvalidParamsException.php 0%
Exception/InvalidRequestException.php 0%
Exception/MethodNotSupportedException.php 0%
Exception/TransportException.php 0%
Message/JsonRpcMessage.php 89%
Registry/Registry.php 97%
Resource/Attribute/ResourceUri.php 100%
Resource/BlobResourceContents.php 100%
Resource/Resource.php 100%
Resource/ResourceContents.php 100%
Resource/ResourceRegistry.php 100%
Resource/TextResourceContents.php 100%
Server.php 69%
Tool/Attribute/Parameter.php 100%
Tool/Attribute/Tool.php 100%
Tool/Attribute/ToolAnnotations.php 100%
Tool/Content/AbstractContent.php 100%
Tool/Content/AbstractMediaContent.php 100%
Tool/Content/Annotations.php 100%
Tool/Content/AudioContent.php 100%
Tool/Content/ContentItemInterface.php 0%
Tool/Content/EmbeddedResource.php 100%
Tool/Content/ImageContent.php 100%
Tool/Content/TextContent.php 100%
Tool/Tool.php 92%
Tool/ToolRegistry.php 100%
Transport/AbstractTransport.php 0%
Transport/HttpTransport.php 70%
Transport/StdioTransport.php 96%
Transport/TransportInterface.php 0%
Summary 86% (837 / 978)

@james2037 james2037 merged commit f132a47 into master Jun 4, 2025
3 checks passed
@james2037 james2037 deleted the feature/tool-ergonomics-and-method-rename branch June 4, 2025 19:38
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.

2 participants