以太坊地址信息服务

提供跨链以太坊地址信息查询及实时更新的服务器,支持Server-Sent Events (SSE)协议。

  • by doronaviguy
  • JavaScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 doronaviguy/mpc-0x

使用说明

MCP以太坊地址信息服务器

此服务器使用模型上下文协议(MCP)提供有关多个链上以太坊地址的信息。它包括一个用于实时更新的服务器发送事件(SSE)端点。

目录

设置

  1. 克隆存储库:

    git clone <repository-url>
    cd mcp-0x-address
    
  2. 安装依赖项:

    npm install
    
  3. 创建一个 .env 包含以下变量的文件:

    MCP_PORT=3002
    

运行服务器

启动HTTP MCP服务器:

npm run start:http

这将在端口3002(或您的 .env 文件)。

可用端点

服务器提供以下端点:

  • GET /health -服务器健康检查
  • POST /mcp -用于工具调用的MCP端点
  • GET /sse -用于实时更新的服务器发送事件端点
  • GET /sse/clients -获取有关连接的SSE客户端的信息
  • POST /sse/subscribe/:clientId -订阅地址更新
  • POST /sse/unsubscribe/:clientId -取消订阅地址更新

使用SSE端点

SSE端点允许客户端从服务器接收实时更新。以下是如何使用它:

  1. 连接到SSE端点
  2. 从连接响应中获取您的客户端ID
  3. 订阅特定地址
  4. 接收这些地址的实时更新

使用Curl进行测试

1.连接到SSE端点

curl -N http://localhost:3002/sse

这将建立与SSE端点的连接并开始接收事件。连接将保持打开状态,直到您手动终止它。

2.检查已连接的客户端

curl http://localhost:3002/sse/clients

3.订阅地址更新

连接到SSE端点后,您将收到一个客户端ID。使用该ID订阅地址更新:

curl -X POST \
  http://localhost:3002/sse/subscribe/YOUR_CLIENT_ID \
  -H "Content-Type: application/json" \
  -d '{"addresses": ["0x742d35Cc6634C0532925a3b844Bc454e4438f44e", "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"]}'

替换 YOUR_CLIENT_ID 使用连接到SSE端点时收到的客户端ID。

4.取消订阅地址更新

curl -X POST \
  http://localhost:3002/sse/unsubscribe/YOUR_CLIENT_ID \
  -H "Content-Type: application/json" \
  -d '{"addresses": ["0x742d35Cc6634C0532925a3b844Bc454e4438f44e"]}'

5.触发地址更新

要触发地址更新(将发送给订阅的客户端),请调用 get-address-info 工具:

curl -X POST \
  http://localhost:3002/mcp \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/call",
    "params": {
      "name": "get-address-info",
      "arguments": {
        "address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e"
      }
    }
  }'

6.检查服务器运行状况

curl http://localhost:3002/health

7.测试Ping工具

curl -X POST \
  http://localhost:3002/mcp \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "tools/call",
    "params": {
      "name": "ping",
      "arguments": {}
    }
  }'

工作流示例

以下是测试SSE功能的完整工作流程:

  1. 启动服务器:

    npm run start:http
    
  2. 在新终端中,连接到SSE端点:

    curl -N http://localhost:3002/sse
    

    您将收到如下回复:

    data: {"type":"connection","clientId":"client-1234567890abcdef","message":"Connected to MCP SSE endpoint","timestamp":"2023-01-01T00:00:00.000Z"}
    
  3. 注意 clientId 从回应中。

  4. 在另一个终端中,订阅地址更新:

    curl -X POST \
      http://localhost:3002/sse/subscribe/client-1234567890abcdef \
      -H "Content-Type: application/json" \
      -d '{"addresses": ["0x742d35Cc6634C0532925a3b844Bc454e4438f44e"]}'
    
  5. 触发地址更新:

    curl -X POST \
      http://localhost:3002/mcp \
      -H "Content-Type: application/json" \
      -d '{
        "jsonrpc": "2.0",
        "id": 1,
        "method": "tools/call",
        "params": {
          "name": "get-address-info",
          "arguments": {
            "address": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e"
          }
        }
      }'
    
  6. 在您连接到SSE端点的终端中,您将看到地址的更新。

自动测试脚本

为了进行更自动化的测试,您可以使用以下bash脚本:

#!/bin/bash

# Start SSE connection in the background and capture the output
curl -N http://localhost:3002/sse > sse_output.txt &
SSE_PID=$!

# Wait a moment for the connection to establish
sleep 2

# Extract the client ID from the output
CLIENT_ID=$(grep -o '"clientId":"[^"]*"' sse_output.txt | head -1 | cut -d'"' -f4)

if [ -z "$CLIENT_ID" ]; then
  echo "Failed to get client ID"
  kill $SSE_PID
  exit 1
fi

echo "Connected with client ID: $CLIENT_ID"

# Subscribe to an address
curl -X POST \
  http://localhost:3002/sse/subscribe/$CLIENT_ID \
  -H "Content-Type: application/json" \
  -d '{"addresses": ["0x742d35Cc6634C0532925a3b844Bc454e4438f44e"]}'

echo "Subscribed to address. Waiting for updates..."
echo "Press Ctrl+C to stop"

# Keep the script running to see updates
tail -f sse_output.txt

# Clean up on exit
trap "kill $SSE_PID; rm sse_output.txt" EXIT

将此另存为 test_sse.sh,使其可执行 chmod +x test_sse.sh,并运行它 ./test_sse.sh.