aws / aws/bedrock-agentcore-sdk-python

Agentcore Search Tool plugin must covers allow list

Offen
#580 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Python
Sterne
761
Forks
147
Ø Merge
1 T. 23 Std.
Gemergte PRs (30 T.)
7

Beschreibung

**Is your feature request related to a problem? Please describe.**
When defining McpClient for agentcoregateway we can define an allowlist , but using the semantic search using Agentcore Search Tool breaks the alowlist functionality and can introduce extra level of halucination and difficulties during agent loop and tool selection.

**Describe the solution you'd like**
The Plugin can apply the allowlist after semantic search call , this way , even agentcore gateway retourns Many tools form an OpenApi Spec ( etc..) this allow list validation will only return the tools that are present in allow list.

**Describe alternatives you've considered**
In every project we create a custom plugin to apply the solution. while this works but create a lot of repeated code for all strands agent SDK users

**Additional context**
A example of plugin used

```python
import logging

from bedrock_agentcore.gateway.integrations.strands.plugins import AgentCoreToolSearchPlugin
from strands.hooks import BeforeInvocationEvent
from strands.plugins import Plugin, hook
from strands.tools.mcp import MCPClient

from .intent import _UNIFIED_INTENT_PROVIDER, UnifiedIntentProvider

logger = logging.getLogger(__name__)

class McpClientSearchPlugin(AgentCoreToolSearchPlugin):
"""Tool-search plugin scoped to one gateway's MCP client, driven by ``ParallelToolSearchPlugin``."""

def __init__(
self,
mcp_client: MCPClient,
*,
name: str,
domain: str,
allowed_tool_names: frozenset[str] = frozenset(),
):
"""
Args:
mcp_client: MCP client connected to this gateway.
name: Plugin name for each gateway.
domain: ``UnifiedIntentDecision`` field name this gateway's intent comes from.
allowed_tool_names: Drops search results not in this set; empty (default) means no restriction.
"""
self.name = name
self.domain = domain
super().__init__(mcp_client)
self._allowed_tool_names = allowed_tool_names

def search_and_filter_tools(self, intent: str) -> list:
"""Search the gateway for given intent. And filter allowed tools """
if not intent:
logger.info("%s loaded 0 tool(s): intent empty or out of scope", self.name)
return []

try:
result = self._mcp_client.call_tool_sync(
tool_use_id="intent-search",
name="x_amz_bedrock_agentcore_search",
arguments={"query": intent},
)
agent_tools = self._build_tools_from_search_result(result)
except Exception as e:
logger.error("AgentCore Gateway search failed: %s", e)
return []

if not self._allowed_tool_names:
return agent_tools

allowed, dropped = [], []
for agent_tool in agent_tools:
(allowed if agent_tool.tool_name in self._allowed_tool_names else dropped).append(agent_tool)
if dropped:
logger.warning(
"%s: dropping non-allowlisted tool(s) surfaced by search: %s",
self.name,
sorted(t.tool_name for t in dropped),
)
return allowed

def apply_tools(self, event: BeforeInvocationEvent, agent_tools: list) -> None:
"""Swap the tools this plugin registered in a previous invocation of this session for ``agent_tools``."""
for name in list(self._loaded_tool_names):
event.agent.tool_registry.registry.pop(name, None)
self._loaded_tool_names.clear()

for agent_tool in agent_tools:
event.agent.tool_registry.register_tool(agent_tool)
self._loaded_tool_names.add(agent_tool.tool_name)

logger.info(
"%s loaded %d tool(s): %s",
self.name,
len(self._loaded_tool_names),
sorted(self._loaded_tool_names),
)

```

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginne mit dem Lesen von AgentCoreToolSearchPlugin und seinem Einstiegspunkt search_and_filter_tools, einschließlich des Aufrufs x_amz_bedrock_agentcore_search und des Ablaufs _build_tools_from_search_result. Implementiere das im Beispiel gezeigte Allowlist-Verhalten, sodass die Ergebnisse der semantischen Suche nur zulässige Tools enthalten, während eine leere Allowlist das aktuelle uneingeschränkte Verhalten beibehält. Überprüfe sowohl eingeschränkte als auch uneingeschränkte Suchen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
aws, python
Bereich
tooling
Issue-Typ
Feature
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
64/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.