getChainById
Retrieve blockchain details by chain ID. Input a numeric chain identifier to get name, native currency, RPC endpoints, and explorers in Markdown format.
Instructions
Retrieve information about a blockchain by its chain ID, returned as Markdown.
**Parameters**:
- `chain_id` (integer): The unique identifier of the blockchain (e.g., 1 for Ethereum Mainnet).
**Returns**:
- A Markdown-formatted string containing the chain's details (Name, Chain ID, Native Currency, TVL, RPC Endpoints, Explorers) or an error message if no chain is found.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| chain_id | Yes |
Implementation Reference
- main.py:67-81 (handler)The main handler function for the 'getChainById' tool, decorated with @mcp.tool() for registration. It fetches chain data, searches for the chain by ID, formats the details as Markdown using helper functions, or returns an error if not found. The docstring provides the input schema (chain_id: int) and output description.async def getChainById(chain_id: int) -> str: """ Retrieve information about a blockchain by its chain ID, returned as Markdown. **Parameters**: - `chain_id` (integer): The unique identifier of the blockchain (e.g., 1 for Ethereum Mainnet). **Returns**: - A Markdown-formatted string containing the chain's details (Name, Chain ID, Native Currency, TVL, RPC Endpoints, Explorers) or an error message if no chain is found. """ chains = await fetch_chain_data() for chain in chains: if chain.get("chainId") == chain_id: return format_chain_as_markdown(chain) return f"**Error**: No chain found with ID {chain_id}"
- main.py:14-22 (helper)Helper function to fetch and cache the list of all chains from the Chainlist API, used by getChainById.async def fetch_chain_data() -> list[Dict[str, Any]]: """Fetch and cache chain data from Chainlist API.""" global chain_data if chain_data is None: async with httpx.AsyncClient() as client: response = await client.get("https://chainlist.org/rpcs.json") response.raise_for_status() chain_data = response.json() return chain_data
- main.py:24-64 (helper)Helper function to format a single chain's data into a Markdown string with tables for RPC endpoints and explorers, used by getChainById.def format_chain_as_markdown(chain: Dict[str, Any]) -> str: """ Format a single chain's details as Markdown, extracting specified fields with RPC and Explorers as tables. """ # Extract required fields name = chain.get('name', 'N/A') chain_id = chain.get('chainId', 'N/A') native_currency = chain.get('nativeCurrency', {}) tvl = chain.get('tvl', 'N/A') rpc_list = chain.get('rpc', []) explorers_list = chain.get('explorers', []) # Format native currency currency_info = f"{native_currency.get('name', 'N/A')} ({native_currency.get('symbol', 'N/A')}, {native_currency.get('decimals', 'N/A')} decimals)" if native_currency else 'N/A' # Format RPC list as a table rpc_data = [[rpc.get('url', 'N/A'), rpc.get('tracking', 'N/A')] for rpc in rpc_list] rpc_output = "**RPC Endpoints**:\n" if rpc_data: rpc_output += tabulate(rpc_data, headers=["URL", "Tracking"], tablefmt="pipe") else: rpc_output += "None" # Format explorers list as a table explorers_data = [[explorer.get('name', 'N/A'), explorer.get('url', 'N/A'), explorer.get('standard', 'N/A')] for explorer in explorers_list] explorers_output = "**Explorers**:\n" if explorers_data: explorers_output += tabulate(explorers_data, headers=["Name", "URL", "Standard"], tablefmt="pipe") else: explorers_output += "None" # Combine all fields return f"""**Chain Details** - **Name**: {name} - **Chain ID**: {chain_id} - **Native Currency**: {currency_info} - **TVL**: {tvl} {rpc_output} {explorers_output} """