web3-tools-mcp 2.0.2 → 3.1.0

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 (180) hide show
  1. package/CHANGELOG.md +182 -1
  2. package/README.md +76 -47
  3. package/dist/anvil.d.ts +17 -19
  4. package/dist/anvil.d.ts.map +1 -1
  5. package/dist/anvil.js +124 -228
  6. package/dist/anvil.js.map +1 -1
  7. package/dist/chain-meta.d.ts.map +1 -1
  8. package/dist/chain-meta.js +19 -8
  9. package/dist/chain-meta.js.map +1 -1
  10. package/dist/chains.d.ts +9 -1
  11. package/dist/chains.d.ts.map +1 -1
  12. package/dist/chains.js +11 -2
  13. package/dist/chains.js.map +1 -1
  14. package/dist/clear-signing.d.ts.map +1 -1
  15. package/dist/clear-signing.js +17 -6
  16. package/dist/clear-signing.js.map +1 -1
  17. package/dist/client.d.ts.map +1 -1
  18. package/dist/client.js.map +1 -1
  19. package/dist/context.d.ts +22 -0
  20. package/dist/context.d.ts.map +1 -0
  21. package/dist/context.js +24 -0
  22. package/dist/context.js.map +1 -0
  23. package/dist/hosted.d.ts +12 -0
  24. package/dist/hosted.d.ts.map +1 -0
  25. package/dist/hosted.js +12 -0
  26. package/dist/hosted.js.map +1 -0
  27. package/dist/http-server.d.ts +13 -0
  28. package/dist/http-server.d.ts.map +1 -1
  29. package/dist/http-server.js +22 -7
  30. package/dist/http-server.js.map +1 -1
  31. package/dist/index.js +20 -12
  32. package/dist/index.js.map +1 -1
  33. package/dist/kv-storage.d.ts +2 -1
  34. package/dist/kv-storage.d.ts.map +1 -1
  35. package/dist/kv-storage.js +5 -2
  36. package/dist/kv-storage.js.map +1 -1
  37. package/dist/lib.d.ts +24 -0
  38. package/dist/lib.d.ts.map +1 -0
  39. package/dist/lib.js +23 -0
  40. package/dist/lib.js.map +1 -0
  41. package/dist/mcp-server.d.ts +4 -0
  42. package/dist/mcp-server.d.ts.map +1 -0
  43. package/dist/mcp-server.js +43 -0
  44. package/dist/mcp-server.js.map +1 -0
  45. package/dist/oauth.d.ts +57 -8
  46. package/dist/oauth.d.ts.map +1 -1
  47. package/dist/oauth.js +133 -63
  48. package/dist/oauth.js.map +1 -1
  49. package/dist/preview.d.ts +7 -0
  50. package/dist/preview.d.ts.map +1 -1
  51. package/dist/preview.js +21 -7
  52. package/dist/preview.js.map +1 -1
  53. package/dist/signing-requests.d.ts +81 -0
  54. package/dist/signing-requests.d.ts.map +1 -0
  55. package/dist/signing-requests.js +138 -0
  56. package/dist/signing-requests.js.map +1 -0
  57. package/dist/tools/index.d.ts +27 -1
  58. package/dist/tools/index.d.ts.map +1 -1
  59. package/dist/tools/index.js +50 -15
  60. package/dist/tools/index.js.map +1 -1
  61. package/dist/tools/{advanced.d.ts → read/advanced.d.ts} +39 -30
  62. package/dist/tools/read/advanced.d.ts.map +1 -0
  63. package/dist/tools/{advanced.js → read/advanced.js} +53 -41
  64. package/dist/tools/read/advanced.js.map +1 -0
  65. package/dist/tools/{balance.d.ts → read/balance.d.ts} +8 -7
  66. package/dist/tools/read/balance.d.ts.map +1 -0
  67. package/dist/tools/{balance.js → read/balance.js} +18 -33
  68. package/dist/tools/read/balance.js.map +1 -0
  69. package/dist/tools/{contract-info.d.ts → read/contract-info.d.ts} +20 -15
  70. package/dist/tools/read/contract-info.d.ts.map +1 -0
  71. package/dist/tools/{contract-info.js → read/contract-info.js} +54 -123
  72. package/dist/tools/read/contract-info.js.map +1 -0
  73. package/dist/tools/{contract.d.ts → read/contract.d.ts} +14 -12
  74. package/dist/tools/read/contract.d.ts.map +1 -0
  75. package/dist/tools/{contract.js → read/contract.js} +3 -2
  76. package/dist/tools/read/contract.js.map +1 -0
  77. package/dist/tools/read/ens.d.ts +104 -0
  78. package/dist/tools/read/ens.d.ts.map +1 -0
  79. package/dist/tools/{ens.js → read/ens.js} +25 -33
  80. package/dist/tools/read/ens.js.map +1 -0
  81. package/dist/tools/read/gas.d.ts +171 -0
  82. package/dist/tools/read/gas.d.ts.map +1 -0
  83. package/dist/tools/read/gas.js +333 -0
  84. package/dist/tools/read/gas.js.map +1 -0
  85. package/dist/tools/read/logs.d.ts +101 -0
  86. package/dist/tools/read/logs.d.ts.map +1 -0
  87. package/dist/tools/read/logs.js +211 -0
  88. package/dist/tools/read/logs.js.map +1 -0
  89. package/dist/tools/{signatures.d.ts → read/signatures.d.ts} +8 -4
  90. package/dist/tools/read/signatures.d.ts.map +1 -0
  91. package/dist/tools/{signatures.js → read/signatures.js} +4 -3
  92. package/dist/tools/read/signatures.js.map +1 -0
  93. package/dist/tools/read/signing.d.ts +27 -0
  94. package/dist/tools/read/signing.d.ts.map +1 -0
  95. package/dist/tools/read/signing.js +81 -0
  96. package/dist/tools/read/signing.js.map +1 -0
  97. package/dist/tools/sign/errors.d.ts +9 -0
  98. package/dist/tools/sign/errors.d.ts.map +1 -0
  99. package/dist/tools/sign/errors.js +29 -0
  100. package/dist/tools/sign/errors.js.map +1 -0
  101. package/dist/tools/sign/transactions.d.ts +141 -0
  102. package/dist/tools/sign/transactions.d.ts.map +1 -0
  103. package/dist/tools/sign/transactions.js +361 -0
  104. package/dist/tools/sign/transactions.js.map +1 -0
  105. package/dist/tools/sign/wallet.d.ts +50 -0
  106. package/dist/tools/sign/wallet.d.ts.map +1 -0
  107. package/dist/tools/sign/wallet.js +180 -0
  108. package/dist/tools/sign/wallet.js.map +1 -0
  109. package/dist/utils.d.ts +23 -2
  110. package/dist/utils.d.ts.map +1 -1
  111. package/dist/utils.js +48 -3
  112. package/dist/utils.js.map +1 -1
  113. package/dist/wallet-client.d.ts +34 -4
  114. package/dist/wallet-client.d.ts.map +1 -1
  115. package/dist/wallet-client.js +106 -27
  116. package/dist/wallet-client.js.map +1 -1
  117. package/dist/walletconnect.d.ts +100 -13
  118. package/dist/walletconnect.d.ts.map +1 -1
  119. package/dist/walletconnect.js +215 -50
  120. package/dist/walletconnect.js.map +1 -1
  121. package/package.json +12 -9
  122. package/src/anvil.ts +150 -271
  123. package/src/chain-meta.ts +16 -7
  124. package/src/chains.ts +14 -2
  125. package/src/clear-signing.ts +15 -9
  126. package/src/client.ts +0 -1
  127. package/src/context.ts +27 -0
  128. package/src/hosted.ts +11 -0
  129. package/src/http-server.ts +38 -8
  130. package/src/index.ts +20 -13
  131. package/src/kv-storage.ts +6 -3
  132. package/src/lib.ts +24 -0
  133. package/src/mcp-server.ts +47 -0
  134. package/src/oauth.ts +181 -69
  135. package/src/preview.ts +28 -7
  136. package/src/signing-requests.ts +198 -0
  137. package/src/tools/index.ts +78 -16
  138. package/src/tools/{advanced.ts → read/advanced.ts} +74 -43
  139. package/src/tools/{balance.ts → read/balance.ts} +20 -35
  140. package/src/tools/{contract-info.ts → read/contract-info.ts} +66 -146
  141. package/src/tools/{contract.ts → read/contract.ts} +4 -3
  142. package/src/tools/{ens.ts → read/ens.ts} +27 -34
  143. package/src/tools/read/gas.ts +443 -0
  144. package/src/tools/read/logs.ts +281 -0
  145. package/src/tools/{signatures.ts → read/signatures.ts} +5 -5
  146. package/src/tools/read/signing.ts +92 -0
  147. package/src/tools/sign/errors.ts +28 -0
  148. package/src/tools/sign/transactions.ts +479 -0
  149. package/src/tools/sign/wallet.ts +216 -0
  150. package/src/utils.ts +52 -3
  151. package/src/wallet-client.ts +107 -24
  152. package/src/walletconnect.ts +250 -48
  153. package/dist/tools/advanced.d.ts.map +0 -1
  154. package/dist/tools/advanced.js.map +0 -1
  155. package/dist/tools/balance.d.ts.map +0 -1
  156. package/dist/tools/balance.js.map +0 -1
  157. package/dist/tools/contract-info.d.ts.map +0 -1
  158. package/dist/tools/contract-info.js.map +0 -1
  159. package/dist/tools/contract.d.ts.map +0 -1
  160. package/dist/tools/contract.js.map +0 -1
  161. package/dist/tools/ens.d.ts +0 -119
  162. package/dist/tools/ens.d.ts.map +0 -1
  163. package/dist/tools/ens.js.map +0 -1
  164. package/dist/tools/gas.d.ts +0 -99
  165. package/dist/tools/gas.d.ts.map +0 -1
  166. package/dist/tools/gas.js +0 -181
  167. package/dist/tools/gas.js.map +0 -1
  168. package/dist/tools/logs.d.ts +0 -39
  169. package/dist/tools/logs.d.ts.map +0 -1
  170. package/dist/tools/logs.js +0 -230
  171. package/dist/tools/logs.js.map +0 -1
  172. package/dist/tools/signatures.d.ts.map +0 -1
  173. package/dist/tools/signatures.js.map +0 -1
  174. package/dist/tools/transactions.d.ts +0 -155
  175. package/dist/tools/transactions.d.ts.map +0 -1
  176. package/dist/tools/transactions.js +0 -365
  177. package/dist/tools/transactions.js.map +0 -1
  178. package/src/tools/gas.ts +0 -219
  179. package/src/tools/logs.ts +0 -293
  180. package/src/tools/transactions.ts +0 -456
@@ -1,456 +0,0 @@
1
- import { randomBytes } from 'node:crypto'
2
- import qrcode from 'qrcode-terminal'
3
- import { type AbiFunction, type Address, encodeFunctionData, isAddress, parseAbiItem, parseUnits, toHex } from 'viem'
4
- import { z } from 'zod'
5
- import { getClientManager, SUPPORTED_CHAINS } from '../client.js'
6
- import { log } from '../log.js'
7
- import { buildTxPreview, type RawTx, type TxPreview } from '../preview.js'
8
- import type { ChainName } from '../types.js'
9
- import { createTool, formatResponse } from '../utils.js'
10
- import { getWalletClient } from '../wallet-client.js'
11
- import { getPhoneSigner, pairingLinks } from '../walletconnect.js'
12
-
13
- /** Render a pairing URI as an ASCII QR, so it can be scanned straight from the chat. */
14
- function qrFor(uri: string): Promise<string> {
15
- return new Promise((resolve) => qrcode.generate(uri, { small: true }, resolve))
16
- }
17
-
18
- function generateRequestId(): string {
19
- return randomBytes(16).toString('hex')
20
- }
21
-
22
- function requireAddress(label: string, address: string): Address {
23
- if (!isAddress(address)) throw new Error(`Invalid ${label}: ${address}`)
24
- return address
25
- }
26
-
27
- function explorerTxUrl(chain: ChainName, txHash: unknown): string | undefined {
28
- return getClientManager().explorerUrl(chain, `/tx/${txHash}`)
29
- }
30
-
31
- /**
32
- * Which wallet to send the request to. Deliberately required on every signing tool: both
33
- * signers can be live at once, and only the person holding the devices knows which one is
34
- * actually to hand. Guessing strands the request in front of a screen nobody is looking at.
35
- *
36
- * Either way the transaction is decoded and simulated first — the wallet shows its own
37
- * summary, so the preview is returned to the caller too, to be read before approving.
38
- */
39
- const SignWithSchema = z
40
- .enum(['phone', 'browser'])
41
- .describe(
42
- 'Where to approve this: "phone" for a paired WalletConnect wallet, "browser" for the wallet signing page. Ask the user which they want — do not assume, even when only one is connected. Call wallet_status first: it says which signers are ready, and when no signing page is open it returns walletUrl. In that case give the user that link, ask them to open it and connect a wallet, and only call this once they confirm — a signing request cannot reach a page nobody has open.'
43
- )
44
-
45
- type SignWith = z.infer<typeof SignWithSchema>
46
-
47
- async function signOnPhone(chain: ChainName, tx: RawTx & { data?: string }) {
48
- const phone = getPhoneSigner()
49
- if (!phone) {
50
- throw new Error(
51
- 'Phone signing is not configured on this server. Set WALLETCONNECT_PROJECT_ID, or sign in the browser instead.'
52
- )
53
- }
54
- if (!(await phone.isPaired())) {
55
- throw new Error('No phone wallet is paired. Run pair_phone_wallet and scan the QR, or sign in the browser instead.')
56
- }
57
-
58
- const session = await phone.session()
59
- const from = session?.accounts[0]
60
- const preview = await buildTxPreview(chain, tx, from)
61
-
62
- const txHash = await phone.request(chain, 'eth_sendTransaction', [
63
- { from, to: tx.to, value: tx.value ?? '0x0', ...(tx.data && { data: tx.data }) }
64
- ])
65
-
66
- return { txHash, preview, signedWith: 'phone' as const }
67
- }
68
-
69
- async function requestSignature(chain: ChainName, tx: RawTx & { data?: string }, signWith: SignWith) {
70
- log('info', 'Transaction', `${chain} → ${tx.to}, signing with ${signWith}`)
71
- if (signWith === 'phone') return signOnPhone(chain, tx)
72
-
73
- const wallet = getWalletClient()
74
- await wallet.waitForSigner()
75
-
76
- const preview = await buildTxPreview(chain, tx, wallet.getAddress())
77
-
78
- return {
79
- txHash: await wallet.request({
80
- id: generateRequestId(),
81
- type: 'send_transaction',
82
- chain,
83
- data: { to: tx.to, value: tx.value ?? '0x0', ...(tx.data && { data: tx.data }) },
84
- preview
85
- }),
86
- preview,
87
- signedWith: 'browser' as const
88
- }
89
- }
90
-
91
- /** What the caller should see about a transaction before it is approved. */
92
- function previewSummary(preview: TxPreview) {
93
- return {
94
- action: preview.decoded?.intent ?? preview.decoded?.functionName,
95
- protocol: preview.decoded?.protocol ?? preview.toLabel,
96
- details: preview.decoded?.fields.map((f) => `${f.name}: ${f.value}${f.warning ? ` (${f.warning})` : ''}`),
97
- simulation: preview.simulation
98
- }
99
- }
100
-
101
- /**
102
- * WalletConnect rejects with a plain `{ code, message }` rather than an Error, which
103
- * String() renders as "[object Object]" — the reason a wallet gave for refusing is the
104
- * whole point of the message, so dig it out.
105
- */
106
- function describeError(error: unknown): string {
107
- if (error instanceof Error) return error.message
108
- if (error && typeof error === 'object') {
109
- const { message, code, reason } = error as { message?: string; code?: number; reason?: string }
110
- const text = message ?? reason
111
- if (text) return code === undefined ? text : `${text} (code ${code})`
112
- return JSON.stringify(error)
113
- }
114
- return String(error)
115
- }
116
-
117
- function failure(error: unknown, message: string) {
118
- log('warning', 'Transaction', `${message} — ${describeError(error)}`)
119
- return formatResponse({
120
- success: false,
121
- error: describeError(error),
122
- message
123
- })
124
- }
125
-
126
- export default {
127
- send_native_token: createTool(
128
- 'Send Native Token',
129
- 'Send native tokens (ETH, MATIC, BNB, etc.) to an address. Simulates the transaction, then sends it to the signer you choose for approval.',
130
- z.object({
131
- chain: z.enum(SUPPORTED_CHAINS).describe('Blockchain network'),
132
- to: z.string().describe('Recipient address'),
133
- amount: z.string().describe('Amount in native token (e.g., "0.1" for 0.1 ETH)'),
134
- data: z.string().optional().describe('Optional hex-encoded data to include with transaction'),
135
- signWith: SignWithSchema
136
- }),
137
- async (args) => {
138
- try {
139
- const to = requireAddress('recipient address', args.to)
140
- const value = `0x${parseUnits(args.amount, 18).toString(16)}`
141
-
142
- const { txHash, preview, signedWith } = await requestSignature(
143
- args.chain as ChainName,
144
- { to, value, data: args.data },
145
- args.signWith
146
- )
147
-
148
- return formatResponse({
149
- success: true,
150
- chain: args.chain,
151
- transactionHash: txHash,
152
- to,
153
- amount: args.amount,
154
- signedWith,
155
- preview: previewSummary(preview),
156
- explorerUrl: explorerTxUrl(args.chain as ChainName, txHash)
157
- })
158
- } catch (error) {
159
- return failure(error, 'Transaction failed or was rejected')
160
- }
161
- }
162
- ),
163
-
164
- send_erc20_token: createTool(
165
- 'Send ERC20 Token',
166
- 'Send ERC20 tokens to an address. Simulates the transfer, then sends it to the signer you choose for approval.',
167
- z.object({
168
- chain: z.enum(SUPPORTED_CHAINS).describe('Blockchain network'),
169
- tokenAddress: z.string().describe('ERC20 token contract address'),
170
- to: z.string().describe('Recipient address'),
171
- amount: z.string().describe('Amount in token units (e.g., "100" for 100 USDC)'),
172
- decimals: z.number().optional().default(18).describe('Token decimals (default: 18)'),
173
- signWith: SignWithSchema
174
- }),
175
- async (args) => {
176
- try {
177
- const tokenAddress = requireAddress('token address', args.tokenAddress)
178
- const to = requireAddress('recipient address', args.to)
179
-
180
- const data = encodeFunctionData({
181
- abi: [parseAbiItem('function transfer(address to, uint256 amount)')],
182
- args: [to, parseUnits(args.amount, args.decimals ?? 18)]
183
- })
184
-
185
- const { txHash, preview, signedWith } = await requestSignature(
186
- args.chain as ChainName,
187
- {
188
- to: tokenAddress,
189
- data,
190
- value: '0x0'
191
- },
192
- args.signWith
193
- )
194
-
195
- return formatResponse({
196
- success: true,
197
- chain: args.chain,
198
- transactionHash: txHash,
199
- tokenAddress,
200
- to,
201
- amount: args.amount,
202
- signedWith,
203
- preview: previewSummary(preview),
204
- explorerUrl: explorerTxUrl(args.chain as ChainName, txHash)
205
- })
206
- } catch (error) {
207
- return failure(error, 'Token transfer failed or was rejected')
208
- }
209
- }
210
- ),
211
-
212
- write_contract: createTool(
213
- 'Write Contract',
214
- 'Send a transaction that calls a state-changing contract function. Simulates it first, then asks your wallet to approve. Costs gas.',
215
- z.object({
216
- chain: z.enum(SUPPORTED_CHAINS).describe('Blockchain network'),
217
- contractAddress: z.string().describe('Contract address'),
218
- functionAbi: z.string().describe('Function ABI definition (e.g., "function transfer(address to, uint256 amount)")'),
219
- args: z
220
- .array(z.union([z.string(), z.number(), z.boolean()]))
221
- .optional()
222
- .describe('Function arguments in order matching the ABI signature'),
223
- value: z.string().optional().describe('Optional native value to send with transaction (in ETH units, e.g., "0.1")'),
224
- signWith: SignWithSchema
225
- }),
226
- async (args) => {
227
- try {
228
- const contractAddress = requireAddress('contract address', args.contractAddress)
229
- const abiItem = parseAbiItem(args.functionAbi) as AbiFunction
230
-
231
- const data = encodeFunctionData({
232
- abi: [abiItem],
233
- functionName: abiItem.name,
234
- args: (args.args || []) as readonly unknown[]
235
- })
236
- const value = args.value ? `0x${parseUnits(args.value, 18).toString(16)}` : '0x0'
237
-
238
- const { txHash, preview, signedWith } = await requestSignature(
239
- args.chain as ChainName,
240
- {
241
- to: contractAddress,
242
- data,
243
- value
244
- },
245
- args.signWith
246
- )
247
-
248
- return formatResponse({
249
- success: true,
250
- chain: args.chain,
251
- transactionHash: txHash,
252
- contractAddress,
253
- functionName: abiItem.name,
254
- signedWith,
255
- preview: previewSummary(preview),
256
- explorerUrl: explorerTxUrl(args.chain as ChainName, txHash)
257
- })
258
- } catch (error) {
259
- return failure(error, 'Contract call failed or was rejected')
260
- }
261
- }
262
- ),
263
-
264
- sign_message: createTool(
265
- 'Sign Message',
266
- 'Sign a message with your wallet, on whichever signer you choose.',
267
- z.object({
268
- message: z.string().describe('Message to sign'),
269
- signWith: SignWithSchema
270
- }),
271
- async (args) => {
272
- try {
273
- let signature: unknown
274
-
275
- if (args.signWith === 'phone') {
276
- const phone = getPhoneSigner()
277
- const session = await phone?.session()
278
- if (!session) {
279
- throw new Error('No phone wallet is paired. Run pair_phone_wallet and scan the QR, or sign in the browser instead.')
280
- }
281
- signature = await phone!.request('mainnet', 'personal_sign', [toHex(args.message), session.accounts[0]])
282
- } else {
283
- await getWalletClient().waitForSigner()
284
- signature = await getWalletClient().request({
285
- id: generateRequestId(),
286
- type: 'sign_message',
287
- chain: 'any',
288
- data: { message: args.message }
289
- })
290
- }
291
-
292
- return formatResponse({
293
- success: true,
294
- message: args.message,
295
- signature,
296
- signedWith: args.signWith,
297
- signatureType: 'personal_sign'
298
- })
299
- } catch (error) {
300
- return failure(error, 'Message signing failed or was rejected')
301
- }
302
- }
303
- ),
304
-
305
- pair_phone_wallet: createTool(
306
- 'Pair Phone Wallet',
307
- 'Pair a phone wallet over WalletConnect, so transactions are signed on the phone instead of in a browser. ' +
308
- 'Returns a QR to scan plus links that open a wallet directly — show those to the user as clickable links. ' +
309
- 'Requires a WalletConnect project id.',
310
- z.object({
311
- chains: z
312
- .array(z.enum(SUPPORTED_CHAINS))
313
- .optional()
314
- .describe('Chains to request access to (defaults to every supported network)'),
315
- wallet: z.string().optional().describe('Name of the wallet to open, e.g. "rabby" — omit to list the common ones'),
316
- agent: z
317
- .string()
318
- .optional()
319
- .describe(
320
- 'Who is asking, shown in the wallet approval dialog, e.g. "Claude Code". Say who you actually are — ' +
321
- 'the user reads this when deciding whether to grant signing access.'
322
- )
323
- }),
324
- async (args) => {
325
- const phone = getPhoneSigner()
326
- if (!phone) {
327
- return formatResponse({
328
- success: false,
329
- message:
330
- 'No WalletConnect project id configured. Get a free one at https://dashboard.walletconnect.com and pass --walletconnect-project-id or set WALLETCONNECT_PROJECT_ID.'
331
- })
332
- }
333
-
334
- try {
335
- // Before anything starts the client: its name is fixed the moment it does.
336
- phone.nameAgent(args.agent)
337
- const existing = await phone.session()
338
- if (existing) {
339
- return formatResponse({
340
- alreadyPaired: true,
341
- address: existing.accounts[0],
342
- wallet: existing.peer,
343
- message: 'A phone wallet is already paired. Transactions will be sent to it.'
344
- })
345
- }
346
-
347
- const chains = (args.chains ?? SUPPORTED_CHAINS.filter((c) => c !== 'localhost')) as ChainName[]
348
- const uri = await phone.pair(chains)
349
- const projectId = getClientManager().getConfig().walletConnectProjectId as string
350
-
351
- return formatResponse({
352
- qr: await qrFor(uri),
353
- uri,
354
- // For an agent running on the phone itself there is nothing to scan, so hand over
355
- // links that open the wallet directly. `wallet` narrows the list to one.
356
- openOnThisDevice: await pairingLinks(projectId, uri, args.wallet),
357
- // Approving grants an agent standing permission to request signatures, so the
358
- // wallet dialog names the agent rather than implying a website is connecting.
359
- shownInWallet: args.agent ? `${args.agent} via web3-tools-mcp` : 'web3-tools-mcp (AI agent)',
360
- howToPresent:
361
- 'Render each openOnThisDevice entry as a clickable link labelled with the wallet name, and show the ' +
362
- 'uri as copyable text. On a phone that tap is the whole flow. Show the QR only when the user is on a ' +
363
- 'different device from their wallet — it is unreadable inline on the phone that holds the wallet. ' +
364
- 'If a link does nothing, some clients drop custom schemes like rabby://, so tell them to paste the uri.',
365
- message:
366
- 'On another device: scan the QR with your wallet. On this device: tap one of the openOnThisDevice ' +
367
- 'links, or copy the uri and paste it into your wallet under WalletConnect — every wallet supports ' +
368
- 'that, and it is the one that always works. Then run wallet_status to confirm.'
369
- })
370
- } catch (error) {
371
- return failure(error, 'Could not start WalletConnect pairing')
372
- }
373
- }
374
- ),
375
-
376
- disconnect_phone_wallet: createTool(
377
- 'Disconnect Phone Wallet',
378
- 'End every WalletConnect session and drop the pairings underneath them. Use before pairing a different wallet, or to clear out attempts that were never approved.',
379
- z.object({}),
380
- async () => {
381
- const phone = getPhoneSigner()
382
- if (!phone) {
383
- return formatResponse({ success: false, message: 'No WalletConnect project id configured — nothing to disconnect.' })
384
- }
385
-
386
- try {
387
- const before = await phone.sessions()
388
- const removed = await phone.disconnectAll()
389
-
390
- return formatResponse({
391
- success: true,
392
- disconnectedSessions: removed.sessions,
393
- droppedPairings: removed.pairings,
394
- wallets: before.map((session) => `${session.peer ?? 'unknown wallet'} (${session.accounts[0]})`),
395
- message:
396
- removed.sessions || removed.pairings
397
- ? 'Disconnected. Transactions fall back to the browser wallet until a phone is paired again.'
398
- : 'Nothing was connected.'
399
- })
400
- } catch (error) {
401
- return failure(error, 'Could not disconnect the phone wallet')
402
- }
403
- }
404
- ),
405
-
406
- wallet_status: createTool(
407
- 'Wallet Status',
408
- 'Report every signer and whether it is ready, so a signing tool can be given the right signWith.',
409
- z.object({}),
410
- async () => {
411
- // Both signers are reported, never just the first one found: signWith makes this a
412
- // choice the user owns, so the agent has to see everything available.
413
- const phone = getPhoneSigner()
414
- const phoneSession = await phone?.session().catch(() => undefined)
415
-
416
- const wallet = getWalletClient()
417
- const relayError = await wallet
418
- .connect()
419
- .then(() => undefined)
420
- .catch((error) => describeError(error))
421
-
422
- const browserReady = !relayError && wallet.isConnected()
423
- // Only raise a tab when there is no other way to sign — a status check should not
424
- // open a window at someone who is about to use their phone.
425
- if (!browserReady && !phoneSession && !relayError) wallet.openBrowser()
426
-
427
- const signers = {
428
- phone: phoneSession
429
- ? { ready: true, address: phoneSession.accounts[0], wallet: phoneSession.peer, chains: phoneSession.chains }
430
- : {
431
- ready: false,
432
- reason: phone
433
- ? 'No phone paired. Run pair_phone_wallet and scan the QR.'
434
- : 'Phone signing is not configured — set WALLETCONNECT_PROJECT_ID.'
435
- },
436
- browser: browserReady
437
- ? { ready: true, address: wallet.getAddress(), openTab: wallet.getPageUrl() }
438
- : {
439
- ready: false,
440
- reason: relayError ?? 'No signing page is open.',
441
- walletUrl: relayError ? undefined : wallet.getUrl()
442
- }
443
- }
444
-
445
- const ready = [phoneSession ? 'phone' : undefined, browserReady ? 'browser' : undefined].filter(Boolean)
446
- const message =
447
- ready.length === 2
448
- ? 'Both signers are ready. Ask the user which one to use, then pass it as signWith.'
449
- : ready.length === 1
450
- ? `Only ${ready[0]} is ready. Confirm with the user before signing there, or set the other one up.`
451
- : `No signer is ready. For a browser, give the user this link and ask them to open it and connect a wallet: ${signers.browser.walletUrl ?? '(relay unavailable)'}. For a phone, run pair_phone_wallet.`
452
-
453
- return formatResponse({ ready, signers, message })
454
- }
455
- )
456
- }