OpenAI 凭证库
凭证库(vault)在智能体指令及配置之外保存凭证。创建会话时通过 vault_ids 附加,供该会话使用。
| 请求位置 | 凭证类型 | 用法 |
|---|---|---|
| OpenAI服务发起的MCP连接 | static_bearer、mcp_oauth |
OpenAI向配置的MCP服务器认证 |
| 托管沙箱发起API请求 | environment_variable |
代码读取占位值,网络代理只对批准主机替换成真实秘密 |
例如沙箱可用凭证库令牌访问GitHub REST API,步骤见沙箱请求使用凭证库秘密。读取凭证库或凭证不会返回秘密值。其他MCP连接见认证选项。
权限
受限应用密钥需:
api.vaults.read:列出和读取凭证库及凭证。api.vaults.write:创建、更新及删除。
创建并使用 MCP 凭证库
准备API客户端、MCP服务器URL mcp_url 及访问令牌 access_token。下例使用GitHub工具。先创建凭证库:
来源示例 1(javascript):
// [中文] 示例 1:创建并使用 MCP 凭证库。
// [中文] 创建凭证库并保存其 ID,之后再添加凭证或附加到会话。
const vault = await client.beta.agents.vaults.create({
name: "GitHub credentials",
metadata: {
external_user_id: "user_123",
},
});
来源示例 2(python):
## [中文] 示例 2:创建并使用 MCP 凭证库。
## [中文] 创建凭证库并保存其 ID,之后再添加凭证或附加到会话。
vault = client.beta.agents.vaults.create(
name="GitHub credentials", metadata={"external_user_id": "user_123"}
)
来源示例 3(go):
// [中文] 示例 3:创建并使用 MCP 凭证库。
// [中文] 创建凭证库并保存其 ID,之后再添加凭证或附加到会话。
vault, err := client.Beta.Agents.Vaults.New(ctx,
openai.BetaAgentVaultNewParams{
Name: openai.String("GitHub credentials"),
Metadata: map[string]string{"external_user_id": "user_123"},
})
if err != nil {
panic(err)
}
来源示例 4(java):
// [中文] 示例 4:创建并使用 MCP 凭证库。
// [中文] 创建凭证库并保存其 ID,之后再添加凭证或附加到会话。
var vault =
client
.beta()
.agents()
.vaults()
.create(
VaultCreateParams.builder()
.name("GitHub credentials")
.metadata(
VaultCreateParams.Metadata.builder()
.putAdditionalProperty("external_user_id", JsonValue.from("user_123"))
.build())
.build());
来源示例 5(ruby):
## [中文] 示例 5:创建并使用 MCP 凭证库。
## [中文] 创建凭证库并保存其 ID,之后再添加凭证或附加到会话。
vault = client.beta.agents.vaults.create(
name: "GitHub credentials",
metadata: { external_user_id: "user_123" }
)
保存返回ID为 vault_id,再添加令牌。mcp_server_url 将凭证绑定到对应服务器:
来源示例 6(javascript):
// [中文] 示例 6:创建并使用 MCP 凭证库。
// [中文] 把凭证保存到指定 vault;秘密从应用输入读取,不放入智能体提示词。
// [中文] 本段涉及的请求或服务地址:https://api.githubcopilot.com/mcp/;示例占位地址需替换为实际资源。
// 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,
},
});
来源示例 7(python):
## [中文] 示例 7:创建并使用 MCP 凭证库。
## [中文] 把凭证保存到指定 vault;秘密从应用输入读取,不放入智能体提示词。
## [中文] 本段涉及的请求或服务地址:https://api.githubcopilot.com/mcp/;示例占位地址需替换为实际资源。
## Replace the illustrative IDs and URLs below with your own resource values.
vault_id = "vault_123"
mcp_url = "https://api.githubcopilot.com/mcp/"
access_token = os.environ["GITHUB_TOKEN"]
credential = client.beta.agents.vaults.credentials.create(
vault_id,
name="GitHub access token",
auth={
"type": "static_bearer",
"mcp_server_url": mcp_url,
"token": access_token,
},
)
来源示例 8(go):
// [中文] 示例 8:创建并使用 MCP 凭证库。
// [中文] 本段涉及的请求或服务地址:https://api.githubcopilot.com/mcp/;示例占位地址需替换为实际资源。
// Replace the illustrative IDs and URLs below with your own resource values.
vaultId := "vault_123"
mcpUrl := "https://api.githubcopilot.com/mcp/"
accessToken := os.Getenv("GITHUB_TOKEN")
credential, err := client.Beta.Agents.Vaults.Credentials.New(ctx,
vaultId,
openai.BetaAgentVaultCredentialNewParams{
Name: "GitHub access token",
Auth: openai.CredentialAuthCreateParamUnion{
OfParamStaticBearer: &openai.CredentialAuthCreateParamStaticBearer{
McpServerURL: mcpUrl,
Token: accessToken,
},
},
})
if err != nil {
panic(err)
}
来源示例 9(java):
// [中文] 示例 9:创建并使用 MCP 凭证库。
// [中文] 把凭证保存到指定 vault;秘密从应用输入读取,不放入智能体提示词。
// [中文] 本段涉及的请求或服务地址:https://api.githubcopilot.com/mcp/;示例占位地址需替换为实际资源。
// Replace the illustrative IDs and URLs below with your own resource values.
String vaultId = "vault_123";
String mcpUrl = "https://api.githubcopilot.com/mcp/";
String accessToken = System.getenv("GITHUB_TOKEN");
var credential =
client
.beta()
.agents()
.vaults()
.credentials()
.create(
CredentialCreateParams.builder()
.vaultId(vaultId)
.name("GitHub access token")
.auth(
CredentialAuthCreateParam.StaticBearer.builder()
.mcpServerUrl(mcpUrl)
.token(accessToken)
.build())
.build());
来源示例 10(ruby):
## [中文] 示例 10:创建并使用 MCP 凭证库。
## [中文] 把凭证保存到指定 vault;秘密从应用输入读取,不放入智能体提示词。
## [中文] 本段涉及的请求或服务地址:https://api.githubcopilot.com/mcp/;示例占位地址需替换为实际资源。
## Replace the illustrative IDs and URLs below with your own resource values.
vault_id = "vault_123"
mcp_url = "https://api.githubcopilot.com/mcp/"
access_token = ENV.fetch("GITHUB_TOKEN")
credential = client.beta.agents.vaults.credentials.create(
vault_id,
name: "GitHub access token",
auth: {
type: "static_bearer",
mcp_server_url: mcp_url,
token: access_token
}
)
保存 credential_id 供后续更新。创建会话时在 vault_ids 中提供凭证库ID,MCP配置使用相同URL:
来源示例 11(javascript):
程序文字说明36:查找过去一周报告且仍未关闭的缺陷。
// [中文] 示例 11:创建并使用 MCP 凭证库。
// [中文] 创建会话,保存返回会话 ID;设置 stream 时继续读取事件,创建请求本身不保证任务成功。
// [中文] 本段涉及的请求或服务地址:https://api.githubcopilot.com/mcp/;示例占位地址需替换为实际资源。
// 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],
});
来源示例 12(python):
沿用程序文字说明36。
## [中文] 示例 12:创建并使用 MCP 凭证库。
## [中文] 创建会话,保存返回会话 ID;设置 stream 时继续读取事件,创建请求本身不保证任务成功。
## [中文] 本段涉及的请求或服务地址:https://api.githubcopilot.com/mcp/;示例占位地址需替换为实际资源。
## Replace the illustrative IDs and URLs below with your own resource values.
mcp_url = "https://api.githubcopilot.com/mcp/"
vault_id = "vault_123"
session = client.beta.agents.sessions.create(
agent={
"model": "gpt-6-astra",
"tools": [
{
"type": "mcp",
"server_label": "github",
"transport": {
"type": "http",
"server_url": mcp_url,
},
"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=[vault_id],
)
来源示例 13(go):
沿用程序文字说明36。
// [中文] 示例 13:创建并使用 MCP 凭证库。
// [中文] 创建会话,保存返回会话 ID;设置 stream 时继续读取事件,创建请求本身不保证任务成功。
// [中文] 本段涉及的请求或服务地址:https://api.githubcopilot.com/mcp/;示例占位地址需替换为实际资源。
// Replace the illustrative IDs and URLs below with your own resource values.
mcpUrl := "https://api.githubcopilot.com/mcp/"
vaultId := "vault_123"
session, err := client.Beta.Agents.Sessions.New(ctx,
openai.BetaAgentSessionNewParams{
Agent: openai.BetaAgentSessionNewParamsAgent{
Model: openai.String("gpt-6-astra"),
Tools: []openai.AgentToolParamUnion{
{
OfParamMcp: &openai.AgentToolParamMcp{
ServerLabel: "github",
Transport: openai.McpTransportParamUnion{OfParamHTTP: &openai.McpTransportParamHTTP{ServerURL: mcpUrl}},
AllowedTools: []string{"search_issues", "issue_read"},
Required: openai.Bool(true),
ConnectionOrigin: "service",
},
},
},
},
Environment: openai.EnvironmentParamUnion{OfParamNone: &openai.EnvironmentParamNone{}},
Input: openai.BetaAgentSessionNewParamsInputUnion{OfString: openai.String("Find open bugs reported in the last week.")},
VaultIDs: []string{vaultId},
})
if err != nil {
panic(err)
}
来源示例 14(java):
沿用程序文字说明36。
// [中文] 示例 14:创建并使用 MCP 凭证库。
// [中文] 创建会话,保存返回会话 ID;设置 stream 时继续读取事件,创建请求本身不保证任务成功。
// [中文] 本段涉及的请求或服务地址:https://api.githubcopilot.com/mcp/;示例占位地址需替换为实际资源。
// Replace the illustrative IDs and URLs below with your own resource values.
String mcpUrl = "https://api.githubcopilot.com/mcp/";
String vaultId = "vault_123";
var session =
client
.beta()
.agents()
.sessions()
.create(
SessionCreateParams.builder()
.agent(
SessionCreateParams.Agent.builder()
.model("gpt-6-astra")
.addTool(
AgentToolParam.Mcp.builder()
.serverLabel("github")
.transport(
McpTransportParam.Http.builder().serverUrl(mcpUrl).build())
.allowedTools(List.of("search_issues", "issue_read"))
.required(true)
.connectionOrigin(
AgentToolParam.Mcp.ConnectionOrigin.of("service"))
.build())
.build())
.environmentNone()
.input("Find open bugs reported in the last week.")
.vaultIds(List.of(vaultId))
.build());
来源示例 15(ruby):
沿用程序文字说明36。
## [中文] 示例 15:创建并使用 MCP 凭证库。
## [中文] 创建会话,保存返回会话 ID;设置 stream 时继续读取事件,创建请求本身不保证任务成功。
## [中文] 本段涉及的请求或服务地址:https://api.githubcopilot.com/mcp/;示例占位地址需替换为实际资源。
## Replace the illustrative IDs and URLs below with your own resource values.
mcp_url = "https://api.githubcopilot.com/mcp/"
vault_id = "vault_123"
session = client.beta.agents.sessions.create(
agent: {
model: "gpt-6-astra",
tools: [
{
type: "mcp",
server_label: "github",
transport: {
type: "http",
server_url: mcp_url
},
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: [vault_id]
)
API选择URL匹配凭证;附加的多条凭证都匹配时,用MCP工具的 credential_id 指定。
沙箱 API 请求使用凭证库秘密
使用 environment_variable 凭证,为OpenAI托管沙箱请求供给秘密。沙箱指定环境变量只有占位值,真实秘密留在沙箱外。
此流程要求 openai_hosted,不向自托管环境或应用执行的函数工具提供凭证。
保存 API 令牌
先创建凭证库,再向 POST /v1/vaults/{vault_id}/credentials 提供:
| 字段 | GitHub令牌示例值 |
|---|---|
name |
GitHub API token |
auth.type |
environment_variable |
auth.secret_name |
GITHUB_TOKEN |
auth.secret_value |
从应用秘密环境变量读取的真实令牌 |
auth.networking.type |
limited |
auth.networking.allowed_hosts |
["api.github.com"] |
secret_name 是沙箱代码读取的变量名,secret_value 是真实凭证,不能写入提示词、源码或日志。
allowed_hosts 使用精确主机名,不含协议、路径、端口或通配符。代理只对443或8443端口的HTTPS目标提供秘密。
将凭证库附加到托管会话
创建会话时,将以下字段与 agent 并列。用返回的真实ID替换 vault_123:
来源示例 16(json):
{
"vault_ids": ["vault_123"],
"environment": {
"type": "openai_hosted",
"network": {
"access": "restricted",
"allowed_domains": ["api.github.com"]
}
}
}
两份主机列表用途不同:allowed_domains 允许沙箱连接,凭证 allowed_hosts 允许代理提供秘密。
受限网络中,每个凭证主机也须列入 allowed_domains。有环境凭证的会话不能用 network.access: "disabled"。
各附加环境凭证必须有唯一 secret_name,不要在 environment.env 再定义同名变量。
从沙箱调用 API
发送消息,让智能体在沙箱运行:
来源示例 17(bash):
## [中文] 示例 17:从沙箱调用 API。
## [中文] 本段涉及的请求或服务地址:https://api.github.com/user;示例占位地址需替换为实际资源。
curl https://api.github.com/user \
-H "Authorization: Bearer $GITHUB_TOKEN"
命令读取 GITHUB_TOKEN 占位值,代理在请求到达 api.github.com 前替换真实令牌。成功返回已认证GitHub账户的JSON信息。在沙箱打印变量,只会看到占位值。
HTTPS请求头必须原样传递占位值。不能用它在本地计算签名等需要真实秘密的操作;此类任务把凭证留在应用,并通过函数工具开放操作。
使用 OAuth 凭证
应用处理提供商授权与同意流程,将结果以 auth.type: "mcp_oauth" 保存。已知过期时间时,将 expires_at 设为RFC 3339时间戳。
下例使用提供商OAuth流程返回值,包含 refresh 以允许Agents API刷新令牌:
来源示例 18(javascript):
// [中文] 示例 18:使用 OAuth 凭证。
// [中文] 把凭证保存到指定 vault;秘密从应用输入读取,不放入智能体提示词。
// [中文] 本段涉及的请求或服务地址:https://mcp.example.com/mcp、https://auth.example.com/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 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",
},
},
},
});
来源示例 19(python):
## [中文] 示例 19:使用 OAuth 凭证。
## [中文] 把凭证保存到指定 vault;秘密从应用输入读取,不放入智能体提示词。
## [中文] 本段涉及的请求或服务地址:https://mcp.example.com/mcp、https://auth.example.com/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.
vault_id = "vault_123"
mcp_url = "https://mcp.example.com/mcp"
access_token = os.environ["OAUTH_ACCESS_TOKEN"]
expires_at = "2030-01-01T00:00:00Z"
token_endpoint = "https://auth.example.com/oauth/token"
client_id = "example-client-id"
refresh_token = os.environ["OAUTH_REFRESH_TOKEN"]
credential = client.beta.agents.vaults.credentials.create(
vault_id,
name="Example MCP OAuth credential",
auth={
"type": "mcp_oauth",
"mcp_server_url": mcp_url,
"access_token": access_token,
"expires_at": expires_at,
"refresh": {
"token_endpoint": token_endpoint,
"client_id": client_id,
"refresh_token": refresh_token,
"token_endpoint_auth": {"type": "none"},
},
},
)
来源示例 20(go):
// [中文] 示例 20:使用 OAuth 凭证。
// [中文] 本段涉及的请求或服务地址:https://mcp.example.com/mcp、https://auth.example.com/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.
vaultId := "vault_123"
mcpUrl := "https://mcp.example.com/mcp"
accessToken := os.Getenv("OAUTH_ACCESS_TOKEN")
expiresAt := "2030-01-01T00:00:00Z"
tokenEndpoint := "https://auth.example.com/oauth/token"
clientId := "example-client-id"
refreshToken := os.Getenv("OAUTH_REFRESH_TOKEN")
credential, err := client.Beta.Agents.Vaults.Credentials.New(ctx,
vaultId,
openai.BetaAgentVaultCredentialNewParams{
Name: "Example MCP OAuth credential",
Auth: openai.CredentialAuthCreateParamUnion{
OfParamMcpOAuth: &openai.CredentialAuthCreateParamMcpOAuth{
McpServerURL: mcpUrl,
AccessToken: accessToken,
ExpiresAt: openai.String(expiresAt),
Refresh: openai.CredentialAuthCreateParamMcpOAuthRefresh{
TokenEndpoint: tokenEndpoint,
ClientID: clientId,
RefreshToken: refreshToken,
TokenEndpointAuth: openai.McpOAuthTokenEndpointAuthCreateParamUnion{OfParamNone: &openai.McpOAuthTokenEndpointAuthCreateParamNone{}},
},
},
},
})
if err != nil {
panic(err)
}
来源示例 21(java):
// [中文] 示例 21:使用 OAuth 凭证。
// [中文] 把凭证保存到指定 vault;秘密从应用输入读取,不放入智能体提示词。
// [中文] 本段涉及的请求或服务地址:https://mcp.example.com/mcp、https://auth.example.com/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.
String vaultId = "vault_123";
String mcpUrl = "https://mcp.example.com/mcp";
String accessToken = System.getenv("OAUTH_ACCESS_TOKEN");
String expiresAt = "2030-01-01T00:00:00Z";
String tokenEndpoint = "https://auth.example.com/oauth/token";
String clientId = "example-client-id";
String refreshToken = System.getenv("OAUTH_REFRESH_TOKEN");
var credential =
client
.beta()
.agents()
.vaults()
.credentials()
.create(
CredentialCreateParams.builder()
.vaultId(vaultId)
.name("Example MCP OAuth credential")
.auth(
CredentialAuthCreateParam.McpOAuth.builder()
.mcpServerUrl(mcpUrl)
.accessToken(accessToken)
.expiresAt(expiresAt)
.refresh(
CredentialAuthCreateParam.McpOAuth.Refresh.builder()
.tokenEndpoint(tokenEndpoint)
.clientId(clientId)
.refreshToken(refreshToken)
.tokenEndpointAuthNone()
.build())
.build())
.build());
来源示例 22(ruby):
## [中文] 示例 22:使用 OAuth 凭证。
## [中文] 把凭证保存到指定 vault;秘密从应用输入读取,不放入智能体提示词。
## [中文] 本段涉及的请求或服务地址:https://mcp.example.com/mcp、https://auth.example.com/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.
vault_id = "vault_123"
mcp_url = "https://mcp.example.com/mcp"
access_token = ENV.fetch("OAUTH_ACCESS_TOKEN")
expires_at = "2030-01-01T00:00:00Z"
token_endpoint = "https://auth.example.com/oauth/token"
client_id = "example-client-id"
refresh_token = ENV.fetch("OAUTH_REFRESH_TOKEN")
credential = client.beta.agents.vaults.credentials.create(
vault_id,
name: "Example MCP OAuth credential",
auth: {
type: "mcp_oauth",
mcp_server_url: mcp_url,
access_token: access_token,
expires_at: expires_at,
refresh: {
token_endpoint: token_endpoint,
client_id: client_id,
refresh_token: refresh_token,
token_endpoint_auth: { type: "none" }
}
}
)
使用提供商要求的令牌端点认证方式。示例为 none,也支持 client_secret_basic 和 client_secret_post,字段见创建凭证参考。
过期令牌无法刷新时提供有效替代令牌。过期不会自动删除凭证或凭证库。
轮换或删除凭证
更新凭证可替换秘密而保留ID及认证类型;MCP服务器URL也不变。OAuth用已保存 vault_id、credential_id 提交新令牌及过期时间:
来源示例 23(javascript):
// [中文] 示例 23:轮换或删除凭证。
// [中文] 以原 vault 和 credential ID 更新凭证;环境凭证更新后需新会话使用。
// 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,
},
},
}
);
来源示例 24(python):
## [中文] 示例 24:轮换或删除凭证。
## [中文] 以原 vault 和 credential ID 更新凭证;环境凭证更新后需新会话使用。
## Replace the illustrative expiry with your access token's actual expiry.
## Replace the illustrative IDs and URLs below with your own resource values.
credential_id = "cred_123"
vault_id = "vault_123"
access_token = os.environ["OAUTH_ACCESS_TOKEN"]
expires_at = "2030-01-01T00:00:00Z"
credential = client.beta.agents.vaults.credentials.update(
credential_id,
vault_id=vault_id,
auth={
"type": "mcp_oauth",
"access_token": access_token,
"expires_at": expires_at,
},
)
来源示例 25(go):
// [中文] 示例 25:轮换或删除凭证。
// [中文] 以原 vault 和 credential ID 更新凭证;环境凭证更新后需新会话使用。
// Replace the illustrative expiry with your access token's actual expiry.
// Replace the illustrative IDs and URLs below with your own resource values.
vaultId := "vault_123"
credentialId := "cred_123"
accessToken := os.Getenv("OAUTH_ACCESS_TOKEN")
expiresAt := "2030-01-01T00:00:00Z"
credential, err := client.Beta.Agents.Vaults.Credentials.Update(ctx,
vaultId,
credentialId,
openai.BetaAgentVaultCredentialUpdateParams{
Auth: openai.CredentialAuthRotateParamUnion{
OfParamMcpOAuth: &openai.CredentialAuthRotateParamMcpOAuth{
AccessToken: openai.String(accessToken),
ExpiresAt: openai.String(expiresAt),
},
},
})
if err != nil {
panic(err)
}
来源示例 26(java):
// [中文] 示例 26:轮换或删除凭证。
// [中文] 以原 vault 和 credential ID 更新凭证;环境凭证更新后需新会话使用。
// Replace the illustrative expiry with your access token's actual expiry.
// Replace the illustrative IDs and URLs below with your own resource values.
String credentialId = "cred_123";
String vaultId = "vault_123";
String accessToken = System.getenv("OAUTH_ACCESS_TOKEN");
String expiresAt = "2030-01-01T00:00:00Z";
var credential =
client
.beta()
.agents()
.vaults()
.credentials()
.update(
CredentialUpdateParams.builder()
.credentialId(credentialId)
.vaultId(vaultId)
.auth(
CredentialAuthRotateParam.McpOAuth.builder()
.accessToken(accessToken)
.expiresAt(expiresAt)
.build())
.build());
来源示例 27(ruby):
## [中文] 示例 27:轮换或删除凭证。
## [中文] 以原 vault 和 credential ID 更新凭证;环境凭证更新后需新会话使用。
## Replace the illustrative expiry with your access token's actual expiry.
## Replace the illustrative IDs and URLs below with your own resource values.
credential_id = "cred_123"
vault_id = "vault_123"
access_token = ENV.fetch("OAUTH_ACCESS_TOKEN")
expires_at = "2030-01-01T00:00:00Z"
credential = client.beta.agents.vaults.credentials.update(
credential_id,
vault_id: vault_id,
auth: {
type: "mcp_oauth",
access_token: access_token,
expires_at: expires_at
}
)
新令牌会过期时,提供 expires_at。提供新令牌却省略过期时间会清除已存时间,显式 null 也会清除。
环境凭证向 POST /v1/vaults/{vault_id}/credentials/{credential_id} 提交 auth.type: "environment_variable" 与新 auth.secret_value。需新建会话才能使用新值;更新凭证库不会改变已有沙箱配置的凭证。更改 secret_name 或 networking 需新建凭证。
删除已存凭证不会向提供商撤销原令牌,也不会停止运行会话。提供商侧撤销与会话取消由应用负责。

TRAE-AI编程


更多建议: