Loading...
    • 建構
    • 管理
    • 模型與定價
    • 客戶端 SDK
    • API 參考
    Search...
    ⌘K
    第一步
    Claude 簡介快速入門
    使用 Claude 建構
    功能概覽使用 Messages API處理停止原因
    模型功能
    延伸思考自適應思考效能快速模式(測試版:研究預覽)結構化輸出引用來源串流訊息批次處理搜尋結果串流拒絕多語言支援嵌入向量
    工具
    概覽工具使用方式網路搜尋工具網路擷取工具程式碼執行工具記憶體工具Bash 工具電腦使用工具文字編輯器工具
    工具基礎架構
    工具搜尋程式化工具呼叫細粒度工具串流
    上下文管理
    上下文視窗壓縮上下文編輯提示快取Token 計數
    處理檔案
    Files APIPDF 支援圖像與視覺
    技能
    概覽快速入門最佳實踐企業版技能API 中的技能
    MCP
    遠端 MCP 伺服器MCP 連接器
    提示工程
    概覽提示最佳實踐Console 提示工具
    測試與評估
    定義成功標準並建立評估在 Console 中使用評估工具降低延遲
    強化防護欄
    減少幻覺提高輸出一致性防範越獄減少提示洩漏
    資源
    詞彙表
    版本說明
    Claude Platform
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    定義您的代理

    權限政策

    控制代理和 MCP 工具的執行時機。

    權限政策控制伺服器執行的工具(預建代理工具集和 MCP 工具集)是自動執行還是等待您的批准。自訂工具由您的應用程式執行並由您控制,因此不受權限政策管轄。

    所有 Managed Agents API 請求都需要 managed-agents-2026-04-01 beta 標頭。SDK 會自動設定 beta 標頭。

    權限政策類型

    政策行為
    always_allow工具自動執行,無需確認。
    always_ask工作階段發出 session.status_idle 事件,並在執行前等待 user.tool_confirmation 事件。

    為工具集設定政策

    代理工具集權限

    建立代理時,您可以選擇性地使用 default_config.permission_policy 將政策套用至 agent_toolset_20260401 中的每個工具:

    agent=$(curl -fsSL https://api.anthropic.com/v1/agents \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: managed-agents-2026-04-01" \
      -H "content-type: application/json" \
      -d '{
        "name": "Coding Assistant",
        "model": "claude-sonnet-4-6",
        "tools": [
          {
            "type": "agent_toolset_20260401",
            "default_config": {
              "permission_policy": {"type": "always_ask"}
            }
          }
        ]
      }')

    default_config 是選用設定。如果省略,代理工具集將以預設權限政策 always_allow 啟用。

    MCP 工具集權限

    MCP 工具集預設為 always_ask。這確保新增至 MCP 伺服器的新工具不會在未經批准的情況下在您的應用程式中執行。若要自動批准來自受信任 MCP 伺服器的工具,請在 mcp_toolset 項目上設定 permission_policy。

    mcp_server_name 必須與 mcp_servers 陣列中參照的 name 相符。

    此範例連接 GitHub MCP 伺服器,並允許其工具在無需確認的情況下執行:

    agent=$(curl -fsSL https://api.anthropic.com/v1/agents \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: managed-agents-2026-04-01" \
      -H "content-type: application/json" \
      -d '{
        "name": "Dev Assistant",
        "model": "claude-sonnet-4-6",
        "mcp_servers": [
          {"type": "url", "name": "github", "url": "https://mcp.example.com/github"}
        ],
        "tools": [
          {"type": "agent_toolset_20260401"},
          {
            "type": "mcp_toolset",
            "mcp_server_name": "github",
            "default_config": {
              "permission_policy": {"type": "always_allow"}
            }
          }
        ]
      }')

    覆寫個別工具政策

    使用 configs 陣列覆寫個別工具的預設值。此範例預設允許完整代理工具集,但在執行任何 bash 命令前需要確認:

    tools='[
      {
        "type": "agent_toolset_20260401",
        "default_config": {
          "permission_policy": {"type": "always_allow"}
        },
        "configs": [
          {
            "name": "bash",
            "permission_policy": {"type": "always_ask"}
          }
        ]
      }
    ]'

    回應確認請求

    當代理以 always_ask 政策呼叫工具時:

    1. 工作階段發出 agent.tool_use 或 agent.mcp_tool_use 事件。
    2. 工作階段暫停,並發出包含 stop_reason: requires_action 的 session.status_idle 事件。封鎖事件 ID 位於 stop_reason.requires_action.event_ids 陣列中。
    3. 為每個事件發送 user.tool_confirmation 事件,在 tool_use_id 參數中傳遞事件 ID。將 result 設為 "allow" 或 "deny"。使用 deny_message 說明拒絕原因。
    4. 一旦所有封鎖事件都已解決,工作階段將轉換回 running 狀態。

    在工作階段事件串流指南中了解更多關於事件處理的資訊。

    # 允許工具執行
    curl -fsSL "https://api.anthropic.com/v1/sessions/$SESSION_ID/events" \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: managed-agents-2026-04-01" \
      -H "content-type: application/json" \
      -d '{
        "events": [
          {
            "type": "user.tool_confirmation",
            "tool_use_id": "'$AGENT_TOOL_USE_EVENT_ID'",
            "result": "allow"
          }
        ]
      }'
    
    # 或拒絕並附上說明
    curl -fsSL "https://api.anthropic.com/v1/sessions/$SESSION_ID/events" \
      -H "x-api-key: $ANTHROPIC_API_KEY" \
      -H "anthropic-version: 2023-06-01" \
      -H "anthropic-beta: managed-agents-2026-04-01" \
      -H "content-type: application/json" \
      -d '{
        "events": [
          {
            "type": "user.tool_confirmation",
            "tool_use_id": "'$MCP_TOOL_USE_EVENT_ID'",
            "result": "deny",
            "deny_message": "Don'\''t create issues in the production project. Use the staging project."
          }
        ]
      }'

    自訂工具

    權限政策不適用於自訂工具。當代理呼叫自訂工具時,您的應用程式會收到 agent.custom_tool_use 事件,並負責在發送回 user.custom_tool_result 之前決定是否執行它。請參閱工作階段事件串流以了解完整流程。

    Was this page helpful?

    • MCP 工具集權限