2424from ...agents .readonly_context import ReadonlyContext
2525from ...auth .auth_credential import ServiceAccount
2626from ...auth .auth_schemes import OpenIdConnectWithConfig
27+ from ...tools .base_tool import BaseTool
2728from ...tools .base_toolset import BaseToolset
2829from ...tools .base_toolset import ToolPredicate
2930from ..openapi_tool import OpenAPIToolset
@@ -48,6 +49,7 @@ class GoogleApiToolset(BaseToolset):
4849 tool_name_prefix: Optional prefix to add to all tool names in this toolset.
4950 additional_headers: Optional dict of HTTP headers to inject into every request
5051 executed by this toolset.
52+ additional_scopes: Optional list of additional scopes to request.
5153 """
5254
5355 def __init__ (
@@ -61,6 +63,7 @@ def __init__(
6163 tool_name_prefix : Optional [str ] = None ,
6264 * ,
6365 additional_headers : Optional [Dict [str , str ]] = None ,
66+ additional_scopes : Optional [List [str ]] = None ,
6467 ):
6568 super ().__init__ (tool_filter = tool_filter , tool_name_prefix = tool_name_prefix )
6669 self .api_name = api_name
@@ -69,12 +72,13 @@ def __init__(
6972 self ._client_secret = client_secret
7073 self ._service_account = service_account
7174 self ._additional_headers = additional_headers
75+ self ._additional_scopes = additional_scopes
7276 self ._openapi_toolset = self ._load_toolset_with_oidc_auth ()
7377
7478 @override
7579 async def get_tools (
7680 self , readonly_context : Optional [ReadonlyContext ] = None
77- ) -> List [ GoogleApiTool ]:
81+ ) -> list [ BaseTool ]:
7882 """Get all tools in the toolset."""
7983 return [
8084 GoogleApiTool (
@@ -95,11 +99,17 @@ def _load_toolset_with_oidc_auth(self) -> OpenAPIToolset:
9599 spec_dict = GoogleApiToOpenApiConverter (
96100 self .api_name , self .api_version
97101 ).convert ()
98- scope = list (
102+ discovery_scopes = list (
99103 spec_dict ['components' ]['securitySchemes' ]['oauth2' ]['flows' ][
100104 'authorizationCode'
101105 ]['scopes' ].keys ()
102- )[0 ]
106+ )
107+ default_scope = discovery_scopes [0 ] if discovery_scopes else None
108+
109+ scopes = ([default_scope ] if default_scope else []) + (
110+ self ._additional_scopes or []
111+ )
112+
103113 return OpenAPIToolset (
104114 spec_dict = spec_dict ,
105115 spec_str_type = 'yaml' ,
@@ -117,7 +127,7 @@ def _load_toolset_with_oidc_auth(self) -> OpenAPIToolset:
117127 'client_secret_basic' ,
118128 ],
119129 grant_types_supported = ['authorization_code' ],
120- scopes = [ scope ] ,
130+ scopes = scopes ,
121131 ),
122132 )
123133
0 commit comments