Skip to main content
Glama
faizullahbalti29

result-analyzer-mcp

README.md
# MongoDB Read-Only MCP Server (`mcp-db-direct`)

A secure, enterprise-ready [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) server built with TypeScript that exposes read-only MongoDB queries and schema introspection to AI assistants and LLM agents.

Supports both **Stdio** (local execution) and **Streamable HTTP / SSE** transports with **OAuth 2.0 (Auth0)** protection and an environment-based authentication bypass for flexible development.

---

## ๐Ÿ“‘ Table of Contents

- [Features](#-features)
- [Architecture](#-architecture)
- [Project Structure](#-project-structure)
- [Prerequisites](#-prerequisites)
- [Quick Start](#-quick-start)
- [Environment Configuration](#-environment-configuration)
- [Authentication & Security](#-authentication--security)
  - [OAuth 2.0 (Auth0)](#oauth-20-auth0)
  - [Toggling Authentication](#toggling-authentication)
- [MCP Tools Reference](#-mcp-tools-reference)
  - [`get_student_schema`](#1-get_student_schema)
  - [`mongodb_query`](#2-mongodb_query)
- [HTTP Endpoints & Discovery](#-http-endpoints--discovery)
- [Connecting MCP Clients](#-connecting-mcp-clients)
  - [Claude Desktop (Stdio)](#claude-desktop-stdio)
  - [Remote HTTP / SSE Clients](#remote-http--sse-clients)
- [Development & Testing](#-development--testing)
- [License](#-license)

---

## โœจ Features

- ๐Ÿ”’ **Read-Only Safety**: Strictly enforces non-mutating database operations (`find`, `findOne`, `aggregate`, `countDocuments`, `distinct`).
- ๐Ÿš€ **Dual Transport Options**:
  - **Stdio Transport**: Seamless local integration with Claude Desktop, Cursor, and CLI agents.
  - **Streamable HTTP / SSE Transport**: Scalable Express server supporting streaming JSON-RPC sessions over HTTP.
- ๐Ÿ›ก๏ธ **OAuth 2.0 & RFC Compliance**:
  - Auth0 JWT token verification via `express-oauth2-jwt-bearer`.
  - **RFC 9728** Protected Resource Metadata (`/.well-known/oauth-protected-resource`).
  - **RFC 8414** OAuth Authorization Server discovery proxies.
- ๐ŸŽ›๏ธ **Zero-Friction Dev Mode**: Easily toggle OAuth enforcement on (`ENABLE_OAUTH=1`) or off (`ENABLE_OAUTH=0`) via `.env`.
- ๐Ÿ“Š **Intelligent Schema Introspection**: Provides rich collection models and business domain rules to AI models for accurate query generation.
- ๐Ÿ”„ **Safe BSON Serialization**: Automatically handles MongoDB `ObjectId`, `Date`, and special BSON types during JSON serialization.

---

## ๐Ÿ—๏ธ Architecture

```mermaid
flowchart LR
    subgraph Clients["AI Clients & LLMs"]
        Claude["Claude Desktop"]
        Cursor["Cursor IDE"]
        RemoteApp["Remote MCP Client"]
    end

    subgraph Server["MCP Server (TypeScript)"]
        direction TB
        Auth["Auth & CORS Middleware\n(RFC 9728 / Auth0 JWT)"]
        TransportHTTP["Streamable HTTP Transport\n(/mcp, SSE)"]
        TransportStdio["Stdio Transport"]
        
        Tools["Registered Tools\nโ€ข get_student_schema\nโ€ข mongodb_query"]
    end

    subgraph Database["Database"]
        MongoDB[(MongoDB\nRead-Only)]
    end

    Claude -->|Stdio| TransportStdio
    RemoteApp -->|HTTP + Bearer Token| Auth
    Cursor -->|HTTP / Stdio| Auth

    Auth --> TransportHTTP
    TransportHTTP --> Tools
    TransportStdio --> Tools

    Tools -->|Read-Only Ops| MongoDB
```

---

## ๐Ÿ“ Project Structure

```text
mcp-direct-master/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ db/
โ”‚   โ”‚   โ”œโ”€โ”€ mongodb.ts             # MongoDB client connection pool
โ”‚   โ”‚   โ””โ”€โ”€ serialize.ts           # BSON / ObjectId JSON serializer
โ”‚   โ”œโ”€โ”€ middleware/
โ”‚   โ”‚   โ””โ”€โ”€ middleware.ts          # Auth0 JWT validation & URL resolver
โ”‚   โ”œโ”€โ”€ models/
โ”‚   โ”‚   โ”œโ”€โ”€ student-schema.ts      # Database schema definitions
โ”‚   โ”‚   โ””โ”€โ”€ student-collections.ts # Collection metadata
โ”‚   โ”œโ”€โ”€ tools/
โ”‚   โ”‚   โ”œโ”€โ”€ query.ts               # Read-only query execution tool
โ”‚   โ”‚   โ”œโ”€โ”€ query-schema.ts        # Zod input validation schemas
โ”‚   โ”‚   โ””โ”€โ”€ schema.ts              # Schema introspection tool
โ”‚   โ”œโ”€โ”€ http.ts                    # Streamable HTTP / SSE Express server
โ”‚   โ”œโ”€โ”€ index.ts                   # Stdio transport entry point
โ”‚   โ””โ”€โ”€ test-auth.ts               # Auth0 token acquisition & endpoint test
โ”œโ”€โ”€ .env                           # Environment configuration
โ”œโ”€โ”€ package.json                   # Project scripts and dependencies
โ”œโ”€โ”€ tsconfig.json                  # TypeScript compiler configuration
โ””โ”€โ”€ README.md                      # Project documentation
```

---

## ๐Ÿ“‹ Prerequisites

- **Node.js**: `v18.0.0` or higher
- **MongoDB**: `v5.0` or higher (local instance or MongoDB Atlas)
- *(Optional)* **Auth0 Account**: If enabling OAuth 2.0 authorization

---

## ๐Ÿš€ Quick Start

### 1. Clone & Install Dependencies

```bash
git clone <repository-url>
cd mcp-direct-master
npm install
```

### 2. Configure Environment Variables

Create or update `.env` in the project root:

```env
# MongoDB Connection
MONGODB_URI=mongodb://localhost:27017
MONGODB_DATABASE=results-analyzer

# Server Port & Public URL
PORT=3000
MCP_PUBLIC_URL=http://localhost:3000

# Authentication Mode (1 = Enabled, 0 = Disabled)
ENABLE_OAUTH=0

# Auth0 Configuration (Required only if ENABLE_OAUTH=1)
AUTH0_DOMAIN=your-tenant.us.auth0.com
AUTH0_AUDIENCE=http://localhost:3000/mcp
```

### 3. Run the Server

#### Option A: HTTP / SSE Server (Recommended for Web & Remote Clients)
```bash
npm run dev:http
```

#### Option B: Stdio Server (For direct CLI or Claude Desktop)
```bash
npm run dev
```

---

## โš™๏ธ Environment Configuration

| Variable | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| `MONGODB_URI` | `string` | `mongodb://localhost:27017` | MongoDB connection string |
| `MONGODB_DATABASE` | `string` | `results-analyzer` | Target database name |
| `PORT` | `number` | `3000` | Port for the HTTP server |
| `MCP_PUBLIC_URL` | `string` | `http://localhost:<PORT>` | Publicly reachable base URL (useful behind ngrok/proxies) |
| `ENABLE_OAUTH` | `0` \| `1` | `1` | **`1`**: Require Auth0 Bearer token.<br>**`0`**: Bypass token check (Public/Dev mode). |
| `AUTH0_DOMAIN` | `string` | โ€” | Auth0 custom domain or tenant domain (e.g. `tenant.auth0.com`) |
| `AUTH0_AUDIENCE` | `string` | โ€” | Auth0 API identifier / audience |
| `CLIENT_ID` | `string` | โ€” | *(Testing)* OAuth Client ID |
| `CLIENT_SECRET` | `string` | โ€” | *(Testing)* OAuth Client Secret |

---

## ๐Ÿ” Authentication & Security

### OAuth 2.0 (Auth0)

When `ENABLE_OAUTH=1`:
- All requests to `/mcp` must include a valid Bearer token in the `Authorization` header:
  ```http
  Authorization: Bearer <AUTH0_ACCESS_TOKEN>
  ```
- Unauthenticated requests receive HTTP `401 Unauthorized` along with RFC 9728 compliant `WWW-Authenticate` headers pointing clients to discovery endpoints.

### Toggling Authentication

Toggle authentication instantly using `.env`:

```env
# Disable auth for local development / testing:
ENABLE_OAUTH=0

# Enable auth for staging / production:
ENABLE_OAUTH=1
```

---

## ๐Ÿ› ๏ธ MCP Tools Reference

### 1. `get_student_schema`
Returns full schema information, collection descriptions, field definitions, and business domain rules. Helps the LLM understand collection structures before issuing queries.

- **Inputs**: None
- **Output**: JSON payload with collection metadata and schema documentation.

---

### 2. `mongodb_query`
Executes safe, read-only queries against MongoDB collections.

#### Parameters

| Field | Type | Required | Description |
| :--- | :--- | :---: | :--- |
| `collection` | `string` | โœ… | Name of the collection to query. |
| `operation` | `enum` | โœ… | One of: `"find"`, `"findOne"`, `"aggregate"`, `"countDocuments"`, `"distinct"` |
| `filter` | `object` | โŒ | MongoDB filter query (e.g., `{ "grade": "A" }`). |
| `projection` | `object` | โŒ | Field projection specification (e.g., `{ "name": 1, "score": 1 }`). |
| `sort` | `object` | โŒ | Sorting criteria (e.g., `{ "createdAt": -1 }`). |
| `skip` | `number` | โŒ | Number of documents to skip (pagination). |
| `limit` | `number` | โŒ | Max documents to return (default: `50`, max: `100`). |
| `pipeline` | `array` | โŒ | Aggregation pipeline stages (required for `aggregate`). |
| `field` | `string` | โŒ | Target field name (required for `distinct`). |

---

## ๐ŸŒ HTTP Endpoints & Discovery

| Route | Method | Auth Required | Description |
| :--- | :---: | :---: | :--- |
| `/mcp` | `POST` | Dependent on `ENABLE_OAUTH` | Initialize MCP session and execute JSON-RPC calls |
| `/mcp` | `GET` | Dependent on `ENABLE_OAUTH` | Establish Server-Sent Events (SSE) stream for active session |
| `/mcp` | `DELETE` | Dependent on `ENABLE_OAUTH` | Terminate active MCP session |
| `/health` | `GET` | โŒ (Public) | Server health, status, and auth configuration status |
| `/.well-known/oauth-protected-resource` | `GET` | โŒ (Public) | RFC 9728 Protected Resource Metadata |
| `/.well-known/oauth-authorization-server` | `GET` | โŒ (Public) | RFC 8414 Authorization Server discovery proxy |
| `/.well-known/openid-configuration` | `GET` | โŒ (Public) | OpenID Connect discovery proxy |

---

## ๐Ÿ”Œ Connecting MCP Clients

### Claude Desktop (Stdio)

Add the server to your `claude_desktop_config.json`:

```json
{
  "mcpServers": {
    "mongodb-readonly": {
      "command": "npx",
      "args": [
        "tsx",
        "/absolute/path/to/mcp-direct-master/src/index.ts"
      ],
      "env": {
        "MONGODB_URI": "mongodb://localhost:27017",
        "MONGODB_DATABASE": "results-analyzer"
      }
    }
  }
}
```

### Remote HTTP / SSE Clients

Configure your MCP client with the server endpoint:

- **Server URL**: `http://localhost:3000/mcp` (or your `MCP_PUBLIC_URL`)
- **Headers**:
  ```json
  {
    "Authorization": "Bearer <ACCESS_TOKEN>"
  }
  ```
  *(Omit `Authorization` header if `ENABLE_OAUTH=0`)*

---

## ๐Ÿงช Development & Testing

```bash
# Start the HTTP server with hot-reloading
npm run dev:http

# Start the Stdio transport
npm run dev

# Test Auth0 token acquisition and authenticated request flow
npm run test:auth

# Compile TypeScript to dist/
npm run build
```

---

## ๐Ÿ“„ License

This project is licensed under the ISC License.