get_contract_abi
Retrieve the ABI (Application Binary Interface) of a smart contract using its address and chain ID to format function calls and interpret contract data effectively.
Instructions
Get smart contract ABI (Application Binary Interface). An ABI defines all functions, events, their parameters, and return types. The ABI is required to format function calls or interpret contract data.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| address | Yes | Smart contract address | |
| chain_id | Yes | The ID of the blockchain |
Implementation Reference
- The core asynchronous handler function for the 'get_contract_abi' tool. It resolves the Blockscout URL for the given chain_id, fetches the contract ABI from the API endpoint, processes the response, reports progress, and returns a structured ToolResponse containing the ABI.@log_tool_invocation async def get_contract_abi( chain_id: Annotated[str, Field(description="The ID of the blockchain")], address: Annotated[str, Field(description="Smart contract address")], ctx: Context, ) -> ToolResponse[ContractAbiData]: """ Get smart contract ABI (Application Binary Interface). An ABI defines all functions, events, their parameters, and return types. The ABI is required to format function calls or interpret contract data. """ # noqa: E501 api_path = f"/api/v2/smart-contracts/{address}" # Report start of operation await report_and_log_progress( ctx, progress=0.0, total=2.0, message=f"Starting to fetch contract ABI for {address} on chain {chain_id}...", ) base_url = await get_blockscout_base_url(chain_id) # Report progress after resolving Blockscout URL await report_and_log_progress( ctx, progress=1.0, total=2.0, message="Resolved Blockscout instance URL. Fetching contract ABI...", ) response_data = await make_blockscout_request(base_url=base_url, api_path=api_path) # Report completion await report_and_log_progress( ctx, progress=2.0, total=2.0, message="Successfully fetched contract ABI.", ) # Extract the ABI from the API response as it is abi_data = ContractAbiData(abi=response_data.get("abi")) return build_tool_response(data=abi_data)
- blockscout_mcp_server/server.py:173-176 (registration)Registration of the 'get_contract_abi' tool with the FastMCP server instance, specifying structured_output=False and custom annotations.mcp.tool( structured_output=False, annotations=create_tool_annotations("Get Contract ABI"), )(get_contract_abi)
- Pydantic model defining the output data structure for the 'get_contract_abi' tool, containing the contract ABI as a list of dictionaries.# --- Model for get_contract_abi Data Payload --- class ContractAbiData(BaseModel): """A structured representation of a smart contract's ABI.""" abi: list[dict[str, Any]] | None = Field( description="The Application Binary Interface (ABI) of the smart contract." )