aws / aws/bedrock-agentcore-sdk-python
Agentcore Search Tool plugin must covers allow list
- 主要言語
- Python
- スター
- 761
- フォーク
- 147
- 平均マージ
- 1日 23時間
- マージ済み PR(30日)
- 7
説明
**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),
)
```
コントリビューションガイド
調査の方向性
まず AgentCoreToolSearchPlugin と、そのエントリーポイントである search_and_filter_tools を読み、x_amz_bedrock_agentcore_search の呼び出しと _build_tools_from_search_result のフローを確認します。例に示されている allowlist の動作を実装し、セマンティック検索の結果に許可されたツールのみが含まれるようにします。一方、空の allowlist の場合は現在の制限のない動作を維持します。制限付き検索と制限なし検索の両方を検証します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- aws, python
- 領域
- tooling
- issue の種類
- 機能追加
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 静か
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 64/100