Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ language, description, and tags to help you find what you need quickly.
| [typescript/bnbchain-mcp](./typescript/bnbchain-mcp) | TypeScript | AI-powered blockchain assistant using Claude | AI, BSC, MCP |
| [typescript/eliza-chatbot](./typescript/eliza-chatbot) | TypeScript | A chatbot example using Eliza plugin-bnb | AI, BSC, opBNB |
| [typescript/ai-trading-assistant](./typescript/ai-trading-assistant) | Typescript | AI-powered trading assistant for BNB Chain ecosystem with real USDT→BNB swaps via PancakeSwap, technical analysis, and natural language interface | BNBChain, trading, analysis, PancakeSwap, AI, MCP |
| [go/mcp-bnb-wallet-agent](./go/mcp-bnb-wallet-agent) | Go | This is an example that contains two mcp server, bnb agent and wallet, and a mcp client that has langchain from open ai | BSC, opBNB, AI, Wallet |
More examples are coming soon—stay tuned for updates!

## How to Add a New Example
Expand Down
13 changes: 13 additions & 0 deletions go/mcp-bnb-wallet-agent/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# MongoDB Connection String
# Default: mongodb://localhost:27017
MONGO_URI=mongodb://localhost:27017

# BNB Chain RPC Endpoint
# For BSC Testnet (recommended for testing):
BNB_RPC=https://data-seed-prebsc-1-s1.binance.org:8545

# For BSC Mainnet (production):
# BNB_RPC=https://bsc-dataseed.binance.org/

# MCP Server Port (optional, defaults to 8085)
PORT=8085
39 changes: 39 additions & 0 deletions go/mcp-bnb-wallet-agent/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool
*.out

# Go workspace file
go.work
go.work.sum

# Dependency directories
vendor/

# Environment files
.env
.env.local

# IDE specific files
.idea/
.vscode/
*.swp
*.swo
*~

# OS specific files
.DS_Store
Thumbs.db

# Build artifacts
bin/
dist/

240 changes: 240 additions & 0 deletions go/mcp-bnb-wallet-agent/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
# BNB Chain Wallet MCP Server

A Model Context Protocol (MCP) server implementation for managing BNB Chain wallets. This example demonstrates how to build an MCP server that provides wallet creation, transaction signing, and balance checking capabilities for BNB Smart Chain.

## Overview

This project provides:
- **MCP Server**: Exposes wallet operations as MCP tools that can be used by AI agents
- **MongoDB Integration**: Stores user wallets securely with their private keys
- **BNB Chain Support**: Works with both BSC Mainnet (chain ID 56) and BSC Testnet (chain ID 97)
- **Example Client**: Shows how to interact with the MCP server programmatically

## Features

### Available MCP Tools

1. **create_wallet** - Generate a new Ethereum-compatible wallet for a user
2. **read_wallet** - Retrieve a user's public wallet address
3. **get_wallet_balance** - Check the BNB balance of a wallet
4. **transfer_asset** - Send native BNB to another address
5. **sign_transaction** - Sign and broadcast custom transactions

## Prerequisites

- Go 1.23 or higher
- MongoDB (local or remote instance)
- BNB Chain RPC endpoint (testnet or mainnet)

## Installation

1. Clone the repository:
```bash
git clone <repository-url>
cd go/mcp-bnb-wallet-agent
```

2. Install dependencies:
```bash
go mod download
```

3. Set up environment variables:
```bash
# MongoDB connection string
export MONGO_URI="mongodb://localhost:27017"

# BNB Chain RPC endpoint
export BNB_RPC="https://data-seed-prebsc-1-s1.binance.org:8545" # Testnet
# Or for mainnet: https://bsc-dataseed.binance.org/

# Optional: Custom server port (defaults to 8085)
export PORT="8085"
```

## Running the Project

### Start the MCP Server

```bash
cd wallet-mcp
go run main.go
```

The server will start on `http://localhost:8085/mcp`

You should see output like:
```
Connected to MongoDB successfully
Registered MCP tool: create_wallet
Registered MCP tool: read_wallet
Registered MCP tool: sign_transaction
Registered MCP tool: transfer_asset
Registered MCP tool: get_wallet_balance
Starting MCP server on port 8085...
```

### Run the Example Client

In a separate terminal:

```bash
go run example_client.go
```

This demonstrates how to:
- Create a wallet for a user
- Retrieve the wallet address
- Check wallet balance
- Transfer BNB to another address
- Sign custom transactions

## Usage

### Creating a Wallet

```go
client := NewMCPClient("http://localhost:8085/mcp")

walletAddress, err := client.CallTool("create_wallet", map[string]interface{}{
"user_id": "user_12345",
})
if err != nil {
log.Fatal(err)
}
```

### Checking Balance

```go
balance, err := client.CallTool("get_wallet_balance", map[string]interface{}{
"user_id": "user_12345",
})
if err != nil {
log.Fatal(err)
}
```

### Transferring BNB

```go
txHash, err := client.CallTool("transfer_asset", map[string]interface{}{
"user_id": "user_12345",
"chain_id": "97", // BSC Testnet
"to_address": "0x5A2D55362b3ce1Bb5434c16a2aBd923c429a3446",
"amount": "100000000000000", // 0.0001 BNB in wei
})
if err != nil {
log.Fatal(err)
}
```

## Project Structure

```
mcp-bnb-wallet-agent/
├── wallet-mcp/
│ ├── main.go # MCP server entry point
│ ├── db/
│ │ └── mongodb.go # MongoDB connection utilities
│ ├── types/
│ │ └── types.go # Type definitions
│ └── functions/
│ ├── structs.go # Data structures
│ ├── create.go # Wallet creation
│ ├── read.go # Wallet reading
│ ├── sign.go # Transaction signing
│ ├── transferAsset.go # BNB transfers
│ ├── walletBalance.go # Balance checking
│ ├── mcpToolGenerator.go # MCP tool registration
│ └── tests/ # Unit tests
├── example_client.go # Example usage
├── go.mod # Go module definition
└── README.md # This file
```

## Testing

Run the test suite:

```bash
cd wallet-mcp/functions
go test ./tests/... -v
```

**Note**: Tests require:
- MongoDB running on `localhost:27017`
- `BNB_RPC` environment variable set
- Sufficient testnet BNB in test wallets for transaction tests

## Security Considerations

⚠️ **Important**: This is an example project for educational purposes.

- Private keys are stored unencrypted in MongoDB
- In production, you should:
- Encrypt private keys before storage
- Use hardware security modules (HSM) or secure enclaves
- Implement proper access controls
- Add rate limiting
- Use secure key management solutions

## Chain IDs

- **BSC Mainnet**: 56
- **BSC Testnet**: 97

## API Reference

### MCP Tools

#### create_wallet
- **Description**: Create a new BNB Chain wallet for a user
- **Parameters**:
- `user_id` (string, required): Unique identifier for the user
- **Returns**: Public wallet address

#### read_wallet
- **Description**: Retrieve a user's public wallet address
- **Parameters**:
- `user_id` (string, required): Unique identifier for the user
- **Returns**: Public wallet address

#### get_wallet_balance
- **Description**: Get the native BNB balance of a user's wallet
- **Parameters**:
- `user_id` (string, required): Unique identifier for the user
- **Returns**: Balance in wei (string)

#### transfer_asset
- **Description**: Transfer native BNB to another address
- **Parameters**:
- `user_id` (string, required): Unique identifier for the user
- `chain_id` (string, required): Chain ID (e.g., "56" for mainnet, "97" for testnet)
- `to_address` (string, required): Recipient wallet address
- `amount` (string, required): Amount to send in wei
- **Returns**: Transaction hash

#### sign_transaction
- **Description**: Sign and broadcast a transaction to BNB Chain
- **Parameters**:
- `user_id` (string, required): Unique identifier for the user
- `chain_id` (string, required): Chain ID
- `to_address` (string, required): Destination address
- `data` (string, optional): Hex-encoded transaction data
- `value` (string, optional): Amount of BNB to send in wei
- **Returns**: Transaction hash

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is part of the BNB Chain examples repository.

## Resources

- [BNB Chain Documentation](https://docs.bnbchain.org/)
- [MCP Protocol Specification](https://modelcontextprotocol.io/)
- [Go Ethereum Documentation](https://geth.ethereum.org/docs)
56 changes: 56 additions & 0 deletions go/mcp-bnb-wallet-agent/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module mcp-bnb-wallet-agent

go 1.23.0

toolchain go1.24.4

require (
github.com/ethereum/go-ethereum v1.16.2
github.com/mark3labs/mcp-go v0.37.0
github.com/stretchr/testify v1.10.0
go.mongodb.org/mongo-driver/v2 v2.3.0
)

require (
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/bits-and-blooms/bitset v1.20.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/consensys/gnark-crypto v0.18.0 // indirect
github.com/crate-crypto/go-eth-kzg v1.3.0 // indirect
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/ethereum/c-kzg-4844/v2 v2.1.0 // indirect
github.com/ethereum/go-verkle v0.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/golang/snappy v1.0.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/holiman/uint256 v1.3.2 // indirect
github.com/invopop/jsonschema v0.13.0 // indirect
github.com/klauspost/compress v1.17.6 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/supranational/blst v0.3.14 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
golang.org/x/crypto v0.36.0 // indirect
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 // indirect
golang.org/x/sync v0.12.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/text v0.23.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading