MCP Demo - Document Search Server

MCP Demo - Document Search Server

A production-style MCP server that lets Claude Desktop search your local documents using TF-IDF keyword search. No heavy frameworks, just plain Python and the official MCP SDK.

Category
访问服务器

README

MCP Demo: Document Search Server

Companion repo for the YouTube video "MCP Explained for Engineers — Not Just Another API Wrapper"

A production-style MCP server that lets Claude Desktop search your local documents. No LangChain. No heavy frameworks. Plain Python + the official MCP SDK.

Clone → install → add to Claude Desktop → done in under 10 minutes.


What is MCP?

MCP (Model Context Protocol) is an open standard for connecting AI models to external tools and data sources. Think of it as a USB-C port for AI — one protocol, many connectors.

The problem it solves: every AI integration used to be custom code. You'd write OpenAI function calling differently than Anthropic tool use, differently again for Gemini. MCP standardizes the interface so a single server works with any compatible client.

┌─────────────────┐    JSON-RPC over stdio    ┌──────────────────────┐
│  Claude Desktop │ ◄──────────────────────► │  Your MCP Server     │
│  (MCP Client)   │                           │  (this repo)         │
│                 │   list_tools()            │                      │
│                 │   call_tool("search_documents", {query: "..."})  │
│                 │ ◄─── results ─────────── │  TF-IDF search over  │
│                 │                           │  local .md/.txt docs │
└─────────────────┘                           └──────────────────────┘

The server speaks JSON-RPC 2.0 over stdin/stdout. Claude Desktop manages the connection. You write Python functions; the protocol handles the rest.


What This Demo Does

The server exposes three tools to Claude:

Tool What it does
search_documents TF-IDF keyword/phrase search, returns ranked results with snippets
get_document Returns the full text of any indexed document
list_documents Lists all documents with word counts

Five sample engineering documents are included (async Python, API design, Docker, Git, system design). Drop any .md or .txt files into documents/ and restart the server to index them.


Quick Start

Prerequisites

  • Python 3.10 or higher
  • Claude Desktop installed (for the full demo)
  • pip or uv

Step 1 — Clone and install

git clone https://github.com/YOUR_USERNAME/mcp-demo.git
cd mcp-demo
pip install -r requirements.txt

Step 2 — Run the smoke test

This verifies the search engine works correctly without needing Claude Desktop:

python test_server.py

Expected output:

=== MCP Demo — Search Engine Smoke Test ===

Indexed 5 document(s) from .../documents

  [PASS] at least 5 documents indexed (got 5)
  [PASS] all documents have >50 words
Search relevance checks:
  [PASS] 'async await event loop' → python_async.md (got python_async.md)
  [PASS] 'REST API versioning idempotent' → api_design.md (got api_design.md)
  ...

All checks passed.

Step 3 — Connect to Claude Desktop

Find your Claude Desktop config file:

OS Path
macOS ~/Library/Application Support/Claude/claude_desktop_config.json
Windows %APPDATA%\Claude\claude_desktop_config.json

Add this block to the config (replace the path):

{
  "mcpServers": {
    "doc-search": {
      "command": "python",
      "args": ["-m", "server.main"],
      "cwd": "/absolute/path/to/mcp-demo"
    }
  }
}

Windows example:

{
  "mcpServers": {
    "doc-search": {
      "command": "python",
      "args": ["-m", "server.main"],
      "cwd": "C:\\Users\\you\\mcp-demo"
    }
  }
}

Restart Claude Desktop. You should see a hammer icon (🔨) in the chat input bar — that confirms MCP tools loaded successfully.

Step 4 — Try it in Claude

Ask Claude any of these to see MCP working:

What documents do I have indexed?
Search my docs for information about async Python and the event loop
Find everything about Docker multi-stage builds and summarize the key points
Compare what my docs say about caching strategies

Watch Claude automatically invoke list_documents, search_documents, and get_document as needed — reasoning over your local files without any copy-paste.


How It Works

The MCP Handshake

When Claude Desktop starts, it launches your server as a subprocess and sends an initialize request. The server responds with its capabilities. Claude then calls tools/list to discover available tools and their schemas.

All subsequent calls use the same stdio pipe:

Claude Desktop                    server/main.py
     │                                  │
     │── initialize ──────────────────► │
     │◄─ initialized ─────────────────  │
     │── tools/list ────────────────── ►│
     │◄─ [search_documents, ...] ─────  │
     │                                  │
     │   (user asks a question)         │
     │── tools/call ────────────────── ►│  search_documents(query="async")
     │◄─ result ──────────────────────  │  TF-IDF scores → ranked results

The Search Engine

server/search.py implements TF-IDF scoring from scratch — no scikit-learn, no embeddings:

  • TF (term frequency): how often a term appears in a document, normalized by document length
  • IDF (inverse document frequency): log(N / df) — penalizes terms that appear in every document
  • Score: sum of TF×IDF for each query term present in the document

This is the same algorithm that powered early web search. It works well for keyword queries over small document collections and has zero runtime dependencies.

FastMCP

server/main.py uses FastMCP — the high-level API from the official MCP SDK:

from mcp.server.fastmcp import FastMCP

mcp = FastMCP("doc-search")

@mcp.tool()
def search_documents(query: str, max_results: int = 5) -> str:
    """Ranked keyword search across indexed documents."""
    ...

mcp.run()  # starts stdio transport

FastMCP introspects your function signatures to generate the JSON Schema that Claude uses to understand what arguments each tool accepts. The docstring becomes the tool description shown to the model.


Repository Structure

mcp-demo/
├── server/
│   ├── main.py          # FastMCP server — 3 tools, ~60 lines
│   └── search.py        # TF-IDF engine — no ML dependencies
├── documents/
│   ├── python_async.md
│   ├── api_design.md
│   ├── docker_guide.md
│   ├── git_workflow.md
│   └── system_design.md
├── test_server.py                    # smoke test (no Claude needed)
├── claude_desktop_config_example.json
├── requirements.txt                  # mcp[cli]>=1.0.0
└── pyproject.toml

Adding Your Own Documents

Drop any .md or .txt files into documents/ and restart Claude Desktop (which restarts the server subprocess). The index rebuilds at startup.

Ideas:

  • Your team's runbooks and internal docs
  • Architecture decision records (ADRs)
  • Personal notes exported from Notion or Obsidian
  • API documentation in markdown format

Troubleshooting

No hammer icon in Claude Desktop

  • Check the config path is correct for your OS
  • Verify the cwd path is absolute and the directory exists
  • Check Claude Desktop logs: ~/Library/Logs/Claude/ (macOS) or Event Viewer (Windows)

ModuleNotFoundError: No module named 'mcp'

  • Make sure you installed dependencies: pip install -r requirements.txt
  • If using a virtual environment, Claude Desktop needs to use the same Python: replace "command": "python" with the full path to your venv's Python

Server starts but returns no results

  • Run python test_server.py to verify the search engine directly
  • Check that documents/ contains .md or .txt files

Testing the server manually (without Claude Desktop)

echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test","version":"0.1"}}}' | python -m server.main

Going Further

  • Add semantic search: replace TF-IDF with embeddings using sentence-transformers and cosine similarity for better recall on paraphrased queries
  • Add resources: expose documents as MCP Resources (read-only, URI-addressed) in addition to tools — clients can subscribe to resource changes
  • Add prompts: package common workflows as MCP Prompts that pre-fill Claude's context
  • Connect other clients: the same server works with Cursor, Zed, or any MCP-compatible editor

Official MCP docs: https://modelcontextprotocol.io
MCP Python SDK: https://github.com/modelcontextprotocol/python-sdk

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选