Meta Ad Library MCP

Meta Ad Library MCP

Production MCP server for querying the official Meta Ad Library API to search and retrieve ads data without scraping.

Category
Visit Server

README

Meta Ad Library MCP

Servidor MCP de produção para consultar a Meta Ad Library API oficial (/ads_archive) — sem scraping.

Construído com FastMCP, httpx, Pydantic v2 e cache SQLite com TTL (preparado para migrar para Redis).

Ferramentas (v1)

Ferramenta Descrição
search_meta_ads Busca anúncios por palavras-chave + país
get_ads_from_page Lista anúncios de uma página (page_id)
health_check Verifica token e saúde do servidor

Limitações reais da API (2026)

A Meta Ad Library API é oficial, mas tem restrições importantes:

  1. UE e Reino Unido — A API retorna a maior parte dos anúncios comerciais entregues nessas regiões, incluindo dados de transparência (alcance, targeting, beneficiários, etc.).

  2. Fora da UE/UK (ex.: EUA, Brasil) — A cobertura de anúncios comerciais é muito limitada. Em geral, a API retorna principalmente:

    • Anúncios de temas sociais, eleições e política (POLITICAL_AND_ISSUE_ADS)
    • Anúncios que atingiram audiência na UE, mesmo que a campanha seja global
  3. Sem tradução automáticasearch_terms devem estar no idioma dos anúncios que você busca.

  4. Campos condicionaisspend, impressions, demographic_distribution e similares só aparecem em categorias/regiões específicas (principalmente política e UE).

  5. Rate limits — A Graph API aplica limites; o cliente faz retry automático em erros 429/5xx.

  6. Não é substituto do site — O site facebook.com/ads/library pode exibir mais anúncios do que a API em alguns cenários fora da UE.

Resumo para agentes de IA: Para pesquisa competitiva comercial no Brasil ou EUA, espere resultados parciais. Para compliance/transparência na UE/UK ou ads políticos globais, a API é confiável.

Pré-requisitos

Como obter o User Access Token

  1. Acesse developers.facebook.com e crie um app (tipo Business ou Other).
  2. No app, vá em Tools → Graph API Explorer.
  3. Selecione seu app e adicione a permissão ads_read.
  4. Gere um User Access Token (não Page Token).
  5. Para produção, prefira um token de longa duração via Access Token DebuggerExtend Access Token.
  6. Copie o token para META_ACCESS_TOKEN.

Documentação oficial: Ad Library API

Setup local

cd meta-ad-library-mcp
python -m venv .venv

# Windows
.venv\Scripts\activate

# macOS/Linux
source .venv/bin/activate

pip install -r requirements.txt
cp .env.example .env
# Edite .env e defina META_ACCESS_TOKEN

Executar

python server.py

Por padrão usa transporte stdio (ideal para Claude Desktop, Cursor, etc.).

Modo HTTP (remoto / Railway)

# .env
MCP_TRANSPORT=http
MCP_PORT=8000
python server.py
# Endpoint MCP: http://localhost:8000/mcp

Conectar clientes MCP

Cursor

Em Settings → MCP, adicione:

{
  "mcpServers": {
    "meta-ad-library": {
      "command": "python",
      "args": ["D:/caminho/absoluto/para/meta-ad-library-mcp/server.py"],
      "env": {
        "META_ACCESS_TOKEN": "seu_token_aqui"
      }
    }
  }
}

Use o caminho absoluto do server.py e o Python do seu venv se preferir.

Claude Desktop

Em claude_desktop_config.json:

{
  "mcpServers": {
    "meta-ad-library": {
      "command": "python",
      "args": ["/caminho/absoluto/para/meta-ad-library-mcp/server.py"],
      "env": {
        "META_ACCESS_TOKEN": "seu_token_aqui"
      }
    }
  }
}

Cliente HTTP (Ollama, scripts, etc.)

Com MCP_TRANSPORT=http:

import asyncio
from fastmcp import Client

async def main():
    async with Client("http://localhost:8000/mcp") as client:
        result = await client.call_tool(
            "search_meta_ads",
            {"search_terms": "fitness", "country": "GB", "limit": 10},
        )
        print(result)

asyncio.run(main())

Deploy no Railway

1. Preparar o repositório

Faça push desta pasta para um repositório Git (GitHub, GitLab, etc.).

2. Criar projeto no Railway

  1. railway.appNew ProjectDeploy from GitHub repo
  2. Selecione o repositório

3. Variáveis de ambiente

Variável Valor
META_ACCESS_TOKEN Seu token Meta
MCP_TRANSPORT http
MCP_HOST 0.0.0.0
MCP_PORT ${{PORT}} (Railway injeta a porta)
CACHE_DB_PATH /data/cache.db

4. Volume persistente (cache SQLite)

  1. No serviço Railway → VolumesAdd Volume
  2. Mount path: /data
  3. Garanta CACHE_DB_PATH=/data/cache.db

Sem volume, o cache é perdido a cada redeploy.

5. Dockerfile

O projeto já inclui um Dockerfile otimizado. Railway detecta e usa automaticamente.

6. Verificar deploy

curl https://seu-app.up.railway.app/mcp

Use a URL pública no cliente MCP com transporte HTTP.

Variáveis de ambiente

Variável Padrão Descrição
META_ACCESS_TOKEN Obrigatório. Token com ads_read
META_API_VERSION v25.0 Versão da Graph API
CACHE_TTL_SECONDS 3600 TTL do cache em segundos
CACHE_DB_PATH ./data/cache.db Caminho do SQLite
MCP_TRANSPORT stdio stdio ou http
MCP_HOST 0.0.0.0 Host HTTP
MCP_PORT 8000 Porta HTTP
LOG_LEVEL INFO Nível de log
REQUEST_TIMEOUT_SECONDS 30 Timeout httpx
MAX_RETRIES 3 Retries em 429/5xx

Estrutura do projeto

meta-ad-library-mcp/
├── server.py                 # FastMCP server + tools
├── config.py                 # Settings via env vars
├── clients/
│   └── meta_api_client.py    # httpx client + pagination/errors
├── models/
│   └── ad_models.py          # Pydantic v2 models
├── cache/
│   ├── base.py               # CacheBackend protocol (Redis-ready)
│   └── sqlite_cache.py       # SQLite implementation
├── data/                     # SQLite cache (gitignored)
├── requirements.txt
├── Dockerfile
└── README.md

Migrar cache para Redis

Implemente CacheBackend em cache/redis_cache.py com os mesmos métodos de SqliteCache, injete no MetaApiClient em server.py, e troque a instância — nenhuma tool precisa mudar.

Exemplos de uso (tools)

Busca por keyword (Reino Unido — boa cobertura comercial):

{
  "search_terms": "protein powder",
  "country": "GB",
  "limit": 25
}

Anúncios de uma página:

{
  "page_id": "123456789012345",
  "country": "US",
  "ad_active_status": "ALL",
  "limit": 50
}

Paginação:

Passe after com o valor de paging.next_cursor da resposta anterior.

Licença

MIT — use livremente em projetos pessoais e comerciais.

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured