aws / aws/bedrock-agentcore-sdk-python

Agentcore Search Tool plugin must covers allow list

Aperta
#580 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Python
Stelle
761
Fork
147
Merge medio
1g 23h
PR unite (30g)
7

Descrizione

**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),
)

```

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia leggendo AgentCoreToolSearchPlugin e il suo punto di ingresso search_and_filter_tools, inclusi la chiamata x_amz_bedrock_agentcore_search e il flusso _build_tools_from_search_result. Implementa il comportamento della allowlist mostrato nell’esempio, in modo che i risultati della ricerca semantica includano solo gli strumenti consentiti, mentre una allowlist vuota mantenga il comportamento attuale senza restrizioni. Verifica sia le ricerche con restrizioni sia quelle senza restrizioni.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
aws, python
Ambito
tooling
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
64/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.