🚀 命令行工具,用于快速搭建支持ChatGPT小部件的MCP(模型上下文协议)服务器。
几秒钟内快速设置带有小部件和完整开发环境的MCP服务器。
# 使用npx(推荐)
npx mcp-widget create my-app
# 或全局安装
npm install -g mcp-widget
mcp-widget create my-app
# 或使用特定包管理器
pnpm dlx mcp-widget create my-app
yarn dlx mcp-widget create my-app
只需运行命令并跟随提示:
npx mcp-widget create my-app
您将被询问:
对于脚本和自动化,您可以通过命令行参数指定所有选项:
# 使用特定模板创建
npx mcp-widget create my-app --template vite
npx mcp-widget create my-app --template nextjs
# 使用短标志
npx mcp-widget create my-app -t vite
# 使用--yes标志跳过所有提示(使用默认值)
n
px mcp-widget create my-app --yes
# 组合选项
npx mcp-widget create my-app -t nextjs -y
可用选项:
--template, -t - 要使用的模板(vite或nextjs)--yes, -y - 跳过交互式提示并使用默认值示例:
# 交互模式
npx mcp-widget create
# 混合模式 - 提供名称,提示模板
npx mcp-widget create my-app
# 完全非交互模式
npx mcp-widget create my-app -t nextjs
您的新项目包括:
my-chatgpt-app/
├── src/
│ ├── server/
│ │ └── server.ts # MCP服务器实现
│ └── widgets/
│ └── hello-world/
│ └── index.tsx # 示例小部件
├── scripts/
│ └── dev.js # 开发服务器
├── package.json
├── tsconfig.json # TypeScript配置
├── tsconfig.node.json # Node.js的TypeScript配置
├── tsconfig.server.json # 服务器的TypeScript配置
├── vite.config.js # 小部件构建配置
└── README.md # 项目特定文档
适用于快速原型设计和专注于小部件开发,具有全面的TypeScript支持。
架构:
/mcp处的SSE端点启动开发:
cd my-app
npm run dev
端点:
全栈应用,带TypeScript和API路由。
架构:
/api/mcp用于MCP协议/api/mcp/messages用于工具调用启动开发:
cd my-app
npm run dev
端点:
npx mcp-widget create my-app
# 跟随提示
cd your-project-name
npm install # 如果依赖项未自动安装
npm run dev
# 在新的终端中
npx @modelcontextprotocol/inspector http://localhost:8000/mcp
或者对于Next.js:
npx @modelcontextprotocol/inspector http://localhost:3000/api/mcp
使用ngrok测试您的MCP服务器与ChatGPT:
# 安装ngrok
brew install ngrok/ngrok/ngrok
# 启动隧道(当您的应用正在运行时)
ngrok http 8000 # 或3000用于Next.js
# 使用HTTPS URL在ChatGPT中创建自定义GPT
完整指南: 查看docs/CHATGPT_INTEGRATION.md
npm run build
生成的项目实现了模型上下文协议,包含:
hello-world工具演示了完整的流程:
// 调用工具
{
"name": "hello-world",
"arguments": {
"message": "Hello from ChatGPT!"
}
}
// 返回
{
"content": [
{ "type": "text", "text": "Hello World Tool called..." }
],
"structuredContent": [
{
"type": "message",
"message": "Hello from ChatGPT!",
"timestamp": "2025-10-19T..."
}
],
"_meta": {
"outputTemplate": "ui://widget/hello-world.html"
}
}
小部件是React组件,可以:
window.openai.toolOutput获取数据window.openai.displayMode(浅色/深色)src/widgets/my-widget/index.tsx:import React, { useState, useEffect } from "react";
import { createRoot } from "react-dom/client";
// 定义工具输出类型
interface ToolOutput {
message?: string;
[key: string]: any;
}
// 扩展window接口
declare global {
interface Window {
openai?: {
toolOutput?: ToolOutput;
displayMode?: string;
};
}
}
function MyWidget() {
const [data, setData] = useState<ToolOutput | null>(null);
useEffect(() => {
const output = window.openai?.toolOutput;
if (output) {
setData(output);
}
}, []);
return <div>我的自定义小部件: {data?.message}</div>;
}
const root = createRoot(document.getElementById("root")!);
root.render(<MyWidget />);
ui://widget/my-widget.html访问npm test
查看TESTING.md以获得全面的测试指南。
Inspector是您最好的测试伙伴:
npx @modelcontextprotocol/inspector http://localhost:8000/mcp
验证:
欢迎贡献!请随意提交拉取请求。
git checkout -b feature/amazing-feature)git commit -m '添加精彩功能')git push origin feature/amazing-feature)MIT © [您的名字]
查看examples/目录:
当前模板使用SSE(服务器发送事件)进行MCP通信。虽然MCP SDK正向StreamableHttp迁移,但SSE简单且可靠地适用于开发。
两个模板现在默认使用TypeScript,提供了更好的开发者体验,包括类型安全和IntelliSense。然而,您可以通过简单地将.ts/.tsx文件重命名为.js/.jsx并移除类型注解来轻松使用JavaScript。
查看examples/auth/中的身份验证示例(即将推出)。
可以!查看docs/DEPLOYMENT.md中的部署指南(即将推出)。
愉快地构建吧! 🚀
如果您发现此工具有用,请⭐星标该仓库!
MIT