diff --git a/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_toolset.py b/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_toolset.py index 26c8a0f5c3..db5edb0a09 100644 --- a/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_toolset.py +++ b/src/google/adk/tools/openapi_tool/openapi_spec_parser/openapi_toolset.py @@ -69,6 +69,7 @@ def __init__( auth_scheme: Optional[AuthScheme] = None, auth_credential: Optional[AuthCredential] = None, tool_filter: Optional[Union[ToolPredicate, List[str]]] = None, + tool_name_prefix: Optional[str] = None, ssl_verify: Optional[Union[bool, str, ssl.SSLContext]] = None, ): """Initializes the OpenAPIToolset. @@ -104,6 +105,9 @@ def __init__( ``google.adk.tools.openapi_tool.auth.auth_helpers`` tool_filter: The filter used to filter the tools in the toolset. It can be either a tool predicate or a list of tool names of the tools to expose. + tool_name_prefix: The prefix to prepend to the names of the tools returned + by the toolset. Useful when multiple OpenAPI specs have tools with + similar names. ssl_verify: SSL certificate verification option for all tools. Can be: - None: Use default verification (True) - True: Verify SSL certificates using system CA @@ -113,7 +117,7 @@ def __init__( This is useful for enterprise environments where requests go through a TLS-intercepting proxy with a custom CA certificate. """ - super().__init__(tool_filter=tool_filter) + super().__init__(tool_filter=tool_filter, tool_name_prefix=tool_name_prefix) if not spec_dict: spec_dict = self._load_spec(spec_str, spec_str_type) self._ssl_verify = ssl_verify diff --git a/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_toolset.py b/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_toolset.py index 80fcc1d7eb..f5d9e99701 100644 --- a/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_toolset.py +++ b/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_toolset.py @@ -169,3 +169,24 @@ def test_openapi_toolset_configure_verify_all(openapi_spec: Dict[str, Any]): for tool in toolset._tools: assert tool._ssl_verify == ca_bundle_path + + +async def test_openapi_toolset_tool_name_prefix(openapi_spec: Dict[str, Any]): + """Test tool_name_prefix parameter prefixes tool names.""" + prefix = "my_api" + toolset = OpenAPIToolset(spec_dict=openapi_spec, tool_name_prefix=prefix) + + # Verify the toolset has the prefix set + assert toolset.tool_name_prefix == prefix + + prefixed_tools = await toolset.get_tools_with_prefix() + assert len(prefixed_tools) == 5 + + # Verify all tool names are prefixed + for tool in prefixed_tools: + assert tool.name.startswith(f"{prefix}_") + + # Verify specific tool name is prefixed + expected_prefixed_name = "my_api_calendar_calendars_insert" + prefixed_tool_names = [t.name for t in prefixed_tools] + assert expected_prefixed_name in prefixed_tool_names