MCP Web3钱包测试仪
一个MCP服务器,充当可编程的以太坊钱包,用于自动Web3 dApp测试。LLM可以通过MCP工具控制钱包操作(批准交易、签署消息、管理帐户),而Playwright则可以自动化浏览器。
特性
- 完整的EIP-1193提供商 -将完整的以太坊提供商注入任何dApp
- EIP-6963支持 -多钱包环境下的现代钱包发现
- MCP集成 -通过任何LLM的MCP工具控制钱包
- 多账户 -在10个Anvil测试帐户之间切换或使用自定义密钥
- 自动审批模式 -在需要时启用全自动测试
- 供应商服务 -通过HTTP获取提供者脚本,便于注入
快速开始
先决条件
- Node.js 18+
- 铁砧 (来自Foundry):
curl -L https://foundry.paradigm.xyz | bash && foundryup
安装
npm install
npm run build启动服务器
启动一切的最简单方法(使用启动脚本):
# Start both Anvil and the wallet server
./server.sh --anvil
# Or start just the wallet server (if Anvil is already running)
./server.sh
# Custom ports
./server.sh --port=4000 --ws-port=9000 --anvil
# For mainnet fork testing
./server.sh --chain-id=1 --anvil或手动:
# Terminal 1: Start Anvil (local blockchain)
anvil
# Terminal 2: Start the wallet server
npm start或者使用CLI:
npx web3-wallet-tester使用克劳德代码注册
claude mcp add --transport http wallet-tester http://localhost:3001/mcp建筑
Browser (dApp) Wallet Server Blockchain
│ │ │
│ window.ethereum.request() │ │
├─────────────────────────────────►│ │
│ WebSocket │ wallet_approveRequest() │
│ │◄──────────────────────────────┤ LLM
│ │ MCP Tools │
│ │ │
│ │ eth_sendTransaction │
│ ├──────────────────────────────►│
│ │ JSON-RPC │ Anvil
│ result │ │
│◄─────────────────────────────────┤ │用法
1.注入提供者(剧作家)
// Fetch provider from the wallet server
const providerScript = await fetch('http://localhost:3001/provider.js').then(r => r.text());
// Inject BEFORE navigating (critical for proper detection)
await page.addInitScript(providerScript);
// Navigate to dApp
await page.goto('https://app.uniswap.org');2.通过MCP工具进行控制
// Check wallet status
const status = await wallet_getStatus();
// Wait for a wallet request
const request = await wallet_waitForRequest({ timeoutMs: 30000 });
// Approve the request
await wallet_approveRequest({ requestId: request.id });
// Or enable auto-approve for automated testing
await wallet_setAutoApprove({ enabled: true });MCP工具
| 工具 | 说明 |
|---|---|
wallet_getStatus | 获取钱包状态(地址、链、余额、待处理计数) |
wallet_getAddress | 获取当前钱包地址 |
wallet_getBalance | 获取ETH余额 |
wallet_getChainId | 获取链ID |
wallet_getPendingRequests | 列出等待批准的待处理请求 |
wallet_approveRequest | 批准待处理的请求 |
wallet_rejectRequest | 拒绝待处理的请求 |
wallet_waitForRequest | 等待请求到达 |
wallet_setAutoApprove | 启用/禁用自动批准模式 |
wallet_getTransactionReceipt | 通过哈希获取交易收据 |
wallet_listAccounts | 列出所有10个Anvil测试帐户 |
wallet_switchAccount | 按索引切换到Anvil帐户(0-9) |
wallet_setPrivateKey | 使用自定义私钥 |
wallet_getProviderScript | 获取provider.js进行注射 |
MCP资源
服务器将文档作为MCP资源公开:
| 资源URI | 描述 |
|---|---|
wallet://docs/instructions | LLM使用指南,包括工作流程和示例 |
wallet://docs/testing-guide | 完整的测试文档 |
wallet://docs/tools | 带参数的工具参考 |
LLM可以阅读这些资源来学习如何使用钱包测试仪。
HTTP端点
| 端点 | 描述 |
|---|---|
GET /health | 健康检查 |
GET /provider.js | 获取注射提供者脚本 |
POST /mcp | MCP服务器端点 |
配置
| 环境变量 | 默认值 | 描述 |
|---|---|---|
MCP_PORT | 3001 | HTTP服务器端口 |
WS_PORT | 8546 | WebSocket网桥端口 |
ANVIL_RPC_URL | http://127.0.0.1:8545 | Anvil RPC URL |
CHAIN_ID | (自动检测) | 要报告的链ID |
ACCOUNT_INDEX | 0 | Anvil账户指数(0-9) |
PRIVATE_KEY | (来自索引) | 自定义私钥 |
使用MetaMask
安装MetaMask后,它会锁定 window.ethereum钱包测试仪通过以下方式处理此问题:
- EIP-6963公告 -现代dApp通过事件发现钱包
- 重复公告 -多次宣布以捕捉最新发现
- 模态检测 -钱包选择UI打开时重新通知
为了进行可靠的测试:
- 使用剧作家
addInitScript(在MetaMask之前注射) - 使用隐身模式(禁用扩展)
- 暂时禁用MetaMask
chrome://extensions
示例:完整的测试流程
// 1. Setup
const providerScript = await fetch('http://localhost:3001/provider.js').then(r => r.text());
await page.addInitScript(providerScript);
await page.goto('https://example-dapp.com');
// 2. Connect wallet
await page.click('button:has-text("Connect Wallet")');
await new Promise(r => setTimeout(r, 500)); // Wait for requests
// 3. Approve connection
const pending = await wallet_getPendingRequests();
for (const req of pending) {
await wallet_approveRequest({ requestId: req.id });
}
// 4. Send transaction
await page.fill('input[name="amount"]', '1.5');
await page.click('button:has-text("Send")');
await new Promise(r => setTimeout(r, 500));
// 5. Approve transaction
const txRequests = await wallet_getPendingRequests();
const txReq = txRequests.find(r => r.method === 'eth_sendTransaction');
const result = await wallet_approveRequest({ requestId: txReq.id });
// 6. Verify
const receipt = await wallet_getTransactionReceipt({ hash: result.result });
console.log('Transaction confirmed:', receipt.status);发展
# Type check
npm run type-check
# Build everything
npm run build
# Build provider only
npm run build:provider
# Build with dev UI enabled
npm run build:provider:dev
# Watch mode
npm run dev项目结构
├── src/
│ ├── index.ts # Entry point, starts all services
│ ├── mcp-server.ts # MCP server with tools and resources
│ ├── ws-bridge.ts # WebSocket bridge for browser
│ ├── queue.ts # Request queue management
│ ├── wallet.ts # Viem wallet wrapper
│ ├── types.ts # TypeScript types
│ └── provider/
│ ├── injected.ts # Browser provider (EIP-1193)
│ └── ui/ # Developer wallet UI (optional)
├── dist/
│ ├── index.js # Compiled server
│ └── provider.js # Compiled browser provider
├── docs/
│ ├── LLM_INSTRUCTIONS.md
│ ├── TESTING_GUIDE.md
│ └── ...
└── examples/
├── test-dapp.html # Test dApp for development
└── bookmarklet.html # Manual injection bookmarklet文档
许可证
麻省理工学院
作者
Dennison Bertram
