nftdata-mcp 1.0.0 → 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.
- package/README.md +241 -50
- package/dist/lib.js +24 -5
- package/dist/server.js +29 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,51 +1,252 @@
|
|
|
1
|
-
#
|
|
1
|
+
# NFT Data MCP
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A multichain NFT data MCP server that gives AI assistants, agents, and other MCP-compatible clients access to the [NFT Data API](https://www.nftdata.app/) through a single tool: `get_collection_data`.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Compatible with Claude Desktop and other MCP-compatible applications, AI agents, and clients, including future clients that support the Model Context Protocol (MCP).
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Each API request is paid automatically at **$0.002 in USDC on Base** using the [x402 payment protocol](https://docs.x402.org/).
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Retrieve NFT collection data across 30+ blockchain networks through the NFT Data API's OpenSea integration, including Ethereum, Base, Ink, Arc, HyperEVM, Robinhood Chain, Solana, and more.
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
- **npm:** https://www.npmjs.com/package/nftdata-mcp
|
|
12
|
+
- **MCP Registry:** https://registry.modelcontextprotocol.io/?q=nftdata
|
|
13
|
+
- **NFT Data GitHub:** https://github.com/Nomad07/nft-data
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
---
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
## Prerequisites
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
- **Node.js 20 or newer** (required by the x402 SDK)
|
|
20
|
+
- **A dedicated wallet** funded with a small amount of USDC on Base mainnet
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
- A wallet private key funded with a small amount of USDC on Base
|
|
22
|
+
> Always use a dedicated wallet with only a small balance. Never use your primary wallet or share its private key.
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
---
|
|
25
25
|
|
|
26
|
-
##
|
|
26
|
+
## Claude Desktop setup
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
Add to `claude_desktop_config.json`:
|
|
28
|
+
Add the following block to `claude_desktop_config.json` (usually found in `~/Library/Application Support/Claude/` on macOS or `%APPDATA%\Claude\` on Windows):
|
|
31
29
|
|
|
32
30
|
```json
|
|
33
31
|
{
|
|
34
32
|
"mcpServers": {
|
|
35
33
|
"nftdata": {
|
|
36
34
|
"command": "npx",
|
|
37
|
-
"args": ["-y", "nftdata-mcp"],
|
|
35
|
+
"args": ["-y", "nftdata-mcp@1.0.2"],
|
|
38
36
|
"env": {
|
|
39
|
-
"EVM_PRIVATE_KEY": "
|
|
37
|
+
"EVM_PRIVATE_KEY": "YOUR_EVM_PRIVATE_KEY"
|
|
40
38
|
}
|
|
41
39
|
}
|
|
42
40
|
}
|
|
43
41
|
}
|
|
44
42
|
```
|
|
45
43
|
|
|
46
|
-
|
|
44
|
+
Replace `YOUR_EVM_PRIVATE_KEY` with the private key of your dedicated wallet (`0x` followed by 64 hex characters). Then restart Claude Desktop.
|
|
45
|
+
|
|
46
|
+
The server communicates over **stdio** and is launched automatically by Claude Desktop. Running it by hand shows no output; it waits for an MCP client to connect.
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## First query
|
|
51
|
+
|
|
52
|
+
After restarting Claude Desktop:
|
|
53
|
+
|
|
54
|
+
1. Open a new conversation.
|
|
55
|
+
2. To make sure Claude selects the right server when you have multiple MCP servers configured, start with an explicit request:
|
|
56
|
+
|
|
57
|
+
> Use the NFT Data MCP server to look up an NFT collection.
|
|
58
|
+
|
|
59
|
+
3. When Claude asks for a collection slug, enter:
|
|
60
|
+
|
|
61
|
+
> Azuki
|
|
62
|
+
|
|
63
|
+
4. Continue with natural follow-up questions. If Claude does not use the NFT Data server, ask it explicitly to do so.
|
|
64
|
+
|
|
65
|
+
You can also ask directly by collection name or OpenSea slug. Claude will retrieve the latest collection statistics, including floor price, sales volume, owner count, supply, and lifetime metrics. Each successful request includes a payment transaction hash and a BaseScan verification link.
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
## Example: Azuki collection report
|
|
70
|
+
|
|
71
|
+
The following is an example response from one query. Market data changes continuously; these values are from a specific query and are not current live data.
|
|
72
|
+
|
|
73
|
+
**Collection overview**
|
|
74
|
+
|
|
75
|
+
| Field | Value |
|
|
76
|
+
|---|---|
|
|
77
|
+
| Name | Azuki |
|
|
78
|
+
| Slug | `azuki` |
|
|
79
|
+
| Chain | Ethereum |
|
|
80
|
+
| Contract | `0xed5af388653567af2f388e6224dc7c4b3241c544` |
|
|
81
|
+
| Created | January 12, 2022 |
|
|
82
|
+
| Total supply | 10,000 |
|
|
83
|
+
| Unique owners | 4,394 |
|
|
84
|
+
| Floor price | 0.4933 ETH |
|
|
85
|
+
|
|
86
|
+
**Market activity**
|
|
87
|
+
|
|
88
|
+
| Period | Sales | Volume |
|
|
89
|
+
|---|---|---|
|
|
90
|
+
| 24 hours | 21 | 10.15 ETH |
|
|
91
|
+
| 7 days | 163 | 96.68 ETH |
|
|
92
|
+
| 30 days | 471 | 325.72 ETH |
|
|
93
|
+
| Lifetime | 93,156 | 831,992.65 ETH |
|
|
94
|
+
|
|
95
|
+
**Payment**
|
|
96
|
+
|
|
97
|
+
| Field | Value |
|
|
98
|
+
|---|---|
|
|
99
|
+
| Cost | $0.002 |
|
|
100
|
+
| Protocol | x402 |
|
|
101
|
+
| Transaction | [`0x4fb3...580f8`](https://basescan.org/tx/0x4fb35acc31a9f6841647ca2533a7f9ac7113f54364e979b8de4786bf1de580f8) |
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## Customize your results
|
|
106
|
+
|
|
107
|
+
The server supplies the underlying collection data returned by the API. Claude can organize that information in the format you prefer, from a concise summary to a detailed report, tables, or comparisons. Simply ask Claude how you want the results presented. This changes the presentation, not the source values.
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
## Raw request and response
|
|
112
|
+
|
|
113
|
+
Claude Desktop may show the tool request and returned data through its tool-activity details (the exact label varies by client version).
|
|
114
|
+
|
|
115
|
+
**Tool request**
|
|
116
|
+
|
|
117
|
+
```json
|
|
118
|
+
{ "collection": "azuki" }
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
<details>
|
|
122
|
+
<summary>Full raw response</summary>
|
|
123
|
+
|
|
124
|
+
```json
|
|
125
|
+
{
|
|
126
|
+
"name": "Azuki",
|
|
127
|
+
"slug": "azuki",
|
|
128
|
+
"contracts": [
|
|
129
|
+
{
|
|
130
|
+
"address": "0xed5af388653567af2f388e6224dc7c4b3241c544",
|
|
131
|
+
"chain": "ethereum"
|
|
132
|
+
}
|
|
133
|
+
],
|
|
134
|
+
"floor_price": 0.49328599,
|
|
135
|
+
"floor_price_currency": "ETH",
|
|
136
|
+
"one_day_sales": 21,
|
|
137
|
+
"one_day_volume": 10.150091000000002,
|
|
138
|
+
"seven_day_sales": 163,
|
|
139
|
+
"seven_day_volume": 96.67526161112121,
|
|
140
|
+
"thirty_day_sales": 471,
|
|
141
|
+
"thirty_day_volume": 325.72023569476767,
|
|
142
|
+
"total_sales": 93156,
|
|
143
|
+
"total_volume": 831992.6461159982,
|
|
144
|
+
"num_owners": 4394,
|
|
145
|
+
"total_supply": 10000,
|
|
146
|
+
"created_date": "2022-01-12",
|
|
147
|
+
"one_day_volume_currency": "ETH",
|
|
148
|
+
"seven_day_volume_currency": "ETH",
|
|
149
|
+
"thirty_day_volume_currency": "ETH",
|
|
150
|
+
"total_volume_currency": "ETH"
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Raw floating-point values have more decimal places than the rounded values in the report above.
|
|
155
|
+
|
|
156
|
+
</details>
|
|
157
|
+
|
|
158
|
+
**Payment verification**
|
|
159
|
+
|
|
160
|
+
Transaction hash: `0x4fb35acc31a9f6841647ca2533a7f9ac7113f54364e979b8de4786bf1de580f8`
|
|
161
|
+
|
|
162
|
+
Verify on BaseScan: https://basescan.org/tx/0x4fb35acc31a9f6841647ca2533a7f9ac7113f54364e979b8de4786bf1de580f8
|
|
163
|
+
|
|
164
|
+
---
|
|
47
165
|
|
|
48
|
-
|
|
166
|
+
## Data networks vs. payment network
|
|
167
|
+
|
|
168
|
+
### Payment Protocols and Supported Networks
|
|
169
|
+
|
|
170
|
+
| Product | Payment Protocols | Payment Networks |
|
|
171
|
+
|---|---|---|
|
|
172
|
+
| NFT Data (website/API) | x402 and MPP | x402 - Base and Solana; MPP - Base |
|
|
173
|
+
| NFT Data (MCP server) | x402 | Base |
|
|
174
|
+
|
|
175
|
+
> **Note:** The NFT Data website/API and the NFT Data MCP server have different payment capabilities. The MCP server currently supports x402 payments on Base only; MPP support is not implemented in this package.
|
|
176
|
+
|
|
177
|
+
**Data network** refers to the blockchain where an NFT collection exists. NFT Data is a multichain service supporting NFT collections across **30+ blockchain networks** through the NFT Data API's OpenSea integration, including Ethereum, Base, Ink, Arc, HyperEVM, Robinhood Chain, and Solana, among others. OpenSea continues to expand its supported blockchain networks, so coverage may grow over time. The data network is independent of the payment network used to pay for API requests.
|
|
178
|
+
|
|
179
|
+
**Payment network** refers to the blockchain used to pay for each API request. This server pays on **Base mainnet only**, using USDC via x402, regardless of which chain the queried collection is on.
|
|
180
|
+
|
|
181
|
+
---
|
|
182
|
+
|
|
183
|
+
## x402 and payment verification
|
|
184
|
+
|
|
185
|
+
Each request is paid automatically using the [x402 protocol](https://docs.x402.org/) over Base mainnet. The payment flow:
|
|
186
|
+
|
|
187
|
+
1. The server sends the API request.
|
|
188
|
+
2. The API responds with HTTP 402 and a payment challenge.
|
|
189
|
+
3. Before signing, the server validates the challenge:
|
|
190
|
+
- Scheme: `exact`
|
|
191
|
+
- Network: Base mainnet (`eip155:8453`)
|
|
192
|
+
- Token: USDC on Base (`0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913`)
|
|
193
|
+
- Amount: exactly 2000 atomic units ($0.002)
|
|
194
|
+
- Recipient: `0x344143199642e87320785823c20c2df107d17534`
|
|
195
|
+
4. If everything matches, the wallet signs and submits the payment.
|
|
196
|
+
5. The API fulfills the request and returns the collection data along with the on-chain transaction hash.
|
|
197
|
+
|
|
198
|
+
Every successful response includes:
|
|
199
|
+
|
|
200
|
+
```
|
|
201
|
+
Payment: 0x...
|
|
202
|
+
Verify: https://basescan.org/tx/0x...
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
This server supports **x402 on Base only**. Solana and MPP payment methods are not implemented.
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
## Configuration
|
|
210
|
+
|
|
211
|
+
| Variable | Required | Default | Description |
|
|
212
|
+
|---|---|---|---|
|
|
213
|
+
| `EVM_PRIVATE_KEY` | yes | - | Private key (`0x` + 64 hex chars) of the wallet that pays for calls |
|
|
214
|
+
| `NFTDATA_MAX_REQUESTS` | no | `100` | Max signed payments per session. Integer 1–1000; any other value stops startup with an error |
|
|
215
|
+
| `NFTDATA_TIMEOUT_MS` | no | `15000` | Request timeout in milliseconds (1000–60000) |
|
|
216
|
+
| `NFTDATA_URL` | no | `https://www.nftdata.app/api/get-collection-data` | Accepts only the official NFT Data endpoint; anything else stops startup |
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
## Behavior
|
|
221
|
+
|
|
222
|
+
- Sends `POST /api/get-collection-data` with body `{"collection": "<slug>"}`.
|
|
223
|
+
- The slug is trimmed and lowercased, then must match `^[a-z0-9][a-z0-9_-]*$` (max 100 characters); otherwise no request is made.
|
|
224
|
+
- Each signed payment counts against `NFTDATA_MAX_REQUESTS`, including payments for requests that later fail or time out.
|
|
225
|
+
- No automatic retries: one tool call makes at most one payment attempt.
|
|
226
|
+
- Successful responses are cached for 5 minutes per slug. Concurrent identical calls share a single in-flight request.
|
|
227
|
+
- Cache hits do not include a payment transaction hash (no new payment was made).
|
|
228
|
+
- Returns generic errors for `400` (invalid slug), `404` (collection not found), `429` (rate limit, with retry delay), timeouts, and other failures. Server and payment details are not exposed to the MCP client.
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
## Troubleshooting
|
|
233
|
+
|
|
234
|
+
**Server does not appear in Claude Desktop**
|
|
235
|
+
Verify the `claude_desktop_config.json` path, check for JSON syntax errors, and restart Claude Desktop fully.
|
|
236
|
+
|
|
237
|
+
**`EVM_PRIVATE_KEY is missing or malformed`**
|
|
238
|
+
The key must be `0x` followed by exactly 64 hex characters. Check for extra whitespace or a missing `0x` prefix.
|
|
239
|
+
|
|
240
|
+
**`paid request limit ... reached`**
|
|
241
|
+
The session has reached `NFTDATA_MAX_REQUESTS`. Restart Claude Desktop to reset the counter, or set a higher limit.
|
|
242
|
+
|
|
243
|
+
**Request timed out**
|
|
244
|
+
A payment may have been signed before the timeout. The server does not retry automatically.
|
|
245
|
+
|
|
246
|
+
**Imports fail after an SDK update**
|
|
247
|
+
The x402 SDK changes frequently. Check the [x402 docs](https://docs.x402.org/) and the [nftdata-mcp changelog](https://github.com/Nomad07/nftdata-mcp/releases) for the latest compatible version.
|
|
248
|
+
|
|
249
|
+
---
|
|
49
250
|
|
|
50
251
|
## Run from source
|
|
51
252
|
|
|
@@ -73,44 +274,34 @@ set EVM_PRIVATE_KEY=0x...
|
|
|
73
274
|
npm start
|
|
74
275
|
```
|
|
75
276
|
|
|
76
|
-
|
|
277
|
+
To use a local checkout with Claude Desktop, set `"command": "npx"` and `"args": ["tsx", "/absolute/path/to/nftdata-mcp/server.ts"]`.
|
|
77
278
|
|
|
78
|
-
|
|
279
|
+
---
|
|
79
280
|
|
|
80
|
-
## Build
|
|
281
|
+
## Build and test
|
|
81
282
|
|
|
82
283
|
```bash
|
|
83
|
-
npm run build # compiles
|
|
284
|
+
npm run build # compiles to dist/
|
|
84
285
|
npm test # offline unit tests, no network and no payments
|
|
85
286
|
```
|
|
86
287
|
|
|
87
|
-
|
|
288
|
+
---
|
|
88
289
|
|
|
89
|
-
|
|
90
|
-
|------------------------|----------|---------|-------------|
|
|
91
|
-
| `EVM_PRIVATE_KEY` | yes | – | Private key (`0x` + 64 hex chars) of the wallet that pays for calls |
|
|
92
|
-
| `NFTDATA_MAX_REQUESTS` | no | `100` | Max signed payments per session. Integer 1–1000; any other value stops startup with an error |
|
|
93
|
-
| `NFTDATA_TIMEOUT_MS` | no | `15000` | Request timeout in milliseconds (1000–60000) |
|
|
94
|
-
| `NFTDATA_URL` | no | `https://www.nftdata.app/api/get-collection-data` | Accepts only the official NFT Data endpoint (`www.nftdata.app` or `nftdata.app`); anything else stops startup |
|
|
290
|
+
## Links
|
|
95
291
|
|
|
96
|
-
|
|
292
|
+
- [NFT Data website](https://www.nftdata.app/)
|
|
293
|
+
- [NFT Data API docs](https://www.nftdata.app/docs)
|
|
294
|
+
- [NFT Data OpenAPI spec](https://www.nftdata.app/openapi.json)
|
|
295
|
+
- [x402 protocol docs](https://docs.x402.org/)
|
|
296
|
+
- [BaseScan](https://basescan.org/)
|
|
297
|
+
- [nftdata-mcp on npm](https://www.npmjs.com/package/nftdata-mcp)
|
|
298
|
+
- [nftdata-mcp in the MCP Registry](https://registry.modelcontextprotocol.io/?q=nftdata)
|
|
97
299
|
|
|
98
|
-
|
|
99
|
-
- The slug is trimmed and lowercased, then must match `^[a-z0-9][a-z0-9_-]*$` (max 100 characters); otherwise no request is made.
|
|
100
|
-
- Only Base mainnet (`eip155:8453`) is registered with the x402 client. Before signing, every payment challenge is checked: scheme `exact`, network Base mainnet, token USDC on Base, amount at most 2000 units ($0.002), and recipient (must be `0x344143199642e87320785823c20c2df107d17534`). Anything else is refused without signing.
|
|
101
|
-
- Each signed payment counts against `NFTDATA_MAX_REQUESTS`, including payments for requests that later fail or time out.
|
|
102
|
-
- No automatic retries: one tool call makes at most one payment attempt.
|
|
103
|
-
- Caches successful responses for 5 minutes per slug. Concurrent identical calls share a single in-flight request.
|
|
104
|
-
- Returns generic errors for `400` (invalid slug), `404` (collection not found), `429` (rate limit, with `Retry-After`), timeouts and other failures; server and payment details are not passed to the MCP client.
|
|
300
|
+
---
|
|
105
301
|
|
|
106
302
|
## Limitations
|
|
107
303
|
|
|
108
|
-
- Only x402 on Base is supported. Solana and MPP
|
|
109
|
-
- The expected recipient address is hardcoded
|
|
110
|
-
- The payment hook
|
|
111
|
-
- The x402
|
|
112
|
-
|
|
113
|
-
## API reference
|
|
114
|
-
|
|
115
|
-
- OpenAPI: https://www.nftdata.app/openapi.json
|
|
116
|
-
- Docs: https://www.nftdata.app/docs
|
|
304
|
+
- Only x402 on Base mainnet is supported for payments. Solana and MPP are not implemented.
|
|
305
|
+
- The expected recipient address is hardcoded. If the NFT Data payout address changes, update `OFFICIAL_PAY_TO` in `lib.ts` and release a new version.
|
|
306
|
+
- The payment hook depends on the installed `@x402/*` SDK and is not covered by the offline unit tests.
|
|
307
|
+
- The x402 SDK changes quickly. If imports fail, check the [x402 docs](https://docs.x402.org/).
|
package/dist/lib.js
CHANGED
|
@@ -108,8 +108,27 @@ export class SpendLimiter {
|
|
|
108
108
|
}
|
|
109
109
|
export const ok = (text) => ({ content: [{ type: "text", text }] });
|
|
110
110
|
export const fail = (text) => ({ isError: true, content: [{ type: "text", text }] });
|
|
111
|
+
// Appends a payment receipt line to a successful tool result.
|
|
112
|
+
// txHash must already be validated (0x + 64 hex chars).
|
|
113
|
+
export function appendPaymentInfo(result, txHash) {
|
|
114
|
+
if (result.isError)
|
|
115
|
+
return result;
|
|
116
|
+
const receipt = `\n\nPayment: ${txHash}` +
|
|
117
|
+
`\nVerify: https://basescan.org/tx/${txHash}`;
|
|
118
|
+
return ok(result.content[0].text + receipt);
|
|
119
|
+
}
|
|
120
|
+
// Validates a tx hash string. Returns the hash if it looks like a valid
|
|
121
|
+
// Ethereum tx hash (0x + 64 hex chars), or null otherwise.
|
|
122
|
+
// This function has no x402 imports so it can be tested offline.
|
|
123
|
+
export function validateTxHash(raw) {
|
|
124
|
+
if (typeof raw !== "string")
|
|
125
|
+
return null;
|
|
126
|
+
return /^0x[0-9a-fA-F]{64}$/.test(raw) ? raw : null;
|
|
127
|
+
}
|
|
111
128
|
// Cache (5 min per slug) + reuse of in-flight requests so concurrent identical calls
|
|
112
129
|
// produce a single (paid) request. Failures are never cached.
|
|
130
|
+
// The loader returns { result, txHash }. Only result.content[0].text (the collection
|
|
131
|
+
// body) is cached; txHash is never stored, so cache hits always return txHash: null.
|
|
113
132
|
export class CachedFetcher {
|
|
114
133
|
cache = new Map();
|
|
115
134
|
inflight = new Map();
|
|
@@ -122,23 +141,23 @@ export class CachedFetcher {
|
|
|
122
141
|
async get(slug) {
|
|
123
142
|
const hit = this.cache.get(slug);
|
|
124
143
|
if (hit && this.now() - hit.at < CACHE_TTL_MS)
|
|
125
|
-
return ok(hit.body);
|
|
144
|
+
return { result: ok(hit.body), txHash: null };
|
|
126
145
|
if (hit)
|
|
127
146
|
this.cache.delete(slug);
|
|
128
147
|
const pending = this.inflight.get(slug);
|
|
129
148
|
if (pending)
|
|
130
149
|
return pending;
|
|
131
150
|
const p = this.load(slug)
|
|
132
|
-
.then((
|
|
133
|
-
if (!
|
|
151
|
+
.then((lr) => {
|
|
152
|
+
if (!lr.result.isError) {
|
|
134
153
|
if (this.cache.size >= CACHE_MAX_ENTRIES) {
|
|
135
154
|
const oldest = this.cache.keys().next().value;
|
|
136
155
|
if (oldest !== undefined)
|
|
137
156
|
this.cache.delete(oldest);
|
|
138
157
|
}
|
|
139
|
-
this.cache.set(slug, { at: this.now(), body:
|
|
158
|
+
this.cache.set(slug, { at: this.now(), body: lr.result.content[0].text });
|
|
140
159
|
}
|
|
141
|
-
return
|
|
160
|
+
return lr;
|
|
142
161
|
})
|
|
143
162
|
.finally(() => {
|
|
144
163
|
this.inflight.delete(slug);
|
package/dist/server.js
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
3
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
4
4
|
import { z } from "zod";
|
|
5
|
-
import { x402Client, wrapFetchWithPayment } from "@x402/fetch";
|
|
5
|
+
import { x402Client, wrapFetchWithPayment, decodePaymentResponseHeader } from "@x402/fetch";
|
|
6
6
|
import { ExactEvmScheme } from "@x402/evm/exact/client";
|
|
7
7
|
import { privateKeyToAccount } from "viem/accounts";
|
|
8
|
-
import { BASE_MAINNET, CachedFetcher, REFUSAL_PREFIX, SpendLimiter, fail, isValidPrivateKey, mapError, mapResponse, normalizeSlug, parseConfig, validateRequirements, withTimeout, } from "./lib.js";
|
|
8
|
+
import { BASE_MAINNET, CachedFetcher, REFUSAL_PREFIX, SpendLimiter, appendPaymentInfo, fail, isValidPrivateKey, mapError, mapResponse, normalizeSlug, parseConfig, validateRequirements, validateTxHash, withTimeout, } from "./lib.js";
|
|
9
9
|
let config;
|
|
10
10
|
try {
|
|
11
11
|
config = parseConfig(process.env);
|
|
@@ -73,16 +73,37 @@ async function load(slug) {
|
|
|
73
73
|
redirect: "error",
|
|
74
74
|
signal,
|
|
75
75
|
});
|
|
76
|
-
|
|
76
|
+
// Extract the payment response header before consuming the body.
|
|
77
|
+
const paymentHeader = r.headers.get("PAYMENT-RESPONSE") ?? r.headers.get("X-PAYMENT-RESPONSE");
|
|
78
|
+
return {
|
|
79
|
+
status: r.status,
|
|
80
|
+
body: await r.text(),
|
|
81
|
+
retryAfter: r.headers.get("Retry-After"),
|
|
82
|
+
paymentHeader,
|
|
83
|
+
};
|
|
77
84
|
}, config.timeoutMs);
|
|
78
|
-
|
|
85
|
+
const result = mapResponse(res.status, res.body, res.retryAfter);
|
|
86
|
+
// Decode the payment header and extract the tx hash only on success.
|
|
87
|
+
// Any failure (malformed header, missing field, wrong format) is silently
|
|
88
|
+
// ignored — the collection result is still returned without payment info.
|
|
89
|
+
let txHash = null;
|
|
90
|
+
if (!result.isError && res.paymentHeader) {
|
|
91
|
+
try {
|
|
92
|
+
const settle = decodePaymentResponseHeader(res.paymentHeader);
|
|
93
|
+
txHash = validateTxHash(settle.transaction);
|
|
94
|
+
}
|
|
95
|
+
catch {
|
|
96
|
+
// Malformed header: proceed without payment info.
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return { result, txHash };
|
|
79
100
|
}
|
|
80
101
|
catch (e) {
|
|
81
|
-
return mapError(e);
|
|
102
|
+
return { result: mapError(e), txHash: null };
|
|
82
103
|
}
|
|
83
104
|
}
|
|
84
105
|
const fetcher = new CachedFetcher(load);
|
|
85
|
-
const server = new McpServer({ name: "nftdata", version: "1.0.
|
|
106
|
+
const server = new McpServer({ name: "nftdata", version: "1.0.2" });
|
|
86
107
|
server.tool("get_collection_data", "Get OpenSea NFT collection market data in one call: name, contracts, floor price, " +
|
|
87
108
|
"1/7/30-day and lifetime sales and volume, unique owners, total supply, creation date. " +
|
|
88
109
|
"Costs $0.002 per call (x402). " +
|
|
@@ -95,6 +116,7 @@ server.tool("get_collection_data", "Get OpenSea NFT collection market data in on
|
|
|
95
116
|
if (slug === null) {
|
|
96
117
|
return fail("Invalid slug: use only letters, digits, '_' and '-' (max 100 characters).");
|
|
97
118
|
}
|
|
98
|
-
|
|
119
|
+
const { result, txHash } = await fetcher.get(slug);
|
|
120
|
+
return txHash ? appendPaymentInfo(result, txHash) : result;
|
|
99
121
|
});
|
|
100
122
|
await server.connect(new StdioServerTransport());
|
package/package.json
CHANGED