Skip to content

Comments

feat: add Close() method to Toolset interface#502

Open
ju4nv1e1r4 wants to merge 4 commits intogoogle:mainfrom
ju4nv1e1r4:feat/481-add-close-method
Open

feat: add Close() method to Toolset interface#502
ju4nv1e1r4 wants to merge 4 commits intogoogle:mainfrom
ju4nv1e1r4:feat/481-add-close-method

Conversation

@ju4nv1e1r4
Copy link

Description

This PR adds a Close() error method to the tool.Toolset interface to support resource cleanup and graceful shutdown of toolsets.

Motivation:

  1. Parity with adk-python: Aligns the Go SDK with the Python SDK (BaseToolset.close), as adk-python is the source of truth.
  2. Resource Management: Addresses the need to properly close persistent connections, specifically for the MCP Toolset (related to issue Add Close method to tool.Toolset interface #481 and mcptoolset: cached MCP session not validated, causes "connection closed" errors on subsequent requests #399), preventing resource leaks (e.g., open sessions/connections).

Changes:

  • Updated tool.Toolset interface to include Close() error.
  • Implemented Close() in filteredToolset to delegate the call to the underlying toolset.
  • Implemented Close() in mcptoolset to clean up the client session/transport.
  • Updated all internal mocks and test implementations to satisfy the new interface.

Related Issues

Fixes #481
Relates to #399

Breaking Change

Yes.
This modifies the public tool.Toolset interface. Any external implementation of this interface will need to implement the Close() method.

Testing Plan

I have verified the changes using the following methods:

1. Unit Tests:

  • Added TestFilteredToolset_Close in tool/tool_test.go to verify that filteredToolset correctly delegates the Close call to the underlying toolset.
  • Updated existing tests and mocks to ensure no regressions.

2. Regression Testing:
Ran the full test suite with race detection enabled. All tests passed.

add-2close-method add-1close-method
go test -v -race ./...

3. Format & Style:
Verified code compliance with gofmt -s.

Adds a `Close()` method to the `Toolset` interface to allow implementations to release resources.

- Implements `Close` in `mcptoolset` to close the underlying MCP client.
- Updates `filteredToolset` to propagate the close call.
- Ensures proper cleanup in examples and tests.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @ju4nv1e1r4, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a crucial Close() method to the tool.Toolset interface, standardizing resource cleanup across toolset implementations. This enhancement ensures better resource management, particularly for persistent connections like those in the MCP Toolset, and aligns the Go SDK with its Python counterpart. The change facilitates graceful shutdown and prevents potential resource leaks, making the system more robust and maintainable.

Highlights

  • Interface Extension: The tool.Toolset interface has been extended to include a new Close() error method, enabling explicit resource cleanup for toolset implementations.
  • Resource Management: The mcptoolset implementation now includes a Close() method to gracefully shut down and clean up the underlying MCP client session and transport, preventing resource leaks.
  • Delegation for Filtered Toolsets: The filteredToolset now implements the Close() method by delegating the call to its wrapped underlying toolset, ensuring proper cleanup through the filter.
  • Breaking Change: This change is a breaking change as any external implementations of the tool.Toolset interface will now need to implement the new Close() method.
  • Test Coverage and Examples: New unit tests have been added for filteredToolset.Close(), and existing tests and examples (like examples/mcp/main.go and tool/mcptoolset/set_test.go) have been updated to utilize the new Close() method with defer.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a Close() method to the tool.Toolset interface to enable resource cleanup, which is a good addition for managing resources like network connections. The implementation is propagated correctly to filteredToolset and mcptoolset. My main feedback is to consistently handle the error returned by the new Close() method, especially in deferred calls within tests, to prevent potential issues from being silently ignored.

@ju4nv1e1r4
Copy link
Author

Another test:
image

@dpasiukevich dpasiukevich added the enhancement New feature or request label Feb 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Close method to tool.Toolset interface

2 participants