本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
在 AgentCore 中使用 AgentCore Gateway 搭配 政策
遵循閘道授權和身分驗證指南,取得閘道存取所需的登入資料。
政策評估僅適用於 MCP 工具。無論政策評估模式為何,閘道一律允許 MCP 提示 (prompts/list、prompts/get) 和資源 (resources/list、resources/read、resources/templates/list)。
工具清單會視為中繼動作 。當委託人列出可用的工具時,政策引擎不會評估特定工具調用的完整內容 (例如輸入參數)。
委託人只能查看清單中允許依政策呼叫的工具。由於工具呼叫的完整內容在列出期間不可用,這表示如果存在允許對該工具進行呼叫的任何一組情況,則允許委託人列出工具。
因此,清單中出現的工具不保證對該工具的後續呼叫會獲得授權。實際工具調用的授權決策會使用完整的請求內容分別評估,包括輸入參數。
選取下列其中一種方法:
範例
- curl
-
-
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
-
-
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
-
-
# 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
-
-
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))
傳遞暫時政策的政策工作階段 ID
若要啟用暫時政策評估,請在請求中包含 x-amzn-bedrock-agentcore-policy-session-id標頭。這會將多個調用分組為單一工作階段,以強制執行工作階段感知政策。
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" \
-H "x-amzn-bedrock-agentcore-policy-session-id: YOUR_SESSION_ID" \
-d '{
"jsonrpc": "2.0",
"id": "test-temporal-policy",
"method": "tools/call",
"params": {
"name": "tool_name",
"arguments": {arguments}
}
}'
Gateway 不會代表您產生工作階段 ID。您必須產生工作階段 ID,並在每次請求時傳送,從第一個請求開始。如果您省略 標頭,或傳送空值,閘道不會建立工作階段。如果關聯的政策引擎包含暫時政策,則沒有工作階段 ID 的請求會失敗並出現驗證錯誤。傳送與相同工作階段中每個請求相同的 ID。
政策回應
當政策允許請求時:
{
"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
}
}