Bitcoin MCP 服务器是一个基于 Go 语言开发的服务,借助模型上下文协议(MCP),通过各类 RPC 命令提供统一接口,实现与比特币节点的交互。它将比特币 RPC 功能进行逻辑分类,为访问区块链数据和操作提供简洁且文档完备的 API。
在使用 Bitcoin MCP 服务器之前,需要确保满足以下先决条件:
git clone https://github.com/moncho/bitcoin-mcp.git
cd bitcoin-mcp
go mod download
go build -o bitcoin-mcp
创建一个配置文件(config.json),其结构如下:
{
"bitcoin": {
"rpc_url": "http://localhost:8332",
"username": "your_rpc_username",
"password": "your_rpc_password"
},
"server": {
"port": 8080,
"host": "localhost"
}
}
bitcoind -server -rpcuser=your_rpc_username -rpcpassword=your_rpc_password
./bitcoin-mcp -config config.json
BITCOIN_RPC_URL=http://localhost:8332 \
BITCOIN_RPC_USER=your_rpc_username \
BITCOIN_RPC_PASS=your_rpc_password \
./bitcoin-mcp
服务器提供了用于访问比特币 RPC 命令的 RESTful API,以下是一些示例:
curl -X POST http://localhost:8080/api/v1/blockchain/info
curl -X POST http://localhost:8080/api/v1/blockchain/block \
-H "Content-Type: application/json" \
-d '{"blockhash": "0000000000000000000123456789abcdef", "verbose": true}'
curl -X POST http://localhost:8080/api/v1/mempool/info
bitcoin-mcp/
├── cmd/
│ └── server/ # 主服务器应用程序
├── pkg/
│ ├── bitcoin/ # 比特币 RPC 客户端
│ └── tools/ # RPC 命令实现
├── config.json # 配置文件
└── README.md # 本文件
pkg/bitcoin/client.go
中添加方法到比特币 RPC 客户端。pkg/tools/
目录下实现新的命令处理逻辑。server/main.go
。package tools
import (
"encoding/json"
"log"
"net/http"
"github.com/moncho/bitcoin-mcp/pkg/api"
)
func BlockExplorer(w http.ResponseWriter, r *http.Request) {
// 获取块哈希
blockHash := getBlockHashFromRequest(r)
// 获取块详细信息
blockData, err := api.GetBlockDetails(blockHash)
if err != nil {
log.Printf("无法获取块详细信息: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
// 返回 JSON 响应
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(blockData)
}
package main
import (
"log"
"github.com/moncho/bitcoin-mcp/pkg/tools"
)
func main() {
// 启动服务器并注册路由
http.HandleFunc("/api/v1/block/explorer", tools.BlockExplorer)
log.Fatal(http.ListenAndServe(":8080", nil))
}
git checkout -b feature/new-command
项目在 MIT License 下开源,欢迎自由使用和分发。