MCP Blockchain Server

hybrid server

The server is able to function both locally and remotely, depending on the configuration or use case.

Integrations

  • Handles environment variable configuration for the MCP Blockchain Server

  • Provides containerization support for running the PostgreSQL database, Redis cache, MCP Server, and Web DApp as a complete system

  • Enables reading balances, contract state, and other on-chain data from the Ethereum blockchain, as well as preparing unsigned transactions for user approval and signing

MCP 区块链服务器和 DApp

一个安全的系统,使人工智能助手能够与区块链智能合约交互,同时确保用户完全控制他们的私钥和交易签名。

概述

该项目解决了人工智能区块链集成中的一个关键挑战:允许人工智能助手读取区块链数据并准备交易,同时确保用户对交易签名和私钥保持独占控制。

该系统包括:

  1. MCP 服务器:模型上下文协议服务器,将区块链操作公开为可供 AI 助手使用的工具
  2. Web DApp :一个 React 应用程序,提供用于钱包连接和交易签名的用户界面
  3. 数据库:PostgreSQL 数据库,用于存储用户、API 密钥和交易记录
  4. 缓存:Redis 用于缓存经常访问的数据

特征

MCP 服务器功能

  • 区块链数据访问:读取余额、合约状态和其他链上数据
  • 交易准备:创建未签名的交易以供用户批准
  • 多链支持:可与以太坊、Polygon 和其他 EVM 兼容链配合使用
  • 智能合约交互:从支持网络上已验证的智能合约中读取
  • 安全第一的设计:私钥永远不会离开用户的钱包

Web DApp 功能

  • 钱包集成:连接 MetaMask 和其他 Web3 钱包
  • 交易审查:清晰的用户界面,用于在签名前审查交易详情
  • 交易签名:使用连接的钱包签署交易
  • 交易跟踪:监控已提交交易的状态
  • 移动兼容性:响应式设计适用于所有设备

安全原则

  1. 私钥隔离:密钥永远不会离开用户的钱包
  2. 交易验证:清晰的用户界面,用于审查交易详情
  3. API 身份验证:安全 API 密钥管理
  4. 速率限制:防止滥用
  5. 输入验证:清理所有输入
  6. 审计日志:跟踪所有操作
  7. 仅 HTTPS :安全通信
  8. 内容安全策略:防止XSS

交易流程

  1. AI助手通过MCP服务器请求交易
  2. MCP 服务器使用 UUID 准备未签名交易
  3. MCP 服务器返回交易 URL 给 AI 助手
  4. AI助手向用户提供URL
  5. 用户在浏览器中打开 URL
  6. 用户连接钱包并查看交易详情
  7. 用户使用钱包批准并签署交易
  8. Web DApp 将签名的交易提交至区块链
  9. 交易状态已更新并跟踪

入门

先决条件

  • Node.js(v18 或更高版本)
  • npm 或 yarn
  • PostgreSQL
  • Redis(可选,用于缓存)
  • Infura API 密钥(用于区块链访问)
  • Etherscan API 密钥(用于合约 ABI)

安装

  1. 克隆存储库:
git clone https://github.com/zhangzhongnan928/mcp-blockchain-server.git cd mcp-blockchain-server
  1. 安装依赖项:
npm install # or yarn install
  1. 设置环境变量:在根目录中创建一个.env文件(或从.env.example复制):
cp .env.example .env # Edit .env with your configurations
  1. 设置数据库:
# For detailed instructions, see the Database Setup Guide # docs/database-setup.md # Create the PostgreSQL database createdb mcp_blockchain # Run database migrations npm run db:migrate # or yarn db:migrate

有关安装和配置 PostgreSQL 的详细说明,请参阅数据库设置指南

  1. 启动服务器:
npm run dev # or yarn dev

使用 Docker Compose

快速开始使用 Docker:

# Create .env file with required environment variables cp .env.example .env # Edit .env with your configurations # Start the services docker-compose up -d

这将开始:

  • PostgreSQL 数据库
  • Redis 缓存
  • MCP 服务器
  • Web DApp

发展

服务器结构

  • src/mcp :MCP 服务器实现
  • src/services :核心业务逻辑服务
  • src/utils :实用程序函数
  • src/index.ts :主入口点

Web DApp 结构

  • web/src/components :React 组件
  • web/src/hooks :自定义 React hooks
  • web/src/services :API 服务
  • web/src/pages :页面组件

使用 MCP 服务器

MCP 服务器提供了几种可供 AI 助手使用的工具:

  • get-chains :获取支持的区块链网络列表
  • get-balance :获取地址的账户余额
  • read-contract :从智能合约读取数据
  • prepare-transaction :准备未签名的交易以供用户批准
  • get-transaction-status :获取交易的当前状态

工具使用示例

// Example of using the get-balance tool const result = await callTool("get-balance", { chainId: "1", address: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e" });

故障排除

如果您遇到依赖关系问题:

# MCP SDK issue - install directly from GitHub npm uninstall @modelcontextprotocol/sdk npm install modelcontextprotocol/typescript-sdk

有关数据库连接问题,请参阅数据库设置指南

执照

该项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅LICENSE文件。

-
security - not tested
A
license - permissive license
-
quality - not tested

一个安全的系统,使人工智能助手能够与区块链数据交互并准备交易,同时确保用户对其私钥和交易签名保持独占控制。

  1. Overview
    1. Features
      1. MCP Server Features
      2. Web DApp Features
    2. Security Principles
      1. Transaction Flow
        1. Getting Started
          1. Prerequisites
          2. Installation
          3. Using Docker Compose
        2. Development
          1. Server Structure
          2. Web DApp Structure
        3. Using the MCP Server
          1. Example Tool Usage
        4. Troubleshooting
          1. License
            ID: 1hfkunwb7p