Skip to content

Commit 55dd83f

Browse files
authored
Merge branch 'main' into codex/provider-prefixed-gemini-models
2 parents 56047c7 + 282db87 commit 55dd83f

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/google/adk/tools/mcp_tool/mcp_toolset.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def __init__(
118118
use_mcp_resources: Optional[bool] = False,
119119
sampling_callback: Optional[SamplingFnT] = None,
120120
sampling_capabilities: Optional[SamplingCapability] = None,
121+
credential_key: str | None = None,
121122
):
122123
"""Initializes the McpToolset.
123124
@@ -157,6 +158,8 @@ def __init__(
157158
sampling_callback: Optional callback to handle sampling requests from the
158159
MCP server.
159160
sampling_capabilities: Optional capabilities for sampling.
161+
credential_key: A user specified key used to load and save this credential
162+
in a credential service. Used with auth_scheme.
160163
"""
161164

162165
# --- BEGIN BOUND TOKEN PATCH ---
@@ -197,6 +200,7 @@ def __init__(
197200
AuthConfig(
198201
auth_scheme=auth_scheme,
199202
raw_auth_credential=auth_credential,
203+
credential_key=credential_key,
200204
)
201205
if auth_scheme
202206
else None
@@ -469,6 +473,7 @@ def from_config(
469473
tool_name_prefix=mcp_toolset_config.tool_name_prefix,
470474
auth_scheme=mcp_toolset_config.auth_scheme,
471475
auth_credential=mcp_toolset_config.auth_credential,
476+
credential_key=mcp_toolset_config.credential_key,
472477
use_mcp_resources=mcp_toolset_config.use_mcp_resources,
473478
)
474479

@@ -520,6 +525,8 @@ class McpToolsetConfig(BaseToolConfig):
520525

521526
auth_credential: Optional[AuthCredential] = None
522527

528+
credential_key: str | None = None
529+
523530
use_mcp_resources: bool = False
524531

525532
@model_validator(mode="after")

tests/unittests/tools/mcp_tool/test_mcp_toolset.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from google.adk.auth.auth_credential import AuthCredentialTypes
2828
from google.adk.auth.auth_credential import HttpAuth
2929
from google.adk.auth.auth_credential import HttpCredentials
30+
from google.adk.auth.auth_credential import OAuth2Auth
3031
from google.adk.auth.auth_tool import AuthConfig
3132
from google.adk.tools.load_mcp_resource_tool import LoadMcpResourceTool
3233
from google.adk.tools.mcp_tool.mcp_session_manager import MCPSessionManager
@@ -35,6 +36,7 @@
3536
from google.adk.tools.mcp_tool.mcp_session_manager import StreamableHTTPConnectionParams
3637
from google.adk.tools.mcp_tool.mcp_tool import MCPTool
3738
from google.adk.tools.mcp_tool.mcp_toolset import McpToolset
39+
from google.adk.tools.tool_configs import ToolArgsConfig
3840
from mcp import StdioServerParameters
3941
from mcp.types import BlobResourceContents
4042
from mcp.types import ListResourcesResult
@@ -136,10 +138,8 @@ def test_init_with_tool_filter_list(self):
136138
def test_init_with_auth(self):
137139
"""Test initialization with authentication."""
138140
# Create real auth scheme instances
139-
from fastapi.openapi.models import OAuth2
140141

141142
auth_scheme = OAuth2(flows={})
142-
from google.adk.auth.auth_credential import OAuth2Auth
143143

144144
auth_credential = AuthCredential(
145145
auth_type="oauth2",
@@ -155,6 +155,42 @@ def test_init_with_auth(self):
155155
assert toolset._auth_scheme == auth_scheme
156156
assert toolset._auth_credential == auth_credential
157157

158+
def test_init_with_auth_and_credential_key(self):
159+
"""Test initialization with authentication and a custom credential_key."""
160+
161+
auth_scheme = OAuth2(flows={})
162+
163+
auth_credential = AuthCredential(
164+
auth_type="oauth2",
165+
oauth2=OAuth2Auth(client_id="test_id", client_secret="test_secret"),
166+
)
167+
168+
toolset = McpToolset(
169+
connection_params=self.mock_stdio_params,
170+
auth_scheme=auth_scheme,
171+
auth_credential=auth_credential,
172+
credential_key="my_custom_key",
173+
)
174+
175+
assert toolset._auth_scheme == auth_scheme
176+
assert toolset._auth_credential == auth_credential
177+
assert toolset._auth_config.credential_key == "my_custom_key"
178+
179+
def test_from_config_with_credential_key(self):
180+
"""Test that from_config correctly parses credential_key."""
181+
182+
auth_scheme = OAuth2(flows={})
183+
184+
config = ToolArgsConfig(
185+
stdio_server_params=self.mock_stdio_params,
186+
auth_scheme=auth_scheme,
187+
credential_key="my_custom_key",
188+
)
189+
toolset = McpToolset.from_config(config, "")
190+
191+
assert isinstance(toolset._auth_scheme, OAuth2)
192+
assert toolset._auth_config.credential_key == "my_custom_key"
193+
158194
def test_init_missing_connection_params(self):
159195
"""Test initialization with missing connection params raises error."""
160196
with pytest.raises(ValueError, match="Missing connection params"):

0 commit comments

Comments
 (0)