> ## Documentation Index
> Fetch the complete documentation index at: https://moridb.sh/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# MCP Server

> Model Context Protocol server for AI agent integration.

## Overview

Mori includes an MCP (Model Context Protocol) server that lets AI coding agents interact with databases through Mori's copy-on-write proxy. The server registers engine-specific tools and exposes them over HTTP using the Streamable HTTP transport.

All queries from the MCP server go through the Mori proxy -- reads hit prod, writes go to shadow. The AI agent gets real production data for context while mutations are safely isolated.

## How It Works

<Steps>
  <Step title="Start Mori with MCP enabled">
    The MCP server starts alongside the proxy on a separate port.
  </Step>

  <Step title="Tools registered by engine">
    Mori registers the correct tools based on your database engine (SQL, Redis, or Firestore).
  </Step>

  <Step title="Agent connects via HTTP">
    AI agents connect to `http://127.0.0.1:<mcp-port>/mcp` using the MCP protocol.
  </Step>

  <Step title="Queries routed through proxy">
    The MCP server connects to the Mori proxy as a regular database client. From its perspective, Mori is just a database.
  </Step>
</Steps>

## Tools by Engine

### SQL Engines -- `db_query`

All SQL engines share a single `db_query` tool. The underlying database driver varies by protocol:

| Engine                                  | Driver                | Protocol   |
| --------------------------------------- | --------------------- | ---------- |
| PostgreSQL, CockroachDB, SQLite, DuckDB | `pgx`                 | pgwire     |
| MySQL, MariaDB                          | `go-sql-driver/mysql` | MySQL wire |
| MSSQL                                   | `go-mssqldb`          | TDS        |

**Tool: `db_query`**

| Parameter | Type   | Required | Description          |
| --------- | ------ | -------- | -------------------- |
| `query`   | string | Yes      | SQL query to execute |

```json theme={null}
{
  "tool": "db_query",
  "arguments": {
    "query": "SELECT * FROM users WHERE id = 1"
  }
}
```

### Redis -- 4 Tools

| Tool            | Parameters                   | Description                                                         |
| --------------- | ---------------------------- | ------------------------------------------------------------------- |
| `redis_command` | `command` (string, required) | Execute any Redis command (e.g., `SET key value`, `HGETALL myhash`) |
| `redis_get`     | `key` (string, required)     | Get the value of a key                                              |
| `redis_hgetall` | `key` (string, required)     | Get all fields and values of a hash                                 |
| `redis_keys`    | `pattern` (string, required) | Find keys matching a pattern (e.g., `user:*`)                       |

The Redis MCP handler connects to the Mori proxy using the raw RESP protocol with AUTH support.

### Firestore -- 3 Tools

| Tool              | Parameters                                                                                           | Description                                          |
| ----------------- | ---------------------------------------------------------------------------------------------------- | ---------------------------------------------------- |
| `firestore_get`   | `collection` (string), `document_id` (string)                                                        | Get a document by collection and ID                  |
| `firestore_list`  | `collection` (string), `limit` (number, optional)                                                    | List documents in a collection (default 25, max 100) |
| `firestore_query` | `collection` (string), `field` (string), `op` (string), `value` (string), `limit` (number, optional) | Query with a field filter                            |

Supported operators for `firestore_query`: `==`, `!=`, `<`, `<=`, `>`, `>=`, `in`, `array-contains`

The Firestore MCP handler connects to the Mori proxy via gRPC as a Firestore emulator client.

## Configuration

```bash theme={null}
# Start Mori with MCP server on port 9000
mori start --mcp --mcp-port 9000
```

The MCP endpoint is available at `http://127.0.0.1:9000/mcp`.

## Use Cases

<CardGroup cols={2}>
  <Card title="Safe Production Queries">
    AI agents query real production data. All reads go through Mori's read-only prod connection.
  </Card>

  <Card title="Risk-Free Mutations">
    Agents can INSERT, UPDATE, DELETE freely. All writes are captured in shadow -- production is never touched.
  </Card>

  <Card title="Test-Verify Loops">
    Agents make changes, verify results, and iterate. Reset shadow to start fresh at any time.
  </Card>

  <Card title="Schema Exploration">
    Agents inspect table structures, run diagnostic queries, and understand the database safely.
  </Card>
</CardGroup>

<Info>
  The MCP server connects to Mori's proxy using the standard database driver for your engine. It doesn't need to know about the prod/shadow split -- that's handled transparently by the proxy.
</Info>
