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
访问服务器

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.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选