MCP 和连接器 | OpenAI API
来源: https://developers.openai.com/api/docs/guides/tools-connectors-mcp 抓取时间: 2026-07-21 16:19:00
除了使用函数调用向模型提供的工具外,您还可以使用连接器和远程 MCP 服务器为模型提供新功能。这些工具使模型能够在需要时连接和控制外部服务,以响应用户的提示。这些工具调用可以自动允许,也可以限制为需要您作为开发人员明确批准。
- 连接器是 OpenAI 维护的用于流行服务(如 Google Workspace 或 Dropbox)的 MCP 包装器,类似于 ChatGPT 中可用的连接器。
- 远程 MCP 服务器可以是公共互联网上实现远程模型上下文协议(MCP)服务器的任何服务器。
本指南将展示如何使用远程 MCP 服务器和连接器来让模型访问新功能。
安全 MCP 隧道
如果您的 MCP 服务器是私有的、本地部署的或位于防火墙后面,请使用安全 MCP 隧道将其连接到支持的 OpenAI 产品,而无需将服务器暴露给公共互联网。从 openai/tunnel-client 下载最新的公开发布版本。
快速开始
查看下面的示例,了解远程 MCP 服务器和连接器如何通过响应 API 工作。连接器和远程 MCP 服务器都可以与 mcp 内置工具类型一起使用。
使用远程 MCP 服务器
远程 MCP 服务器需要 server_url。根据服务器的不同,您可能还需要一个包含访问令牌的 OAuth authorization 参数。
在响应 API 中使用远程 MCP 服务器
curl https://api.openai.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ***" \
-d '{
"model": "gpt-5.6",
"tools": [
{
"type": "mcp",
"server_label": "dmcp",
"server_description": "A Dungeons and Dragons MCP server to assist with dice rolling.",
"server_url": "https://dmcp-server.deno.dev/mcp",
"require_approval": "never"
}
],
"input": "Roll 2d4+1"
}'
import OpenAI from "openai";
const client = new OpenAI();
const resp = await client.responses.create({
model: "gpt-5.6",
tools: [
{
type: "mcp",
server_label: "dmcp",
server_description:
"A Dungeons and Dragons MCP server to assist with dice rolling.",
server_url: "https://dmcp-server.deno.dev/mcp",
require_approval: "never",
},
],
input: "Roll 2d4+1",
});
console.log(resp.output_text);
from openai import OpenAI
client = OpenAI()
resp = client.responses.create(
model="gpt-5.6",
tools=[
{
"type": "mcp",
"server_label": "dmcp",
"server_description": "A Dungeons and Dragons MCP server to assist with dice rolling.",
"server_url": "https://dmcp-server.deno.dev/mcp",
"require_approval": "never",
},
],
input="Roll 2d4+1",
)
print(resp.output_text)
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.6", apiKey: ***
ResponseCreationOptions options = new();
options.Tools.Add(ResponseTool.CreateMcpTool(
serverLabel: "dmcp",
serverUri: new Uri("https://dmcp-server.deno.dev/mcp"),
toolCallApprovalPolicy: new McpToolCallApprovalPolicy(GlobalMcpToolCallApprovalPolicy.NeverRequireApproval)
));
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("Roll 2d4+1")
])
], options);
Console.WriteLine(response.GetOutputText());
require "openai"
openai = OpenAI::Client.new
response = openai.responses.create(
model: "gpt-5.6",
tools: [
{
type: "mcp",
server_label: "dmcp",
server_description: "A Dungeons and Dragons MCP server to assist with dice rolling.",
server_url: "https://dmcp-server.deno.dev/mcp",
require_approval: "never"
}
],
input: "Roll 2d4+1"
)
puts(response.output_text)
开发人员信任他们与 Responses API 一起使用的任何远程 MCP 服务器非常重要。恶意服务器可以从进入模型上下文的任何内容中窃取敏感数据。在使用此工具之前,请仔细查看下面的风险和安全部分。
使用连接器
连接器需要 connector_id 参数,以及应用程序在 authorization 参数中提供的 OAuth 访问令牌。
在响应 API 中使用连接器
curl https://api.openai.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ***" \
-d '{
"model": "gpt-5.6",
"tools": [
{
"type": "mcp",
"server_label": "Dropbox",
"connector_id": "connector_dropbox",
"authorization": "<oauth access token>",
"require_approval": "never"
}
],
"input": "Summarize the Q2 earnings report."
}'
import OpenAI from "openai";
const client = new OpenAI();
const resp = await client.responses.create({
model: "gpt-5.6",
tools: [
{
type: "mcp",
server_label: "Dropbox",
connector_id: "connector_dropbox",
authorization: "<oauth access token>",
require_approval: "never",
},
],
input: "Summarize the Q2 earnings report.",
});
console.log(resp.output_text);
from openai import OpenAI
client = OpenAI()
resp = client.responses.create(
model="gpt-5.6",
tools=[
{
"type": "mcp",
"server_label": "Dropbox",
"connector_id": "connector_dropbox",
"authorization": "<oauth access token>",
"require_approval": "never",
},
],
input="Summarize the Q2 earnings report.",
)
print(resp.output_text)
using OpenAI.Responses;
string dropboxToken = Environment.GetEnvironmentVariable("DROPBOX_OAUTH_ACCESS_TOKEN")!;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.6", apiKey: ***
ResponseCreationOptions options = new();
options.Tools.Add(ResponseTool.CreateMcpTool(
serverLabel: "Dropbox",
connectorId: McpToolConnectorId.Dropbox,
authorizationToken: dropboxToken,
toolCallApprovalPolicy: new McpToolCallApprovalPolicy(GlobalMcpToolCallApprovalPolicy.NeverRequireApproval)
));
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("Summarize the Q2 earnings report.")
])
], options);
Console.WriteLine(response.GetOutputText());
API 将在模型响应的 output 数组中返回新项。如果模型决定使用连接器或 MCP 服务器,它将首先发出请求以从服务器列出可用工具,这将创建一个 mcp_list_tools 输出项。从上面的简单远程 MCP 服务器示例中,它只包含一个工具定义:
{
"id": "mcpl_68a6102a4968819c8177b05584dd627b0679e572a900e618",
"type": "mcp_list_tools",
"server_label": "dmcp",
"tools": [
{
"annotations": null,
"description": "Given a string of text describing a dice roll...",
"input_schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"diceRollExpression": {
"type": "string"
}
},
"required": ["diceRollExpression"],
"additionalProperties": false
},
"name": "roll"
}
]
}
如果模型决定调用 MCP 服务器中的一个可用工具,您还将找到一个 mcp_call 输出,它将显示模型发送到 MCP 工具的内容,以及 MCP 工具作为输出返回的内容。
{
"id": "mcp_68a6102d8948819c9b1490d36d5ffa4a0679e572a900e618",
"type": "mcp_call",
"approval_request_id": null,
"arguments": "{\"diceRollExpression\":\"2d4 + 1\"}",
"error": null,
"name": "roll",
"output": "4",
"server_label": "dmcp"
}
继续阅读下面的指南,了解 MCP 工具如何工作、如何过滤可用工具以及如何处理工具调用批准请求。
工作原理
MCP 工具(用于远程 MCP 服务器和连接器)在大多数最新模型的响应 API 中可用。检查您的模型的 MCP 工具兼容性此处。当您使用 MCP 工具时,您只需为导入工具定义或进行工具调用时使用的令牌付费。每次工具调用不涉及额外费用。
下面,我们将逐步介绍 API 调用 MCP 工具时的过程。
步骤 1:列出可用工具
当您在 tools 参数中指定远程 MCP 服务器时,API 将尝试从服务器获取工具列表。响应 API 与支持可流式 HTTP 或 HTTP/SSE 传输协议的远程 MCP 服务器配合使用。
如果成功检索到工具列表,模型响应输出中将出现一个新的 mcp_list_tools 输出项。该对象的 tools 属性将显示成功导入的工具。
{
"id": "mcpl_68a6102a4968819c8177b05584dd627b0679e572a900e618",
"type": "mcp_list_tools",
"server_label": "dmcp",
"tools": [
{
"annotations": null,
"description": "Given a string of text describing a dice roll...",
"input_schema": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"diceRollExpression": {
"type": "string"
}
},
"required": ["diceRollExpression"],
"additionalProperties": false
},
"name": "roll"
}
]
}
只要 mcp_list_tools 项存在于 API 请求的上下文中,API 就不会在对话的每一轮再次从 MCP 服务器获取工具列表。我们建议您将此项作为每个对话或工作流执行的一部分保留在模型的上下文中,以优化延迟。
过滤工具
一些 MCP 服务器可能有数十个工具,向模型公开许多工具可能会导致高成本和延迟。如果您只对 MCP 服务器公开的工具子集感兴趣,可以使用 allowed_tools 参数只导入这些工具。
约束允许的工具
curl https://api.openai.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ***" \
-d '{
"model": "gpt-5.6",
"tools": [
{
"type": "mcp",
"server_label": "dmcp",
"server_description": "A Dungeons and Dragons MCP server to assist with dice rolling.",
"server_url": "https://dmcp-server.deno.dev/mcp",
"require_approval": "never",
"allowed_tools": ["roll"]
}
],
"input": "Roll 2d4+1"
}'
import OpenAI from "openai";
const client = new OpenAI();
const resp = await client.responses.create({
model: "gpt-5.6",
tools: [
{
type: "mcp",
server_label: "dmcp",
server_description:
"A Dungeons and Dragons MCP server to assist with dice rolling.",
server_url: "https://dmcp-server.deno.dev/mcp",
require_approval: "never",
allowed_tools: ["roll"],
},
],
input: "Roll 2d4+1",
});
console.log(resp.output_text);
from openai import OpenAI
client = OpenAI()
resp = client.responses.create(
model="gpt-5.6",
tools=[{
"type": "mcp",
"server_label": "dmcp",
"server_description": "A Dungeons and Dragons MCP server to assist with dice rolling.",
"server_url": "https://dmcp-server.deno.dev/mcp",
"require_approval": "never",
"allowed_tools": ["roll"],
}],
input="Roll 2d4+1",
)
print(resp.output_text)
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.6", apiKey: ***
ResponseCreationOptions options = new();
options.Tools.Add(ResponseTool.CreateMcpTool(
serverLabel: "dmcp",
serverUri: new Uri("https://dmcp-server.deno.dev/mcp"),
allowedTools: new McpToolFilter() { ToolNames = { "roll" } },
toolCallApprovalPolicy: new McpToolCallApprovalPolicy(GlobalMcpToolCallApprovalPolicy.NeverRequireApproval)
));
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("Roll 2d4+1")
])
], options);
Console.WriteLine(response.GetOutputText());
步骤 2:调用工具
一旦模型可以访问这些工具定义,它可能会根据模型上下文中的内容决定调用它们。当模型决定调用 MCP 工具时,API 将向远程 MCP 服务器发出请求以调用工具,并将其输出放入模型的上下文中。这将创建一个 mcp_call 项,如下所示:
{
"id": "mcp_68a6102d8948819c9b1490d36d5ffa4a0679e572a900e618",
"type": "mcp_call",
"approval_request_id": null,
"arguments": "{\"diceRollExpression\":\"2d4 + 1\"}",
"error": null,
"name": "roll",
"output": "4",
"server_label": "dmcp"
}
此项包含模型决定用于此工具调用的参数,以及远程 MCP 服务器返回的 output。所有模型都可以选择进行多个 MCP 工具调用,因此您可能会在单个 API 请求中看到生成的多个这些项。
失败的工具调用将在此项的错误字段中填充 MCP 协议错误、MCP 工具执行错误或一般连接错误。MCP 错误记录在 MCP 规范此处。
批准
默认情况下,OpenAI 将在任何数据共享到连接器或远程 MCP 服务器之前请求您的批准。批准帮助您保持控制权,并可见地了解哪些数据正在发送到 MCP 服务器。我们强烈建议您仔细审查(并可选地记录)与远程 MCP 服务器共享的所有数据。请求批准进行 MCP 工具调用会在响应的输出中创建一个 mcp_approval_request 项,如下所示:
{
"id": "mcpr_68a619e1d82c8190b50c1ccba7ad18ef0d2d23a86136d339",
"type": "mcp_approval_request",
"arguments": "{\"diceRollExpression\":\"2d4 + 1\"}",
"name": "roll",
"server_label": "dmcp"
}
然后,您可以通过创建一个新的响应对象并向其附加一个 mcp_approval_response 项来响应此。
批准 API 请求中工具的使用
curl https://api.openai.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ***" \
-d '{
"model": "gpt-5.6",
"tools": [
{
"type": "mcp",
"server_label": "dmcp",
"server_description": "A Dungeons and Dragons MCP server to assist with dice rolling.",
"server_url": "https://dmcp-server.deno.dev/mcp",
"require_approval": "always",
}
],
"previous_response_id": "resp_682d498bdefc81918b4a6aa477bfafd904ad1e533afccbfa",
"input": [{
"type": "mcp_approval_response",
"approve": true,
"approval_request_id": "mcpr_682d498e3bd4819196a0ce1664f8e77b04ad1e533afccbfa"
}]
}'
import OpenAI from "openai";
const client = new OpenAI();
const resp = await client.responses.create({
model: "gpt-5.6",
tools: [
{
type: "mcp",
server_label: "dmcp",
server_description:
"A Dungeons and Dragons MCP server to assist with dice rolling.",
server_url: "https://dmcp-server.deno.dev/mcp",
require_approval: "always",
},
],
previous_response_id: "resp_682d498bdefc81918b4a6aa477bfafd904ad1e533afccbfa",
input: [
{
type: "mcp_approval_response",
approve: true,
approval_request_id:
"mcpr_682d498e3bd4819196a0ce1664f8e77b04ad1e533afccbfa",
},
],
});
console.log(resp.output_text);
from openai import OpenAI
client = OpenAI()
resp = client.responses.create(
model="gpt-5.6",
tools=[{
"type": "mcp",
"server_label": "dmcp",
"server_description": "A Dungeons and Dragons MCP server to assist with dice rolling.",
"server_url": "https://dmcp-server.deno.dev/mcp",
"require_approval": "always",
}],
previous_response_id="resp_682d498bdefc81918b4a6aa477bfafd904ad1e533afccbfa",
input=[{
"type": "mcp_approval_response",
"approve": True,
"approval_request_id": "mcpr_682d498e3bd4819196a0ce1664f8e77b04ad1e533afccbfa",
}],
)
print(resp.output_text)
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.6", apiKey: ***
ResponseCreationOptions options = new();
options.Tools.Add(ResponseTool.CreateMcpTool(
serverLabel: "dmcp",
serverUri: new Uri("https://dmcp-server.deno.dev/mcp"),
toolCallApprovalPolicy: new McpToolCallApprovalPolicy(GlobalMcpToolCallApprovalPolicy.AlwaysRequireApproval)
));
// STEP 1: Create response that requests tool call approval
OpenAIResponse response1 = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("Roll 2d4+1")
])
], options);
McpToolCallApprovalRequestItem? approvalRequestItem = response1.OutputItems.Last() as McpToolCallApprovalRequestItem;
// STEP 2: Approve the tool call request and get final response
options.PreviousResponseId = response1.Id;
OpenAIResponse response2 = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateMcpApprovalResponseItem(approvalRequestItem!.Id, approved: true),
], options);
Console.WriteLine(response2.GetOutputText());
在这里,我们使用 previous_response_id 参数将这个新响应与生成批准请求的先前响应链接起来。但您也可以将一个响应的输出作为另一个响应的输入传递回去,以最大程度地控制进入模型上下文的内容。
当您感到可以信任远程 MCP 服务器时,您可以选择跳过批准以减少延迟。为此,您可以将 MCP 工具的 require_approval 参数设置为一个对象,只列出您想要跳过批准的工具,如下所示,或者将其设置为值 'never' 以跳过该远程 MCP 服务器中所有工具的批准。
对某些工具永远不需要批准
curl https://api.openai.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ***" \
-d '{
"model": "gpt-5.6",
"tools": [
{
"type": "mcp",
"server_label": "deepwiki",
"server_url": "https://mcp.deepwiki.com/mcp",
"require_approval": {
"never": {
"tool_names": ["ask_question", "read_wiki_structure"]
}
}
}
],
"input": "What transport protocols does the 2025-03-26 version of the MCP spec (modelcontextprotocol/modelcontextprotocol) support?"
}'
import OpenAI from "openai";
const client = new OpenAI();
const resp = await client.responses.create({
model: "gpt-5.6",
tools: [
{
type: "mcp",
server_label: "deepwiki",
server_url: "https://mcp.deepwiki.com/mcp",
require_approval: {
never: {
tool_names: ["ask_question", "read_wiki_structure"],
},
},
},
],
input:
"What transport protocols does the 2025-03-26 version of the MCP spec (modelcontextprotocol/modelcontextprotocol) support?",
});
console.log(resp.output_text);
from openai import OpenAI
client = OpenAI()
resp = client.responses.create(
model="gpt-5.6",
tools=[
{
"type": "mcp",
"server_label": "deepwiki",
"server_url": "https://mcp.deepwiki.com/mcp",
"require_approval": {
"never": {
"tool_names": ["ask_question", "read_wiki_structure"]
}
}
},
],
input="What transport protocols does the 2025-03-26 version of the MCP spec (modelcontextprotocol/modelcontextprotocol) support?",
)
print(resp.output_text)
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.6", apiKey: ***
ResponseCreationOptions options = new();
options.Tools.Add(ResponseTool.CreateMcpTool(
serverLabel: "deepwiki",
serverUri: new Uri("https://mcp.deepwiki.com/mcp"),
allowedTools: new McpToolFilter() { ToolNames = { "ask_question", "read_wiki_structure" } },
toolCallApprovalPolicy: new McpToolCallApprovalPolicy(GlobalMcpToolCallApprovalPolicy.NeverRequireApproval)
));
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("What transport protocols does the 2025-03-26 version of the MCP spec (modelcontextprotocol/modelcontextprotocol) support?")
])
], options);
Console.WriteLine(response.GetOutputText());
身份验证
与我们上面使用的示例 MCP 服务器不同,大多数其他 MCP 服务器需要身份验证。最常见的方案是 OAuth 访问令牌。使用 MCP 工具的 authorization 字段提供此令牌:
使用 Stripe MCP 工具
curl https://api.openai.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ***" \
-d '{
"model": "gpt-5.6",
"input": "Create a payment link for $20",
"tools": [
{
"type": "mcp",
"server_label": "stripe",
"server_url": "https://mcp.stripe.com",
"authorization": "$STRIPE_OAUTH_ACCESS_TOKEN"
}
]
}'
import OpenAI from "openai";
const client = new OpenAI();
const resp = await client.responses.create({
model: "gpt-5.6",
input: "Create a payment link for $20",
tools: [
{
type: "mcp",
server_label: "stripe",
server_url: "https://mcp.stripe.com",
authorization: "$STRIPE_OAUTH_ACCESS_TOKEN",
},
],
});
console.log(resp.output_text);
from openai import OpenAI
client = OpenAI()
resp = client.responses.create(
model="gpt-5.6",
input="Create a payment link for $20",
tools=[
{
"type": "mcp",
"server_label": "stripe",
"server_url": "https://mcp.stripe.com",
"authorization": "$STRIPE_OAUTH_ACCESS_TOKEN"
}
]
)
print(resp.output_text)
using OpenAI.Responses;
string authToken = Environment.GetEnvironmentVariable("STRIPE_OAUTH_ACCESS_TOKEN")!;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.6", apiKey: ***
ResponseCreationOptions options = new();
options.Tools.Add(ResponseTool.CreateMcpTool(
serverLabel: "stripe",
serverUri: new Uri("https://mcp.stripe.com"),
authorizationToken: authToken
));
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("Create a payment link for $20")
])
], options);
Console.WriteLine(response.GetOutputText());
为了防止敏感令牌泄露,响应 API 不会存储您在 authorization 字段中提供的值。此值也不会在创建的响应对象中可见。因此,您必须在每次发出的响应 API 创建请求中发送 authorization 值。
连接器
响应 API 内置支持有限的一组第三方服务连接器。这些连接器让您可以从流行应用程序(如 Dropbox 和 Gmail)中提取上下文,以允许模型与流行服务交互。
连接器可以与远程 MCP 服务器相同的方式使用。两者都让 OpenAI 模型在 API 请求中访问额外的第三方工具。但是,您不需要像调用远程 MCP 服务器那样传递 server_url,而是传递一个 connector_id,它唯一标识 API 中可用的连接器。
可用连接器
- Dropbox:
connector_dropbox - Gmail:
connector_gmail - Google Calendar:
connector_googlecalendar - Google Drive:
connector_googledrive - Microsoft Teams:
connector_microsoftteams - Outlook Calendar:
connector_outlookcalendar - Outlook Email:
connector_outlookemail - SharePoint:
connector_sharepoint
我们优先考虑没有官方远程 MCP 服务器的服务。例如,GitHub 有一个官方 MCP 服务器,您可以通过将 https://api.githubcopilot.com/mcp/ 传递给 MCP 工具中的 server_url 字段来连接它。
授权连接器
在 authorization 字段中,传入 OAuth 访问令牌。OAuth 客户端注册和授权必须由您的应用程序单独处理。
出于测试目的,您可以使用 Google 的 OAuth 2.0 Playground 生成临时访问令牌,您可以在 API 请求中使用该令牌。
要使用 Playground 测试连接器 API 功能,首先输入:
https://www.googleapis.com/auth/calendar.events
此授权范围将使 API 能够读取 Google Calendar 事件。在 UI 中的“步骤 1:选择和授权 API”下。使用您的 Google 帐户授权应用程序后,您将进入“步骤 2:交换授权代码获取令牌”。这将生成一个访问令牌,您可以在使用 Google Calendar 连接器的 API 请求中使用:
使用 Google Calendar 连接器
curl https://api.openai.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ***" \
-d '{
"model": "gpt-5.6",
"tools": [
{
"type": "mcp",
"server_label": "google_calendar",
"connector_id": "connector_googlecalendar",
"authorization": "ya29.A0AS3H6...",
"require_approval": "never"
}
],
"input": "What is on my Google Calendar for today?"
}'
import OpenAI from "openai";
const client = new OpenAI();
const resp = await client.responses.create({
model: "gpt-5.6",
tools: [
{
type: "mcp",
server_label: "google_calendar",
connector_id: "connector_googlecalendar",
authorization: "ya29.A0AS3H6...",
require_approval: "never",
},
],
input: "What's on my Google Calendar for today?",
});
console.log(resp.output_text);
from openai import OpenAI
client = OpenAI()
resp = client.responses.create(
model="gpt-5.6",
tools=[
{
"type": "mcp",
"server_label": "google_calendar",
"connector_id": "connector_googlecalendar",
"authorization": "ya29.A0AS3H6...",
"require_approval": "never",
},
],
input="What's on my Google Calendar for today?",
)
print(resp.output_text)
using OpenAI.Responses;
string authToken = Environment.GetEnvironmentVariable("GOOGLE_CALENDAR_OAUTH_ACCESS_TOKEN")!;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.6", apiKey: ***
ResponseCreationOptions options = new();
options.Tools.Add(ResponseTool.CreateMcpTool(
serverLabel: "google_calendar",
connectorId: McpToolConnectorId.GoogleCalendar,
authorizationToken: authToken,
toolCallApprovalPolicy: new McpToolCallApprovalPolicy(GlobalMcpToolCallApprovalPolicy.NeverRequireApproval)
));
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("What's on my Google Calendar for today?")
])
], options);
Console.WriteLine(response.GetOutputText());
来自连接器的 MCP 工具调用看起来与来自远程 MCP 服务器的 MCP 工具调用相同,使用 mcp_call 输出项类型。在这种情况下,连接器的参数和响应都是 JSON 字符串:
{
"id": "mcp_68a62ae1c93c81a2b98c29340aa3ed8800e9b63986850588",
"type": "mcp_call",
"approval_request_id": null,
"arguments": "{\"time_min\":\"2025-08-20T00:00:00\",\"time_max\":\"2025-08-21T00:00:00\",\"timezone_str\":null,\"max_results\":50,\"query\":null,\"calendar_id\":null,\"next_page_token\":null}",
"error": null,
"name": "search_events",
"output": "{\"events\": [{\"id\": \"2n8ni54ani58pc3ii6soelupcs_20250820\", \"summary\": \"Home\", \"location\": null, \"start\": \"2025-08-20T00:00:00\", \"end\": \"2025-08-21T00:00:00\", \"url\": \"https://www.google.com/calendar/event?eid=Mm44bmk1NGFuaTU4cGMzaWk2c29lbHVwY3NfMjAyNTA4MjAga3doaW5uZXJ5QG9wZW5haS5jb20&ctz=America/Los_Angeles\", \"description\": \"\\n\\n\", \"transparency\": \"transparent\", \"display_url\": \"https://www.google.com/calendar/event?eid=Mm44bmk1NGFuaTU4cGMzaWk2c29lbHVwY3NfMjAyNTA4MjAga3doaW5uZXJ5QG9wZW5haS5jb20&ctz=America/Los_Angeles\", \"display_title\": \"Home\"}], \"next_page_token\": null}",
"server_label": "Google_Calendar"
}
每个连接器中的可用工具
可用工具取决于您的 OAuth 令牌可用的范围。展开下面的表格,查看连接到每个应用程序时可以使用的工具。
Dropbox
| 工具 | 描述 | 范围 |
|---|---|---|
search | 搜索 Dropbox 中匹配查询的文件 | files.metadata.read, account_info.read |
fetch | 按路径获取文件,可选择原始下载 | files.content.read |
search_files | 搜索 Dropbox 文件并返回结果 | files.metadata.read, account_info.read |
fetch_file | 检索文件的文本或原始内容 | files.content.read, account_info.read |
list_recent_files | 返回用户可访问的最近修改的文件 | files.metadata.read, account_info.read |
get_profile | 检索当前用户的 Dropbox 配置文件 | account_info.read |
Gmail
| 工具 | 描述 | 范围 |
|---|---|---|
get_profile | 返回当前 Gmail 用户的配置文件 | userinfo.email, userinfo.profile |
search_emails | 搜索 Gmail 中匹配查询或标签的邮件 | gmail.modify |
search_email_ids | 检索匹配搜索的 Gmail 消息 ID | gmail.modify |
get_recent_emails | 返回最近收到的 Gmail 消息 | gmail.modify |
read_email | 获取单个 Gmail 消息,包括其正文 | gmail.modify |
batch_read_email | 一次调用读取多个 Gmail 消息 | gmail.modify |
Google Calendar
| 工具 | 描述 | 范围 |
|---|---|---|
get_profile | 返回当前 Calendar 用户的配置文件 | userinfo.email, userinfo.profile |
search | 在可选时间窗口内搜索 Calendar 事件 | calendar.events |
fetch | 获取单个 Calendar 事件的详细信息 | calendar.events |
search_events | 使用筛选器查找 Calendar 事件 | calendar.events |
read_event | 按 ID 读取 Google Calendar 事件 | calendar.events |
Google Drive
| 工具 | 描述 | 范围 |
|---|---|---|
get_profile | 返回当前 Drive 用户的配置文件 | userinfo.email, userinfo.profile |
list_drives | 列出用户可访问的共享驱动器 | drive.readonly |
search | 使用查询搜索 Drive 文件 | drive.readonly |
recent_documents | 返回最近修改的文档 | drive.readonly |
fetch | 下载 Drive 文件的内容 | drive.readonly |
Microsoft Teams
| 工具 | 描述 | 范围 |
|---|---|---|
search | 搜索 Microsoft Teams 聊天和频道消息 | Chat.Read, ChannelMessage.Read.All |
fetch | 按路径获取 Teams 消息 | Chat.Read, ChannelMessage.Read.All |
get_chat_members | 列出 Teams 聊天的成员 | Chat.Read |
get_profile | 返回已验证 Teams 用户的配置文件 | User.Read |
Outlook Calendar
| 工具 | 描述 | 范围 |
|---|---|---|
search_events | 使用日期筛选器搜索 Outlook Calendar 事件 | Calendars.Read |
fetch_event | 检索单个事件的详细信息 | Calendars.Read |
fetch_events_batch | 一次调用检索多个事件 | Calendars.Read |
list_events | 列出日期范围内的日历事件 | Calendars.Read |
get_profile | 检索当前用户的配置文件 | User.Read |
Outlook Email
| 工具 | 描述 | 范围 |
|---|---|---|
get_profile | 返回 Outlook 帐户的配置文件信息 | User.Read |
list_messages | 从文件夹中检索 Outlook 邮件 | Mail.Read |
search_messages | 使用可选筛选器搜索 Outlook 邮件 | Mail.Read |
get_recent_emails | 返回最近收到的邮件 | Mail.Read |
fetch_message | 按 ID 获取单个邮件 | Mail.Read |
fetch_messages_batch | 在一个请求中检索多个邮件 | Mail.Read |
SharePoint
| 工具 | 描述 | 范围 |
|---|---|---|
get_site | 通过主机名和路径解析 SharePoint 站点 | Sites.Read.All |
search | 按关键字搜索 SharePoint/OneDrive 文档 | Sites.Read.All, Files.Read.All |
list_recent_documents | 返回最近访问的文档 | Files.Read.All |
fetch | 从 Graph 文件下载 URL 获取内容 | Files.Read.All |
get_profile | 检索当前用户的配置文件 | User.Read |
延迟加载 MCP 服务器中的工具
如果您正在使用工具搜索,您可以延迟加载 MCP 服务器公开的函数,直到模型决定需要它们。为此,请在 MCP 服务器工具定义上设置 defer_loading: true。
当您延迟加载 MCP 服务器时,模型仍然可以使用 MCP 服务器的标签和描述来决定何时搜索它,但各个函数定义仅在需要时加载。这有助于减少整体令牌使用量,对于公开大量函数的 MCP 服务器最有用。
{
"type": "mcp",
"server_label": "dmcp",
"server_description": "A Dungeons and Dragons MCP server to assist with dice rolling.",
"server_url": "https://dmcp-server.deno.dev/mcp",
"defer_loading": true,
"require_approval": "never"
}
风险和安全
MCP 工具允许您将 OpenAI 模型连接到外部服务。这是一个强大的功能,但也带来了一些风险。
对于连接器,存在可能向 OpenAI 发送敏感数据,或允许模型读取这些服务中潜在敏感数据的风险。
远程 MCP 服务器具有相同的风险,但还没有经过 OpenAI 的验证。这些服务器可以允许模型访问、发送和接收数据,并在这些服务中采取行动。所有 MCP 服务器都是第三方服务,受其自己的条款和条件约束。
如果您遇到恶意 MCP 服务器,请向 security@openai.com 报告。
以下是集成连接器和远程 MCP 服务器时需要考虑的一些最佳实践。
提示注入
提示注入是任何 LLM 应用程序中重要的安全考虑因素,当您授予模型对可以访问敏感数据或采取行动的 MCP 服务器和连接器的访问权限时尤其如此。如果模型的提示包含用户提供的内容,请使用这些工具并采取适当的谨慎和缓解措施。
始终要求敏感操作的批准
使用 require_approval 和 allowed_tools 参数的可用配置,确保任何敏感操作都需要批准流程。
MCP 工具调用和输出中的 URL
请求连接器或远程 MCP 服务器的工具调用输出提供的 URL 或嵌入图像 URL 可能很危险。在应用程序代码中嵌入或以其他方式使用这些 URL 之前,请确保您信任提供这些 URL 的域和服务。
连接到受信任的服务器
选择由服务提供商自己托管的官方服务器(例如,我们建议连接到 Stripe 自己在 mcp.stripe.com 上托管的 Stripe 服务器,而不是由第三方托管的 Stripe MCP 服务器)。因为今天没有太多的官方远程 MCP 服务器,您可能很想使用由不运营该服务器的组织托管的 MCP 服务器,而只是通过您的 API 将请求代理到该服务。如果您必须这样做,请在对这些"聚合器"进行尽职调查时格外小心,并仔细审查它们如何使用您的数据。
记录和审查与第三方 MCP 服务器共享的数据
由于 MCP 服务器定义自己的工具定义,它们可能会请求您可能并不总是愿意与该 MCP 服务器的主机共享的数据。因此,响应 API 中的 MCP 工具默认要求对正在进行的每个 MCP 工具调用进行批准。在开发应用程序时,请仔细、稳健地审查与这些 MCP 服务器共享的数据类型。一旦您对信任此 MCP 服务器的信心增强,您可以跳过这些批准以获得更高性能的执行。
我们还建议记录发送到 MCP 服务器的任何数据。如果您正在使用带有 store=true 的响应 API,除非为您的组织启用了零数据保留,否则这些数据已经通过 API 记录了 30 天。您可能还希望在自己的系统中记录这些数据,并定期审查以确保数据按照您的预期共享。
恶意 MCP 服务器可能包含隐藏的指令(提示注入),旨在使 OpenAI 模型表现出意外行为。虽然 OpenAI 已实施内置保护措施来帮助检测和阻止这些威胁,但必须仔细审查输入和输出,并确保仅与受信任的服务器建立连接。
MCP 服务器可能会意外更新工具行为,可能导致意外或恶意行为。
对零数据保留和数据驻留的影响
MCP 工具与零数据保留和数据驻留兼容,但需要注意的是,MCP 服务器是第三方服务,发送到 MCP 服务器的数据受其数据保留和数据驻留策略约束。
换句话说,如果您是一个在欧洲有数据驻留要求的组织,OpenAI 将限制客户内容的推理和存储在欧洲进行,直到通信或数据发送到 MCP 服务器为止。您有责任确保 MCP 服务器也遵守您可能有的任何零数据保留或数据驻留要求。了解更多关于零数据保留和数据驻留此处。
使用说明
| API 可用性 | 速率限制 | 备注 |
|---|---|---|
| Responses<br>Chat Completions<br>Assistants | 第 1 层<br>200 RPM<br>第 2 层和第 3 层<br>1000 RPM<br>第 4 层和第 5 层<br>2000 RPM | 定价 |