mirror of
https://github.com/EvolutionAPI/adk-python.git
synced 2025-07-14 01:41:25 -06:00
update mcp toolset and sample agent based on new tool_filter definition
PiperOrigin-RevId: 757969950
This commit is contained in:
parent
722028801a
commit
d35b99e6dd
@ -34,8 +34,12 @@ root_agent = LlmAgent(
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
# don't want agent to do write operation
|
# don't want agent to do write operation
|
||||||
tool_predicate=lambda tool, ctx=None: tool.name
|
tool_filter=[
|
||||||
not in ('write_file', 'edit_file', 'create_directory', 'move_file'),
|
'write_file',
|
||||||
|
'edit_file',
|
||||||
|
'create_directory',
|
||||||
|
'move_file',
|
||||||
|
],
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
from abc import ABC
|
from abc import ABC
|
||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
from typing import Optional
|
from typing import Optional, runtime_checkable
|
||||||
from typing import Protocol
|
from typing import Protocol
|
||||||
|
|
||||||
from google.adk.agents.readonly_context import ReadonlyContext
|
from google.adk.agents.readonly_context import ReadonlyContext
|
||||||
from google.adk.tools.base_tool import BaseTool
|
from google.adk.tools.base_tool import BaseTool
|
||||||
|
|
||||||
|
|
||||||
|
@runtime_checkable
|
||||||
class ToolPredicate(Protocol):
|
class ToolPredicate(Protocol):
|
||||||
"""Base class for a predicate that defines the interface to decide whether a
|
"""Base class for a predicate that defines the interface to decide whether a
|
||||||
|
|
||||||
|
@ -97,6 +97,18 @@ class MCPToolset(BaseToolset):
|
|||||||
self.session = await self.session_manager.create_session()
|
self.session = await self.session_manager.create_session()
|
||||||
return self.session
|
return self.session
|
||||||
|
|
||||||
|
def _is_selected(
|
||||||
|
self, tool: ..., readonly_context: Optional[ReadonlyContext]
|
||||||
|
) -> bool:
|
||||||
|
"""Checks if a tool should be selected based on the tool filter."""
|
||||||
|
if self.tool_filter is None:
|
||||||
|
return True
|
||||||
|
if isinstance(self.tool_filter, ToolPredicate):
|
||||||
|
return self.tool_filter(tool, readonly_context)
|
||||||
|
if isinstance(self.tool_filter, list):
|
||||||
|
return tool.name in self.tool_filter
|
||||||
|
return False
|
||||||
|
|
||||||
@override
|
@override
|
||||||
async def close(self):
|
async def close(self):
|
||||||
"""Closes the connection to MCP Server."""
|
"""Closes the connection to MCP Server."""
|
||||||
@ -123,5 +135,5 @@ class MCPToolset(BaseToolset):
|
|||||||
mcp_session_manager=self.session_manager,
|
mcp_session_manager=self.session_manager,
|
||||||
)
|
)
|
||||||
for tool in tools_response.tools
|
for tool in tools_response.tools
|
||||||
if self.tool_filter is None or self.tool_filter(tool, readonly_context)
|
if self._is_selected(tool, readonly_context)
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user