本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用带有策略的 AgentCore 网关 AgentCore
按照网关授权和身份验证指南获取网关访问所需的证书。
策略评估仅适用于 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}
}
}'
网关不会代表您生成会话 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
}
}