跳轉到主要內容
本教程介紹如何使用 ChainStream MCP Server 構建一個能夠查詢鏈上資料、分析市場並輔助交易決策的 AI 助理。
預計時間:30 分鐘
難度等級:⭐⭐ 入門

目標

構建一個具備鏈上感知能力的 AI 助理: 能力展示
  • ✅ 查詢 Token 價格和餘額
  • ✅ 分析錢包持倉
  • ✅ 追蹤 Smart Money

Step 1:配置 MCP

前置條件

  • ChainStream 賬戶(獲取 Client ID 和 Client Secret)
  • Claude Desktop 或 Cursor
  • Node.js 18+

安裝 MCP Server

npm install -g @chainstream/mcp-server

配置 Claude Desktop

編輯配置檔案:
# 配置文件路径
~/Library/Application Support/Claude/claude_desktop_config.json
新增配置:
{
  "mcpServers": {
    "chainstream": {
      "url": "https://mcp.chainstream.io/mcp",
      "headers": {
        "Authorization": "Bearer your_access_token"
      }
    }
  }
}
重啟 Claude Desktop。
詳細步驟請參考 MCP 配置指南

Step 2:對話示例

示例 1:檢視 ETH 價格

用户: 帮我看看 ETH 的价格

Claude: [调用 get_token_price 工具]

ETH 当前价格:
- 价格:$2,015.32
- 24h 变化:+2.5%
- 24h 交易量:$12.5B
- 市值:$242B

示例 2:分析錢包持倉

用户: 分析一下 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045 的持仓

Claude: [调用 get_token_balance 和 analyze_wallet 工具]

该地址(vitalik.eth)持仓分析:

持仓概览
| Token | 数量 | 价值 |
|-------|------|------|
| ETH | 1,234 | $2.48M |
| USDC | 50,000 | $50K |
| ENS | 10,000 | $25K |

钱包特征
- 钱包年龄:7+ 年
- 标签:核心开发者、长期持有者
- 主要活动:DeFi、NFT、捐赠

示例 3:追蹤 Smart Money

用户: 最近有哪些 Smart Money 在买入 ARB?

Claude: [调用 get_smart_money_activities 工具]

过去 24h Smart Money ARB 买入记录:

1. 0xabc...123 (Smart Score: 92)
   - 买入:500,000 ARB ($450K)
   - 时间:2小时前
   - 标签:DeFi 专家、高胜率交易者

2. 0xdef...456 (Smart Score: 85)
   - 买入:200,000 ARB ($180K)
   - 时间:5小时前
   - 标签:机构钱包

趋势分析:Smart Money 整体对 ARB 呈净买入态势

Step 3:高階用法

多步驟分析

用户: 首先查看这个地址的持仓,然后分析它最近的交易,判断它的投资风格

Claude: 好的,我来分步分析:

第一步:持仓查询
[调用 get_token_balance]
...

第二步:交易历史
[调用 get_transaction_history]
...

第三步:综合分析
[调用 analyze_wallet]

基于以上数据,该地址的投资风格是:
- 类型:中长期持有者
- 偏好:DeFi 蓝筹 + Layer2
- 风险偏好:中等
- 特点:善于在回调时加仓

對比分析

用户: 对比这两个地址的交易策略

Claude: [调用 analyze_wallet x2]

| 维度 | 地址 A | 地址 B |
|------|--------|--------|
| 交易频率 | 高(日均50笔)| 低(日均5笔)|
| 持仓风格 | 短线 | 长线 |
| 胜率 | 62% | 78% |
| 偏好板块 | Meme币 | DeFi |
| 风险等级 | 高 | 中 |

安全考量

策略限制

AI 助理僅提供資料查詢和分析,不直接執行交易。
用户: 帮我买入 100 USDC 的 ETH

Claude: 我可以帮你查询当前 ETH 价格和最佳交易路径,
但无法直接执行交易。

当前 ETH 价格:$2,015
建议执行方式:
1. Uniswap V3 - 预计滑点 0.1%
2. 1inch 聚合器 - 可能获得更优价格

请在你的钱包中手动执行交易。

人工確認

對於重要決策,AI 會提供分析但建議人工確認:
根據分析,多個 Smart Money 正在買入 ARB,但請注意:
  • 這不構成投資建議
  • 市場有風險,請獨立判斷
  • 建議結合更多資訊來源

擴充套件:自定義 Agent

使用 MCP SDK 構建

import Anthropic from '@anthropic-ai/sdk';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';

const anthropic = new Anthropic();

// 初始化 MCP Client
async function createMcpClient() {
  const transport = new SSEClientTransport(
    new URL('https://mcp.chainstream.io/sse'),
    {
      requestInit: {
        headers: {
          'Authorization': `Bearer ${process.env.CHAINSTREAM_ACCESS_TOKEN}`
        }
      }
    }
  );

  const client = new Client({
    name: 'custom-agent',
    version: '1.0.0'
  });

  await client.connect(transport);
  return client;
}

async function chat(userMessage) {
  const mcp = await createMcpClient();

  // 获取可用工具
  const { tools } = await mcp.listTools();

  // 转换为 Anthropic 格式
  const anthropicTools = tools.map(tool => ({
    name: tool.name,
    description: tool.description,
    input_schema: tool.inputSchema
  }));

  // 调用 Claude
  let response = await anthropic.messages.create({
    model: 'claude-sonnet-4-20250514',
    max_tokens: 1024,
    tools: anthropicTools,
    messages: [{ role: 'user', content: userMessage }]
  });

  // 处理工具调用
  while (response.stop_reason === 'tool_use') {
    const toolUse = response.content.find(c => c.type === 'tool_use');

    // 调用 MCP 工具
    const toolResult = await mcp.callTool({
      name: toolUse.name,
      arguments: toolUse.input
    });

    // 带工具结果继续对话
    response = await anthropic.messages.create({
      model: 'claude-sonnet-4-20250514',
      max_tokens: 1024,
      tools: anthropicTools,
      messages: [
        { role: 'user', content: userMessage },
        { role: 'assistant', content: response.content },
        {
          role: 'user',
          content: [{
            type: 'tool_result',
            tool_use_id: toolUse.id,
            content: JSON.stringify(toolResult.content)
          }]
        }
      ]
    });
  }

  await mcp.close();

  return response.content.find(c => c.type === 'text')?.text;
}

// 使用示例
const result = await chat('查看 Ethereum 上的 ETH 价格');
console.log(result);

可用 MCP 工具

工具描述
get_token_balance查詢地址代幣餘額
get_token_price獲取實時代幣價格
get_transaction_history查詢地址交易歷史
get_smart_money_activities獲取 Smart Money 活動
get_defi_positions查詢 DeFi 倉位
analyze_wallet分析錢包特徵
get_token_holders獲取代幣持有者分析

常見問題

  1. 確認 MCP Server 配置正確
  2. 重啟 Claude Desktop
  3. 檢查 Client ID 和 Client Secret 是否有效
  4. 嘗試更明確的提問,如”使用 ChainStream 查詢…”
鏈上資料查詢需要一定時間,特別是複雜分析。可以:
  1. 分步提問,減少單次查詢量
  2. 指定具體鏈減少查詢範圍
當前版本僅支援資料查詢,不支援直接執行交易。這是為了安全考慮。如需自動交易,建議基於 AI 分析結果,透過獨立的交易系統執行。

相關文件

MCP 配置指南

詳細配置步驟

MCP 工具目錄

檢視所有可用工具