AWS bedrock-agentcore documentation change
Summary
Updated documentation for MCP semantic search integration, including authorization header standardization, code sample restructuring, and endpoint path corrections
Security assessment
The change replaces a generic 'Bearer ${AccessToken}' reference with a more general '${Authorization header}' while emphasizing authorization credentials from identity providers. This improves security documentation by decoupling implementation details but doesn't address a specific vulnerability. The code samples now include proper authorization header handling patterns, promoting security best practices.
Diff
diff --git a/bedrock-agentcore/latest/devguide/gateway-using-mcp-semantic-search.md b/bedrock-agentcore/latest/devguide/gateway-using-mcp-semantic-search.md index 15fa0b3dd..6bd2c277d 100644 --- a//bedrock-agentcore/latest/devguide/gateway-using-mcp-semantic-search.md +++ b//bedrock-agentcore/latest/devguide/gateway-using-mcp-semantic-search.md @@ -7,2 +6,0 @@ Code samples for tool searching -Amazon Bedrock AgentCore is in preview release and is subject to change. - @@ -11 +9 @@ Amazon Bedrock AgentCore is in preview release and is subject to change. -If you enabled semantic search for your gateway when you created it, you can call the `x_amz_bedrock_agentcore_search` tool to search for tools in your gateway with a natural language query. Semantic search is particularly useful when you have many tools and need to find the most appropriate ones for your use case. To learn how to enable semantic search during gateway creation, see [Creating your Gateway](./gateway-create.html). +If you enabled semantic search for your gateway when you created it, you can call the `x_amz_bedrock_agentcore_search` tool to search for tools in your gateway with a natural language query. Semantic search is particularly useful when you have many tools and need to find the most appropriate ones for your use case. To learn how to enable semantic search during gateway creation, see [Create an Amazon Bedrock AgentCore gateway](./gateway-create.html). @@ -19 +17 @@ To search for a tool using this AgentCore tool, make the following POST request - Authorization: Bearer ${AccessToken} + Authorization: ${Authorization header} @@ -37 +35 @@ Replace the following values: - * `${AccessToken}` – The access token provided by the identity provider when you set up [inbound authorization](./gateway-inbound-auth.html). + * `${Authorization header}` – The authorization credentials from the identity provider when you set up [inbound authorization](./gateway-inbound-auth.html). @@ -81 +79 @@ Python requests package - gateway_url = "https://${GatewayEndpoint}" # Replace with your actual gateway endpoint + gateway_url = "https://${GatewayEndpoint}/mcp" # Replace with your actual gateway endpoint @@ -90,3 +87,0 @@ MCP Client - import json - from datetime import timedelta - @@ -95 +90 @@ MCP Client - + import asyncio @@ -98,0 +94,2 @@ MCP Client + token, + tool_params, @@ -101 +98,5 @@ MCP Client - headers = {**headers} if headers else {} + default_headers = { + "Authorization": f"Bearer {token}" + } + headers = {**default_headers, **(headers or {})} + @@ -116,10 +117,5 @@ MCP Client - # 2. Invoke search tool - print("Invoking search tool...") - tool_name = "x_amz_bedrock_agentcore_search" - args = { - "query": "How do I process images?" - } - invoke_tool_response = await session.call_tool( - tool_name, - arguments=args, - read_timeout_seconds=timedelta(seconds=60) + # 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'] @@ -127,9 +123,17 @@ MCP Client - contents = invoke_tool_response.content - for content in contents: - text = content.text - try: - content = json.dumps(json.loads(text), indent=4) - except: - content = text - print( - f"Invoke tool response: Name - {content}" + 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 @@ -138 +141,0 @@ MCP Client -Strands MCP Client @@ -139,0 +143,2 @@ Strands MCP Client + if __name__ == "__main__": + asyncio.run(main()) @@ -140,0 +146 @@ Strands MCP Client +Strands MCP Client @@ -142,4 +147,0 @@ Strands MCP Client - import asyncio - from strands import Agent - from mcp import ClientSession - from mcp.client.streamable_http import streamablehttp_client @@ -147,3 +148,0 @@ Strands MCP Client - async def main(): - # Create strands agent - agent = Agent() @@ -151,5 +150,2 @@ Strands MCP Client - # Connect to MCP server via streamable HTTP - async with streamablehttp_client("{gatewayURL}") as (read, write, get_session_id): - async with ClientSession(read, write) as session: - # Initialize MCP session - await session.initialize() + from strands.tools.mcp.mcp_client import MCPClient + from mcp.client.streamable_http import streamablehttp_client @@ -157,2 +152,0 @@ Strands MCP Client - result = await session.call_tool('x_amz_bedrock_agentcore_searchaws___recommend', arguments={"query": "How do I process images?"}) - print(f"Result: {result.content}") @@ -163,2 +157,36 @@ Strands MCP Client - if __name__ == "__main__": - asyncio.run(main()) + + 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) @@ -198 +226,3 @@ LangGraph MCP Client - model_id + model_id, + region, + tools @@ -200 +230,3 @@ LangGraph MCP Client - agent = create_react_agent(model_id, filter_search_tool()) + model = ChatBedrock(model_id=model_id, region_name=region) + + agent = create_react_agent(model, filter_search_tool())