INTEGRITY Документация

Плата за инструменты MCP

Agents SDK предоставляет paidTool, готовая замена для tool который добавляет требования оплаты x402. Клиенты платят за каждый вызов инструмента, и вы можете сочетать бесплатные и платные инструменты на одном сервере.

Настройка

Оберните ваш McpServer с withX402 и используйте paidTool для инструментов, за использование которых вы хотите взимать плату:

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { McpAgent } from "agents/mcp";
import { withX402, type X402Config } from "agents/x402";
import { z } from "zod";

const X402_CONFIG: X402Config = {
	network: "base",
	recipient: "0xYourWalletAddress",
	facilitator: { url: "https://x402.org/facilitator" }, // Payment facilitator URL
	// To learn more about facilitators: https://docs.x402.org/core-concepts/facilitator
};

export class PaidMCP extends McpAgent<Env> {
	server = withX402(
		new McpServer({ name: "PaidMCP", version: "1.0.0" }),
		X402_CONFIG,
	);

	async init() {
		// Paid tool — $0.01 per call
		this.server.paidTool(
			"square",
			"Squares a number",
			0.01, // USD
			{ number: z.number() },
			{},
			async ({ number }) => {
				return { content: [{ type: "text", text: String(number ** 2) }] };
			},
		);

		// Free tool
		this.server.tool(
			"echo",
			"Echo a message",
			{ message: z.string() },
			async ({ message }) => {
				return { content: [{ type: "text", text: message }] };
			},
		);
	}
}

Конфигурация

Поле Описание
network base для продакшена, base-sepolia для тестирования
recipient Адрес кошелька для получения платежей
facilitator URL фасилитатора платежа (используйте https://x402.org/facilitator)

сигнатура paidTool

this.server.paidTool(
	name, // Tool name
	description, // Tool description
	price, // Price in USD (e.g., 0.01)
	inputSchema, // Zod schema for inputs
	annotations, // MCP annotations
	handler, // Async function that executes the tool
);

Когда клиент вызывает платный инструмент без оплаты, сервер возвращает 402 с требованиями к оплате. Клиент платит через x402, повторяет запрос с подтверждением платежа и получает результат.

Тестирование

Используйте base-sepolia и получите тестовые USDC из Circle faucet ↗.

Полный рабочий пример см. в x402-mcp на GitHub ↗.