Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@injectivelabs/networks": "^1.14.27",
"@injectivelabs/sdk-ts": "^1.14.27",
"@injectivelabs/utils": "^1.14.27",
"@injectivelabs/x402": "^0.0.1",
"@modelcontextprotocol/sdk": "^1.0.4",
"decimal.js": "^10.4.3",
"zod": "^3.22.0"
Expand Down
39 changes: 39 additions & 0 deletions src/mcp/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { debridge } from '../bridges/debridge.js'
import { evm } from '../evm/index.js'
import { eip712 } from '../evm/eip712.js'
import { authz, TRADING_MSG_TYPES } from '../authz/index.js'
import { createInjectiveClient } from '@injectivelabs/x402/client'

const injAddress = z.string().regex(/^inj1[a-z0-9]{38}$/, 'Must be a valid inj1... address (42 chars)')
const numericString = z.string().regex(/^\d+(\.\d+)?$/, 'Must be a positive numeric string')
Expand Down Expand Up @@ -809,6 +810,44 @@ server.tool(
},
)

// ─── x402 Payment Tools ────────────────────────────────────────────────────────

server.tool(
'x402_fetch',
'Fetch data from an x402-gated API endpoint. Automatically handles 402 Payment Required ' +
'responses by signing a USDC payment using the Injective EVM wallet, submitting it to the facilitator, ' +
'and retrying the request. IMPORTANT: Real on-chain payment with real funds.',
{
address: injAddress.describe('The inj1... address of your trading wallet.'),
password: z.string().describe('Keystore password to decrypt the private key for signing.'),
url: z.string().url().describe('The URL of the x402-gated API endpoint.'),
},
async ({ address, password, url }) => {
const privateKeyHex = wallets.unlock(address, password)
const client = createInjectiveClient({ privateKey: privateKeyHex as `0x${string}` })
const response = await client.fetch(url)

const text = await response.text()
let data;
try {
data = JSON.parse(text)
} catch {
data = text
}

return {
content: [{
type: 'text',
text: JSON.stringify({
status: response.status,
url: response.url,
data
}, null, 2),
}],
}
},
)

// ─── Start ───────────────────────────────────────────────────────────────────

const transport = new StdioServerTransport()
Expand Down