gatepay-local-mcp 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/Readme.md +122 -44
  2. package/dist/src/index.js +160 -68
  3. package/dist/src/index.js.map +1 -1
  4. package/dist/src/modes/build-pay-fetch.d.ts +7 -0
  5. package/dist/src/modes/build-pay-fetch.d.ts.map +1 -0
  6. package/dist/src/modes/build-pay-fetch.js +22 -0
  7. package/dist/src/modes/build-pay-fetch.js.map +1 -0
  8. package/dist/src/modes/input-normalizer.d.ts +17 -0
  9. package/dist/src/modes/input-normalizer.d.ts.map +1 -0
  10. package/dist/src/modes/input-normalizer.js +22 -0
  11. package/dist/src/modes/input-normalizer.js.map +1 -0
  12. package/dist/src/modes/local-private-key.d.ts +9 -0
  13. package/dist/src/modes/local-private-key.d.ts.map +1 -0
  14. package/dist/src/modes/local-private-key.js +35 -0
  15. package/dist/src/modes/local-private-key.js.map +1 -0
  16. package/dist/src/modes/plugin-wallet.d.ts +19 -0
  17. package/dist/src/modes/plugin-wallet.d.ts.map +1 -0
  18. package/dist/src/modes/plugin-wallet.js +153 -0
  19. package/dist/src/modes/plugin-wallet.js.map +1 -0
  20. package/dist/src/modes/quick-wallet.d.ts +15 -0
  21. package/dist/src/modes/quick-wallet.d.ts.map +1 -0
  22. package/dist/src/modes/quick-wallet.js +94 -0
  23. package/dist/src/modes/quick-wallet.js.map +1 -0
  24. package/dist/src/modes/registry.d.ts +26 -0
  25. package/dist/src/modes/registry.d.ts.map +1 -0
  26. package/dist/src/modes/registry.js +126 -0
  27. package/dist/src/modes/registry.js.map +1 -0
  28. package/dist/src/modes/signers.d.ts +40 -0
  29. package/dist/src/modes/signers.d.ts.map +1 -0
  30. package/dist/src/modes/signers.js +259 -0
  31. package/dist/src/modes/signers.js.map +1 -0
  32. package/dist/src/modes/types.d.ts +38 -0
  33. package/dist/src/modes/types.d.ts.map +1 -0
  34. package/dist/src/modes/types.js +2 -0
  35. package/dist/src/modes/types.js.map +1 -0
  36. package/dist/src/wallets/auth-token-store.d.ts +17 -0
  37. package/dist/src/wallets/auth-token-store.d.ts.map +1 -0
  38. package/dist/src/wallets/auth-token-store.js +43 -0
  39. package/dist/src/wallets/auth-token-store.js.map +1 -0
  40. package/dist/src/wallets/device-flow-login.d.ts +27 -0
  41. package/dist/src/wallets/device-flow-login.d.ts.map +1 -0
  42. package/dist/src/wallets/device-flow-login.js +235 -0
  43. package/dist/src/wallets/device-flow-login.js.map +1 -0
  44. package/dist/src/wallets/plugin-wallet-client.d.ts +15 -0
  45. package/dist/src/wallets/plugin-wallet-client.d.ts.map +1 -0
  46. package/dist/src/wallets/plugin-wallet-client.js +83 -0
  47. package/dist/src/wallets/plugin-wallet-client.js.map +1 -0
  48. package/dist/src/wallets/wallet-mcp-clients.d.ts +2121 -0
  49. package/dist/src/wallets/wallet-mcp-clients.d.ts.map +1 -0
  50. package/dist/src/wallets/wallet-mcp-clients.js +264 -0
  51. package/dist/src/wallets/wallet-mcp-clients.js.map +1 -0
  52. package/dist/src/x402/client.d.ts.map +1 -0
  53. package/dist/src/x402/client.js.map +1 -0
  54. package/dist/src/x402/exactEvmScheme.d.ts.map +1 -0
  55. package/dist/src/x402/exactEvmScheme.js.map +1 -0
  56. package/dist/src/x402/fetch.d.ts.map +1 -0
  57. package/dist/src/x402/fetch.js.map +1 -0
  58. package/dist/src/x402/gatelayer.d.ts.map +1 -0
  59. package/dist/src/x402/gatelayer.js.map +1 -0
  60. package/dist/src/x402/http.d.ts.map +1 -0
  61. package/dist/src/x402/http.js.map +1 -0
  62. package/dist/src/x402/types.d.ts.map +1 -0
  63. package/dist/src/x402/types.js.map +1 -0
  64. package/dist/src/{x402-standalone → x402}/utils.d.ts +15 -0
  65. package/dist/src/x402/utils.d.ts.map +1 -0
  66. package/dist/src/{x402-standalone → x402}/utils.js +8 -1
  67. package/dist/src/x402/utils.js.map +1 -0
  68. package/package.json +9 -5
  69. package/dist/src/x402-standalone/client.d.ts.map +0 -1
  70. package/dist/src/x402-standalone/client.js.map +0 -1
  71. package/dist/src/x402-standalone/exactEvmScheme.d.ts.map +0 -1
  72. package/dist/src/x402-standalone/exactEvmScheme.js.map +0 -1
  73. package/dist/src/x402-standalone/fetch.d.ts.map +0 -1
  74. package/dist/src/x402-standalone/fetch.js.map +0 -1
  75. package/dist/src/x402-standalone/gatelayer.d.ts.map +0 -1
  76. package/dist/src/x402-standalone/gatelayer.js.map +0 -1
  77. package/dist/src/x402-standalone/http.d.ts.map +0 -1
  78. package/dist/src/x402-standalone/http.js.map +0 -1
  79. package/dist/src/x402-standalone/signer.d.ts +0 -4
  80. package/dist/src/x402-standalone/signer.d.ts.map +0 -1
  81. package/dist/src/x402-standalone/signer.js +0 -38
  82. package/dist/src/x402-standalone/signer.js.map +0 -1
  83. package/dist/src/x402-standalone/types.d.ts.map +0 -1
  84. package/dist/src/x402-standalone/types.js.map +0 -1
  85. package/dist/src/x402-standalone/utils.d.ts.map +0 -1
  86. package/dist/src/x402-standalone/utils.js.map +0 -1
  87. /package/dist/src/{x402-standalone → x402}/client.d.ts +0 -0
  88. /package/dist/src/{x402-standalone → x402}/client.js +0 -0
  89. /package/dist/src/{x402-standalone → x402}/exactEvmScheme.d.ts +0 -0
  90. /package/dist/src/{x402-standalone → x402}/exactEvmScheme.js +0 -0
  91. /package/dist/src/{x402-standalone → x402}/fetch.d.ts +0 -0
  92. /package/dist/src/{x402-standalone → x402}/fetch.js +0 -0
  93. /package/dist/src/{x402-standalone → x402}/gatelayer.d.ts +0 -0
  94. /package/dist/src/{x402-standalone → x402}/gatelayer.js +0 -0
  95. /package/dist/src/{x402-standalone → x402}/http.d.ts +0 -0
  96. /package/dist/src/{x402-standalone → x402}/http.js +0 -0
  97. /package/dist/src/{x402-standalone → x402}/types.d.ts +0 -0
  98. /package/dist/src/{x402-standalone → x402}/types.js +0 -0
package/Readme.md CHANGED
@@ -1,18 +1,32 @@
1
- # gatepay-local-mcp
1
+ # gatepay-local-mcp
2
2
 
3
- A local (stdio) MCP server that sends HTTP requests to **x402 payment-protected** endpoints. On `402 Payment Required`, it creates the payment payload, signs with your EVM key, and retries the request with the payment header. Exposes a single tool: `x402_request`.
3
+ `gatepay-local-mcp` is a local `stdio` MCP server for calling **x402 payment-protected** HTTP endpoints. It exposes a single MCP tool, `x402_request`. When the upstream service returns `402 Payment Required`, the server parses the payment requirements, prepares the payment payload, signs it with the selected signer, and retries the request automatically.
4
4
 
5
5
  ## Features
6
6
 
7
- - **One tool** `x402_request`: request any URL with optional method and JSON body; 402 is handled automatically (parse → sign → retry).
8
- - **No @x402/* deps** x402 logic is implemented in-repo under `x402-standalone/` (EVM exact scheme; supports `eth`, `base`, etc.).
9
- - **Cursor / Claude Desktop** — add the server via `mcp.json` and set `EVM_PRIVATE_KEY` in `env`; no code changes needed.
7
+ - One MCP tool: `x402_request`
8
+ - Built-in x402 payment flow under `src/x402-standalone/`
9
+ - Multiple signing modes via `sign_mode`
10
+ - Works with Cursor, Claude Desktop, and other MCP clients
11
+ - Supports auto-selecting the first ready signing mode
10
12
 
11
- ## Quick Start (Cursor / Claude Desktop)
13
+ ## Signing Modes
12
14
 
13
- ### With authentication (required for x402 payment)
15
+ The server currently registers these signing modes:
14
16
 
15
- `EVM_PRIVATE_KEY` is required; the server will not start without it.
17
+ | `sign_mode` | Status | Description |
18
+ | ------------------- | ----------------------------------- | ------------------------------------------------------------------------ |
19
+ | `local_private_key` | Ready when `EVM_PRIVATE_KEY` is set | Signs locally with your EVM private key |
20
+ | `quick_wallet` | Ready after login | Uses the remote MCP wallet and can trigger device-flow login when needed |
21
+ | `plugin_wallet` | Placeholder | Reserved for future extension, not implemented yet |
22
+
23
+ If `sign_mode` is omitted, the server auto-selects the highest-priority ready mode.
24
+
25
+ ## Quick Start
26
+
27
+ ### Cursor / Claude Desktop with local private key
28
+
29
+ This is the simplest setup if you want local signing:
16
30
 
17
31
  ```json
18
32
  {
@@ -28,9 +42,11 @@ A local (stdio) MCP server that sends HTTP requests to **x402 payment-protected*
28
42
  }
29
43
  ```
30
44
 
31
- Put this in your MCP config (e.g. `~/.cursor/mcp.json`), then restart Cursor or reload MCP. The AI can then call `x402_request` for x402-protected URLs.
45
+ Put this into your MCP config such as `~/.cursor/mcp.json`, then restart the client or reload MCP.
46
+
47
+ ### Cursor / Claude Desktop with quick wallet
32
48
 
33
- ### Optional: debug logging
49
+ If you prefer remote wallet signing, you can omit `EVM_PRIVATE_KEY` and let the tool use `quick_wallet`:
34
50
 
35
51
  ```json
36
52
  {
@@ -39,67 +55,129 @@ Put this in your MCP config (e.g. `~/.cursor/mcp.json`), then restart Cursor or
39
55
  "command": "npx",
40
56
  "args": ["-y", "gatepay-local-mcp"],
41
57
  "env": {
42
- "EVM_PRIVATE_KEY": "your-evm-private-key"
58
+ "MCP_WALLET_API_KEY": "your-api-key",
59
+ "MCP_WALLET_URL": "https://api.gatemcp.ai/mcp/dex"
43
60
  }
44
61
  }
45
62
  }
46
63
  }
47
64
  ```
48
65
 
49
- Use `tail -f /tmp/x402-debug.log` to watch requests and errors.
66
+ When `quick_wallet` has no saved token, the server can start a device-flow login and persist the token at `~/.gate-pay/auth.json`.
50
67
 
51
68
  ## Environment Variables
52
69
 
53
- | Variable | Required | Default | Description |
54
- | -------- | -------- | ------- | ----------- |
55
- | `EVM_PRIVATE_KEY` | **Yes** | — | EVM wallet private key (hex, with or without `0x`) for x402 payment signing |
56
- | `X402_DEBUG_LOG` or `MCP_X402_DEBUG_LOG` | No | — | File path for debug log (append-only; use `tail -f` to inspect) |
70
+ The server loads `.env` from the repository or package root at startup.
71
+
72
+ ### Runtime variables
73
+
74
+ | Variable | Required | Default | Description |
75
+ | -------------------- | -------- | -------------------------------- | --------------------------------------------------------------------------- |
76
+ | `EVM_PRIVATE_KEY` | No | — | Local EVM private key used by `local_private_key`; hex with or without `0x` |
77
+ | `MCP_WALLET_API_KEY` | No | — | API key used when connecting to the remote MCP wallet |
78
+ | `MCP_WALLET_URL` | No | `https://api.gatemcp.ai/mcp/dex` | Remote MCP wallet server URL used by `quick_wallet` |
79
+
80
+ ### Test and script variables
81
+
82
+ | Variable | Used By | Default | Description |
83
+ | ----------------------------- | ------------------------------- | ----------------------- | ----------------------------------------------- |
84
+ | `RESOURCE_SERVER_URL` | `test/privateKey.ts` | `http://localhost:8080` | Base URL for the local private-key flow test |
85
+ | `ENDPOINT_PATH` | `test/privateKey.ts` | `/flight/order` | Endpoint path appended to `RESOURCE_SERVER_URL` |
86
+ | `GATEPAY_MCP_TEST_TIMEOUT_MS` | `test/mcp-x402-request-tool.ts` | `180000` | Timeout for the MCP tool integration test |
57
87
 
58
- ## Available Tools
88
+ ## Available Tool
59
89
 
60
- ### x402_request
90
+ ### `x402_request`
61
91
 
62
- Execute one HTTP request to an x402-protected endpoint. If the server responds with `402 Payment Required`, the tool parses the payment requirements, builds and signs the payment payload, and retries the request with the `PAYMENT-SIGNATURE` header.
92
+ Executes a single HTTP request to an x402-protected endpoint. If the response is `402 Payment Required`, the server completes the payment flow and retries automatically.
93
+
94
+ Use this tool only for endpoints that are expected to require x402 payment.
95
+
96
+ | Argument | Type | Required | Description |
97
+ | ----------------------- | ------ | -------- | ------------------------------------------------------------------------------- |
98
+ | `url` | string | Yes | Full `http` or `https` URL |
99
+ | `method` | string | No | `GET`, `POST`, `PUT`, or `PATCH`; default is `POST` |
100
+ | `body` | string | No | JSON string request body; omit for `GET` |
101
+ | `sign_mode` | string | No | Preferred signing mode: `local_private_key`, `quick_wallet`, or `plugin_wallet` |
102
+ | `wallet_login_provider` | string | No | Login provider for `quick_wallet`: `google` or `gate`; default is `gate` |
103
+
104
+ ### Tool examples
105
+
106
+ GET request with automatic mode selection:
107
+
108
+ ```json
109
+ {
110
+ "url": "https://api.example.com/resource"
111
+ }
112
+ ```
63
113
 
64
- **Use only for endpoints that require payment (402).** Do not use for public or non-402 endpoints.
114
+ POST request with explicit local signing:
65
115
 
66
- | Argument | Type | Required | Description |
67
- | -------- | ---- | -------- | ----------- |
68
- | `url` | string | **Yes** | Full URL of the endpoint (e.g. `http://localhost:8080/flight/order`) |
69
- | `method` | string | No | `GET`, `POST`, `PUT`, or `PATCH`. Default: `POST`. |
70
- | `body` | string | No | JSON string for request body (POST/PUT/PATCH). Omit for GET. |
116
+ ```json
117
+ {
118
+ "url": "https://api.example.com/order",
119
+ "method": "POST",
120
+ "body": "{\"flightId\":\"FL001\",\"uid\":\"100\"}",
121
+ "sign_mode": "local_private_key"
122
+ }
123
+ ```
71
124
 
72
- **Examples (as passed by the client):**
125
+ POST request with quick wallet login through Gate:
73
126
 
74
- - GET: `{ "url": "https://api.example.com/resource" }`
75
- - POST: `{ "url": "https://api.example.com/order", "method": "POST", "body": "{\"flightId\":\"FL001\",\"uid\":\"100\"}" }`
127
+ ```json
128
+ {
129
+ "url": "https://api.example.com/order",
130
+ "method": "POST",
131
+ "body": "{\"flightId\":\"FL001\",\"uid\":\"100\"}",
132
+ "sign_mode": "quick_wallet",
133
+ "wallet_login_provider": "gate"
134
+ }
135
+ ```
76
136
 
77
137
  ## Development
78
138
 
79
139
  ```bash
80
- # Install dependencies
81
- pnpm install
140
+ # install dependencies
141
+ npm install
82
142
 
83
- # Build (output in dist/)
84
- pnpm run build
143
+ # build TypeScript output into dist/
144
+ npm run build
85
145
 
86
- # Run MCP locally (loads .env from package/repo root for EVM_PRIVATE_KEY)
87
- pnpm start
88
- # or without build step
89
- pnpm run dev
146
+ # start the MCP server from source
147
+ npm run dev
90
148
 
91
- # Run the fetch demo (POST to a configurable x402 endpoint)
92
- pnpm run fetch
149
+ # run the built entrypoint through the package start script
150
+ npm start
151
+
152
+ # run unit tests
153
+ npm run test:unit
154
+
155
+ # run the local private key flow test
156
+ npm run test:privateKey
157
+
158
+ # run the MCP tool integration test
159
+ npm run test:mcp-tool
160
+ ```
161
+
162
+ ### Integration test notes
163
+
164
+ `npm run test:mcp-tool` starts `dist/src/index.js` and calls `x402_request` against the configured remote wallet flow. In practice you usually want:
165
+
166
+ ```bash
167
+ npm run build
168
+ MCP_WALLET_API_KEY=your-key npm run test:mcp-tool
93
169
  ```
94
170
 
95
- The fetch demo uses `RESOURCE_SERVER_URL` (default `http://localhost:4021`) and `ENDPOINT_PATH` (default `/weather`); set them in `.env` or the shell. See `test/fetch.ts` to change URL, method, or body.
171
+ If you already logged in before, the saved token in `~/.gate-pay/auth.json` will be reused. Otherwise the quick wallet flow may require interactive device login. You can increase the timeout with `GATEPAY_MCP_TEST_TIMEOUT_MS`.
96
172
 
97
- ## How it works
173
+ ## How It Works
98
174
 
99
- - On first request, the server may respond with `402` and `PAYMENT-REQUIRED` (or a JSON body with payment requirements).
100
- - The client parses requirements, selects a supported scheme/network (e.g. `exact` + `eth`/`base`), builds the EIP-3009–style payload, and signs with `EVM_PRIVATE_KEY`.
101
- - It retries the same request with `PAYMENT-SIGNATURE` (and `Access-Control-Expose-Headers` for `PAYMENT-RESPONSE`).
102
- - The final response (200 or error) and optional `PAYMENT-RESPONSE` header are returned to the caller.
175
+ 1. The MCP client calls `x402_request`.
176
+ 2. The server normalizes the input and selects a ready `sign_mode`.
177
+ 3. The first request is sent to the target URL.
178
+ 4. If the upstream returns `402 Payment Required`, the server parses the payment requirements.
179
+ 5. The selected signer signs the payment payload.
180
+ 6. The server retries the request with the payment header and returns the final response.
103
181
 
104
182
  ## License
105
183
 
package/dist/src/index.js CHANGED
@@ -1,31 +1,20 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
- * x402 stdio bridge standalone, no @x402/* dependencies.
4
- *
5
- * All x402 logic is inlined under x402-standalone/ so this package
6
- * can be published and run via `npx -y gatepay-local-mcp` without
7
- * depending on unpublished @x402/core, @x402/evm, @x402/fetch.
8
- *
9
- * One MCP tool: x402_request
10
- * - url: full URL (required)
11
- * - method: GET | POST | PUT | PATCH (default POST)
12
- * - body: JSON string for request body (POST/PUT/PATCH); omit for GET
13
- *
14
- * Env:
15
- * EVM_PRIVATE_KEY (required; when run via npx, pass via MCP "env" config)
16
- * X402_DEBUG_LOG (optional) path to file — when set, append debug logs here (tail -f to debug)
3
+ * x402 stdio bridge with pluggable sign_mode support.
17
4
  */
18
5
  import { config } from "dotenv";
19
6
  import { existsSync } from "node:fs";
20
- import { fileURLToPath } from "node:url";
21
7
  import { dirname, join } from "node:path";
8
+ import { fileURLToPath } from "node:url";
22
9
  import { Server } from "@modelcontextprotocol/sdk/server/index.js";
23
10
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
24
- import { ListToolsRequestSchema, CallToolRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
25
- import { X402ClientStandalone } from "./x402-standalone/client.js";
26
- import { ExactEvmScheme } from "./x402-standalone/exactEvmScheme.js";
27
- import { createSignerFromPrivateKey } from "./x402-standalone/signer.js";
28
- import { wrapFetchWithPayment } from "./x402-standalone/fetch.js";
11
+ import { CallToolRequestSchema, ListToolsRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
12
+ import { normalizeX402RequestInput } from "./modes/input-normalizer.js";
13
+ import { LocalPrivateKeyMode } from "./modes/local-private-key.js";
14
+ import { PluginWalletMode } from "./modes/plugin-wallet.js";
15
+ import { createSignModeRegistry, formatSignModeSelectionError, } from "./modes/registry.js";
16
+ import { QuickWalletMode } from "./modes/quick-wallet.js";
17
+ import { getMcpClientSync } from "./wallets/wallet-mcp-clients.js";
29
18
  const __dirname = dirname(fileURLToPath(import.meta.url));
30
19
  function findPackageRoot(startDir) {
31
20
  let dir = startDir;
@@ -41,6 +30,7 @@ function findPackageRoot(startDir) {
41
30
  const packageRoot = findPackageRoot(__dirname);
42
31
  config({ path: join(packageRoot, ".env") });
43
32
  const TOOL_NAME = "x402_request";
33
+ const INSUFFICIENT_BALANCE_CODE = "800001001";
44
34
  const INPUT_SCHEMA = {
45
35
  type: "object",
46
36
  properties: {
@@ -54,30 +44,101 @@ const INPUT_SCHEMA = {
54
44
  },
55
45
  body: {
56
46
  type: "string",
57
- description: 'JSON string request body for POST/PUT/PATCH. Omit for GET.',
47
+ description: "JSON string request body for POST/PUT/PATCH. Omit for GET.",
48
+ },
49
+ sign_mode: {
50
+ type: "string",
51
+ description: "Optional preferred signing mode. Omit to auto-select the highest-priority ready mode.",
52
+ enum: ["local_private_key", "quick_wallet", "plugin_wallet"],
53
+ },
54
+ wallet_login_provider: {
55
+ type: "string",
56
+ description: "When quick_wallet needs login: OAuth provider. google = Google account, gate = Gate account. Defaults to gate.",
57
+ enum: ["google", "gate"],
58
58
  },
59
59
  },
60
60
  required: ["url"],
61
61
  };
62
62
  const TOOL_DESCRIPTION = "Execute a single HTTP request with automatic x402 payment on 402. Use ONLY for endpoints that require payment (402). " +
63
- "Pass full url and JSON body string as documented in the Skill. Do not use for plain/public list endpoints.";
64
- async function main() {
65
- const rawEvmKey = process.env.EVM_PRIVATE_KEY?.trim();
66
- if (!rawEvmKey) {
67
- console.error("❌ EVM_PRIVATE_KEY is required (wallet private key for x402 payment)");
68
- process.exit(1);
63
+ "Set sign_mode to choose a signing mode, or omit it to auto-select the highest-priority ready mode.";
64
+ function parsePossiblyNestedJson(text) {
65
+ try {
66
+ const parsed = JSON.parse(text);
67
+ if (typeof parsed === "string") {
68
+ try {
69
+ return JSON.parse(parsed);
70
+ }
71
+ catch {
72
+ return parsed;
73
+ }
74
+ }
75
+ return parsed;
76
+ }
77
+ catch {
78
+ return undefined;
79
+ }
80
+ }
81
+ function containsInsufficientBalanceSignal(message) {
82
+ return message.toLowerCase().includes("insufficient balance");
83
+ }
84
+ async function buildInsufficientBalanceReply(baseMessage) {
85
+ const mcp = getMcpClientSync();
86
+ if (!mcp || !mcp.isAuthenticated()) {
87
+ return [
88
+ "支付失败:检测到余额不足。",
89
+ `原始信息: ${baseMessage}`,
90
+ "当前无法获取钱包余额(未检测到已登录的托管钱包会话)。",
91
+ ].join("\n");
92
+ }
93
+ try {
94
+ const tokenListResult = await mcp.walletGetTokenList();
95
+ const content = tokenListResult.content;
96
+ const first = Array.isArray(content)
97
+ ? content[0]
98
+ : undefined;
99
+ const balances = first?.type === "text" && typeof first.text === "string"
100
+ ? parsePossiblyNestedJson(first.text) ?? first.text
101
+ : tokenListResult;
102
+ return JSON.stringify({
103
+ code: Number(INSUFFICIENT_BALANCE_CODE),
104
+ message: "余额不足,已返回当前钱包余额信息",
105
+ originalMessage: baseMessage,
106
+ walletBalances: balances,
107
+ }, null, 2);
108
+ }
109
+ catch (error) {
110
+ return [
111
+ "支付失败:检测到余额不足。",
112
+ `原始信息: ${baseMessage}`,
113
+ `查询钱包余额失败: ${error instanceof Error ? error.message : String(error)}`,
114
+ ].join("\n");
115
+ }
116
+ }
117
+ function buildRequestInit(method, body) {
118
+ if (method === "GET") {
119
+ return { method: "GET" };
120
+ }
121
+ if (method === "POST" || method === "PUT" || method === "PATCH") {
122
+ if (body && body.trim()) {
123
+ JSON.parse(body);
124
+ }
125
+ return {
126
+ method,
127
+ headers: { "Content-Type": "application/json" },
128
+ body: body && body.trim() ? body : undefined,
129
+ };
69
130
  }
70
- const evmPrivateKey = (rawEvmKey.startsWith("0x") ? rawEvmKey : `0x${rawEvmKey}`);
71
- const evmSigner = createSignerFromPrivateKey(evmPrivateKey);
72
- const client = new X402ClientStandalone();
73
- client.register("gatelayer_testnet", new ExactEvmScheme(evmSigner));
74
- client.register("eth", new ExactEvmScheme(evmSigner));
75
- client.register("base", new ExactEvmScheme(evmSigner));
76
- client.register("Polygon", new ExactEvmScheme(evmSigner));
77
- client.register("gatelayer", new ExactEvmScheme(evmSigner));
78
- client.register("gatechain", new ExactEvmScheme(evmSigner));
79
- client.register("Arbitrum One", new ExactEvmScheme(evmSigner));
80
- const fetchWithPayment = wrapFetchWithPayment(fetch, client);
131
+ throw new Error(`不支持的 method: ${method}`);
132
+ }
133
+ async function main() {
134
+ const quickWalletMcpUrl = process.env.MCP_WALLET_URL ?? "https://api.gatemcp.ai/mcp/dex";
135
+ const quickWalletApiKey = process.env.MCP_WALLET_API_KEY;
136
+ const pluginWalletServerUrl = process.env.PLUGIN_WALLET_SERVER_URL ?? "https://walletmcp-test.gateweb3.cc/mcp";
137
+ const signModeRegistry = createSignModeRegistry([
138
+ new LocalPrivateKeyMode(),
139
+ new QuickWalletMode({ mcpWalletUrl: quickWalletMcpUrl, mcpApiKey: quickWalletApiKey }),
140
+ new PluginWalletMode({ serverUrl: pluginWalletServerUrl }),
141
+ ]);
81
142
  const server = new Server({
82
143
  name: "x402 Paid Request Bridge (standalone)",
83
144
  version: "1.0.0",
@@ -100,67 +161,98 @@ async function main() {
100
161
  isError: true,
101
162
  };
102
163
  }
103
- const params = (args ?? {});
104
- const url = String(params.url ?? "").trim();
105
- if (!url || !url.startsWith("http")) {
164
+ const normalized = normalizeX402RequestInput((args ?? {}));
165
+ if (!normalized.url || !normalized.url.startsWith("http")) {
106
166
  return {
107
167
  content: [{ type: "text", text: "缺少或无效参数 url(需完整 http/https URL)。" }],
108
168
  isError: true,
109
169
  };
110
170
  }
111
- const method = String(params.method ?? "POST").trim().toUpperCase() || "POST";
112
- const bodyStr = params.body != null ? String(params.body) : "";
171
+ let payFetch;
172
+ try {
173
+ const selectedMode = await signModeRegistry.selectMode(normalized.signMode);
174
+ payFetch = await signModeRegistry.getOrCreatePayFetch(selectedMode.mode, {
175
+ walletLoginProvider: normalized.walletLoginProvider,
176
+ });
177
+ }
178
+ catch (error) {
179
+ return {
180
+ content: [{ type: "text", text: formatSignModeSelectionError(error) }],
181
+ isError: true,
182
+ };
183
+ }
113
184
  try {
114
185
  let init;
115
- if (method === "GET") {
116
- init = { method: "GET" };
117
- }
118
- else if (method === "POST" || method === "PUT" || method === "PATCH") {
119
- if (bodyStr && bodyStr.trim()) {
120
- try {
121
- JSON.parse(bodyStr);
122
- }
123
- catch {
124
- return {
125
- content: [{ type: "text", text: "body 必须是合法 JSON 字符串。" }],
126
- isError: true,
127
- };
128
- }
129
- }
130
- init = {
131
- method,
132
- headers: { "Content-Type": "application/json" },
133
- body: bodyStr && bodyStr.trim() ? bodyStr : undefined,
134
- };
186
+ try {
187
+ init = buildRequestInit(normalized.method, normalized.body);
135
188
  }
136
- else {
189
+ catch (error) {
137
190
  return {
138
- content: [{ type: "text", text: `不支持的 method: ${method}` }],
191
+ content: [{ type: "text", text: error instanceof Error ? error.message : String(error) }],
139
192
  isError: true,
140
193
  };
141
194
  }
142
- const response = await fetchWithPayment(url, init);
195
+ const response = await payFetch(normalized.url, init);
143
196
  const responseText = await response.text();
144
197
  let text;
145
198
  try {
146
199
  const json = JSON.parse(responseText);
147
- text = json.data != null ? JSON.stringify(json.data, null, 2) : JSON.stringify(json, null, 2);
200
+ text =
201
+ json.data != null
202
+ ? JSON.stringify(json.data, null, 2)
203
+ : JSON.stringify(json, null, 2);
148
204
  }
149
205
  catch {
150
206
  text = responseText;
151
207
  }
208
+ const insufficientBalance = containsInsufficientBalanceSignal(responseText) ||
209
+ containsInsufficientBalanceSignal(text);
152
210
  if (!response.ok && response.status !== 402) {
211
+ if (insufficientBalance) {
212
+ return {
213
+ content: [
214
+ {
215
+ type: "text",
216
+ text: await buildInsufficientBalanceReply(text),
217
+ },
218
+ ],
219
+ isError: true,
220
+ };
221
+ }
153
222
  return {
154
223
  content: [{ type: "text", text: `HTTP ${response.status}: ${text}` }],
155
224
  isError: true,
156
225
  };
157
226
  }
227
+ // 上游常以 HTTP 200 + 业务 JSON(message 含 insufficient balance)表示余额不足,需同样拉取钱包余额
228
+ if (insufficientBalance) {
229
+ return {
230
+ content: [
231
+ {
232
+ type: "text",
233
+ text: await buildInsufficientBalanceReply(text),
234
+ },
235
+ ],
236
+ isError: true,
237
+ };
238
+ }
158
239
  return { content: [{ type: "text", text }], isError: false };
159
240
  }
160
241
  catch (err) {
161
242
  const message = err instanceof Error ? err.message : String(err);
243
+ if (containsInsufficientBalanceSignal(message)) {
244
+ return {
245
+ content: [
246
+ {
247
+ type: "text",
248
+ text: await buildInsufficientBalanceReply(message),
249
+ },
250
+ ],
251
+ isError: true,
252
+ };
253
+ }
162
254
  const hint = message.toLowerCase().includes("fetch") || message.toLowerCase().includes("econnrefused")
163
- ? " 请确认 url 可访问;402 支付需 EVM_PRIVATE_KEY 对应钱包有足够余额。"
255
+ ? " 请确认 url 可访问;402 支付需托管钱包已登录且有足够余额。"
164
256
  : "";
165
257
  return {
166
258
  content: [{ type: "text", text: `请求失败: ${message}.${hint}` }],
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAqB,UAAU,EAAE,MAAM,SAAS,CAAC;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EACL,sBAAsB,EACtB,qBAAqB,GACtB,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,MAAM,qCAAqC,CAAC;AACrE,OAAO,EAAE,0BAA0B,EAAE,MAAM,6BAA6B,CAAC;AACzE,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAElE,MAAM,SAAS,GAAG,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAE1D,SAAS,eAAe,CAAC,QAAgB;IACvC,IAAI,GAAG,GAAG,QAAQ,CAAC;IACnB,SAAS,CAAC;QACR,IAAI,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,cAAc,CAAC,CAAC;YAAE,OAAO,GAAG,CAAC;QACtD,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;QAC/B,IAAI,MA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E,MAAe;4BACrB,IAAI,EAAE,MAAM,6BAA6B,CAAC,OAAO,CAAC;yBACnD;qBACF;oBACD,OAAO,EAAE,IAAI;iBACd,CAAC;YACJ,CAAC;YACD,MAAM,IAAI,GACN,OAAO,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,cAAc,CAAC;gBACrF,CAAC,CAAC,oCAAoC;gBACtC,CAAC,CAAC,EAAE,CAAC;YACb,OAAO;gBACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,SAAS,OAAO,IAAI,IAAI,EAAE,EAAE,CAAC;gBACtE,OAAO,EAAE,IAAI;aACd,CAAC;QACJ,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,MAAM,KAAK,GAAG,IAAI,oBAAoB,EAAE,CAAC;IACzC,MAAM,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9B,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;IACnB,OAAO,CAAC,KAAK,CAAC,cAAc,EAAE,GAAG,CAAC,CAAC;IACnC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { PayFetchFactory } from "./types.js";
2
+ export declare class DefaultPayFetchFactory implements PayFetchFactory {
3
+ build({ signer }: {
4
+ signer: import("../x402/types.js").ClientEvmSigner;
5
+ }): typeof fetch;
6
+ }
7
+ //# sourceMappingURL=build-pay-fetch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"build-pay-fetch.d.ts","sourceRoot":"","sources":["../../../src/modes/build-pay-fetch.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAYlD,qBAAa,sBAAuB,YAAW,eAAe;IAC5D,KAAK,CAAC,EAAE,MAAM,EAAE,EAAE;QAAE,MAAM,EAAE,OAAO,kBAAkB,EAAE,eAAe,CAAA;KAAE,GAAG,OAAO,KAAK;CAOxF"}
@@ -0,0 +1,22 @@
1
+ import { X402ClientStandalone } from "../x402/client.js";
2
+ import { ExactEvmScheme } from "../x402/exactEvmScheme.js";
3
+ import { wrapFetchWithPayment } from "../x402/fetch.js";
4
+ const SUPPORTED_NETWORKS = [
5
+ "gatelayer_testnet",
6
+ "eth",
7
+ "base",
8
+ "Polygon",
9
+ "gatelayer",
10
+ "gatechain",
11
+ "Arbitrum One",
12
+ ];
13
+ export class DefaultPayFetchFactory {
14
+ build({ signer }) {
15
+ const client = new X402ClientStandalone();
16
+ for (const network of SUPPORTED_NETWORKS) {
17
+ client.register(network, new ExactEvmScheme(signer));
18
+ }
19
+ return wrapFetchWithPayment(fetch, client);
20
+ }
21
+ }
22
+ //# sourceMappingURL=build-pay-fetch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"build-pay-fetch.js","sourceRoot":"","sources":["../../../src/modes/build-pay-fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAGxD,MAAM,kBAAkB,GAAG;IACzB,mBAAmB;IACnB,KAAK;IACL,MAAM;IACN,SAAS;IACT,WAAW;IACX,WAAW;IACX,cAAc;CACN,CAAC;AAEX,MAAM,OAAO,sBAAsB;IACjC,KAAK,CAAC,EAAE,MAAM,EAA0D;QACtE,MAAM,MAAM,GAAG,IAAI,oBAAoB,EAAE,CAAC;QAC1C,KAAK,MAAM,OAAO,IAAI,kBAAkB,EAAE,CAAC;YACzC,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC;QACvD,CAAC;QACD,OAAO,oBAAoB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAC7C,CAAC;CACF"}
@@ -0,0 +1,17 @@
1
+ import type { SignModeId } from "./types.js";
2
+ export interface RawX402RequestInput {
3
+ url?: unknown;
4
+ method?: unknown;
5
+ body?: unknown;
6
+ sign_mode?: unknown;
7
+ wallet_login_provider?: unknown;
8
+ }
9
+ export interface NormalizedX402RequestInput {
10
+ url: string;
11
+ method: string;
12
+ body?: string;
13
+ signMode?: SignModeId | string;
14
+ walletLoginProvider: "google" | "gate";
15
+ }
16
+ export declare function normalizeX402RequestInput(input: RawX402RequestInput): NormalizedX402RequestInput;
17
+ //# sourceMappingURL=input-normalizer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"input-normalizer.d.ts","sourceRoot":"","sources":["../../../src/modes/input-normalizer.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,MAAM,WAAW,mBAAmB;IAClC,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,qBAAqB,CAAC,EAAE,OAAO,CAAC;CACjC;AAED,MAAM,WAAW,0BAA0B;IACzC,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,UAAU,GAAG,MAAM,CAAC;IAC/B,mBAAmB,EAAE,QAAQ,GAAG,MAAM,CAAC;CACxC;AAQD,wBAAgB,yBAAyB,CACvC,KAAK,EAAE,mBAAmB,GACzB,0BAA0B,CAgB5B"}