feat: add metadata support to MCP tools and refactor metadata tests#602
feat: add metadata support to MCP tools and refactor metadata tests#602alexshopee wants to merge 17 commits intogoogle:mainfrom
Conversation
Summary of ChangesHello, 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 enhances the MCP toolset by integrating metadata forwarding capabilities, allowing request-scoped information to be passed to downstream MCP servers. Concurrently, it streamlines the associated test suite through refactoring, making it more concise and easier to maintain. These changes collectively improve the toolset's extensibility and the robustness of its testing infrastructure. Highlights
🧠 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. Changelog
Using Gemini Code AssistThe 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
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 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
|
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request introduces metadata support for MCP tools, allowing request-scoped data to be forwarded to tool calls. The implementation is clean and follows the description. The changes also include a nice refactoring of the metadata tests to use a generic helper function, which improves maintainability. I've added a couple of suggestions to further improve the robustness of the new tests by ensuring the tool handlers are always executed as expected.
| var receivedMeta map[string]any | ||
| echoToolFunc := func(ctx context.Context, req *mcp.CallToolRequest, input struct{}) (*mcp.CallToolResult, struct{ Message string }, error) { | ||
| receivedMeta = req.Params.Meta | ||
| return nil, struct{ Message string }{Message: "ok"}, nil | ||
| } | ||
|
|
||
| testMetadata := map[string]any{ | ||
| "request_id": "test-123", | ||
| "user_id": "user-456", | ||
| "nested_data": map[string]any{"key": "value"}, | ||
| } | ||
| metadataProvider := func(ctx tool.Context) map[string]any { | ||
| return testMetadata | ||
| } | ||
|
|
||
| result := runMetadataTest(t, metadataProvider, echoToolFunc) | ||
| if result == nil { | ||
| t.Fatal("Expected non-nil result") | ||
| } | ||
|
|
||
| if diff := cmp.Diff(testMetadata, receivedMeta); diff != "" { | ||
| t.Errorf("metadata mismatch (-want +got):\n%s", diff) | ||
| } |
There was a problem hiding this comment.
For improved test robustness, it's a good practice to explicitly verify that the tool handler was actually executed. This can be done by using a boolean flag, similar to the pattern used in TestMetadataProviderReturnsNil. This ensures the test doesn't pass silently if the tool handler is never called.
var receivedMeta map[string]any
var toolCalled bool
echoToolFunc := func(ctx context.Context, req *mcp.CallToolRequest, input struct{}) (*mcp.CallToolResult, struct{ Message string }, error) {
toolCalled = true
receivedMeta = req.Params.Meta
return nil, struct{ Message string }{Message: "ok"}, nil
}
testMetadata := map[string]any{
"request_id": "test-123",
"user_id": "user-456",
"nested_data": map[string]any{"key": "value"},
}
metadataProvider := func(ctx tool.Context) map[string]any {
return testMetadata
}
result := runMetadataTest(t, metadataProvider, echoToolFunc)
if result == nil {
t.Fatal("Expected non-nil result")
}
if !toolCalled {
t.Fatal("Tool was not called")
}
if diff := cmp.Diff(testMetadata, receivedMeta); diff != "" {
t.Errorf("metadata mismatch (-want +got):\n%s", diff)
}| echoToolFunc := func(ctx context.Context, req *mcp.CallToolRequest, input struct{}) (*mcp.CallToolResult, struct{ Message string }, error) { | ||
| if req.Params.Meta != nil { | ||
| t.Errorf("Expected nil metadata, got %v", req.Params.Meta) | ||
| } | ||
| return nil, struct{ Message string }{Message: "ok"}, nil | ||
| } | ||
| _ = runMetadataTest(t, nil, echoToolFunc) |
There was a problem hiding this comment.
To make this test more robust, it's good practice to confirm that the tool handler was actually executed. Without this check, the test could pass silently if the tool handler is never called. You can add a boolean flag that is set within the handler and checked after the test run, similar to the pattern in TestMetadataProviderReturnsNil.
| echoToolFunc := func(ctx context.Context, req *mcp.CallToolRequest, input struct{}) (*mcp.CallToolResult, struct{ Message string }, error) { | |
| if req.Params.Meta != nil { | |
| t.Errorf("Expected nil metadata, got %v", req.Params.Meta) | |
| } | |
| return nil, struct{ Message string }{Message: "ok"}, nil | |
| } | |
| _ = runMetadataTest(t, nil, echoToolFunc) | |
| var toolCalled bool | |
| echoToolFunc := func(ctx context.Context, req *mcp.CallToolRequest, input struct{}) (*mcp.CallToolResult, struct{ Message string }, error) { | |
| toolCalled = true | |
| if req.Params.Meta != nil { | |
| t.Errorf("Expected nil metadata, got %v", req.Params.Meta) | |
| } | |
| return nil, struct{ Message string }{Message: "ok"}, nil | |
| } | |
| _ = runMetadataTest(t, nil, echoToolFunc) | |
| if !toolCalled { | |
| t.Fatal("Tool was not called") | |
| } |
…MetadataProvider & TestMetadataProviderNil
Summary
This PR adds metadata support to the MCP toolset (aligned with google/adk-go#475) and refactors the metadata tests for better maintainability.
Purpose
From upstream #475
MetadataProvideronmcptoolset.Config; when set and returning non-nil metadata, it is attached tomcp.CallToolParams.Metabefore calling the MCP tool.Changes in this PR
runMetadataTest[In, Out any](t, provider, toolFunc)so that:TestMetadataProvider,TestMetadataProviderNil,TestMetadataProviderReturnsNil) only pass the appropriate provider and tool handler and assert on the resulting metadata behavior.Testing
go test ./tool/mcptoolset/ -run 'TestMetadataProvider|TestMetadataProviderNil|TestMetadataProviderReturnsNil' -v