Skip to main content
Glama
teoobarca

perplexity-mcp

by teoobarca
README.md
<div align="center">

<!-- Hero -->
<br />

<img src="docs/images/logo.svg" alt="Logo" width="160" />

<br />

<img src="https://img.shields.io/badge/Perplexity-MCP_Server-1a1a2e?style=for-the-badge&labelColor=09090b" alt="Perplexity MCP Server" />

<br /><br />

**The only Perplexity MCP server with multi-account pooling, an admin dashboard, and zero-cost monitoring.**<br />
**No API keys. No per-query fees. Uses your existing Perplexity Pro session.**

<br />

<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-14b8a6?style=flat-square&labelColor=18181b" alt="MIT License" /></a>&nbsp;
<a href="https://www.python.org/downloads/"><img src="https://img.shields.io/badge/Python-3.10+-3b82f6?style=flat-square&labelColor=18181b" alt="Python 3.10+" /></a>&nbsp;
<a href="https://modelcontextprotocol.io/"><img src="https://img.shields.io/badge/MCP-Compatible-22c55e?style=flat-square&labelColor=18181b" alt="MCP Compatible" /></a>&nbsp;
<img src="https://img.shields.io/badge/Token_Pool-Multi_Account-f59e0b?style=flat-square&labelColor=18181b" alt="Token Pool" />&nbsp;
<img src="https://img.shields.io/badge/Admin_Panel-React-8b5cf6?style=flat-square&labelColor=18181b" alt="Admin Panel" />

[Features](#-features) ยท [Quick Start](#-quick-start) ยท [Admin Panel](#%EF%B8%8F-admin-panel) ยท [Configuration](#%EF%B8%8F-configuration) ยท [Architecture](#%EF%B8%8F-architecture)

<br />

</div>

---

## ๐ŸŽฏ Why This One?

Most Perplexity MCP servers are single-account wrappers around the paid Sonar API. **This one is different:**

- ๐Ÿ†“ **No API costs** โ€” uses session cookies, not the paid API. Same features, zero per-query fees
- ๐ŸŠ **Multi-account pool** โ€” round-robin across N accounts with automatic failover
- ๐Ÿ“Š **Admin dashboard** โ€” React UI to monitor quotas, manage tokens, tail logs in real-time
- โค๏ธ **Zero-cost health checks** โ€” monitors all accounts via rate-limit API without consuming queries
- ๐Ÿ›ก๏ธ **Downgrade protection** โ€” detects when Perplexity silently returns a regular result instead of deep research
- ๐Ÿ“ฑ **Telegram alerts** โ€” get notified when tokens expire or quota runs out

---

## โœจ Features

<table>
<tr>
<td width="50%">

### ๐Ÿ” Smart Search
- **Pro Search** โ€” fast, accurate answers with citations
- **Reasoning** โ€” multi-model thinking for complex decisions
- **Deep Research** โ€” comprehensive 10-30+ citation reports
- **Multi-source** โ€” web, scholar, and social

### ๐Ÿค– 9 Models Available
- `sonar` ยท `gpt-5.2` ยท `claude-4.5-sonnet` ยท `grok-4.1`
- `gpt-5.2-thinking` ยท `claude-4.5-sonnet-thinking`
- `gemini-3.0-pro` ยท `kimi-k2-thinking` ยท `grok-4.1-reasoning`

</td>
<td width="50%">

### ๐ŸŠ Token Pool Engine
- **Round-robin** rotation across accounts
- **Exponential backoff** on failures (60s โ†’ 120s โ†’ ... โ†’ 1h cap)
- **3-level fallback** โ€” Pro โ†’ auto (exhausted) โ†’ anonymous
- **Smart quota tracking** โ€” decrements locally, verifies at zero
- **Hot-reload** โ€” add/remove tokens without restart

### ๐Ÿ›ก๏ธ Production Hardened
- Silent deep research downgrade detection
- Atomic config saves (no corruption on crash)
- Connection drop handling
- Cross-process state sharing via `pool_state.json`
- 53 unit tests

</td>
</tr>
</table>

---

## ๐Ÿ–ผ๏ธ Screenshots

<div align="center">

### Token Pool Dashboard

<img src="docs/images/dashboard.png" alt="Token Pool Dashboard" width="100%" />

<sub>Stats grid, monitor controls, sortable token table with per-account quotas (Pro / Research / Agentic), filter pills, and one-click actions.</sub>

<br /><br />

### Log Viewer

<img src="docs/images/logs.png" alt="Log Viewer" width="100%" />

<sub>Live log streaming with auto-refresh, level filtering, search highlighting, follow mode, and line numbers.</sub>

</div>

---

## ๐Ÿš€ Quick Start

### 1. Clone & Install

```bash
git clone https://github.com/teoobarca/perplexity-mcp.git
cd perplexity-mcp
uv sync
```

### 2. Add to Your AI Tool

<details>
<summary><b>๐ŸŸฃ Claude Code</b></summary>

```bash
claude mcp add perplexity -s user -- uv --directory /path/to/perplexity-mcp run perplexity-mcp
```
</details>

<details>
<summary><b>๐ŸŸข Cursor</b></summary>

Go to **Settings โ†’ MCP โ†’ Add new server** and paste:

```json
{
  "command": "uv",
  "args": ["--directory", "/path/to/perplexity-mcp", "run", "perplexity-mcp"]
}
```
</details>

<details>
<summary><b>๐Ÿ”ต Windsurf / VS Code / Other MCP clients</b></summary>

Add to your MCP config file:

```json
{
  "mcpServers": {
    "perplexity": {
      "command": "uv",
      "args": ["--directory", "/path/to/perplexity-mcp", "run", "perplexity-mcp"]
    }
  }
}
```
</details>

**That's it.** Works immediately with anonymous sessions. Add your tokens for Pro access โ€” see [Authentication](#-authentication).

---

## ๐Ÿ› ๏ธ Tools

Two MCP tools with LLM-optimized descriptions so your AI assistant picks the right one automatically:

### `perplexity_ask`

> AI-powered answer engine for tech questions, documentation lookups, and how-to guides.

| Parameter | Type | Default | Description |
|:----------|:-----|:--------|:------------|
| `query` | string | *required* | Natural language question with context |
| `model` | string | `null` | Model selection (see [models](#-9-models-available)) |
| `sources` | array | `["web"]` | Sources: `web`, `scholar`, `social` |
| `language` | string | `en-US` | ISO 639 language code |

**Mode auto-detection:** Models with `thinking` or `reasoning` in the name automatically switch to **Reasoning mode**.

```
"gpt-5.2"          โ†’ Pro Search
"gpt-5.2-thinking"  โ†’ Reasoning Mode  โ† auto-detected
```

### `perplexity_research`

> Deep research agent for comprehensive analysis. Returns extensive reports with 10-30+ citations.

| Parameter | Type | Default | Description |
|:----------|:-----|:--------|:------------|
| `query` | string | *required* | Detailed research question with full context |
| `sources` | array | `["web", "scholar"]` | Sources: `web`, `scholar`, `social` |
| `language` | string | `en-US` | ISO 639 language code |

> [!TIP]
> Deep research takes 2-5 minutes per query. Provide detailed context and constraints for better results. The server has a 15-minute timeout to accommodate this.

---

## ๐Ÿ–ฅ๏ธ Admin Panel

A built-in web dashboard for managing your token pool. Start it with:

```bash
perplexity-server
```

Opens automatically at **`http://localhost:8123/admin/`**

| Feature | Description |
|:--------|:------------|
| ๐Ÿ“Š **Stats Grid** | Total clients, Online/Exhausted counts, Monitor status |
| ๐Ÿ“‹ **Token Table** | Sortable columns, filter pills (Online/Exhausted/Offline/Unknown), icon actions |
| ๐Ÿ’ฐ **Quota Column** | Per-token breakdown โ€” Pro remaining, Research quota, Agentic research |
| โค๏ธ **Health Monitor** | Zero-cost checks via rate-limit API, configurable interval |
| ๐Ÿ“ฑ **Telegram Alerts** | Notifications on token state changes (expired, exhausted, back online) |
| ๐Ÿ”„ **Fallback Toggle** | Enable/disable automatic Pro โ†’ free fallback |
| ๐Ÿ“ฅ **Import/Export** | Bulk token management via JSON config files |
| ๐Ÿ“ **Log Viewer** | Live streaming, level filter (Error/Warning/Info/Debug), search, follow mode |
| ๐Ÿงช **Test Button** | Run health check on individual tokens or all at once |

---

## ๐Ÿ” Authentication

By default, the server uses **anonymous Perplexity sessions** (rate limited). For Pro access, add your session tokens.

### How to Get Tokens

1. Sign in at [perplexity.ai](https://perplexity.ai)
2. Open **DevTools** (F12) โ†’ **Application** โ†’ **Cookies**
3. Copy these two cookies:
   - `next-auth.csrf-token`
   - `next-auth.session-token`

### Single Token

Create `token_pool_config.json` in the project root:

```json
{
  "tokens": [
    {
      "id": "my-account",
      "csrf_token": "your-csrf-token-here",
      "session_token": "your-session-token-here"
    }
  ]
}
```

### Multi-Token Pool

Add multiple accounts for **round-robin rotation** with automatic failover:

```json
{
  "monitor": {
    "enable": true,
    "interval": 6,
    "tg_bot_token": "optional-telegram-bot-token",
    "tg_chat_id": "optional-chat-id"
  },
  "fallback": {
    "fallback_to_auto": true
  },
  "tokens": [
    { "id": "account-1", "csrf_token": "...", "session_token": "..." },
    { "id": "account-2", "csrf_token": "...", "session_token": "..." },
    { "id": "account-3", "csrf_token": "...", "session_token": "..." }
  ]
}
```

> [!NOTE]
> Session tokens last ~30 days. The monitor detects expired tokens and alerts you via Telegram.

---

## โš™๏ธ Configuration

### Environment Variables

| Variable | Default | Description |
|:---------|:--------|:------------|
| `PERPLEXITY_TIMEOUT` | `900` | Request timeout in seconds (15 min for deep research) |
| `SOCKS_PROXY` | โ€” | SOCKS5 proxy URL (`socks5://host:port`) |

### Token States

Token state is computed automatically from `session_valid` + `rate_limits` (never set manually):

| State | Meaning | Badge | Behavior |
|:------|:--------|:------|:---------|
| ๐ŸŸข `normal` | Session valid, pro quota available | **Online** | Used for all requests |
| ๐ŸŸก `exhausted` | Session valid, pro quota = 0 | **Exhausted** | Skipped for Pro, used as auto fallback |
| ๐Ÿ”ด `offline` | Session invalid/expired | **Offline** | Not used for any requests |
| ๐Ÿ”ต `unknown` | Not yet checked | **Unknown** | Used normally (quota assumed available) |

### Fallback Chain

When a Pro request fails, the server tries progressively:

```
1. โœ… Next client with Pro quota (round-robin)
2. โœ… Next client with Pro quota ...
3. ๐ŸŸก Any available client (auto mode)
4. ๐Ÿ”ต Anonymous session (auto mode)
5. โŒ Error returned to caller
```

---

## ๐Ÿ—๏ธ Architecture

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Your AI Assistant (Claude Code / Cursor / Windsurf)    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                       โ”‚ MCP (stdio)
                       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  perplexity-mcp                                          โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚  tools.py       โ”‚  โ”‚  server.py                     โ”‚  โ”‚
โ”‚  โ”‚  โ€ข ask          โ”‚โ”€โ”€โ”‚  โ€ข Pool state sync             โ”‚  โ”‚
โ”‚  โ”‚  โ€ข research     โ”‚  โ”‚  โ€ข Timeout handling            โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                       โ”‚
                       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Backend Engine (perplexity/)                            โ”‚
โ”‚                                                          โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚  client.py   โ”‚  โ”‚  client_pool โ”‚  โ”‚  admin.py      โ”‚  โ”‚
โ”‚  โ”‚  โ€ข Search    โ”‚  โ”‚  โ€ข Rotation  โ”‚  โ”‚  โ€ข REST API    โ”‚  โ”‚
โ”‚  โ”‚  โ€ข Upload    โ”‚  โ”‚  โ€ข Backoff   โ”‚  โ”‚  โ€ข Static      โ”‚  โ”‚
โ”‚  โ”‚  โ€ข Validate  โ”‚  โ”‚  โ€ข Monitor   โ”‚  โ”‚    files       โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚  โ€ข Fallback  โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚         โ”‚          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜           โ”‚          โ”‚
โ”‚         โ–ผ                                     โ–ผ          โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚
โ”‚  โ”‚ Perplexity  โ”‚                    โ”‚ React Admin UI โ”‚   โ”‚
โ”‚  โ”‚ (web API)   โ”‚                    โ”‚ :8123/admin/   โ”‚   โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

| Component | File | Role |
|:----------|:-----|:-----|
| **MCP Server** | `src/server.py` | Stdio transport, pool state sync, timeout handling |
| **Tool Definitions** | `src/tools.py` | 2 MCP tools with LLM-optimized descriptions |
| **API Client** | `perplexity/client.py` | Perplexity API via curl_cffi (bypasses Cloudflare) |
| **Client Pool** | `perplexity/server/client_pool.py` | Round-robin, backoff, monitor, state persistence |
| **Query Engine** | `perplexity/server/app.py` | Rotation loop, 3-level fallback, validation |
| **Admin API** | `perplexity/server/admin.py` | REST endpoints + static file serving |
| **Admin UI** | `perplexity/server/web/` | React + Vite + Tailwind dashboard |

---

## ๐Ÿงช Development

```bash
# Install in development mode
uv pip install -e ".[dev]" --python .venv/bin/python

# Run unit tests (53 tests)
.venv/bin/python -m pytest tests/ -v

# Frontend development
cd perplexity/server/web
npm install
npm run dev      # Dev server with proxy to :8123
npm run build    # Production build
```

### Project Structure

```
src/                          # MCP stdio server (thin wrapper)
  server.py                   #   Entry point, pool state sync
  tools.py                    #   Tool definitions

perplexity/                   # Backend engine
  client.py                   #   Perplexity API client (curl_cffi)
  config.py                   #   Constants, endpoints, model mappings
  exceptions.py               #   Custom exception hierarchy
  logger.py                   #   Centralized logging
  server/
    app.py                    #   Starlette app, query engine
    client_pool.py            #   ClientPool, rotation, monitor
    admin.py                  #   Admin REST API
    utils.py                  #   Validation helpers
    main.py                   #   HTTP server entry point
    web/                      #   React admin frontend (Vite + Tailwind)

tests/                        # 53 unit tests
```

---

## โš ๏ธ Limitations

- **Unofficial** โ€” uses Perplexity's web interface, may break if they change it
- **Cookie-based auth** โ€” session tokens expire after ~30 days
- **Rate limits** โ€” anonymous sessions have strict query limits
- **Deep research** โ€” takes 2-5 minutes per query (this is normal)

---

## ๐Ÿ“„ License

[MIT](LICENSE)

TDQS

A3.9/5.0

Scored across 2 tools

Disambiguation5/5

The two tools have clearly distinct purposes: one for quick answers and tech lookups, the other for deep research with extensive citations. There is no overlap in functionality.

Naming Consistency5/5

Both tools follow a consistent pattern with the 'perplexity_' prefix and a descriptive verb ('ask', 'research'), making it easy to understand their purpose.

Tool Count4/5

With only two tools, the server covers the core use cases of quick queries and deep research. While minimal, it is well-scoped and not overly sparse for its intended purpose.

Completeness4/5

The tool set covers the primary interactions with the Perplexity AI model: quick synthesis and comprehensive analysis. There are no major gaps, though additional tools for streaming or specific models could be considered.

Maintenance

ActivityInactive
ResponsivenessNo issues