stock-rag-mcp

stock-rag-mcp

Enables RAG-based querying of local stock company data using a local LLM and vector database, providing tools to ask questions, search raw chunks, and list documents.

Category
Visit Server

README

Stock Agent — RAG with LangChain + pgVector + Ollama

A fully local Retrieval-Augmented Generation (RAG) sample app. It loads details about several stock-trading companies, stores their embeddings in a pgVector database, and answers questions using a local LLM served by Ollama — no cloud API keys, nothing leaves your machine.

┌──────────┐   load+split   ┌──────────────┐  embed (Ollama)  ┌───────────┐
│ data/*.md │ ─────────────▶ │  LangChain    │ ───────────────▶ │ pgVector  │
└──────────┘                 │  ingest.py    │                  │ (Postgres)│
                             └──────────────┘                  └─────┬─────┘
                                                                     │ top-k
┌──────────┐   question    ┌──────────────┐   context + prompt      │
│  you     │ ────────────▶ │  query.py     │ ◀───────────────────────┘
└──────────┘               │  (RAG chain)  │ ── Ollama LLM ──▶ grounded answer
                           └──────────────┘

Tech stack

  • LangChain — orchestration (loaders, splitter, retriever, prompt chain)
  • pgVector — Postgres extension used as the vector store
  • Ollama — runs the local LLM (llama3.1) and embedding model (nomic-embed-text)
  • MCP — an optional server exposing the pipeline as tools to MCP clients like Claude Desktop (see below)

Prerequisites

Install the pieces below. Commands assume Windows + PowerShell.

Python version: use Python 3.12 (or 3.11). The pinned dependencies in requirements.txt ship prebuilt wheels for these versions. On Python 3.13/3.14 some packages (e.g. numpy 1.26.4) have no wheel yet and fall back to a source build that fails without a C compiler. If py -3.12 isn't available, install it with winget install Python.Python.3.12.

1. Ollama (local LLM)

Download and install from https://ollama.com/download (Windows installer). Then pull the two models this app uses:

ollama pull llama3.1
ollama pull nomic-embed-text

Ollama runs a server at http://localhost:11434 automatically after install. Verify:

ollama list

Tip: llama3.1 (8B) needs ~5–6 GB RAM. If low on memory, use a smaller model like llama3.2:3b and set LLM_MODEL=llama3.2:3b in .env.

2. Postgres with pgVector

Option A — Docker. Install Docker Desktop from https://www.docker.com/products/docker-desktop/, then from this folder:

docker compose up -d

That starts Postgres 16 with the pgvector extension on port 5432 (db stockrag, user/pass postgres/postgres).

On Windows, Docker Desktop's engine requires WSL2. If WSL2 isn't installed, docker compose up -d fails with a 500 Internal Server Error and nothing listens on 5432. Install it from an admin terminal with wsl --install (needs a reboot), or use Option B, which needs neither WSL2 nor Docker.

Option B — native Postgres (no Docker/WSL2 needed). Install Postgres 16:

winget install PostgreSQL.PostgreSQL.16

The installer runs a Windows service on port 5432 with superuser postgres/postgres — matching the defaults in config.py. Postgres does not bundle pgvector, so install it too:

  1. Download the prebuilt Windows binary matching your Postgres minor version (e.g. vector.v0.8.3-pg16.zip for Postgres 16.14) from andreiramani/pgvector_pgsql_windows. (Community-compiled — a third-party binary. If you'd rather not trust one, build from source with the Visual Studio C++ tools instead.)
  2. Copy its contents into your Postgres install (needs admin): vector.dllC:\Program Files\PostgreSQL\16\lib\, and everything under share\extension\C:\Program Files\PostgreSQL\16\share\extension\.

Then create the database and enable the extension (psql lives in C:\Program Files\PostgreSQL\16\bin):

CREATE DATABASE stockrag;
\c stockrag
CREATE EXTENSION IF NOT EXISTS vector;

3. Python dependencies

Create the venv with Python 3.12 (see the version note above):

cd C:\Users\khema\stock-rag
py -3.12 -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r requirements.txt

Configure

copy .env.example .env

Edit .env only if your Postgres credentials/host differ from the defaults.


Run

Step 1 — ingest the company data into pgVector (run once, or after changing files in data/):

python ingest.py

Step 2 — ask questions using the local LLM:

python query.py "Who is the CEO of Zenith Capital?"
python query.py "Which company does crypto market making and is not publicly listed?"
python query.py "Compare the 2024 revenue of Summit Brokerage and Meridian Securities."

Or interactive mode:

python query.py
> What ticker does Meridian Securities trade under, and on which exchange?

Each answer is grounded strictly in the retrieved context and prints its sources.

Example output

Example query output


The sample data

Four fictional stock-trading companies live in data/:

File Company Ticker
zenith_capital.md Zenith Capital Markets, Inc. ZCM
meridian_securities.md Meridian Securities Group PLC MSG.L
apex_trading.md Apex Trading Technologies Ltd. (private)
summit_brokerage.md Summit Brokerage Corporation SMB

Drop in your own .md, .txt, or .pdf files and re-run python ingest.py to expand the knowledge base.


Use it from an MCP client (Claude Desktop, IDEs, …)

The same RAG pipeline is exposed as an MCP server (mcp_server.py) so any MCP client can call it as tools. Three tools are published:

Tool What it does
ask_companies Full RAG answer (retrieve + local LLM) with sources
search_companies Raw top-k retrieved chunks, no LLM — fast lookup
list_companies Lists the documents loaded in the knowledge base

You must still run python ingest.py once first, and have Ollama + pgVector running (the server calls them on the first tool invocation).

Wiring it into Claude Desktop

  1. Copy the sample config into Claude Desktop's config file: %APPDATA%\Claude\claude_desktop_config.json (use claude_desktop_config.example.json as the template — adjust the two absolute paths if your project isn't at C:\Users\khema\stock-rag).
  2. Make sure the command points at the venv's Python (.venv\Scripts\python.exe) so the dependencies are on the path.
  3. Fully quit and reopen Claude Desktop. The stock-rag tools appear under the tools (🔧) menu.
  4. Ask, e.g. "Use stock-rag to tell me which company does crypto market making." Claude will call ask_companies and answer from your local data.

The server speaks MCP over stdio, which is what Claude Desktop and most IDE MCP integrations expect. The client launches the process for you — you don't run mcp_server.py yourself in normal use.

Quick sanity check (optional)

Install the MCP Inspector and point it at the server:

npx @modelcontextprotocol/inspector .\.venv\Scripts\python.exe mcp_server.py

Project layout

stock-rag/
├─ data/                              # source documents (the RAG knowledge base)
├─ config.py                          # env-driven settings
├─ ingest.py                          # load → split → embed → store in pgVector
├─ query.py                           # retrieve → prompt → local LLM answer (CLI + importable ask())
├─ mcp_server.py                      # MCP server exposing the RAG tools
├─ docker-compose.yml                 # Postgres + pgvector
├─ claude_desktop_config.example.json # sample MCP client config
├─ requirements.txt
└─ .env.example

Troubleshooting

  • No module named 'langchain_ollama' (or any dependency) — you're running the system Python, not the venv. Activate it (.\.venv\Scripts\Activate.ps1) or call it directly: .\.venv\Scripts\python.exe ingest.py.
  • Socket is not connected / connection refused on 5432 — Postgres isn't running. Start your native Postgres service, or Docker Desktop + docker compose up -d.
  • docker compose up500 Internal Server Error — Docker's engine needs WSL2. Install it (wsl --install from an admin terminal, then reboot) or use native Postgres (Prerequisites → Option B).
  • Failed to create vector extension / could not open extension control file "vector.control" — pgvector isn't installed into Postgres. See Prerequisites → Option B.
  • pip install fails building numpy — you're on Python 3.13/3.14. Rebuild the venv with Python 3.12 (see the version note under Prerequisites).
  • model 'llama3.1' not found — run ollama pull llama3.1.
  • Ollama call failed / connection error — make sure the Ollama app is running (ollama list should respond).
  • Slow first answer — the model loads into memory on first use; later queries are faster.

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured