Skip to main content

Add your description here

Project description

DeFiLlama MCP

DeFiLlama MCP Logo

License: MIT Python Version Docker

Overview

DeFiLlama MCP is a powerful and flexible tool that provides a microservice-based API wrapper around the DeFi Llama ecosystem. It leverages the FastMCP framework to transform DeFi Llama's comprehensive DeFi data into easily accessible tool endpoints that can be integrated with various AI applications, including LLM agents and autonomous systems.

This project serves as a bridge between the rich data sources provided by DeFi Llama and the emerging needs of AI-driven applications in the Web3 space. By wrapping DeFi Llama's APIs in a standardized MCP (Microservice Communication Protocol) format, developers can quickly integrate real-time DeFi data into their AI systems without dealing with the complexities of direct API integration.

Features

  • Protocol Data Access: Retrieve comprehensive information about DeFi protocols including TVL (Total Value Locked) metrics.
  • Blockchain Analytics: Access historical TVL data for specific blockchains to analyze trends and growth patterns.
  • Token Price Tracking: Fetch current price information for various tokens across multiple chains.
  • Liquidity Pool Data: Get detailed insights into liquidity pools, including TVL and other critical metrics.
  • Standardized Interface: All data is accessible through a consistent API pattern, making it easy to integrate with AI systems.
  • Docker Support: Ready-to-deploy containerization for easy implementation in any environment.
  • Server-Sent Events: Real-time data updates using SSE transport mechanism.

Architecture

DeFiLlama MCP is built using a modular architecture that separates the data-fetching logic from the API interface. The core components include:

  1. API Clients: Specialized HTTP clients for each of DeFi Llama's API endpoints (Main, Coins, Yields).
  2. MCP Tools: Function-based tools that transform raw API responses into structured data for consumption by AI agents.
  3. FastMCP Server: A lightweight server that exposes the tools via HTTP and SSE (Server-Sent Events).
  4. Error Handling: Robust error management to ensure reliability even when upstream services experience issues.

Installation

Prerequisites

  • Python 3.13 or higher
  • uv (Python package installer and environment manager)

Option 1: Local Installation

# Clone the repository
git clone https://github.com/demcp/defillama-mcp.git
cd defillama-mcp

# Create a virtual environment and install dependencies
uv venv
uv pip install -e .

# Run the server
uv run defillama.py

Option 2: Docker Installation

# Clone the repository
git clone https://github.com/demcp/defillama-mcp.git
cd defillama-mcp

# Build the Docker image
docker build -t defillama-mcp .

# Run the container
docker run -p 8080:8080 defillama-mcp

Usage

Once the server is running, it exposes several endpoints that can be used to interact with DeFi Llama data:

Available Tools

  • get_protocols: Retrieve information about top DeFi protocols.
  • get_protocol_tvl: Get TVL data for a specific protocol.
  • get_chain_tvl: Access historical TVL data for a specific blockchain.
  • get_token_prices: Obtain current price information for specific tokens.
  • get_pools: List available liquidity pools.
  • get_pool_tvl: Get detailed information about a specific liquidity pool.

Example: Using with Python

import httpx

# Query the MCP server for protocol data
async def get_protocol_data(protocol_name: str):
    async with httpx.AsyncClient() as client:
        response = await client.post(
            "http://localhost:8080/tools/get_protocol_tvl",
            json={"protocol": protocol_name}
        )
        return response.json()

# Usage
import asyncio
result = asyncio.run(get_protocol_data("aave"))
print(result)

Example: Using with LangChain

from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI

# Load DeFiLlama MCP tools
tools = load_tools(["defillama-mcp"], base_url="http://localhost:8080")

# Initialize an agent with the tools
llm = OpenAI(temperature=0)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)

# Run the agent
agent.run("What is the current TVL of Uniswap?")

Example: Using with Autonomous Agents

from autogen import Agent, ConversableAgent

financial_analyst = ConversableAgent(
    name="FinancialAnalyst",
    llm_config={
        "tools": [
            {
                "name": "defillama_protocol_tvl",
                "url": "http://localhost:8080/tools/get_protocol_tvl"
            }
        ]
    }
)

# The agent can now access DeFi data during its reasoning process
financial_analyst.initiate_chat("Analyze the TVL trends for Aave protocol")

API Reference

GET /protocols

Returns a list of DeFi protocols tracked by DeFi Llama.

Response:

[
  {
    "id": "ethereum:0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9",
    "name": "Aave",
    "symbol": "AAVE",
    "chain": "Ethereum",
    "tvl": 6240000000
  },
  ...
]

POST /tools/get_protocol_tvl

Get TVL information for a specific protocol.

Request:

{
  "protocol": "aave"
}

Response:

{
  "ethereum": 3240000000,
  "polygon": 980000000,
  "avalanche": 570000000,
  "optimism": 450000000,
  "arbitrum": 1000000000,
  "total": 6240000000
}

POST /tools/get_chain_tvl

Get historical TVL data for a blockchain.

Request:

{
  "chain": "ethereum"
}

Response:

[
  {
    "date": "2023-01-01",
    "tvl": 28500000000
  },
  {
    "date": "2023-01-02",
    "tvl": 28700000000
  },
  ...
]

Contributing

Contributions are welcome! Here's how you can help improve DeFiLlama MCP:

  1. Fork the Repository: Create your own fork of the project.
  2. Create a Feature Branch: git checkout -b feature/amazing-feature
  3. Commit Your Changes: git commit -m 'Add some amazing feature'
  4. Push to the Branch: git push origin feature/amazing-feature
  5. Open a Pull Request: Submit your changes for review.

Development Guidelines

  • Follow PEP 8 style guidelines for Python code.
  • Write tests for new features.
  • Update documentation to reflect changes.
  • Ensure backward compatibility when possible.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • DeFi Llama for providing the comprehensive DeFi data APIs.
  • The FastMCP team for creating the microservice framework.
  • All contributors who have helped shape this project.

Roadmap

  • Add support for more DeFi Llama endpoints
  • Implement caching layer for improved performance
  • Develop authentication and rate limiting
  • Create detailed documentation site
  • Build example integrations with popular AI frameworks
  • Add metric collection and monitoring

Contact

For questions, suggestions, or discussions about this project, please open an issue on GitHub or contact the maintainers:


Built with ❤️ for the Web3 and AI communities

uv run defillama.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iflow_mcp_demcp_defillama_mcp-0.1.2.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iflow_mcp_demcp_defillama_mcp-0.1.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file iflow_mcp_demcp_defillama_mcp-0.1.2.tar.gz.

File metadata

  • Download URL: iflow_mcp_demcp_defillama_mcp-0.1.2.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_demcp_defillama_mcp-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f1647beba18dc296733381acd7c24e85d7313a7c9f30bf98422a25c5e3faddd6
MD5 f739007640abd9d8521b76f2a23dff03
BLAKE2b-256 3f45e3cf32100babafdf8753efa648164e984655f27640563075045da62c146f

See more details on using hashes here.

File details

Details for the file iflow_mcp_demcp_defillama_mcp-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: iflow_mcp_demcp_defillama_mcp-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_demcp_defillama_mcp-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6ed60a86c74bfd5724eb3a02f55e2d3d8adbc1e6bad14eb36f55c078c0eb85ec
MD5 31f41a3c106ff13a2f051f20c32f115f
BLAKE2b-256 ad1edbf55ca13e5e95cd96e2dd5176b45806b2ec7caf407468df7685e7d8f7ae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page