space-ngrams

space-ngrams

MCP server that enables AI agents to search code, find files, and read files in a codebase at lightning speed using ripgrep.

Category
访问服务器

README

space-ngrams

MCP server that gives AI agents superpowers to search through your codebase at lightning speed.

Space-nGrams connects to AI coding assistants (Claude Code, Codex CLI, Qwen CLI, OpenCode) and provides them with three essential tools: search code, find files, and read files. All powered by ripgrep for millisecond-level performance.


🏗️ How It Works

Architecture

┌─────────────────┐     ┌──────────────────┐     ┌─────────────────┐
│  AI Agent       │     │  Space-nGrams    │     │  ripgrep        │
│  (Claude Code,  │────▶│  MCP Server      │────▶│  (rg)           │
│   Qwen, etc.)   │◀────│  (Python)        │◀────│  Search Engine  │
└─────────────────┘     └──────────────────┘     └─────────────────┘
        │                       │                         │
        │                       ▼                         │
        │              ┌──────────────────┐               │
        │              │  Cache Layer     │               │
        │              │  (~/.space-ngrams│               │
        │              │   /cache/)       │               │
        │              └──────────────────┘               │
        │                       │                         │
        │                       ▼                         │
        │              ┌──────────────────┐               │
        └─────────────▶│  Metrics & Logs  │◀──────────────┘
                       │  (~/.space-ngrams│
                       │   /server.log)   │
                       └──────────────────┘

Why MCP?

MCP (Model Context Protocol) is a standard that allows AI agents to access external tools and data sources. Instead of embedding all your code into the AI's context (which is slow and expensive), Space-nGrams gives the AI the ability to:

  1. Search on demand — Find any pattern, function, or string in your codebase in milliseconds
  2. Navigate efficiently — Locate files by name, then read only what's needed
  3. Work with large codebases — No need to load everything into context

Why ripgrep?

  • Blazing fast — SIMD acceleration, regex compilation, parallel search
  • Smart filtering — Respects .gitignore, skips binary files
  • Rich output — JSON format with line numbers and context
  • Battle tested — Used by developers worldwide daily

Why caching?

Repeated searches are common when AI agents explore code. Our cache layer:

  • Stores results for 5 minutes (configurable)
  • Persists across sessions on disk
  • Reduces latency from ~100ms to ~2ms on cache hits
  • Automatically manages size limits (50 MB default)

⚡ Features

Feature Benefit
Caching 10-50x faster for repeated searches
Metrics Track performance and cache hit rates
Configuration Customize limits, timeouts, ignore patterns
Logging Debug issues via ~/.space-ngrams/server.log

🛠️ Tools

Tool Description
search_code Search for regex/string in code with context
find_files Find files by glob pattern
read_file Read file content (max 200 lines/call)
get_metrics Get session performance statistics

📦 Installation

1. Clone the repository

git clone https://github.com/your-username/space-ngrams.git
cd space-ngrams

2. Install ripgrep

# Windows
winget install BurntSushi.ripgrep.MSVC

# macOS
brew install ripgrep

# Linux
sudo apt install ripgrep

Verify: rg --version

3. Install Python dependencies

pip install mcp

4. Verify the server starts

python src/server.py

The process will wait on stdin — that's correct. Stop with Ctrl+C.


🔌 Connecting to AI Tools

Claude Code

claude mcp add space-ngrams -- python /path/to/space-ngrams/src/server.py

Verify:

claude mcp list
# space-ngrams: python ... - ✓ Connected

Codex CLI

Add to ~/.codex/config.toml:

[mcp_servers.space-ngrams]
command = "python"
args = [ "/path/to/space-ngrams/src/server.py" ]

Qwen CLI

Add to ~/.qwen/settings.json:

{
  "mcpServers": {
    "space-ngrams": {
      "command": "python",
      "args": ["/path/to/space-ngrams/src/server.py"]
    }
  }
}

OpenCode

Add to opencode.json in your project root or home directory:

{
  "mcp": {
    "space-ngrams": {
      "type": "local",
      "command": ["python", "/path/to/space-ngrams/src/server.py"]
    }
  }
}

💬 Usage

The AI agent automatically uses these tools when needed. You can also trigger them explicitly:

find all calls to getUserById in D:/Projects/MyApp
show all .ts files in the src folder
read D:/Projects/MyApp/src/auth/service.ts lines 50-100

Tool Parameters

search_code

Parameter Required Description
pattern yes Regex or literal string
path yes Directory or file to search in
file_glob no File type filter, e.g. *.py or **/*.ts
context_lines no Lines of context (default: 2)

find_files

Parameter Required Description
pattern yes Glob, e.g. *.py, **/*controller*
path yes Root directory to search in
max_results no Maximum results (default: 100)

read_file

Parameter Required Description
path yes Absolute or relative path
start no First line to read (default: 1)
end no Last line (inclusive)

get_metrics

Returns performance statistics:

{
  "search_code": {
    "total_calls": 15,
    "cache_hits": 8,
    "cache_hit_rate": 53.3,
    "avg_duration_ms": 45.2,
    "min_duration_ms": 2.1,
    "max_duration_ms": 234.5
  }
}

⚙️ Configuration

Create a config file at ./space-ngrams.toml (project-specific) or ~/.space-ngrams/config.toml (global):

[cache]
enabled = true
ttl_seconds = 300       # 5 minutes
max_size_mb = 50

[limits]
max_search_results = 50
max_files_results = 100
max_read_lines = 200
default_context_lines = 2
search_timeout_seconds = 15

[ignore_patterns]
# Additional patterns to ignore (beyond .gitignore)
ignore_patterns = [
    "*.log",
    "*.tmp",
    "node_modules/**",
    "__pycache__/**",
]

[metrics]
enabled = true

See space-ngrams.example.toml for a full example with comments.


📁 Project Structure

space-ngrams/
├── src/
│   └── server.py           # MCP server implementation
├── pyproject.toml          # Python package metadata
├── space-ngrams.example.toml  # Example configuration
├── LICENSE
├── README.md               # This file (English)
├── README_RU.md            # Russian translation
└── docs/
    └── ARCHITECTURE.md     # Architecture notes (optional)

📖 Also available in Russian.


📊 Logs and Cache

Location Purpose
~/.space-ngrams/server.log Server logs and metrics
~/.space-ngrams/cache/ Persistent cache storage

📄 License

MIT

推荐服务器

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

官方
精选