View a markdown version of this page

使用自然語言查詢在 AgentCore 閘道中搜尋工具 - Amazon Bedrock AgentCore

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用自然語言查詢在 AgentCore 閘道中搜尋工具

如果您在建立閘道時啟用了閘道的語意搜尋,則可以呼叫 x_amz_bedrock_agentcore_search工具,使用自然語言查詢在閘道中搜尋工具。當您有許多工具且需要為您的使用案例尋找最合適的工具時,語意搜尋特別有用。若要了解如何在閘道建立期間啟用語意搜尋,請參閱建立 Amazon Bedrock AgentCore 閘道。

支援語意搜尋 AWS 的區域

下列 AWS 區域支援語意搜尋:

區域名稱 區域

美國東部 (維吉尼亞北部)

us-east-1

美國東部 (俄亥俄)

us-east-2

美國西部 (奧勒岡)

us-west-2

亞太地區 (海德拉巴)

ap-south-2

亞太區域 (孟買)

ap-south-1

亞太區域 (首爾)

ap-northeast-2

亞太區域 (新加坡)

ap-southeast-1

亞太區域 (雪梨)

ap-southeast-2

亞太區域 (東京)

ap-northeast-1

加拿大 (中部)

ca-central-1

歐洲 (法蘭克福)

eu-central-1

歐洲 (愛爾蘭)

eu-west-1

歐洲 (倫敦)

eu-west-2

歐洲 (米蘭)

eu-south-1

Europe (Paris)

eu-west-3

歐洲 (西班牙)

eu-south-2

Europe (Stockholm)

eu-north-1

南美洲 (聖保羅)

sa-east-1

若要使用此 AgentCore 工具搜尋工具,請使用 tools/call方法向閘道的 MCP 端點提出下列 POST 請求:

範例
2025-11-25 and earlier
POST /mcp HTTP/1.1 Host: ${GatewayEndpoint} Accept: application/json, text/event-stream Content-Type: application/json Authorization: ${Authorization header} MCP-Protocol-Version: ${McpProtocolVersion} { "jsonrpc": "2.0", "id": "${RequestName}", "method": "tools/call", "params": { "name": "x_amz_bedrock_agentcore_search", "arguments": { "query": ${Query} } } }
2026-07-28

在版本 上2026-07-28,每個請求都會攜帶 MCP-Protocol-Version標頭、 Mcp-Method和 Mcp-Name請求中繼資料標頭,以及內文中的_meta版本欄位。

POST /mcp HTTP/1.1 Host: ${GatewayEndpoint} Accept: application/json, text/event-stream Content-Type: application/json Authorization: ${Authorization header} MCP-Protocol-Version: 2026-07-28 Mcp-Method: tools/call Mcp-Name: x_amz_bedrock_agentcore_search { "jsonrpc": "2.0", "id": "${RequestName}", "method": "tools/call", "params": { "name": "x_amz_bedrock_agentcore_search", "arguments": { "query": ${Query} }, "_meta": { "io.modelcontextprotocol/protocolVersion": "2026-07-28", "io.modelcontextprotocol/clientInfo": { "name": "my-agent", "version": "1.0.0" }, "io.modelcontextprotocol/clientCapabilities": {} } } }
注意

閘道僅接受其protocolConfiguration.mcp組態 supportedVersions 欄位中列出的 MCP 通訊協定版本。若要使用版本 2026-07-28,請確定閘道的 supportedVersions包含它。您可以使用 UpdateGateway API 變更支援的版本。

取代以下的值:

  • ${GatewayEndpoint} – 閘道的 URL,如 CreateGateway API 的回應所提供。

  • ${Authorization header} – 當您設定傳入授權時,來自身分提供者的授權憑證。

  • ${McpProtocolVersion} – 請求的 MCP 通訊協定版本,例如 2025-11-25。版本必須是閘道支援的版本。

  • ${RequestName} – 請求的名稱。

  • ${Query} – 搜尋工具的自然語言查詢。

回應會傳回與查詢相關的工具清單。

工具搜尋的程式碼範例

若要查看使用自然語言查詢在閘道中尋找工具的範例,請選取下列其中一種方法:

範例
Python requests package (2025-11-25 and earlier)

將 MCP-Protocol-Version標頭設定為閘道支援的版本。

import requests import json def search_tools(gateway_url, access_token, query): headers = { "Content-Type": "application/json", "Authorization": f"Bearer {access_token}", "MCP-Protocol-Version": "2025-11-25" } payload = { "jsonrpc": "2.0", "id": "search-tools-request", "method": "tools/call", "params": { "name": "x_amz_bedrock_agentcore_search", "arguments": { "query": query } } } response = requests.post(gateway_url, headers=headers, json=payload) return response.json() # Example usage gateway_url = "https://${GatewayEndpoint}/mcp" # Replace with your actual gateway endpoint access_token = "${AccessToken}" # Replace with your actual access token results = search_tools(gateway_url, access_token, "find order information") print(json.dumps(results, indent=2))
Python requests package (2026-07-28)

在版本 上2026-07-28,在內文中包含 Mcp-Method和 Mcp-Name request-metadata 標頭和_meta版本欄位。MCP-Protocol-Version 標頭必須符合 _meta.io.modelcontextprotocol/protocolVersion。閘道的 supportedVersions 必須包含 2026-07-28。

import requests import json def search_tools(gateway_url, access_token, query): headers = { "Content-Type": "application/json", "Authorization": f"Bearer {access_token}", "MCP-Protocol-Version": "2026-07-28", "Mcp-Method": "tools/call", "Mcp-Name": "x_amz_bedrock_agentcore_search" } payload = { "jsonrpc": "2.0", "id": "search-tools-request", "method": "tools/call", "params": { "name": "x_amz_bedrock_agentcore_search", "arguments": { "query": query }, "_meta": { "io.modelcontextprotocol/protocolVersion": "2026-07-28", "io.modelcontextprotocol/clientInfo": {"name": "my-agent", "version": "1.0.0"}, "io.modelcontextprotocol/clientCapabilities": {} } } } response = requests.post(gateway_url, headers=headers, json=payload) return response.json() # Example usage gateway_url = "https://${GatewayEndpoint}/mcp" # Replace with your actual gateway endpoint access_token = "${AccessToken}" # Replace with your actual access token results = search_tools(gateway_url, access_token, "find order information") print(json.dumps(results, indent=2))
MCP Client
from mcp import ClientSession from mcp.client.streamable_http import streamablehttp_client import asyncio async def execute_mcp( url, token, tool_params, headers=None ): default_headers = { "Authorization": f"Bearer {token}" } headers = {**default_headers, **(headers or {})} async with streamablehttp_client( url=url, headers=headers, ) as ( read_stream, write_stream, callA, ): async with ClientSession(read_stream, write_stream) as session: # 1. Perform initialization handshake print("Initializing MCP...") _init_response = await session.initialize() print(f"MCP Server Initialize successful! - {_init_response}") # 2. Call specific tool print(f"Calling tool: {tool_params['name']}") tool_response = await session.call_tool( name=tool_params['name'], arguments=tool_params['arguments'] ) print(f"Tool response: {tool_response}") return tool_response async def main(): url = "https://${GatewayEndpoint}/mcp" token = "your_bearer_token_here" tool_params = { "name": "x_amz_bedrock_agentcore_search", "arguments": { "query": "How do I find order details?" } } await execute_mcp( url=url, token=token, tool_params=tool_params ) if __name__ == "__main__": asyncio.run(main())
Strands MCP Client
from strands.tools.mcp.mcp_client import MCPClient from mcp.client.streamable_http import streamablehttp_client def create_streamable_http_transport(mcp_url: str, access_token: str): return streamablehttp_client(mcp_url, headers={"Authorization": f"Bearer {access_token}"}) def get_full_tools_list(client): """ List tools w/ support for pagination """ more_tools = True tools = [] pagination_token = None while more_tools: tmp_tools = client.list_tools_sync(pagination_token=pagination_token) tools.extend(tmp_tools) if tmp_tools.pagination_token is None: more_tools = False else: more_tools = True pagination_token = tmp_tools.pagination_token return tools def run_agent(mcp_url: str, access_token: str): mcp_client = MCPClient(lambda: create_streamable_http_transport(mcp_url, access_token)) with mcp_client: tools = get_full_tools_list(mcp_client) print(f"Found the following tools: {[tool.tool_name for tool in tools]}") result = mcp_client.call_tool_sync( tool_use_id="tool-123", # A unique ID for the tool call name="x_amz_bedrock_agentcore_search", # The name of the tool to invoke arguments={"query": "find order information"} # A dictionary of arguments for the tool ) print(result) url = {gatewayUrl} token = {AccessToken} run_agent(url, token)
LangGraph MCP Client
import asyncio from langchain_mcp_adapters.client import MultiServerMCPClient from langgraph.prebuilt import create_react_agent url = "" headers = {} def filter_search_tool( ): mcp_client = MultiServerMCPClient( { "agent": { "transport": "streamable_http", "url": url, "headers": headers, } } ) tools = asyncio.run(mcp_client.get_tools()) builtin_search_tool = [] for tool in tools: if tool.name == "x_amz_bedrock_agentcore_search": builtin_search_tool.append(tool) return builtin_search_tool def execute_agent( user_prompt, model_id, region, tools ): model = ChatBedrock(model_id=model_id, region_name=region) agent = create_react_agent(model, filter_search_tool()) _response = asyncio.run(agent.ainvoke({ "messages": user_prompt })) _response = _response.get('messages', {})[1].content print( f"Invoke Langchain Agents Response" f"Response - \n{_response}\n" ) return _response