View a markdown version of this page

在 AgentCore 中使用 AgentCore Gateway 搭配 政策 - Amazon Bedrock AgentCore

在 AgentCore 中使用 AgentCore Gateway 搭配 政策

遵循閘道授權和身分驗證指南,取得閘道存取所需的登入資料。

僅限 MCP 工具

政策評估僅適用於 MCP 工具。無論政策評估模式為何,閘道一律允許 MCP 提示 (prompts/listprompts/get) 和資源 (resources/listresources/readresources/templates/list)。

在 AgentCore 中列出具有政策的 AgentCore 閘道工具

工具清單會視為中繼動作 。當委託人列出可用的工具時,政策引擎不會評估特定工具調用的完整內容 (例如輸入參數)。

委託人只能查看清單中允許依政策呼叫的工具。由於工具呼叫的完整內容在列出期間不可用,這表示如果存在允許對該工具進行呼叫的任何一組情況,則允許委託人列出工具

因此,清單中出現的工具不保證對該工具的後續呼叫會獲得授權。實際工具調用的授權決策會使用完整的請求內容分別評估,包括輸入參數。

選取下列其中一種方法:

範例
curl
  1. curl -X POST \ https://mygateway-abcdefghij.gateway.bedrock-agentcore.us-west-2.amazonaws.com/mcp \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -d '{ "jsonrpc": "2.0", "id": "list-tools-request", "method": "tools/list" }'
Python requests package
  1. import requests import json def list_tools(gateway_url, access_token): headers = { "Content-Type": "application/json", "Authorization": f"Bearer {access_token}" } payload = { "jsonrpc": "2.0", "id": "list-tools-request", "method": "tools/list" } response = requests.post(gateway_url, headers=headers, json=payload) return response.json() # Example usage gateway_url = "https://mygateway-abcdefghij.gateway.bedrock-agentcore.us-west-2.amazonaws.com/mcp" access_token = "YOUR_ACCESS_TOKEN" tools = list_tools(gateway_url, access_token) print(json.dumps(tools, indent=2))

    回應只會傳回政策允許您查看的工具。政策拒絕的工具不會出現在清單中。

使用政策呼叫閘道工具

對閘道進行工具呼叫。政策評估會決定是否允許或拒絕呼叫。

選取下列其中一種方法:

範例
curl
  1. # Call a tool to test policy enforcement curl -X POST \ https://mygateway-abcdefghij.gateway.bedrock-agentcore.us-west-2.amazonaws.com/mcp \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -d '{ "jsonrpc": "2.0", "id": "test-policy", "method": "tools/call", "params": { "name": "tool_name", "arguments": {arguments} } }'
Python requests package
  1. import requests import json def call_gateway_tool(gateway_url, access_token, tool_name, arguments): headers = { "Content-Type": "application/json", "Authorization": f"Bearer {access_token}" } payload = { "jsonrpc": "2.0", "id": "test-policy", "method": "tools/call", "params": { "name": tool_name, "arguments": arguments } } response = requests.post(gateway_url, headers=headers, json=payload) return response.json() # Example usage gateway_url = "https://mygateway-abcdefghij.gateway.bedrock-agentcore.us-west-2.amazonaws.com/mcp" access_token = "YOUR_ACCESS_TOKEN" result = call_gateway_tool( gateway_url, access_token, "RefundTool___process_refund", { "orderId": "12345", "amount": 450, "reason": "Defective product" } ) print(json.dumps(result, indent=2))

政策回應

當政策允許請求時:

{ "jsonrpc": "2.0", "id": 2, "result": { "isError": false, "content": [ { "type": "text", "text": "ToolResult" } ] } }

當政策拒絕請求時:

{ "jsonrpc": "2.0", "id": 2, "result": { "content": [ { "type": "text", "text": "AuthorizeActionException - Tool Execution Denied: Tool call not allowed due to policy enforcement [No policy applies to the request (denied by default).]" } ], "isError": true } }