ChatGPT Apps SDK Next.js Starter
A minimal MCP server demonstrating how to build ChatGPT-compatible applications using Next.js with widget rendering capabilities. Provides a starter template for integrating Next.js applications with the ChatGPT Apps SDK through the Model Context Protocol.
README
ChatGPT Apps SDK Next.js Starter
A minimal Next.js application demonstrating how to build an OpenAI Apps SDK compatible MCP server with widget rendering in ChatGPT.
Overview
This project shows how to integrate a Next.js application with the ChatGPT Apps SDK using the Model Context Protocol (MCP). It includes a working MCP server that exposes tools and resources that can be called from ChatGPT, with responses rendered natively in ChatGPT.
Key Components
1. MCP Server Route (app/mcp/route.ts)
The core MCP server implementation that exposes tools and resources to ChatGPT.
Key features:
- Tool registration with OpenAI-specific metadata
- Resource registration that serves HTML content for iframe rendering
- Cross-linking between tools and resources via
templateUri
OpenAI-specific metadata:
{
"openai/outputTemplate": widget.templateUri, // Links to resource
"openai/toolInvocation/invoking": "Loading...", // Loading state text
"openai/toolInvocation/invoked": "Loaded", // Completion state text
"openai/widgetAccessible": false, // Widget visibility
"openai/resultCanProduceWidget": true // Enable widget rendering
}
Full configuration options: OpenAI Apps SDK MCP Documentation
2. Asset Configuration (next.config.ts)
Critical: Set assetPrefix to ensure /_next/ static assets are fetched from the correct origin:
const nextConfig: NextConfig = {
assetPrefix: baseURL, // Prevents 404s on /_next/ files in iframe
};
Without this, Next.js will attempt to load assets from the iframe's URL, causing 404 errors.
3. CORS Middleware (middleware.ts)
Handles browser OPTIONS preflight requests required for cross-origin RSC (React Server Components) fetching during client-side navigation:
export function middleware(request: NextRequest) {
if (request.method === "OPTIONS") {
// Return 204 with CORS headers
}
// Add CORS headers to all responses
}
4. SDK Bootstrap (app/layout.tsx)
The <NextChatSDKBootstrap> component patches browser APIs to work correctly within the ChatGPT iframe:
What it patches:
history.pushState/history.replaceState- Prevents full-origin URLs in historywindow.fetch- Rewrites same-origin requests to use the correct base URL<html>attribute observer - Prevents ChatGPT from modifying the root element
Required configuration:
<html lang="en" suppressHydrationWarning>
<head>
<NextChatSDKBootstrap baseUrl={baseURL} />
</head>
<body>{children}</body>
</html>
Note: suppressHydrationWarning is currently required because ChatGPT modifies the initial HTML before the Next.js app hydrates, causing hydration mismatches.
Getting Started
Installation
npm install
# or
pnpm install
Development
npm run dev
# or
pnpm dev
Open http://localhost:3000 to see the app.
Testing the MCP Server
The MCP server is available at:
http://localhost:3000/mcp
Connecting from ChatGPT
- Deploy your app to Vercel
- In ChatGPT, navigate to Settings → Connectors → Create and add your MCP server URL with the
/mcppath (e.g.,https://your-app.vercel.app/mcp)
Note: Connecting MCP servers to ChatGPT requires developer mode access. See the connection guide for setup instructions.
Project Structure
app/
├── mcp/
│ └── route.ts # MCP server with tool/resource registration
├── layout.tsx # Root layout with SDK bootstrap
├── page.tsx # Homepage content
└── globals.css # Global styles
middleware.ts # CORS handling for RSC
next.config.ts # Asset prefix configuration
How It Works
- Tool Invocation: ChatGPT calls a tool registered in
app/mcp/route.ts - Resource Reference: Tool response includes
templateUripointing to a registered resource - Widget Rendering: ChatGPT fetches the resource HTML and renders it in an iframe
- Client Hydration: Next.js hydrates the app inside the iframe with patched APIs
- Navigation: Client-side navigation uses patched
fetchto load RSC payloads
Learn More
- OpenAI Apps SDK Documentation
- OpenAI Apps SDK - MCP Server Guide
- Model Context Protocol
- Next.js Documentation
Deployment
This project is designed to work seamlessly with Vercel deployment. The baseUrl.ts configuration automatically detects Vercel environment variables and sets the correct asset URLs.
The configuration automatically handles:
- Production URLs via
VERCEL_PROJECT_PRODUCTION_URL - Preview/branch URLs via
VERCEL_BRANCH_URL - Asset prefixing for correct resource loading in iframes
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。