Skip to main content
Glama
yandex-metrica-mcp

yandex-metrica-mcp

by BoxLab-Ltd
README.md
# yandex-metrica-mcp

[![CI](https://github.com/BoxLab-Ltd/yandex-metrica-mcp/actions/workflows/ci.yml/badge.svg)](https://github.com/BoxLab-Ltd/yandex-metrica-mcp/actions/workflows/ci.yml)
[![npm](https://badgen.net/npm/v/yandex-metrica-mcp)](https://www.npmjs.com/package/yandex-metrica-mcp)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](./LICENSE)

**Ask your Yandex Metrica analytics in plain language — from Claude, Cursor, or
any MCP client.**

A [Model Context Protocol](https://modelcontextprotocol.io) (MCP) server for
**Yandex Metrica**. It lets an AI agent query your web-analytics data — traffic,
sources, landing pages, conversions, geography, devices and trends — through a
small set of flexible, read-only tools — plus opt-in write tools that you
confirm one change at a time.

> Read-only by default, no secrets stored in the package: interactive login uses
> a built-in public OAuth client with PKCE, and the server talks only to Yandex.

General-purpose: marketing, product analytics, e-commerce and SEO are all fair
game — the server is not tied to any one use case.

## Demo

Point an AI agent at your counter and ask about your traffic — the server queries
Yandex Metrica and hands back real, read-only data, no dashboards. Here the
`run_report` tool answers a “traffic sources, last 7 days” question against a live
counter:

![yandex-metrica-mcp querying live Yandex Metrica traffic sources, read-only](docs/demo.gif)

## Quickstart

**1. Add the server to your MCP client** (e.g. Claude Desktop) — no token
required up front; you log in interactively in step 2:

```json
{
    "mcpServers": {
        "yandex-metrica": {
            "command": "npx",
            "args": ["-y", "yandex-metrica-mcp"],
            "env": { "YANDEX_METRIKA_COUNTER_ID": "12345678" }
        }
    }
}
```

**2. Log in once — one command, no app registration, no secret stored:**

```bash
npx yandex-metrica-mcp auth
```

Approve access in the browser and you're done — the code is handed back
automatically over a local redirect, no copy-paste. The login uses
authorization-code + PKCE, so **no client secret ever touches your machine**; the
token is cached (mode 0600) and valid for ~1 year.

**3. Ask your agent** about traffic, sources, conversions, geography, devices, or
trends — see [Examples](#examples) for prompts.

Prefer a static token (CI / non-interactive) or your own OAuth app? See
[Authentication](#authentication).

### Or install as a Claude Code plugin

The repo doubles as the plugin marketplace for BoxLab's Yandex MCP family, so
you can install the server through Claude Code's plugin system instead of the
config above:

```bash
/plugin marketplace add BoxLab-Ltd/yandex-metrica-mcp
/plugin install yandex-metrica-mcp@boxlab
```

Then sign in from the chat with the `login` tool (or run
`npx yandex-metrica-mcp auth`). The same marketplace also carries
[`yandex-webmaster-mcp`](https://github.com/BoxLab-Ltd/yandex-webmaster-mcp).
To turn on the write tools in the plugin, start Claude Code with
`YANDEX_METRIKA_ENABLE_WRITE=1` in the environment.

### Or install as a Claude Desktop extension (.mcpb)

For a one-click install with no JSON, download the `.mcpb` from the
[latest release](https://github.com/BoxLab-Ltd/yandex-metrica-mcp/releases/latest)
and open it with Claude Desktop (or drag it into Settings → Extensions). It asks
for an optional default counter id; then sign in from the chat with the `login`
tool (or run `npx yandex-metrica-mcp auth`).

## Why

There is no official Yandex Metrica MCP server, and existing community ones are
mostly thin, unmaintained, or dump raw data straight into the model's context.
This server aims to be the well-engineered, well-maintained, open option:
flexible report tools, strict token/context discipline, read-only by default.

## Features

- `run_report` — flexible wrapper over the Reporting API (`/stat/v1/data`).
- `run_comparison` — compare two periods with absolute and percentage deltas.
- `run_drilldown` — drill down through a dimension tree.
- `run_timeseries` — metrics split into a time series (`/bytime`) for trends.
- `get_metadata` — discover the counters on your account and a catalog of common
  dimensions/metrics (and Logs API fields) so the model queries with real names.
- `describe_counter` — read one counter's configuration (goals, segments,
  filters, operations, access grants, chart annotations) via an `include`
  selector. The goals
  section gives the goal ids needed for conversion metrics in `run_report`.
- `logs_request` / `logs_status` / `logs_download` / `logs_clean` — Logs API:
  export raw, un-sampled session (`visits`) or hit (`hits`) rows. Async lifecycle
  (request → poll → download → clean); `logs_download` returns a bounded sample
  inline by default, or streams the full export to a file — never dumping raw
  rows into the model's context.
- `login` / `submit_code` — sign in to Yandex Metrica from your MCP client, no
  terminal needed: `login` opens the browser and captures the code over a local
  redirect, or hands back a URL and takes the pasted code via `submit_code`.
- Built-in context control: field selection on by default, low default row
  limits, and sampling/quota surfaced back to the model.
- Analysis hygiene: the server hands every client a short set of rules
  (exclude robots, compare complete periods, never sum users, use explicit
  goals, …). The Claude Code plugin adds the fuller
  [`metrica-hygiene`](skills/metrica-hygiene/SKILL.md) skill.

### Write tools (opt-in)

Off unless you set `YANDEX_METRIKA_ENABLE_WRITE=1`. Then the server adds:

- `manage_annotation` — create, update or delete a note pinned to a chart date
  (a release, a campaign start, an outage).
- `upload_expenses` — import ad costs from a non-Yandex source as CSV, so
  Metrica reports cost and ROI. `dryRun: true` validates the file and shows the
  rows, dates and totals without sending anything.

With the flag on, sign-in also asks for the `metrika:write` scope — run `auth`
(or the `login` tool) once more after turning it on. Without the flag the
sign-in asks for read access only, and tokens issued earlier keep working.
Every change is confirmed by you before it is sent: your client shows a
confirmation prompt if it supports MCP elicitation, otherwise the tool returns
a summary and a one-time token that the agent can use only after you agree.
The server also checks that your account has edit access to the counter.

### Local HTTP mode

By default the server talks stdio, started by your MCP client. For clients
that connect by URL, or to share one running server between several clients,
start it over Streamable HTTP:

```bash
npx yandex-metrica-mcp --http            # http://127.0.0.1:7801/mcp
npx yandex-metrica-mcp --http --port 9000
```

Or set `MCP_TRANSPORT=http` (and `MCP_HTTP_PORT`). Every client gets its own
session; they share one sign-in and one request budget. The server listens on
`127.0.0.1` only and rejects requests whose `Host` or `Origin` is not local,
so a web page cannot reach it. To listen on another interface
(`--host 0.0.0.0`, e.g. in Docker) you must set `MCP_HTTP_TOKEN`; clients then
send `Authorization: Bearer <token>`. This is a local mode, not hosting: one
user, one Yandex account.

## Requirements

- Node.js >= 18
- Yandex Metrica credentials with the `metrika:read` scope (see
  **Authentication**), plus `metrika:write` for the opt-in write tools.
  Whoever the credentials belong to must have access to the counters you
  query.

## Authentication

**Recommended: interactive login.** No app registration needed — the server
ships a built-in public OAuth client. Run once:

```bash
yandex-metrica-mcp auth     # or, in dev: bun run auth
```

It opens the Yandex consent page in your browser; after you approve, the code is
returned automatically over a loopback redirect (`http://127.0.0.1:53682`) — no
copy-paste. If that port is taken, it falls back to showing a code you paste in
(force that flow with `auth --oob`, or change the port with
`YANDEX_OAUTH_LOOPBACK_PORT`). The token is cached at
`~/.config/yandex-metrica-mcp/token.json` (mode 0600) and is valid for ~1 year;
re-run `auth` when it expires. The login uses authorization-code + PKCE, so **no
client secret is stored anywhere**. A cached login takes precedence over
`YANDEX_METRIKA_TOKEN`.

**From your MCP client (no terminal).** Not signed in yet? The server still
starts — ask your agent to run the `login` tool and it does the same browser
flow in-process (or returns a URL and takes the code via `submit_code`). Handy
for GUI clients like Claude Desktop, where there is no terminal to run `auth`.

**Static token.** Get a token for an app with the `metrika:read` scope at
<https://oauth.yandex.ru> and pass it as `YANDEX_METRIKA_TOKEN` — handy for CI
or non-interactive use.

**Own OAuth app (optional).** To use your own app instead of the built-in one,
set `YANDEX_OAUTH_CLIENT_ID`; add `YANDEX_OAUTH_CLIENT_SECRET` to also enable
automatic token refresh.

## Configuration

The [Quickstart](#quickstart) covers the happy path. For all options — static
token, your own OAuth app, default counter, request tuning, language — see
[`.env.example`](./.env.example). The published package runs on Node (so
`npx`/MCP clients work out of the box); local development uses
[Bun](https://bun.sh).

## Examples

Once connected, an agent can answer questions like:

- “How many visits and users did counter 12345678 get last week, split by traffic
  source?” → `run_report` with `metrics: ["ym:s:visits","ym:s:users"]`,
  `dimensions: ["ym:s:lastsignTrafficSource"]`.
- “Compare this week's organic conversions to last week's.” → `run_comparison`
  (server returns A, B, and the deltas).
- “Which operating systems do my visitors use? Let me drill into Windows
  versions.” → `run_drilldown`, then again with `parentId`.
- “What counters do I have?” → `get_metadata`.
- “List this counter's goals, then show how the ‘Purchase’ goal converted last
  week.” → `describe_counter` (`include: ["goals"]`) for the goal id, then
  `run_report` with `ym:s:goal<id>conversionRate`.
- “Export last month's raw sessions with landing pages and referrers for offline
  analysis.” → `logs_request` (`source: "visits"`), poll `logs_status`, then
  `logs_download` (`mode: "file"`), then `logs_clean` to free the quota.

## Development

This project is Bun-first:

```bash
bun install
bun run dev        # run from source with hot reload
bun run typecheck  # tsc --noEmit
bun run lint       # eslint
bun test           # bun's test runner
bun run build      # emit dist/ with tsc (Node-compatible)
```

The skill has an eval suite in [`evals/`](evals) that runs against mocked
Metrica tools, so it never touches the real API:

```bash
claude plugin eval . --ablation none
```

## License

[MIT](./LICENSE) © boxlab

TDQS

A4.4/5.0

Scored across 12 tools

Disambiguation5/5

Each tool has a distinct role: authentication, counter discovery/config, four reporting shapes, and the Logs API lifecycle. The reporting tools are clearly differentiated by output shape (flat rows, A/B comparison, tree drilldown, time series), and the metadata/counter-config split is explicitly cross-referenced.

Naming Consistency4/5

Most names follow predictable patterns within workflow groups: run_* for reports, logs_* for log-request lifecycle, and verb_noun for get_metadata, describe_counter, and submit_code. The lone outliers are login and the noun-prefixed logs_* group, but they remain readable and conventional.

Tool Count5/5

Twelve tools is well scoped for auth, discovery, reporting, and Logs API workflows. Each tool earns its place, with no obvious redundant variants or thin single-purpose surface.

Completeness5/5

The set covers the full practical lifecycle: sign-in with fallback, counter discovery and configuration, multiple report modalities, and log request/status/download/clean operations. No obvious dead ends are visible for the stated Yandex Metrica analytics purpose.

Maintenance

ActivityActive
ResponsivenessNo recent community issues