MCP锅炉板:简单设置指南
此项目可帮助您在Cloudflare上创建自己的远程MCP服务器,并提供用户登录和支付选项。你不需要成为技术专家来运行它。
\[!注意\] 这个项目现在是免费使用和开源的。如果你想支持我,就在X上跟着我 @安努塔尔 并订阅 我的时事通讯.
你会得到什么
- 与Cursor、Claude和其他AI助手配合使用的MCP服务器
- 用户登录谷歌或GitHub
- 使用Stripe进行支付处理
- 创建免费和付费MCP工具的能力
设置检查表
在开始之前,请确保您已经:
- 已安装Node.js(从下载 )
- Cloudflare帐户(注册地址: dash.cloudflare.com/signup)
- 用于设置登录的Google帐户(如果您愿意,也可以使用GitHub)
- Stripe支付账户(注册地址: dashboard.stripe.com/register)
逐步设置
第一步:获取代码
- 将此存储库克隆到您的计算机:
git clone https://github.com/iannuttall/mcp-boilerplate.git
cd mcp-boilerplate- 安装所需的一切:
npm install步骤2:设置数据库
- 如果您还没有安装Wrangler(Cloudflare的工具):
npm install -g wrangler- 创建用户登录数据库:
npx wrangler kv namespace create "OAUTH_KV"注意:您不能为此数据库使用其他名称。它必须是“OAUTH_KV”。
- 运行此命令后,您将看到一些文本,其中包括
id和preview_id价值观
- 打开
wrangler.jsonc项目文件夹中的文件
- 查找包含以下内容的部分
"kv_namespaces": [
- 在此处添加您的数据库信息:
"kv_namespaces": [
{
"binding": "OAUTH_KV",
"id": "paste-your-id-here",
"preview_id": "paste-your-preview-id-here"
}
]步骤3:设置本地设置
- 为您的设置创建一个文件:
cp .dev.vars.example .dev.vars- 打开
.dev.vars代码编辑器中的文件
- 您需要在此处添加几个值(我们将在接下来的步骤中获取它们)
步骤4a:设置谷歌登录(推荐)
- 转到 谷歌云控制台
- 使用您喜欢的任何名称创建新项目
- 转到“API和服务”>“凭据”
- 点击“+创建证书”并选择“OAuth客户端ID”
- 如果出现提示,请设置同意屏幕:
- 选择“外部”作为用户类型 - 添加应用程序名称(如“我的AI工具”) - 在需要的地方添加您的电子邮件地址 - 您可以跳过“范围”和“测试用户”部分
- 对于OAuth客户端:
- 选择“Web应用程序”作为应用程序类型 - 给它起个名字 - 在“授权重定向URI”下添加以下内容:
http://localhost:8787/callback/google- 点击“创建”
- 现在,您将看到您的客户端ID和客户端密码-复制这些值
- 将它们添加到您的
.dev.vars文件:
GOOGLE_CLIENT_ID="paste-your-client-id-here"
GOOGLE_CLIENT_SECRET="paste-your-client-secret-here"完成此步骤后,如果不需要登录GitHub,可以直接进入步骤5。
步骤4b:设置GitHub登录(可选)
如果你更喜欢使用GitHub而不是谷歌登录:
- 转到您的GitHub帐户
- 点击右上角的个人资料图片,然后转到“设置”
- 在左侧边栏中,向下滚动并单击“开发人员设置”
- 点击“OAuth应用”,然后点击“新建OAuth应用”按钮
- 填写表格:
- 应用程序名称:给它起个名字(比如“我的AI工具”) - 主页网址: http://localhost:8787 - 应用程序描述:应用程序的简要描述(可选) - 授权回调URL: http://localhost:8787/callback/github
- 点击“注册申请”
- 在下一页,您将看到您的客户ID
- 点击“生成新的客户端密码”
- 立即复制您的客户端密码(您将无法再次看到它)
- 将这些值添加到您的
.dev.vars文件:
GITHUB_CLIENT_ID="paste-your-client-id-here"
GITHUB_CLIENT_SECRET="paste-your-client-secret-here"- 您还需要更新代码中的默认身份验证:
-打开 src/index.ts -查找Google处理程序的导入: import { GoogleHandler } from "./auth/google-handler"; -替换为: import { GitHubHandler } from "./auth/github-handler"; -找到这条线 defaultHandler: GoogleHandler as any, -将其更改为: defaultHandler: GitHubHandler as any,
完成步骤4a或4b后,继续执行步骤5。
步骤5:设置Stripe支付
- 登录到您的 条纹仪表板
- 获取测试API密钥:
- 转到开发人员>API密钥 - 复制您的“密钥”(以开头 sk_test_)
- 创建产品和价格:
- 转到产品>添加产品 - 给它起个名字和描述 - 添加价格(这是用户将支付的价格) - 保存产品 - 保存后,查找并复制“价格ID”(以 price_)
- 将这些值添加到您的
.dev.vars文件:
STRIPE_SECRET_KEY="sk_test_your-key-here"
STRIPE_SUBSCRIPTION_PRICE_ID="price_your-price-id-here"
STRIPE_METERED_PRICE_ID="your-stripe-metered-price-id"步骤5a:配置Stripe客户计费门户
这个样板包括一个工具(check_user_subscription_status)它可以为您的最终用户提供指向其Stripe客户计费门户的链接。此门户允许他们管理订阅,例如取消订阅,或者如果您配置了此门户,则可以在不同计划之间切换。
初始设置(重要):
默认情况下,Stripe客户计费门户可能未在您的Stripe帐户中完全配置,特别是在测试环境中。
- 设置Stripe密钥和产品(步骤5)并运行服务器后,您可以测试
check_user_subscription_status工具(例如,通过MCP Inspector,或通过AI助手触发)。 - 如果该工具返回JSON响应,其中
billingPortal.message包含以下错误: *“无法生成指向客户计费门户的链接:未提供配置,也未创建测试模式默认配置。请提供配置或通过将客户门户设置保存在测试模式来创建默认配置https://dashboard.stripe.com/test/settings/billing/portal."* - 你 必须 访问错误消息中提供的URL(通常
https://dashboard.stripe.com/test/settings/billing/portal)并将您的门户设置保存在Stripe中。这将激活测试环境的门户。您需要对实时环境进行类似的检查和配置。
一旦激活 check_user_subscription_status 该工具将在 billingPortal.url JSON响应的字段,您的用户可以使用。
允许用户切换计划(可选):
默认情况下,计费门户允许用户取消现有订阅。如果您为MCP服务器提供多种订阅产品,并希望允许用户在它们之间切换:
- 在您的Stripe仪表板中,导航到 设置 (单击右上角的齿轮图标),然后找到 客户门户 在“账单”下。(或者,使用直接链接:
https://dashboard.stripe.com/settings/billing/portal对于直播模式,或https://dashboard.stripe.com/test/settings/billing/portal用于测试模式)。 - 在“产品“客户门户设置页面的部分,查找”订阅产品".
- 启用“客户可以更改计划“切换。
- 在出现的“选择客户可以更新的合格产品”小节中,单击“查找测试产品。..“(或在实时模式下“查找产品…”)并添加您想让用户切换到的其他订阅产品。您之前提供的图像在Stripe中显示了此UI。
- 您还可以在此处配置其他选项,例如允许客户更改其计划的数量(如果适用)。
此配置使您的用户能够直接通过Stripe托管的门户更灵活地管理他们的订阅。
步骤6:完成设置
确保你的 .dev.vars 文件具有所有这些值:
BASE_URL="http://localhost:8787"
COOKIE_ENCRYPTION_KEY="generate-a-random-string-at-least-32-characters"
GOOGLE_CLIENT_ID="your-google-client-id"
GOOGLE_CLIENT_SECRET="your-google-client-secret"
STRIPE_SECRET_KEY="your-stripe-secret-key"
STRIPE_SUBSCRIPTION_PRICE_ID="your-stripe-price-id"
STRIPE_METERED_PRICE_ID="your-stripe-metered-price-id"对于 COOKIE_ENCRYPTION_KEY,您可以使用以下命令生成随机字符串:
openssl rand -hex 32步骤7:在本地启动服务器
- 运行此命令以启动服务器:
npx wrangler dev- 您的服务器将在以下时间启动
http://localhost:8787
- 人工智能工具的主要终点将在
http://localhost:8787/sse
第八步:尝试一下
您可以通过使用AI助手连接到服务器来测试您的服务器:
- 首选 Cloudflare AI游乐场
- 请输入您的服务器URL:
http://localhost:8787/sse - 您将被重定向到使用Google登录
- 登录后,您可以开始测试这些工具
或者使用Claude Desktop:
- 打开克劳德桌面
- 前往“设置”>“开发人员”>“编辑配置”
- 添加您的服务器:
{
"mcpServers": {
"my_server": {
"command": "npx",
"args": [
"mcp-remote",
"http://localhost:8787/sse"
]
}
}
}- 重新启动克劳德桌面
- 您的工具现在应该可以在Claude中使用了
或与MCP检查员联系:
- 运行MCP检查器并连接到您的服务器:
npx @modelcontextprotocol/inspector@0.11.0 \[!警告\] MCP Inspector的最新版本是0.12.0,但使用npx@modelcontextprotocol/inspector@latest现在不起作用。正在努力。
- 请输入您的服务器URL:
http://localhost:8787/sse - 使用web界面测试和调试您的工具
- 您可以直接调用您的工具,查看请求/响应数据,并在开发过程中快速迭代
步骤9:上线(部署)
当您准备好让服务器联机时:
- 部署到Cloudflare:
npx wrangler deploy- 部署后,您将获得一个URL,如下所示
https://your-worker-name.your-account.workers.dev
3a。更新您的Google OAuth设置:
- 返回谷歌云控制台>API和服务>凭据。
- 编辑您的OAuth客户端。
- 添加另一个重定向URI:
https://your-worker-name.your-account.workers.dev/callback/google. - 接下来,导航到“OAuth同意屏幕”页面(仍在“API和服务”中)。
- 在“发布状态”下,如果当前显示“测试”,请单击“发布应用程序”按钮并确认将其移动到“生产”。如果您最初将登录设置为“外部”,这允许GSuite组织之外的用户使用登录。
3b。更新您的GitHub OAuth应用程序设置:(可选)
- 转到GitHub开发人员设置>OAuth应用程序
- 选择您的OAuth应用程序
- 将“授权回调URL”更新为:
https://your-worker-name.your-account.workers.dev/callback/github
- 通过运行以下命令将您的设置添加到Cloudflare(系统将提示您输入每个值):
npx wrangler secret put BASE_URL
npx wrangler secret put COOKIE_ENCRYPTION_KEY
npx wrangler secret put GOOGLE_CLIENT_ID
npx wrangler secret put GOOGLE_CLIENT_SECRET
npx wrangler secret put STRIPE_SECRET_KEY
npx wrangler secret put STRIPE_SUBSCRIPTION_PRICE_ID
npx wrangler secret put STRIPE_METERED_PRICE_ID对于 BASE_URL,使用您的Cloudflare URL: https://your-worker-name.your-account.workers.dev
创建自己的工具
您可以通过向数据库中添加新文件来轻松创建自己的AI工具 src/tools 文件夹。该项目附带了免费和付费工具的示例。
创建免费工具
要创建一个免费工具(用户可以免费访问):
- 在中创建新文件
src/tools文件夹(例如:myTool.ts) - 从现有模板复制此模板
add.ts例子:
import { z } from "zod";
import { experimental_PaidMcpAgent as PaidMcpAgent } from "@stripe/agent-toolkit/cloudflare";
export function myTool(agent: PaidMcpAgent) {
const server = agent.server;
// @ts-ignore
server.tool(
"my_tool_name", // The tool name
"This tool does something cool.", // Description of what your tool does
{ // Input parameters
input1: z.string(), // Parameter definitions using Zod
input2: z.number() // E.g., strings, numbers, booleans
},
async ({ input1, input2 }: { input1: string; input2: number }) => ({
// The function that runs when the tool is called
content: [{ type: "text", text: `You provided: ${input1} and ${input2}` }],
})
);
}- 修改代码以创建自己的工具:
- 更改函数名称(myTool) - 更改工具名称(my_tool_name) - 更新描述 - 定义工具所需的输入参数 - 编写调用工具时运行的代码
- 将您的工具添加到
src/tools/index.ts:
// Add this line with your other exports
export * from './myTool';- 在中注册您的工具
src/index.ts:
// Inside the init() method, add:
tools.myTool(this);创建付费工具:订阅、计费或一次性付款
您可以创建需要以三种方式付款的工具:定期订阅、按计量使用或一次性付款。
选项1:创建基于订阅的付费工具
如果您想向用户收取访问工具或工具套件的经常性费用(例如每月),则此选项非常合适。
订阅账单的条纹设置:
- 在您的Stripe仪表板中,创建一个新产品。
- 为您的产品命名(例如,“Pro Access Tier”)。
- 为此产品添加价格:
- 为定价模型选择“重复”。 - 设置价格金额和计费间隔(例如,每月10美元)。 - 节省价格。
- 创建价格后,Stripe将向您显示价格ID(例如。,
price_xxxxxxxxxxxxxx).这是您将使用的IDSTRIPE_SUBSCRIPTION_PRICE_ID在你的.dev.vars文件和注册工具时。
工具实施:
- 在中创建新文件
src/tools文件夹(例如:mySubscriptionTool.ts) - 从现有模板复制此模板
subscriptionAdd.ts例子:
import { z } from "zod";
import { experimental_PaidMcpAgent as PaidMcpAgent } from "@stripe/agent-toolkit/cloudflare";
import { REUSABLE_PAYMENT_REASON } from "../helpers/constants";
export function mySubscriptionTool(
agent: PaidMcpAgent,
env?: { STRIPE_SUBSCRIPTION_PRICE_ID: string; BASE_URL: string }
) {
const priceId = env?.STRIPE_SUBSCRIPTION_PRICE_ID || null;
const baseUrl = env?.BASE_URL || null;
if (!priceId || !baseUrl) {
throw new Error("Stripe Price ID and Base URL must be provided for paid tools");
}
agent.paidTool(
"my_subscription_tool_name", // The tool name
{
// Input parameters
input1: z.string(), // Parameter definitions using Zod
input2: z.number(), // E.g., strings, numbers, booleans
},
async ({ input1, input2 }: { input1: string; input2: number }) => ({
// The function that runs when the tool is called
content: [
{ type: "text", text: `You provided: ${input1} and ${input2}` },
],
}),
{
priceId, // Uses the Stripe price ID for a subscription product
successUrl: `${baseUrl}/payment/success`,
paymentReason: REUSABLE_PAYMENT_REASON, // General reason shown to user
}
);
}- 修改代码:
- 更改函数名称(mySubscriptionTool) - 更改工具名称(my_subscription_tool_name) - 更新输入参数和工具的逻辑。
- 将您的工具添加到
src/tools/index.ts:
// Add this line with your other exports
export * from './mySubscriptionTool';- 在中注册您的工具
src/index.ts:
// Inside the init() method, add:
tools.mySubscriptionTool(this, {
STRIPE_SUBSCRIPTION_PRICE_ID: this.env.STRIPE_SUBSCRIPTION_PRICE_ID, // Ensure this matches a subscription Price ID
BASE_URL: this.env.BASE_URL
});选项2:创建计量使用付费工具
如果你想根据用户使用MCP工具的程度向他们收费,这个选项是合适的。
计量计费的条纹设置:
- 在您的Stripe仪表板中,创建一个新产品。
- 为此产品添加价格。
- 根据您的型号选择“标准定价”或“套餐定价”。 - 在“价格选项”下,选中“使用量按计量”。 - 然后,您可以定义如何报告使用情况(例如,“每单位”)。 - 如果你想提供免费套餐(前3次使用是免费的),你可以设置“分级定价”。例如: - 前3个单位:每单位0.00美元 - 下一个单位(4个及以上):每单位0.10美元
- 创建价格后,Stripe将向您显示价格ID(例如。,
price_xxxxxxxxxxxxxx). - 如果您还没有,您还需要在Stripe中为此产品/价格定义一个“米”。该仪表将具有事件名称(例如。,
metered_add_usage)您将在工具代码中使用。您通常可以在产品的“使用”选项卡下或定义计量价格时进行设置。
工具实施:
- 在中创建新文件
src/tools文件夹(例如。,myMeteredTool.ts). - 使用此模板,灵感来自
meteredAdd.ts例子:
import { z } from "zod";
import { experimental_PaidMcpAgent as PaidMcpAgent } from "@stripe/agent-toolkit/cloudflare";
import { METERED_TOOL_PAYMENT_REASON } from "../helpers/constants"; // You might want a specific constant
export function myMeteredTool(
agent: PaidMcpAgent,
env?: { STRIPE_METERED_PRICE_ID: string; BASE_URL: string }
) {
const priceId = env?.STRIPE_METERED_PRICE_ID || null;
const baseUrl = env?.BASE_URL || null;
if (!priceId || !baseUrl) {
throw new Error("Stripe Metered Price ID and Base URL must be provided for metered tools");
}
agent.paidTool(
"my_metered_tool_name", // The tool name
{
// Input parameters
a: z.number(),
b: z.number(),
},
async ({ a, b }: { a: number; b: number }) => {
// The function that runs when the tool is called
// IMPORTANT: Business logic for your tool
const result = a + b; // Example logic
return {
content: [{ type: "text", text: String(result) }],
};
},
{
checkout: {
success_url: `${baseUrl}/payment/success`,
line_items: [
{
price: priceId, // Uses the Stripe Price ID for a metered product
},
],
mode: 'subscription', // Metered plans are usually set up as subscriptions
},
paymentReason:
"METER INFO: Details about your metered usage. E.g., Your first X uses are free, then $Y per use. " +
METERED_TOOL_PAYMENT_REASON, // Customize this message
meterEvent: "your_meter_event_name_from_stripe", // ** IMPORTANT: Use the event name from your Stripe meter setup **
// e.g., "metered_add_usage"
}
);
}- 修改代码:
- 更改函数名称(myMeteredTool). - 更改工具名称(my_metered_tool_name). - 更新输入参数和工具的核心逻辑。 - 至关重要的是,更新 meterEvent 以匹配您在Stripe仪表中配置的事件名称。 - 自定义 paymentReason 向用户清楚地解释计量计费。
- 将您的工具添加到
src/tools/index.ts:
// Add this line with your other exports
export * from './myMeteredTool';- 在中注册您的工具
src/index.ts:
// Inside the init() method, add:
tools.myMeteredTool(this, {
STRIPE_METERED_PRICE_ID: this.env.STRIPE_METERED_PRICE_ID, // Ensure this matches your metered Price ID
BASE_URL: this.env.BASE_URL
});选项3:创建一次性支付工具
如果您想向用户收取访问工具的单一费用,而不是定期订阅或基于使用情况的计量,则此选项是合适的。
一次性付款的条纹设置:
- 在您的Stripe仪表板中,创建一个新产品。
- 为您的产品命名(例如,“单一报告生成”)。
- 为此产品添加价格:
- 为定价模型选择“一次性”。 - 设置价格金额。 - 节省价格。
- 创建价格后,Stripe将向您显示价格ID(例如。,
price_xxxxxxxxxxxxxx).这是您将用于新环境变量的ID,例如,STRIPE_ONE_TIME_PRICE_ID.
工具实施:
- 在中创建新文件
src/tools文件夹(例如:myOnetimeTool.ts). - 使用此模板,灵感来自
onetimeAdd.ts例子:
import { z } from "zod";
import { experimental_PaidMcpAgent as PaidMcpAgent } from "@stripe/agent-toolkit/cloudflare";
import { REUSABLE_PAYMENT_REASON } from "../helpers/constants"; // Or a more specific reason
export function myOnetimeTool(
agent: PaidMcpAgent, // Adjust AgentProps if needed
env?: { STRIPE_ONE_TIME_PRICE_ID: string; BASE_URL: string }
) {
const priceId = env?.STRIPE_ONE_TIME_PRICE_ID || null;
const baseUrl = env?.BASE_URL || null;
if (!priceId || !baseUrl) {
throw new Error("Stripe One-Time Price ID and Base URL must be provided for this tool");
}
agent.paidTool(
"my_onetime_tool_name", // The tool name
{
// Input parameters
input1: z.string(), // Parameter definitions using Zod
},
async ({ input1 }: { input1: string }) => ({
// The function that runs when the tool is called
content: [
{ type: "text", text: `You processed: ${input1}` },
],
}),
{
checkout: { // Defines a one-time payment checkout session
success_url: `${baseUrl}/payment/success`,
line_items: [
{
price: priceId, // Uses the Stripe Price ID for a one-time payment product
quantity: 1,
},
],
mode: 'payment', // Specifies this is a one-time payment, not a subscription
},
paymentReason: "Enter a clear reason for this one-time charge. E.g., 'Unlock premium feature X for a single use.'", // Customize this message
}
);
}- 修改代码:
- 更改函数名称(myOnetimeTool). - 更改工具名称(my_onetime_tool_name). - 更新输入参数和工具的核心逻辑。 - 确保 checkout.mode 设置为 'payment'. - 自定义 paymentReason 向用户清楚地解释一次性费用。
- 将您的工具添加到
src/tools/index.ts:
// Add this line with your other exports
export * from './myOnetimeTool';- 在中注册您的工具
src/index.ts:
// Inside the init() method, add:
tools.myOnetimeTool(this, {
STRIPE_ONE_TIME_PRICE_ID: this.env.STRIPE_ONE_TIME_PRICE_ID, // Ensure this matches your one-time payment Price ID
BASE_URL: this.env.BASE_URL
});- 记得添加
STRIPE_ONE_TIME_PRICE_ID到你的.dev.vars文件和Cloudflare机密:
在……里面 .dev.vars:
STRIPE_ONE_TIME_PRICE_ID="price_your-onetime-price-id-here"对于生产:
npx wrangler secret put STRIPE_ONE_TIME_PRICE_ID您可以通过在Stripe仪表板中创建其他价格ID并将其作为环境变量传递,为不同的Stripe产品(订阅或计量)创建不同的付费工具。
当免费用户尝试付费工具时会发生什么
当用户试图在没有购买的情况下访问付费工具时:
- 服务器检查他们是否已经付款
- 如果没有,AI助手将自动提示他们结账链接
- 在Stripe上完成付款后,他们应该能够立即使用该工具
未来增强功能(可选)
设置条纹Webhooks
上面的基本设置是您开始所需的一切。内置的Stripe集成在用户尝试访问付费工具时直接验证付款——它会自动检查一次性付款和订阅。
Webhooks是完全可选的,但可能对未来更复杂的支付场景有用,例如:
- 构建客户仪表板以显示订阅状态
- 通过计量实现基于使用量的计费
- 创建或取消订阅时创建自定义工作流
- 用特殊逻辑处理退款和争议
如果您想添加webhook支持:
- 前往您的Stripe仪表板>开发人员>Webhooks
- 点击“添加端点”
- 对于端点URL:
- 对于当地发展: http://localhost:8787/webhooks/stripe - 生产: https://your-worker-name.your-account.workers.dev/webhooks/stripe
- 对于“要发送的事件”,选择与您的需求相关的事件,例如:
- checkout.session.com已完成 - 发票.付款_成功 - customer.subcription.已更新
- 创建webhook后,复制“签名密钥”
- 将此值添加到您的设置中:
- 对于当地发展,请添加 .dev.vars:
STRIPE_WEBHOOK_SECRET="whsec_your-webhook-secret-here"- 对于生产,请使用Wrangler进行设置:
npx wrangler secret put STRIPE_WEBHOOK_SECRET需要帮助?
如果您遇到任何错误或样板有问题,请在GitHub存储库上提交问题。请注意,此项目按原样提供,不包括直接支持。
