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.1.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.1-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.1.tar.gz.

File metadata

  • Download URL: iflow_mcp_demcp_defillama_mcp-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 04e9e3500ff923b33afa90972f92aa10c6957cfd2919ac41a8c6b3a8b5a86d1f
MD5 1b0cf9c796da65fb1209055ccc461559
BLAKE2b-256 7b9286b3879058fc3b609a49e32ff15ebc950570e62e1298b054606a35dca239

See more details on using hashes here.

File details

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

File metadata

  • Download URL: iflow_mcp_demcp_defillama_mcp-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fe39044d35c3daf86aad2570d7b51d2538b5944bdceaa176e8e0f409e8cef5fb
MD5 e4f68632a6834de8d34c343d8d5fd56f
BLAKE2b-256 1f03760eb57c2d7edb9d9f471b3ff995d06f26ad7fe675fb7d08e12e02c8dd50

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