Repository navigation
fix: skip reserved ADK framework names in MCP toolsets #1673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NewPeople-star
wants to merge
1
commit into
google:main
Choose a base branch
from
NewPeople-star:fix/mcp-reserved-tool-names-1513
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
47 changes: 47 additions & 0 deletions
47
core/src/main/java/com/google/adk/tools/mcp/McpToolNameFilter.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.adk.tools.mcp; | ||
|
|
||
| import com.google.common.collect.ImmutableSet; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** Excludes MCP server tools that collide with ADK framework tools. */ | ||
| final class McpToolNameFilter { | ||
| private static final Logger logger = LoggerFactory.getLogger(McpToolNameFilter.class); | ||
|
|
||
| // Keep these framework-reserved names aligned with ADK Python's MCP toolset. | ||
| private static final ImmutableSet<String> RESERVED_NAMES = | ||
| ImmutableSet.of( | ||
| "set_model_response", | ||
| "transfer_to_agent", | ||
| "adk_request_confirmation", | ||
| "adk_request_credential", | ||
| "adk_request_input"); | ||
|
|
||
| private McpToolNameFilter() {} | ||
|
|
||
| static boolean isAllowed(String toolName) { | ||
| if (RESERVED_NAMES.contains(toolName)) { | ||
| logger.warn( | ||
| "Skipping MCP tool '{}' because it collides with a reserved ADK framework tool name.", | ||
| toolName); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
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
174 changes: 174 additions & 0 deletions
174
core/src/test/java/com/google/adk/tools/mcp/McpToolsetReservedNamesTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.adk.tools.mcp; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
| import static org.mockito.Mockito.atLeastOnce; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import com.google.adk.JsonBaseModel; | ||
| import com.google.adk.agents.ReadonlyContext; | ||
| import com.google.adk.models.LlmRequest; | ||
| import com.google.adk.tools.BaseTool; | ||
| import com.google.adk.tools.BaseToolset; | ||
| import com.google.adk.tools.NamedToolPredicate; | ||
| import com.google.adk.tools.SetModelResponseTool; | ||
| import com.google.adk.tools.ToolPredicate; | ||
| import com.google.genai.types.Schema; | ||
| import io.modelcontextprotocol.client.McpAsyncClient; | ||
| import io.modelcontextprotocol.client.McpSyncClient; | ||
| import io.modelcontextprotocol.json.McpJsonDefaults; | ||
| import io.modelcontextprotocol.spec.McpSchema.ListToolsResult; | ||
| import io.modelcontextprotocol.spec.McpSchema.Tool; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.jspecify.annotations.Nullable; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.Parameterized; | ||
| import org.junit.runners.Parameterized.Parameters; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| /** Runs the same reserved-name contract against both MCP toolset implementations. */ | ||
| @RunWith(Parameterized.class) | ||
| public class McpToolsetReservedNamesTest { | ||
| private static final List<String> RESERVED_NAMES = | ||
| List.of( | ||
| "set_model_response", | ||
| "transfer_to_agent", | ||
| "adk_request_confirmation", | ||
| "adk_request_credential", | ||
| "adk_request_input"); | ||
|
|
||
| @Parameters(name = "async={0}") | ||
| public static List<Boolean> parameters() { | ||
| return List.of(false, true); | ||
| } | ||
|
|
||
| private final boolean async; | ||
| private final ReadonlyContext context = mock(ReadonlyContext.class); | ||
| private final McpSyncClient syncClient = mock(McpSyncClient.class); | ||
| private final McpAsyncClient asyncClient = mock(McpAsyncClient.class); | ||
|
|
||
| public McpToolsetReservedNamesTest(boolean async) { | ||
| this.async = async; | ||
| } | ||
|
|
||
| @Test | ||
| public void getTools_skipsReservedNamesAndPreservesOtherToolsInOrder() { | ||
| List<String> names = new ArrayList<>(RESERVED_NAMES); | ||
| names.add(0, "get_weather"); | ||
| names.add("google_search"); | ||
| names.add("set_model_response_custom"); | ||
|
|
||
| assertThat(toolNames(createToolset(names, null))) | ||
| .containsExactly("get_weather", "google_search", "set_model_response_custom") | ||
| .inOrder(); | ||
| } | ||
|
|
||
| @Test | ||
| public void getTools_allReservedNames_returnsEmptyAndClosesSession() throws Exception { | ||
| BaseToolset toolset = createToolset(RESERVED_NAMES, null); | ||
|
|
||
| assertThat(toolNames(toolset)).isEmpty(); | ||
| toolset.close(); | ||
|
|
||
| if (async) { | ||
| verify(asyncClient, atLeastOnce()).closeGracefully(); | ||
| } else { | ||
| verify(syncClient).close(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void getTools_emptyServerResponse_returnsEmpty() { | ||
| assertThat(toolNames(createToolset(List.of(), null))).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| public void getTools_nameFilterCannotReenableReservedTools() { | ||
| List<String> names = new ArrayList<>(RESERVED_NAMES); | ||
| names.add("get_weather"); | ||
| names.add("get_time"); | ||
| List<String> selectedNames = new ArrayList<>(RESERVED_NAMES); | ||
| selectedNames.add("get_weather"); | ||
|
|
||
| assertThat(toolNames(createToolset(names, new NamedToolPredicate(selectedNames)))) | ||
| .containsExactly("get_weather"); | ||
| } | ||
|
|
||
| @Test | ||
| public void getTools_predicateOnlySeesNonReservedToolsAndReceivesContext() { | ||
| List<String> names = new ArrayList<>(RESERVED_NAMES); | ||
| names.add("get_weather"); | ||
| names.add("get_time"); | ||
| List<String> inspectedNames = new ArrayList<>(); | ||
| ToolPredicate predicate = | ||
| (tool, readonlyContext) -> { | ||
| assertThat(readonlyContext.orElseThrow()).isSameInstanceAs(context); | ||
| inspectedNames.add(tool.name()); | ||
| return !tool.name().equals("get_time"); | ||
| }; | ||
|
|
||
| assertThat(toolNames(createToolset(names, predicate))).containsExactly("get_weather"); | ||
| assertThat(inspectedNames).containsExactly("get_weather", "get_time").inOrder(); | ||
| } | ||
|
|
||
| @Test | ||
| public void getTools_canBeCombinedWithFrameworkOutputSchemaTool() { | ||
| BaseToolset toolset = createToolset(List.of("set_model_response", "get_weather"), null); | ||
| SetModelResponseTool frameworkTool = | ||
| new SetModelResponseTool(Schema.builder().type("OBJECT").build()); | ||
|
|
||
| LlmRequest request = | ||
| LlmRequest.builder() | ||
| .appendTools(toolset.getTools(context).toList().blockingGet()) | ||
| .appendTools(List.of(frameworkTool)) | ||
| .build(); | ||
|
|
||
| assertThat(request.tools()).containsKey("get_weather"); | ||
| assertThat(request.tools()).containsEntry("set_model_response", frameworkTool); | ||
| assertThat(request.tools()).hasSize(2); | ||
| } | ||
|
|
||
| private List<String> toolNames(BaseToolset toolset) { | ||
| return toolset.getTools(context).map(BaseTool::name).toList().blockingGet(); | ||
| } | ||
|
|
||
| private BaseToolset createToolset(List<String> names, @Nullable ToolPredicate toolFilter) { | ||
| List<Tool> tools = | ||
| names.stream() | ||
| .map(name -> Tool.builder(name, McpJsonDefaults.getMapper(), "{}").build()) | ||
| .toList(); | ||
| ListToolsResult response = ListToolsResult.builder(tools).build(); | ||
| McpSessionManager manager = mock(McpSessionManager.class); | ||
| if (async) { | ||
| when(manager.createAsyncSession()).thenReturn(asyncClient); | ||
| when(asyncClient.initialize()).thenReturn(Mono.empty()); | ||
| when(asyncClient.listTools()).thenReturn(Mono.just(response)); | ||
| when(asyncClient.closeGracefully()).thenReturn(Mono.empty()); | ||
| return new McpAsyncToolset(manager, JsonBaseModel.getMapper(), toolFilter); | ||
| } | ||
| when(manager.createSession()).thenReturn(syncClient); | ||
| when(syncClient.listTools()).thenReturn(response); | ||
| return toolFilter == null | ||
| ? new McpToolset(manager, JsonBaseModel.getMapper()) | ||
| : new McpToolset(manager, JsonBaseModel.getMapper(), toolFilter); | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent log flooding when
getTools()is called repeatedly, we should ensure that the warning for a skipped reserved tool name is logged only once. Additionally, we should add a null check fortoolNameto prevent potentialNullPointerExceptionwhen callingRESERVED_NAMES.contains(toolName)(sinceImmutableSetdoes not permit null elements).