For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
主要導覽

保管庫

儲存 MCP 憑證,並將其附加至智慧體工作階段。

保管庫用於儲存從 OpenAI 建立 MCP 連線所需的憑證。將保管庫附加至工作階段後,智慧體就能使用需要身分驗證的工具,無須接收機密值。

保管庫支援持有者 Token 和現有的 OAuth 授權。若連線是從你的環境發起,請使用其他 MCP 身分驗證選項

權限

若使用受限的應用程式金鑰,請授予下列權限:

  • api.vaults.read:列出及擷取保管庫與憑證。
  • api.vaults.write:建立、更新或刪除保管庫與憑證。

建立及使用保管庫

請使用你的 API 用戶端、MCP 伺服器 URL(mcp_url)以及該伺服器的存取 Token(access_token)。以下範例使用 GitHub 工具。

首先,建立保管庫:

建立保管庫
const vault = await client.beta.agents.vaults.create({
  name: "GitHub credentials",
  metadata: {
    external_user_id: "user_123",
  },
});

將保管庫的 ID 儲存為 vault_id,然後新增 Token。mcp_server_url 會將憑證綁定至該伺服器:

儲存持有者 Token
// Replace the illustrative IDs and URLs below with your own resource values.
const vaultId = "vault_123";
const mcpUrl = "https://api.githubcopilot.com/mcp/";
const accessToken = process.env.GITHUB_TOKEN;

const credential = await client.beta.agents.vaults.credentials.create(vaultId, {
  name: "GitHub access token",
  auth: {
    type: "static_bearer",
    mcp_server_url: mcpUrl,
    token: accessToken,
  },
});

將憑證 ID 儲存為 credential_id,以便日後更新。

建立工作階段時,透過 vault_ids 傳入已儲存的 ID。請在 MCP 組態中使用相同的伺服器 URL:

將保管庫附加至工作階段
// Replace the illustrative IDs and URLs below with your own resource values.
const mcpUrl = "https://api.githubcopilot.com/mcp/";
const vaultId = "vault_123";

const session = await client.beta.agents.sessions.create({
  agent: {
    model: "gpt-6-astra",
    tools: [
      {
        type: "mcp",
        server_label: "github",
        transport: {
          type: "http",
          server_url: mcpUrl,
        },
        allowed_tools: ["search_issues", "issue_read"],
        required: true,
        connection_origin: "service",
      },
    ],
  },
  environment: {
    type: "none",
  },
  input: "Find open bugs reported in the last week.",
  vault_ids: [vaultId],
});

Agents API 會選取與伺服器 URL 相符的憑證。如果附加的憑證中有多個符合條件,請設定 MCP 工具的 credential_id 來指定其中一個。擷取保管庫或憑證時,不會傳回其中的機密值。

使用 OAuth 憑證

你的應用程式負責處理供應商的授權與同意流程。請使用 auth.type: "mcp_oauth" 儲存取得的授權。若已知存取 Token 的到期時間,請將其轉為 RFC 3339 時間戳記,並設為 expires_at 的值。

以下範例使用從供應商 OAuth 流程取得的值。加入 refresh,即可讓 Agents API 重新整理 Token:

儲存 OAuth 授權
// Replace the illustrative expiry with your access token's actual expiry.
// Replace the illustrative IDs and URLs below with your own resource values.
const vaultId = "vault_123";
const mcpUrl = "https://mcp.example.com/mcp";
const accessToken = process.env.OAUTH_ACCESS_TOKEN;
const expiresAt = "2030-01-01T00:00:00Z";
const tokenEndpoint = "https://auth.example.com/oauth/token";
const clientId = "example-client-id";
const refreshToken = process.env.OAUTH_REFRESH_TOKEN;

const credential = await client.beta.agents.vaults.credentials.create(vaultId, {
  name: "Example MCP OAuth credential",
  auth: {
    type: "mcp_oauth",
    mcp_server_url: mcpUrl,
    access_token: accessToken,
    expires_at: expiresAt,
    refresh: {
      token_endpoint: tokenEndpoint,
      client_id: clientId,
      refresh_token: refreshToken,
      token_endpoint_auth: {
        type: "none",
      },
    },
  },
});

請使用供應商要求的 Token 端點身分驗證方法。此範例使用 none;也支援 client_secret_basicclient_secret_post。如需欄位資訊,請參閱建立憑證參考資料

若無法重新整理已過期的 Token,請提供有效的替代 Token。Token 到期不會刪除憑證或其所屬的保管庫。

輪替或移除憑證

更新憑證即可替換其 Token,而不變更憑證的 ID、身分驗證類型或伺服器 URL。若使用 OAuth,請提供已儲存的 vault_idcredential_id,以及替代 Token 與到期時間:

輪替 OAuth Token
// Replace the illustrative expiry with your access token's actual expiry.
// Replace the illustrative IDs and URLs below with your own resource values.
const credentialId = "cred_123";
const vaultId = "vault_123";
const accessToken = process.env.OAUTH_ACCESS_TOKEN;
const expiresAt = "2030-01-01T00:00:00Z";

const credential = await client.beta.agents.vaults.credentials.update(
  credentialId,
  {
    vault_id: vaultId,
    ...{
      auth: {
        type: "mcp_oauth",
        access_token: accessToken,
        expires_at: expiresAt,
      },
    },
  }
);

若替代 Token 有到期時間,請加入 expires_at。提供新的存取 Token 時,若未指定到期時間,便會清除已儲存的到期時間;明確指定 null 也會將其清除。

不再需要憑證時,請刪除憑證刪除保管庫則會移除保管庫及其中的所有憑證。

刪除已儲存的憑證,不會撤銷供應商端的原始 Token,也不會停止正在執行的工作階段。你的應用程式負責處理供應商端的撤銷作業,以及取消工作階段