支付链接生成工具

一个轻量级的JavaScript客户端,用于与Stripe的模型上下文协议(MCP)服务器交互以创建支付链接,适用于需要集成Stripe支付服务的场景。

  • by mattmorgis
  • JavaScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 mattmorgis/stripe-mcp-client

使用说明

条纹MCP客户端

一个轻量级的JavaScript客户端,用于与Stripe的模型上下文协议(MCP)服务器交互以创建支付链接。此客户端旨在在您自己的MCP服务器内使用,以实现与Stripe支付服务的集成。

注: 此代码由Claude 3.7 Sonnet生成。 视频

特性

  • 用于创建Stripe支付链接的简单API
  • 围绕MCP客户端的轻量级包装
  • 支持自定义选项和连接帐户
  • MCP连接的自动管理

安装

npm install stripe-mcp-client

请确保您已安装Node.js 16+。

依赖项

此包取决于:

  • @modelcontextprotocol/sdk:用于MCP客户端实现
  • @stripe/mcp:用于Stripe MCP服务器(对等依赖)

用法

基本用法

import StripeMcpClient from 'stripe-mcp-client';

async function createPaymentLink() {
  // Initialize the client
  const client = new StripeMcpClient({
    apiKey: 'your_stripe_api_key', // Or set STRIPE_API_KEY env var
    debug: true, // Optional: Enable debug logging
  });

  try {
    // Connect to the Stripe MCP server
    await client.connect();

    // Create a payment link
    const paymentLink = await client.createPaymentLink({
      line_items: [
        {
          price: 'price_123', // Your Stripe price ID
          quantity: 1,
        },
      ],
      after_completion: {
        type: 'redirect',
        redirect: {
          url: 'https://example.com/thank-you',
        },
      },
    });

    console.log(`Payment link created: ${paymentLink.url}`);
    return paymentLink;
  } finally {
    // Always close the connection when done
    await client.close();
  }
}

createPaymentLink().catch(console.error);

与环境变量一起使用

// Set in your environment: STRIPE_API_KEY, STRIPE_ACCOUNT (optional)
import StripeMcpClient from 'stripe-mcp-client';

async function createPaymentLink() {
  const client = new StripeMcpClient();
  try {
    await client.connect();
    const paymentLink = await client.createPaymentLink({
      line_items: [{price: 'price_123', quantity: 1}],
    });
    return paymentLink;
  } finally {
    await client.close();
  }
}

在您自己的MCP服务器中使用

import {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js';
import {StdioServerTransport} from '@modelcontextprotocol/sdk/server/stdio.js';
import StripeMcpClient from 'stripe-mcp-client';
import {z} from 'zod';

// Create your MCP server
const server = new McpServer({
  name: 'My Payment Server',
  version: '1.0.0',
});

// Add a tool that creates Stripe payment links
server.tool(
  'create_payment_link',
  'Create a Stripe payment link for checkout',
  {
    product_name: z.string().describe('Name of the product'),
    price_amount: z.number().describe('Price amount in cents'),
    currency: z
      .string()
      .default('usd')
      .describe('Currency code (default: usd)'),
  },
  async ({product_name, price_amount, currency}) => {
    // Initialize the Stripe MCP client
    const stripeClient = new StripeMcpClient();

    try {
      await stripeClient.connect();

      // Create a payment link using the Stripe MCP server
      const paymentLink = await stripeClient.createPaymentLink({
        line_items: [
          {
            price_data: {
              currency: currency,
              product_data: {
                name: product_name,
              },
              unit_amount: price_amount,
            },
            quantity: 1,
          },
        ],
      });

      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify({
              payment_link_url: paymentLink.url,
              payment_link_id: paymentLink.id,
            }),
          },
        ],
      };
    } catch (error) {
      return {
        content: [
          {
            type: 'text',
            text: `Error creating payment link: ${error.message}`,
          },
        ],
        isError: true,
      };
    } finally {
      await stripeClient.close();
    }
  }
);

// Start the server
const transport = new StdioServerTransport();
await server.connect(transport);

配置选项

这 StripeMcpClient 构造函数接受以下选项:

选项 类型 默认值 描述
apiKey string process.env。STRIPE_API_KEY 您的STRIPE API密钥
tools string 'paymentLinks.create' 要启用的条纹工具的逗号分隔列表
stripeAccount string process.env。STRIE_ACCOUNT 可选的STRIPE Connect帐户ID
debug boolean false 启用调试日志记录

api参考

new StripeMcpClient(options)

创建新的客户端实例。

client.connect()

连接到Stripe MCP服务器。如果需要,由其他方法自动调用。

client.createPaymentLink(options)

使用指定选项创建新的付款链接。

参数:

退货:

  • 解析为创建的支付链接对象的Promise

client.close()

关闭与Stripe MCP服务器的连接。

测试

npm test

许可证

麻省理工学院