A blockchain indexer that continuously syncs Bitcoin Testnet 4 data into PostgreSQL, exposed via a REST API and an MCP server.
┌──────────────┐ RPC Calls ┌──────────────┐
│ QuickNode │◄───────────────────│ Indexer │
│ (Testnet4) │ (JSON-RPC) │ (Python) │
└──────────────┘ └──────┬───────┘
│
┌────────▼────────┐
│ PostgreSQL │
│ (blocks, │
│ transactions, │
│ outputs) │
└────────┬────────┘
│
┌─────────────┼─────────────┐
│ │
┌────────▼──────┐ ┌────────▼──────┐
│ REST API │ │ MCP Server │
│ (FastAPI) │ │ (FastMCP) │
│ :8000 │ │ :8001 │
└───────────────┘ └───────────────┘
The system runs as three Docker containers: an indexer that fetches blocks from QuickNode and writes to PostgreSQL, a FastAPI server for REST queries, and a FastMCP server for tool-based access from AI coding agents.
QuickNode RPC instead of a hosted node. Rather than running a full Bitcoin node, the indexer makes JSON-RPC calls to a QuickNode Testnet 4 endpoint. This avoids the infrastructure cost and sync time of maintaining a node while still getting reliable chain data.
Pydantic models for data validation. All data flowing through the system — from RPC responses to API responses — passes through Pydantic BaseModel classes (Block, Transaction, Output). This enforces type checking at the boundary: malformed RPC data is caught immediately, and API responses are guaranteed to match the declared schema.
FastMCP for AI agent access. The MCP server wraps the same CRUD operations as the REST API but exposes them as tools that coding agents (Claude, Cursor, opencode) can call directly. This lets you query the indexed blockchain data from within an AI-assisted workflow without writing HTTP requests.
Atomic block writes. Each block, its transactions, and their outputs are written in a single database transaction. If anything fails mid-write, the entire block is rolled back, preventing partial or corrupted data.
Historical sync then live polling. On startup, the indexer catches up from the last indexed height to the current chain tip. Once caught up, it polls every 5 minutes for new blocks.
- Continuous indexing of blocks, transactions, and outputs
- REST API with Swagger docs at
/docs - MCP server for AI agent integration
- Gap detection and backfill for missed blocks
- Retry logic for failed RPC calls (3 attempts per block)
- Docker and Docker Compose
- A QuickNode Testnet 4 RPC URL (or any Bitcoin RPC endpoint)
- (Optional)
psqlfor direct database queries
- Clone the repository:
git clone https://github.com/zane1502/bitcoin-testnet4-indexer.git
cd bitcoin-testnet4-indexer- Configure environment variables in
.env:
POSTGRES_USER=indexer
POSTGRES_PASSWORD=your_secure_password
POSTGRES_DB=bitcoin_testnet4
DATABASE_URL=postgresql://indexer:your_secure_password@postgres:5432/bitcoin_testnet4
BITCOIN_RPC_URL=https://your-quicknode-endpoint.btc-testnet4.quiknode.pro/your-api-key/- Start all services:
docker compose up -d- Check status:
docker compose logs -f apiThe indexer will begin syncing from block 0 to the current chain tip on first run.
| Method | Endpoint | Description |
|---|---|---|
| GET | /blocks/last-indexed-height |
Latest indexed block height |
| GET | /blocks/latest |
Most recently indexed block |
| GET | /blocks/height/{height} |
Block by height |
| GET | /blocks/block/{hash} |
Block by hash |
| GET | /blocks/{start}-{stop} |
Blocks in a height range |
| GET | /transactions/block/{height} |
Transactions in a block |
| GET | /transactions/{txid} |
Transaction by txid |
| GET | /outputs/block/{txid} |
Outputs for a transaction |
| GET | /outputs/address/{address} |
Outputs sent to an address |
Full interactive docs at http://localhost:8000/docs.
The MCP server runs on port 8001 and exposes the same query capabilities as tools:
mcp_get_block_by_heightmcp_get_block_by_hashmcp_fetch_blocks_by_rangemcp_get_last_indexed_heightmcp_get_transaction_by_txidmcp_get_transactions_by_block_heightmcp_get_outputs_by_txidmcp_get_outputs_by_address
opencode — add to opencode.json:
{
"mcp": {
"bitcoin-indexer": {
"type": "remote",
"url": "http://localhost:8001/mcp",
"enabled": true
}
}
}Claude Desktop — add to claude_desktop_config.json:
{
"mcpServers": {
"bitcoin-indexer": {
"url": "http://localhost:8001/mcp"
}
}
}To expose the MCP server remotely (e.g. from another machine), use ngrok:
ngrok http 8001Install psql and connect:
docker exec -it indexer-postgres psql -U indexer -d bitcoin_testnet4Example queries:
-- Latest indexed block
SELECT * FROM blocks ORDER BY height DESC LIMIT 1;
-- Total indexed
SELECT COUNT(*) AS blocks, (SELECT COUNT(*) FROM transactions) AS txs, (SELECT COUNT(*) FROM outputs) AS outputs FROM blocks;
-- Transactions in a block
SELECT * FROM transactions WHERE block_height = 100;
-- Balance for an address
SELECT address, SUM(value) AS total FROM outputs WHERE address = 'your_address' GROUP BY address;docker compose down # Stop containers (data persists)
docker compose down -v # Stop and delete all data