区块链交互服务器

Hyperledger Fabric MCP服务器是一个使AI助手能够与Hyperledger Fabric区块链网络交互的模型上下文协议服务器,支持查询、调用链码、获取区块信息等功能。

  • by adityajoshi12
  • TypeScript
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 adityajoshi12/fabric-mcp-server

使用说明

Hyperledger Fabric MCP服务器

模型上下文协议(MCP)服务器,使AI助手能够与Hyperledger Fabric区块链网络进行交互。

特性

  • 查询链码 -在智能合约上执行只读查询
  • 调用链码 -将交易提交到区块链
  • 获取区块信息 -检索特定区块的详细信息
  • 获取区块链信息 -获取网络统计数据(块高度、哈希值)
  • 获取交易历史记录 -查看特定资产的历史记录
  • 列出已注册的身份 -查看钱包身份
  • 链码生命周期管理 -查询已安装、已批准和已提交的链码
  • 提交准备情况检查 -提交前验证链码定义
  • 渠道管理 -列出频道并获取频道信息
  • 多种运输方式 -支持stdio和HTTP/SSE(用于web客户端)

先决条件

  • Node.js v18+
  • 正在运行的Hyperledger Fabric网络
  • 有效的连接配置文件JSON
  • 注册用户身份(证书+私钥)

安装

npm install

配置

选项1:环境变量

复制模板并配置:

cp env.template .env

编辑 .env 使用您的设置:

# Fabric Network Configuration
FABRIC_CHANNEL=mychannel
FABRIC_CHAINCODE=basic
FABRIC_MSP_ID=Org1MSP
FABRIC_WALLET_PATH=./wallet
FABRIC_CONNECTION_PROFILE=./connection-profile.json
FABRIC_USER_ID=appUser

# Transport Configuration (optional)
MCP_TRANSPORT=stdio    # "stdio" or "http"
MCP_PORT=3000          # HTTP port (only used when MCP_TRANSPORT=http)

选项2:Claude桌面配置

直接在中添加环境变量 claude_desktop_config.json:

{
  "mcpServers": {
    "hyperledger-fabric": {
      "command": "npx",
      "args": ["-y", "@adityajoshi12/fabric-mcp-server"],
      "env": {
        "FABRIC_CHANNEL": "mychannel",
        "FABRIC_CHAINCODE": "basic",
        "FABRIC_MSP_ID": "Org1MSP",
        "FABRIC_WALLET_PATH": "/path/to/wallet",
        "FABRIC_CONNECTION_PROFILE": "/path/to/connection-profile.json",
        "FABRIC_USER_ID": "appUser"
      }
    }
  }
}

钱包设置

使用您的证书和私钥创建钱包:

npm run create-wallet -- ./path/to/private.key ./path/to/cert.pem Org1MSP appUser

或者设置环境变量并运行:

export PRIVATE_KEY_PATH=./path/to/private.key
export CERTIFICATE_PATH=./path/to/cert.pem
npm run create-wallet

构建与运行

标准模式(默认)

# Build TypeScript
npm run build

# Run the server
npm start

# Or build and run together
npm run dev

HTTP模式(适用于Web客户端)

将MCP服务器作为支持SSE的HTTP服务器运行:

MCP_TRANSPORT=http MCP_PORT=8080 npm start

可用工具

1. invoke_chaincode

向区块链提交交易。

参数:

  • function (string):链码函数名
  • args (array):要传递的参数

例子:

CreateAsset with args: ["asset1", "blue", "20", "john", "500"]

2. query_chaincode

查询区块链(只读)。

参数:

  • function (string):链码函数名
  • args (array):要传递的参数

例子:

GetAllAssets with args: []
ReadAsset with args: ["asset1"]

3. get_block_info

获取特定区块的信息。

参数:

  • blockNumber (number):要检索的块号

4. get_blockchain_info

获取区块链统计数据,包括总区块数和哈希值。

参数: 无

5. get_transaction_history

获取资产的交易历史记录(需要链码支持)。

参数:

  • assetId (string):资产ID

6. list_enrolled_identities

列出钱包中的所有身份。

参数: 无

7. get_installed_chaincodes

获取对等设备上安装的链码列表。

参数: 无

8. get_approved_chaincode

获取组织的已批准链码定义。

参数:

  • chaincodeName (string):链码的名称

9. get_committed_chaincode

在通道上获取已提交的链码定义。

参数:

  • chaincodeName (string):链码的名称

10. check_commit_readiness

检查链码定义是否已准备好提交。

参数:

  • chaincodeName (string):链码的名称
  • sequence (number):链码定义的序列号
  • version (string):链码的版本

11. list_channels

列出对等方已加入的所有频道。

参数: 无

12. get_channel_info

获取特定频道的信息。

参数:

  • channelName (string):频道名称

AI助手使用示例

"How many blocks are in the network?"
→ Uses get_blockchain_info

"List all assets"
→ Uses query_chaincode with GetAllAssets

"Create an asset with ID 1001, color red, size 20, owner john, value 500"
→ Uses invoke_chaincode with CreateAsset

"Show me block 10"
→ Uses get_block_info with blockNumber 10

"What chaincodes are installed?"
→ Uses get_installed_chaincodes

"Is the 'basic' chaincode committed?"
→ Uses get_committed_chaincode with chaincodeName "basic"

"List all channels"
→ Uses list_channels

"Check commit readiness for chaincode 'mycc' version 1.0"
→ Uses check_commit_readiness

项目结构

fabric-mcp-server/
├── app.ts              # Main MCP server
├── create-wallet.ts    # Wallet creation utility
├── dist/               # Compiled JavaScript
├── wallet/             # Identity wallet
├── env.template        # Environment template
├── tsconfig.json       # TypeScript config
└── package.json        # Dependencies

故障排除

“钱包中找不到身份”

跑 npm run create-wallet 凭你的证件。

“找不到模块”错误

确保你已经跑过了 npm run build config中的路径是绝对的。

连接错误

验证您的连接配置文件路径以及结构网络是否正在运行。

用法

使用npx(不安装)

{
  "mcpServers": {
    "hyperledger-fabric": {
      "command": "npx",
      "args": ["-y", "fabric-mcp-server"],
      "env": {
        "FABRIC_CHANNEL": "mychannel",
        "FABRIC_CHAINCODE": "basic",
        "FABRIC_MSP_ID": "Org1MSP",
        "FABRIC_WALLET_PATH": "/path/to/wallet",
        "FABRIC_CONNECTION_PROFILE": "/path/to/connection-profile.json",
        "FABRIC_USER_ID": "appUser"
      }
    }
  }
}

本地安装

npm install -g fabric-mcp-server
{
  "mcpServers": {
    "hyperledger-fabric": {
      "command": "fabric-mcp-server",
      "env": {
        "FABRIC_CHANNEL": "mychannel",
        "FABRIC_CHAINCODE": "basic",
        "FABRIC_MSP_ID": "Org1MSP",
        "FABRIC_WALLET_PATH": "/path/to/wallet",
        "FABRIC_CONNECTION_PROFILE": "/path/to/connection-profile.json",
        "FABRIC_USER_ID": "appUser"
      }
    }
  }
}

使用SSE

{
  "mcpServers": {
    "hyperledger-fabric": {
      "url": "http://localhost:3000/mcp",
    }
  }
}

许可证

国际学生委员会