CockroachDB MCP Server with FastAPI LLM-ready CLI
Deploy a fast MCP server with FastAPI and CockroachDB—schema bootstrapping, JSONB storage, LLM-ready CLI and optional /debug endpoints.
npx -y @viragtripathi/cockroachdb-mcp-serverOverview
This project provides a lightweight Model Context Protocol (MCP) server built with FastAPI and CockroachDB. It stores arbitrary JSON documents in a JSONB-backed table, exposes REST endpoints for ingestion and retrieval, and includes a small command-line utility to prepare content for LLM consumption. The server is intended as a simple, production-ready starting point for applications that need to persist structured context for use with language models or other AI services.
Using CockroachDB’s PostgreSQL-compatible JSONB column gives flexible schema-less storage with ACID guarantees and distributed scaling. The included schema bootstrapping, optional debug endpoints, and an LLM-aware CLI make it easier to integrate document ingestion, querying, and prompt construction into existing pipelines.
Features
- FastAPI-based REST API for document ingestion and retrieval
- CockroachDB (PostgreSQL-compatible) storage using JSONB columns
- Schema bootstrapping SQL for quick setup
- LLM-ready CLI to batch-ingest files or export context payloads
- Optional /debug endpoints for health and schema inspection
- Simple JSON document model with metadata and timestamps
- Docker-friendly runtime and environment configuration
Installation / Configuration
Clone the repository, create a virtual environment, install dependencies, set environment variables, bootstrap the schema, and run the server.
- Clone and install
- Configure environment variables
Create a .env file or export variables directly. Example env values:
DATABASE_URL="postgresql://user:password@hostname:26257/mcp_db?sslmode=require"
APP_HOST="0.0.0.0"
APP_PORT="8080"
DEBUG="false"
Common environment variables
| Variable | Description |
|---|---|
| DATABASE_URL | CockroachDB PostgreSQL connection string (required) |
| APP_HOST | Host to bind FastAPI (default 0.0.0.0) |
| APP_PORT | Port for the HTTP server (default 8080) |
| DEBUG | Enable debug endpoints (true/false) |
- Bootstrap the schema (SQL)
You can run the provided SQL file with psql-compatible tools:
-- bootstrap-schema.sql
(
id UUID PRIMARY KEY DEFAULT gen_random_uuid,
content JSONB NOT NULL,
metadata JSONB DEFAULT '{}'::jsonb,
created_at TIMESTAMPTZ NOT NULL DEFAULT now
);
(created_at);
Run bootstrap:
- Start the server
Using uvicorn:
Or run in Docker (example):
Available Tools / Resources
FastAPI endpoints (typical routes)
- POST /ingest — accept JSON document(s) or file uploads to store as JSONB
- GET /documents/{id} — fetch a stored document by UUID
- GET /search?key=value — simple metadata-based filtering
- GET /export/{id} — export a document as a ready-to-use LLM context payload
- Optional /debug/health and /debug/schema — visibility into server status and DB schema (toggle via DEBUG)
CLI utility (scripts/cli.py)
- Ingest a directory of JSON/markdown files
- Export selected documents into JSONL or combined prompt context
- Example invocations:
# Ingest files from a directory
# Export documents to JSONL for model consumption
- SQL bootstrap script: bootstrap-schema.sql
- Example Postman/HTTPie/cURL snippets included in repo for quick testing
Use Cases
LLM context management for retrieval-augmented generation
- Ingest knowledge base pages or product manuals as JSONB documents.
- Export a matched set of documents as a context array for passing to a language model API (e.g., combined into a prompt or uploaded to an embeddings/vector pipeline).
Structured file storage for applications
- Store and query metadata-rich payloads (e.g., events, enriched logs, or parsed documents) without a rigid schema.
- Use metadata queries to select relevant context slices for downstream processing.
Lightweight content hub for microservices
- Multiple services can push JSON documents to a shared CockroachDB-backed MCP server.
- Use the CLI and REST endpoints to create reproducible export workflows for model inference, testing, or debugging.
Examples
Ingest a document with curl:
Retrieve a document by id:
Export documents for an LLM: