DEV Community

Vinay Kumar K S
Vinay Kumar K S

Posted on AI-assisted

Why AI Agents Need Verifiable Evidence: Building an MCP-Native Retrieval Engine with PostgreSQL

Why standard RAG fails agentic workflows, and how we built Knowledge Fabric using PostgreSQL, pgvector, and FastMCP.

Most Retrieval-Augmented Generation (RAG) setups treat context retrieval as a simple black box: prompt in, cosine similarity ranking, top-k text dump out.

In conversational chatbots, a slightly noisy chunk is usually harmless. But in agentic architectures—where models make tool calls, plan execution steps, and trigger real-world actions—retrieval without provenance is a major vulnerability. If an agent cannot verify where an excerpt originated, what version of a document it reflects, or whether the text was modified, policy checks fail and downstream execution becomes unreliable.

Knowledge Fabric & Intent Fabric

To address this, we built Knowledge Fabric: an open-source, vendor-neutral evidence retrieval engine designed natively for the Model Context Protocol (MCP) and built on PostgreSQL with pgvector.

The Problem: Naive RAG vs. Agentic Evidence

When an agent needs context to answer an enterprise query or decide on an action, standard vector stores present three common issues:

  1. Exact-Term Blind Spots: Pure dense vector search often misses exact identifiers (e.g., invoice IDs, function signatures, error codes).
  2. Missing Citations & Provenance: Agents receive text strings without cryptographic hashing, source revision IDs, or bounding offsets.
  3. Bloated Context Windows: Shoveling sprawling context into prompts increases latency and token costs while confusing agent decision boundaries.

Architectural Design: The Three-Tier Fabric

Rather than building an all-in-one monolith that handles planning, retrieval, and write executions simultaneously, we separate concerns into three distinct roles:

  • Knowledge Fabric (The Librarian): Collects, indexes, searches, and provides verifiable evidence packages with clear citations.
  • Intent Fabric (The Planner & Referee): Translates user intent and evidence into policy-validated plans.
  • Enterprise Adapters (The Controlled Doors): Read from external systems and execute approved write mutations.

Workflow:

Work Flows

Storage Engine: Standard Infrastructure First

We deliberately avoided introducing custom daemons or proprietary vector databases. Knowledge Fabric is built directly on PostgreSQL + pgvector:

  • Hybrid Search with Reciprocal Rank Fusion (RRF): Fuses PostgreSQL BM25-style lexical search (tsvector) with semantic vector embeddings (pgvector) to ensure exact keyword recall alongside conceptual relevance.
  • Evidence Packaging: Every retrieved passage includes document URI, offset boundaries, chunk hash, and retrieval explanation metadata.
  • Native FastMCP Interface: Exposes retrieval directly over stdio and HTTP endpoints for Claude Code, Cursor, Codex, and custom agent harnesses.

FastMCP Tool Surface

Knowledge Fabric exposes five core MCP tools:

  • retrieve_evidence: Performs hybrid search and returns bounded, cited evidence packages.
  • get_document: Fetches full document content and metadata by ID or URI.
  • list_sources: Inspects currently indexed collections and document trees.
  • explain_retrieval: Returns the scoring breakdown (lexical vs. vector RRF ranks) for an evidence query.
  • health: Confirms database connectivity and vector index status.

3-Minute Quickstart

You can test Knowledge Fabric locally using Docker Compose:

  1. Clone and launch PostgreSQL with pgvector:
git clone https://github.com/sagarv48/knowledge-fabric.git
cd knowledge-fabric
docker compose up -d
Enter fullscreen mode Exit fullscreen mode
  1. Install dependencies & initialize the database:
python -m venv .venv
source .venv/bin/activate
pip install -e .
python -m knowledge_fabric.cli init-db
Enter fullscreen mode Exit fullscreen mode
  1. Run the FastMCP Server: python -m knowledge_fabric.mcp.server

You can now add the server to your Claude Desktop or Cursor MCP configuration:

{
  "mcpServers": {
    "knowledge-fabric": {
      "command": "python",
      "args": ["-m", "knowledge_fabric.mcp.server"],
      "env": {
        "DATABASE_URL": "postgresql://postgres:postgres@localhost:5432/knowledge_fabric"
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

What's Next & Getting Involved

Knowledge Fabric is early and actively developed. We are currently refining local embedding pipelines (Ollama integration) and chunking strategies.

If you are building with MCP or experimenting with agent architectures, I would love to hear how you currently tackle citation and evidence verification in your workflows!

Top comments (0)