The onchain-mcp submodule implements a Model Context Protocol (MCP) server for Bankless Onchain. This server serves as a middleware between AI models (LLMs) and blockchain data, allowing LLMs to read contract state, fetch events, and access transaction information across multiple blockchain networks.
-
MCP Server Implementation
- Built using TypeScript
- Uses the
@modelcontextprotocol/sdkfor implementing the MCP standard - Exposes a standardized API for blockchain operations
-
Server Transport
- Implemented as a stdio server transport interface
- Communication with LLMs is handled through stdin/stdout
-
Operational Modules
- Contracts: Reading contract state, fetching ABIs, proxies, and source code
- Events: Fetching blockchain event logs and building event topic signatures
- Transactions: Fetching transaction history and detailed transaction information
- Blocks: Fetching detailed block information by number or hash
- Tokens: Fetching token balances for addresses
-
Runtime Dependencies
@modelcontextprotocol/sdk: Core SDK for implementing MCPaxios: HTTP client for API requestszod: Schema validationzod-to-json-schema: Converts Zod schemas to JSON Schema for tool documentation
-
Dev Dependencies
typescript: For static type checkingshx: For cross-platform shell commands in npm scripts
-
Read Contract State
- Allows reading state from any contract on supported blockchain networks
- Supports method calls with typed inputs and outputs
- Schema:
ReadContractSchema
-
Get Proxy Implementation
- Retrieves the implementation address for proxy contracts
- Schema:
GetProxySchema
-
Get Contract ABI
- Fetches the Application Binary Interface (ABI) for a contract
- Schema:
GetAbiSchema
-
Get Contract Source
- Retrieves the source code and compilation metadata for a contract
- Schema:
GetSourceSchema
-
Get Event Logs
- Fetches event logs filtered by contract addresses and event topics
- Supports optional additional topic filters
- Schema:
GetEventLogsSchema
-
Build Event Topic
- Creates an event signature hash (topic0) based on event name and parameter types
- Schema:
BuildEventTopicSchema
-
Get Transaction History
- Retrieves transaction history for a specific address
- Supports optional filtering by contract, method ID, and starting block
- Schema:
TransactionHistorySchema
-
Get Transaction Info
- Fetches detailed information about a specific transaction
- Includes execution status, gas usage, and transaction receipt
- Schema:
TransactionInfoSchema
- Get Block Info
- Fetches detailed information about a specific block
- Can be queried by block number or block hash
- Includes timestamp, gas used/limit, transactions, and other block data
- Schema:
BlockInfoSchema
-
Get Native Balance
- Retrieves the native token balance for an address on a specified blockchain
- Schema:
NativeBalanceSchema
-
Get Token Balances
- Retrieves all token balances for an address on a specific network
- Schema:
TokenBalancesOnNetworkSchema
- API Authentication: Requires a Bankless API token stored in the
BANKLESS_API_TOKENenvironment variable - Base URL: https://api.bankless.com/
- Error Handling: Comprehensive error handling with specific error classes:
BanklessError: Base error classBanklessValidationError: For input validation errorsBanklessAuthenticationError: For authentication failuresBanklessRateLimitError: For rate limit issues (includes reset timestamp)BanklessResourceNotFoundError: For 404 not found errors
-
Tool Registration
- On startup, the server registers available tools with descriptions and input schemas
- Uses
zodToJsonSchemato convert Zod validation schemas to JSON Schema format
-
Request Handling
- Receives tool call requests from the LLM
- Validates input parameters using Zod schemas
- Makes API calls to the Bankless API
- Returns structured results to the LLM
-
Error Formatting
- Formats errors in a user-friendly way for the LLM to understand
- Preserves error type information to help LLMs retry or provide better guidance
The server requires a Bankless API token set as an environment variable:
const token = process.env.BANKLESS_API_TOKEN;
if (!token) {
throw new BanklessAuthenticationError('BANKLESS_API_TOKEN environment variable is not set');
}All API requests follow a similar pattern:
try {
const response = await axios.post/get(
endpoint,
payload,
{
headers: {
'Content-Type': 'application/json',
'X-BANKLESS-TOKEN': `${token}`
}
}
);
return response.data;
} catch (error) {
// Standardized error handling
}