8000 update mcp toolset and sample agent based on new tool_filter definition · Syntax404-coder/adk-python@d35b99e · GitHub
[go: up one dir, main page]

Skip to content

Commit d35b99e

Browse files
seanzhougooglecopybara-github
authored andcommitted
update mcp toolset and sample agent based on new tool_filter definition
PiperOrigin-RevId: 757969950
1 parent 7220288 commit d35b99e

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

contributing/samples/mcp_agent/agent.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@
3434
],
3535
),
3636
# don't want agent to do write operation
37-
tool_predicate=lambda tool, ctx=None: tool.name
38-
not in ('write_file', 'edit_file', 'create_directory', 'move_file'),
37+
tool_filter=[
38+
'write_file',
39+
'edit_file',
40+
'create_directory',
41+
'move_file',
42+
],
3943
)
4044
],
4145
)

src/google/adk/tools/base_toolset.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from abc import ABC
22
from abc import abstractmethod
3-
from typing import Optional
3+
from typing import Optional, runtime_checkable
44
from typing import Protocol
55

66
from google.adk.agents.readonly_context import ReadonlyContext
77
from google.adk.tools.base_tool import BaseTool
88

99

10+
@runtime_checkable
1011
class ToolPredicate(Protocol):
1112
"""Base class for a predicate that defines the interface to decide whether a
1213

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ async def _initialize(self) -> ClientSession:
9797
self.session = await self.session_manager.create_session()
9898
return self.session
9999

100+
def _is_selected(
101+
self, tool: ..., readonly_context: Optional[ReadonlyContext]
102+
) -> bool:
103+
"""Checks if a tool should be selected based on the tool filter."""
104+
if self.tool_filter is None:
105+
return True
106+
if isinstance(self.tool_filter, ToolPredicate):
107+
return self.tool_filter(tool, readonly_context)
108+
if isinstance(self.tool_filter, list):
109+
return tool.name in self.tool_filter
110+
return False
111+
100112
@override
101113
async def close(self):
102114
"""Closes the connection to MCP Server."""
@@ -123,5 +135,5 @@ async def get_tools(
123135
mcp_session_manager=self.session_manager,
124136
)
125137
for tool in tools_response.tools
126-
if self.tool_filter is None or self.tool_filter(tool, readonly_context)
138+
if self._is_selected(tool, readonly_context)
127139
]

0 commit comments

Comments
 (0)
0