Skip to main content
Glama
README.md
# ai-visibility-mcp

An [MCP](https://modelcontextprotocol.io) server that measures **how often a brand is mentioned and cited in AI
answers**: the "GEO / AI visibility" number that classic rank trackers do not give you.

It asks real customer questions to AI engines that have web search on, stores every answer and its cited sources,
and turns them into mention rate, citation rate, share of voice against competitors, the sources the AI trusts, and
the prompts where competitors show up and you do not.

> **Status:** v0.1. The parsers and the safety logic are covered by 33 offline tests built from documented response
> shapes. The adapters have **not yet been validated against the live OpenAI, Gemini and DataForSEO APIs**:
> run `check_setup`, then a 1-prompt dry run and a single real call before trusting a number. See [Limitations](#limitations).

## Engines

| Engine | Source | Citations come from | Cost |
|---|---|---|---|
| `openai` | OpenAI Responses API + `web_search` tool | `url_citation` annotations | your OpenAI account |
| `gemini` | Gemini API + `google_search` grounding | `groundingChunks` | your Google AI account |
| `dataforseo_chatgpt` | DataForSEO *ChatGPT LLM Responses* | `annotations` | paid, per task |
| `dataforseo_gemini` | DataForSEO *Gemini LLM Responses* | `annotations` | paid, per task |

Plus one non-per-prompt DataForSEO tool, `dataforseo_mentions` (**paid**): their *LLM Mentions* dataset, which tells you
how often a domain or keyword appears in AI answers (Google AI Overviews, or ChatGPT for US/English) without running
any prompt of your own.

DataForSEO is optional and never used unless you pass `confirm_paid=true`. OpenAI and Gemini are used with your own keys.

## Tools

| Tool | Purpose | Calls an API? |
|---|---|---|
| `check_setup` | Which engines are configured (names of missing env vars only) and the safety limits | no |
| `set_tracking` | Brand, domains, aliases and competitors | no |
| `add_prompts` / `list_prompts` / `remove_prompt` | Manage the questions to ask | no |
| `run_prompt_set` | Ask the engines. **`dry_run=true` by default**: returns the call count and cost class first | only with `dry_run=false` |
| `visibility_report` | Mention / citation / visible rate, share of voice, top cited domains, weakest prompts, per engine | no |
| `gap_finder` | Prompts where competitors appear and you never do, with the sources cited there | no |
| `trend` | Weekly visibility | no |
| `cited_sources` | Which domains the AI cites most (overall or per prompt) | no |
| `get_answers` | Read the stored raw answers for a prompt | no |
| `usage` | Stored calls per engine and DataForSEO spend | no |
| `dataforseo_models` | Models DataForSEO offers per engine | free utility call |
| `dataforseo_mentions` | DataForSEO LLM Mentions metrics | paid, `dry_run=true` by default |

Everything that reads results works on stored data, and **mentions are recomputed at read time**: change a brand
alias or add a competitor and the whole history is re-scored without another API call.

## Metrics

For a set of responses:

- **mention rate**: share of answers that name the entity (name, alias or bare domain, whole word, case-insensitive;
  the longest matching name wins so "Acme Inc" counts once).
- **citation rate**: share of answers that cite a source on one of the entity's domains (subdomains included).
- **visible rate**: a mention or a citation.
- **share of voice**: the brand's mentions divided by the mentions of every tracked entity (brand + competitors).

Answers are not deterministic. Use `samples >= 3` per prompt and read rates over many prompts, not single answers.

Sample output on fictional data (`python examples/demo.py`, full output in [examples/sample_output.txt](examples/sample_output.txt)):

| Entity | Mention | Citation | Visible | Share of voice |
|---|---|---|---|---|
| Northwind Analytics (brand) | 57.1 % | 42.9 % | 57.1 % | 40.0 % |
| Contoso Metrics | 57.1 % | 28.6 % | 57.1 % | 40.0 % |
| Fabrikam BI | 28.6 % | 14.3 % | 28.6 % | 20.0 % |

`gap_finder` then flags *"cheap dashboard software for agencies"*: Contoso and Fabrikam appear, the brand never does.

## Safety by design

These calls cost money and keys can be suspended, so the policy is in the code, not in the README:

- **Dry run first.** `run_prompt_set` returns the plan (calls per engine, paid calls, missing credentials, estimated
  minutes) and calls nothing until you pass `dry_run=false`.
- **One process at a time, strictly in series.** A lock file prevents two runs; there is no parallelism.
- **Random pause between calls** (`AIV_WAIT_MIN`..`AIV_WAIT_MAX`, default 3-8 s), never a fixed interval.
- **Hard caps.** `AIV_MAX_CALLS_PER_RUN` (default 60) and `max_calls` per tool call.
- **Idempotent per UTC day.** A (prompt, engine, sample) already stored today is skipped: re-running never pays twice.
- **429 handling.** Wait the provider's retry delay (capped) and retry **once with the same key**. A second 429 stops
  the run. There is no key rotation anywhere in the code.
- **401 / 403 / invalid key / payment required: stop everything**, no retries.
- **Keys** are read from the environment, sent only in headers, and masked (`AIza***`, `sk-***`, `Basic ***`) in
  every error message. `check_setup` never returns a value.

## Setup

```bash
pip install git+https://github.com/gradientseo432/ai-visibility-mcp
export OPENAI_API_KEY=...          # optional
export GEMINI_API_KEY=...          # optional
export DATAFORSEO_LOGIN=...        # optional, paid
export DATAFORSEO_PASSWORD=...
claude mcp add ai-visibility -- ai-visibility-mcp
```

For Claude Desktop / Cursor add to `mcpServers`:

```json
{ "mcpServers": { "ai-visibility": { "command": "ai-visibility-mcp",
  "env": { "OPENAI_API_KEY": "...", "GEMINI_API_KEY": "..." } } } }
```

| Variable | Default | Meaning |
|---|---|---|
| `AIV_DATA_DIR` | `~/.ai-visibility-mcp` | SQLite database and lock file |
| `OPENAI_MODEL` | `gpt-4.1-mini` | Model for the `openai` engine |
| `GEMINI_MODEL` | `gemini-2.5-flash` | Model for the `gemini` engine |
| `DFS_CHATGPT_MODEL` / `DFS_GEMINI_MODEL` | `gpt-4.1-mini` / `gemini-2.5-flash` | Use `dataforseo_models` to list valid names |
| `DFS_COUNTRY_ISO` | unset | Search country for DataForSEO web search (e.g. `US`) |
| `AIV_MAX_CALLS_PER_RUN` | `60` | Global cap per run |
| `AIV_WAIT_MIN` / `AIV_WAIT_MAX` | `3` / `8` | Random pause between calls, seconds |

### Typical flow

1. "Track *Northwind Analytics* (northwind-analytics.example) against Contoso Metrics and Fabrikam BI."
2. "Add these 20 prompts: ..."
3. "Plan a run with openai and gemini, 3 samples." (dry run: shows e.g. 120 calls, 2 batches)
4. Confirm, then run. Repeat weekly from cron with the CLI, which uses the same policy:
   ```bash
   ai-visibility-mcp run --engines openai,gemini --samples 3 --yes
   ```
   Without `--yes` it only prints the plan.
5. "Show the visibility report, the gaps, and which domains the AI cites most."

## Development

```bash
python -m venv .venv && . .venv/bin/activate     # Windows: .venv\Scripts\activate
pip install -e ".[dev]"
pytest
```

Tests need no credentials and no network: engines are exercised with documented response payloads, the safety policy
(429 retry with the same key, stop on 401/403, no retry loops, masking, paid-call confirmation, dry-run-by-default)
is asserted explicitly.

## Limitations

- **API models are not the consumer apps.** These engines are the API models with web search enabled, not the ChatGPT or
  Gemini apps and not Google AI Overviews. Personalization, location and product differences mean the numbers are a
  reproducible *proxy*, best read as a trend over time. Results are stored under the engine name for that reason.
- **Not yet validated against live APIs** (see Status). DataForSEO response parsing follows their published schema; if a
  field differs, the adapter raises a per-call error instead of storing wrong data.
- **Mentions are lexical.** No sentiment and no entity disambiguation: a brand called "Apple" will match the fruit. Choose
  aliases accordingly.
- **One tracked brand per project.** Use the `project` argument to keep several brands or clients apart.
- **DataForSEO prompts are limited to 500 characters**, and its ChatGPT mentions data covers the US and English only.

## License

MIT. See [LICENSE](LICENSE).