Repository navigation
fix(x402): read paidTool payments from MCP SDK v2 handler context #2494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "agents": patch | ||
| --- | ||
|
|
||
| `withX402(...).paidTool` now reads the payment from MCP SDK v2 servers, and matches the `PAYMENT-SIGNATURE` / `X-PAYMENT` headers case-insensitively. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { McpServer as LegacyMcpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
| import type { RegisteredTool as LegacyRegisteredTool } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
| import { | ||
| McpServer, | ||
| type RegisteredTool, | ||
| type ServerContext | ||
| } from "@modelcontextprotocol/server"; | ||
| import { withX402, type X402Config } from "agents/x402"; | ||
| import { z } from "zod"; | ||
|
|
||
| const cfg: X402Config = { | ||
| network: "base-sepolia", | ||
| recipient: "0x000000000000000000000000000000000000dEaD" | ||
| }; | ||
|
|
||
| // SDK v2: the callback receives the v2 ServerContext. | ||
| const v2 = withX402(new McpServer({ name: "v2", version: "1.0.0" }), cfg); | ||
| const v2Tool: RegisteredTool = v2.paidTool( | ||
| "t", | ||
| "d", | ||
| 0.01, | ||
| { a: z.string() }, | ||
| {}, | ||
| async ({ a }, ctx) => { | ||
| const typedCtx: ServerContext = ctx; | ||
| void typedCtx; | ||
| return { content: [{ type: "text", text: a }] }; | ||
| } | ||
| ); | ||
| void v2Tool; | ||
|
|
||
| // SDK v1: a direct call keeps the v1 types. | ||
| const v1 = withX402(new LegacyMcpServer({ name: "v1", version: "1.0.0" }), cfg); | ||
| const v1Tool: LegacyRegisteredTool = v1.paidTool( | ||
| "t", | ||
| "d", | ||
| 0.01, | ||
| { a: z.string() }, | ||
| {}, | ||
| async ({ a }) => ({ content: [{ type: "text", text: a }] }) | ||
| ); | ||
| void v1Tool; | ||
|
|
||
| // SDK v1: a generic wrapper still resolves the callback type. | ||
| function register<T extends LegacyMcpServer>(server: T) { | ||
| return withX402(server, cfg).paidTool( | ||
| "t", | ||
| "d", | ||
| 0.01, | ||
| { a: z.string() }, | ||
| {}, | ||
| async ({ a }, extra) => { | ||
| void extra.signal; | ||
| return { content: [{ type: "text", text: a }] }; | ||
| } | ||
| ); | ||
| } | ||
| void register; | ||
|
|
||
| // SDK v1: a field typed from ReturnType keeps the v1 paidTool. | ||
| declare const fromReturnType: ReturnType<typeof withX402>; | ||
| fromReturnType.paidTool( | ||
| "t", | ||
| "d", | ||
| 0.01, | ||
| { a: z.string() }, | ||
| {}, | ||
| async ({ a }) => ({ content: [{ type: "text", text: a }] }) | ||
| ); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.