📘 OpenAI Codex📚 官方教程中文版实战场景
把你的应用接入 ChatGPT
把产品带进 ChatGPT,不应该从“把整个产品搬进去”开始。更稳的做法是先选一个明确的用户结果,围绕它设计少量 tools,搭建 MCP server,必要时
把产品带进 ChatGPT,不应该从“把整个产品搬进去”开始。更稳的做法是先选一个明确的用户结果,围绕它设计少量 tools,搭建 MCP server,必要时再做 widget,然后在 ChatGPT developer mode 里验证核心流程。
官方页面:https://developers.openai.com/codex/use-cases/chatgpt-apps
你要构建什么
每个 ChatGPT app 通常由三部分组成:
- MCP server:定义 tools,返回数据,处理账号连接,并指向 ChatGPT 需要加载的 UI resources。
- 可选 web component:在 ChatGPT iframe 中渲染。可以用 React,也可以用 plain HTML、CSS、JavaScript。
- model-driven tool calls:模型根据你提供的 metadata 决定何时调用 app tools。
Codex 最适合负责这些重复工程工作:
- 规划 tool surface 和 metadata。
- scaffold server 和 widget。
- 接好 local run scripts。
- 分阶段加入账号连接和部署改动。
- 写验证 loop,证明 app 在 ChatGPT 里能工作。
官方页面里当前 docs 多数使用 app 这个叫法,但部分旧页面或设置里仍可能出现 connector。做本地测试时,可以把它们理解为同一类 setup object。
使用的能力
| 能力 | 用法 | 链接 |
|---|---|---|
$chatgpt-apps | 规划 tools、连接 MCP resources,并按当前 ChatGPT app build flow 实现 | https://github.com/openai/skills/tree/main/skills/.curated/chatgpt-apps |
$openai-docs | 在 Codex 写代码或建议架构前,拉取当前 Apps SDK guidance | https://github.com/openai/skills/tree/main/skills/.curated/openai-docs |
vercel | 使用 Vercel ecosystem guidance、curated skills 和 official Vercel MCP server | https://github.com/openai/plugins/tree/main/plugins/vercel |
相关官方说明:
- Apps SDK quickstart:https://developers.openai.com/apps-sdk/quickstart
- Build an MCP server:https://developers.openai.com/apps-sdk/build/mcp-server
- Testing:https://developers.openai.com/apps-sdk/deploy/testing
- Codex skills:https://developers.openai.com/codex/skills
起始提示词
请结合 $chatgpt-apps 和 $openai-docs,在这个 repo 中为 [use case] 规划一个 ChatGPT app。
要求:
- 先锁定一个核心用户结果。
- 提出 3-5 个 tools,每个 tool 都要有清楚的名称、说明、inputs 和 outputs。
- 判断 v1 是否需要 widget,还是可以先从 data-only 开始。
- MCP server 优先使用 TypeScript,widget 优先使用 React。
- 标出 auth、deployment 和 test requirements。
输出:
- Tool plan
- Proposed file tree
- Golden prompt set
- Risks and open questions这个 prompt 只要求规划,不直接实现。先把 tools 和边界讲清楚,再 scaffold。
推荐技术栈
| 需要 | 推荐默认值 | 原因 |
|---|---|---|
| Widget framework | React | 适合有状态 widgets,尤其是 UI 里有 filters、tables 或 multi-step interaction |
| Hosting | Vercel | preview environments、automatic HTTPS、hosted MCP endpoints 的路径清楚 |
server 可以用 TypeScript 或 Python;widget 可以用 React,也可以从轻量 HTML/CSS/JS 开始。关键不是技术栈复杂,而是 tool contract 清楚。
使用步骤
- 从一个窄的 app outcome 开始,让 Codex 规划 3 到 5 个 tools,每个 tool 都要有清楚的 name、description、inputs、outputs。
- 判断 v1 是否需要 widget。能 data-only 就先 data-only;需要 UI 再做 widget。
- 按现有 repo patterns scaffold MCP server 和可选 widget,不要一上来堆依赖。
- 通过本地 HTTPS 路径运行,例如
ngrok或 Cloudflare Tunnel。 - 在 ChatGPT developer mode 里连接 app。
- 用 small direct、indirect、negative prompt set 测试。
- 迭代 metadata、state handling、
structuredContent和_metapayloads,直到 core read flow 稳定。 - 只有在 user-specific data 或 write actions 需要时,再加入 OAuth 2.1。
- 准备 hosted preview,保持稳定
/mcpendpoint,检查 streaming 和 widget asset hosting。 - 分享或提交前,完成 launch checklist。
Prompt 设计要点
强 prompt 通常包含六个要素:
- One clear outcome:说明 app 应该帮用户在 ChatGPT 里完成什么。
- Concrete stack:说明 server 用 TypeScript 还是 Python,widget 用 React 还是轻量方案。
- Explicit tool boundaries:每个 tool 只做一件事。
- Auth expectations:第一版能否 anonymous,还是必须 linked accounts 和 write actions。
- Local development path:说明用哪条 HTTPS testing path 接入 ChatGPT。
- Verification steps:告诉 Codex 跑哪些命令、测哪些 prompts、回报哪些 evidence。
避免一个 prompt 同时要求 planning、implementation、auth、deployment、submission 和 polish。更稳的 milestone 是:
- Plan the App Before You Scaffold It。
- Scaffold the First Working Version。
- Add Auth Only After the Core Flow Works。
- Prepare the App for Deployment and Review。
Launch Readiness
上线前至少确认:
- app 只有一个用户一眼能看懂的窄 outcome。
- tool set 小而明确,metadata、inputs、outputs 都清楚。
- MCP server 能端到端工作,并返回简洁
structuredContent。 - widget-only data 放在
_meta,不要塞进对话正文。 - widget 如有必要,能在 ChatGPT 内正确渲染。
- 本地 HTTPS 测试 loop 能通过 ChatGPT developer mode 跑通。
- direct、indirect、negative prompt set 都按预期触发 conversation flow 和 tool payloads。
- 账号连接只加在 user-specific data 或 write actions 真正需要的地方。
- deployment plan 和 launch-readiness review 覆盖 metadata、tool hints、privacy 和 test prompts。
常见坑
| 问题 | 更稳的做法 |
|---|---|
| 让 Codex 把整个产品搬进 ChatGPT | 先选一个 core user outcome、3 到 5 个 tools、一个窄 widget |
| 一个巨大 prompt 覆盖所有阶段 | 拆成 planning、scaffold、auth、deployment、review |
| tool contract 还没清楚就写 UI | 先定 tool surface 和 response schema,再做 widget |
| 不让 Codex 查官方 docs | $chatgpt-apps 配合 $openai-docs,按当前 Apps SDK guidance scaffold |
| metadata 最后才补 | 早写 tool descriptions 和 parameter docs,再用 prompt set 回放 |
| 核心 read flow 没通就加账号连接 | 先跑通 anonymous 或 read-only 路径,再给真正需要的 tools 加 OAuth |
| 没在 ChatGPT 里测试就宣布完成 | 在 developer mode 连接 app,检查 tool payloads 和真实 conversation flow |