使用说明
上下文镜头
赋予你的人工智能理解含义的能力,而不仅仅是匹配关键字。
什么是上下文镜头?
Context Lens是一个自包含的模型上下文协议(MCP)服务器,内置无服务器向量存储(LanceDB),为您的AI助手带来语义搜索。将其指向任何代码库、文档或文本文件(本地或GitHub上),您的AI可以立即理解并回答有关内容的问题。
传统关键字搜索 查找包含特定单词的文件。错过了确切的术语?错过密码。
上下文镜头 理解意义。询问“身份验证”并查找有关登录、凭据、令牌、OAuth和访问控制的代码,即使这些文件从未使用过“身份验证“一词
特性
核心能力
- 🔍 语义搜索 -理解含义,而不仅仅是关键字——即使没有精确的匹配,也能找到相关的概念
- 🚀 零设置 -无需安装,无需配置,无需API密钥-只需添加到MCP配置中即可
- 💾 无服务器矢量存储 -内置LanceDB意味着无需管理外部数据库
- 🔒 100%本地和私人 -所有数据、嵌入和处理都保留在您的机器上
内容源
- � *本地文件和文件夹 -为整个项目或特定目录建立索引
- � GitHub集成 -添加公共存储库、特定分支、目录或单个文件
- � 20+文件类型 -Python、JavaScript、TypeScript、Java、Go、Rust、Markdown等
非常适合
- 🔍 代码发现 -“此项目如何处理数据库连接?”
- 📚 学习 -添加React的源代码,并询问“钩子在内部是如何工作的?”
- 🐛 调试 -“在此代码库中查找类似的错误处理模式”
- 👥 入职 -帮助新团队成员了解您的架构
- ♻️ 重构 -“我们还能在哪里使用这种已弃用的模式?”
使用LLM进行设置
无需安装!只需将您的AI助手配置为使用Context Lens:
📘 克劳德桌面
配置文件位置:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - 窗户:
%APPDATA%/Claude/claude_desktop_config.json
{
"mcpServers": {
"context-lens": {
"command": "uvx",
"args": ["context-lens"]
}
}
}
使用CLI的替代方案:
claude mcp add context-lens -- npx context-lens@latest
重新启动Claude Desktop,您就准备好了!
🎯 光标
配置文件: .cursor/mcp.json 在您的工作空间中
{
"mcpServers": {
"context-lens": {
"command": "uvx",
"args": ["context-lens"]
}
}
}
打开 设置→ MCP服务器 确认连接并查看可用工具。
⚡ 开发 IDE
配置文件: .kiro/settings/mcp.json 在您的工作空间中
{
"mcpServers": {
"context-lens": {
"command": "uvx",
"args": ["context-lens"],
"disabled": false,
"autoApprove": ["list_documents", "search_documents"]
}
}
}
重新加载: 命令面板→ “MCP:重新加载服务器”
这 autoApprove 设置允许在没有确认提示的情况下进行只读操作。
🔄 Continue.dev
配置文件: ~/.continue/config.json
{
"mcpServers": [
{
"name": "context-lens",
"command": "uvx",
"args": ["context-lens"]
}
]
}
重新启动Continue.dev以应用更改。
🤖 OpenAI 代码克斯
codex mcp add context-lens -- npx @context-lens@latest
🔧 高级:Node.js Direct
对于自定义设置或本地开发:
{
"mcpServers": {
"context-lens": {
"command": "node",
"args": ["/absolute/path/to/context-lens/dist/index.js"]
}
}
}
🌐 其他MCP客户端
对于任何兼容MCP的客户端,请使用标准配置:
{
"command": "uvx",
"args": ["context-lens"]
}
自定义数据库位置(可选)
{
"mcpServers": {
"context-lens": {
"command": "uvx",
"args": ["context-lens"],
"env": {
"LANCE_DB_PATH": "./my_knowledge_base.db"
}
}
}
}
您可以添加什么
Context Lens适用于任何基于文本的内容:
- 📄 单个文件:
./README.md,/path/to/document.txt - 📁 本地文件夹:
./docs/,/path/to/project/ - 💻 本地存储库:
./my-project/,/Users/you/code/app/ - 🌐 GitHub网址:
- 存储库:
https://github.com/user/repo - 具体文件:
https://github.com/user/repo/blob/main/file.py - 目录:
https://github.com/user/repo/tree/main/src
- 存储库:
- 📚 文件网站:任何标记、文本或代码文件
- 📝 笔记和维基:个人知识库、团队维基
建筑
手动安装(可选)
大多数用户不需要安装任何东西,只需如上所示配置LLM客户端 uvx 将自动处理一切。
如果您更喜欢在本地安装:
pip install context-lens
或者从源代码安装:
git clone https://github.com/cornelcroi/context-lens.git
cd context-lens
pip install -e .
您可以添加什么
📁 本地文件和目录
- 单个文件:
/path/to/script.py - 目录:
/path/to/project/src/ - 递归:自动处理子目录
�L GitHub存储库(公共)
- 整个回购:
https://github.com/user/repo - 具体分支:
https://github.com/user/repo/tree/develop - 子目录:
https://github.com/user/repo/tree/main/src - 单个文件:
https://github.com/user/repo/blob/main/file.py
�E支持的文件类型(20+种格式)
- python:
.py - JavaScript/TypeScript:
.js,.jsx,.ts,.tsx - 网络:
.md,.txt,.json,.yaml,.yml - 系统:
.java,.cpp,.c,.h,.go,.rs - 脚本:
.sh,.bash,.rb,.php
🚫 自动忽略
- 依赖项:
node_modules,venv,vendor - 构建输出:
dist,build,target,out - 缓存:
__pycache__,.cache,.pytest_cache - 版本控制:
.git,.svn,.hg - IDE文件:
.idea,.vscode,.vs - 大文件:大于10MB的文件
💡 尝试这些流行的存储库
Web框架:
https://github.com/django/django # Django web framework
https://github.com/pallets/flask # Flask microframework
https://github.com/fastapi/fastapi # FastAPI modern framework
数据科学:
https://github.com/pandas-dev/pandas # Pandas data analysis
https://github.com/scikit-learn/scikit-learn # Machine learning
https://github.com/pytorch/pytorch # PyTorch deep learning
公用设施:
https://github.com/psf/requests # HTTP library
https://github.com/python/cpython # Python itself!
https://github.com/pallets/click # CLI framework
了解具体功能:
https://github.com/django/django/tree/main/django/contrib/auth # Django auth
https://github.com/fastapi/fastapi/tree/master/fastapi # FastAPI core
https://github.com/requests/requests/tree/main/requests # Requests lib
可用工具
一旦连接到LLM,您将获得六个强大的工具:
📥 add_document(文件路径或url)
将文档添加到知识库
- 本地文件:
"/path/to/file.py" - GitHub仓库:
"https://github.com/user/repo" - GitHub文件:
"https://github.com/user/repo/blob/main/..." - 智能:如果已使用相同内容进行索引,则跳过
- 提取内容,创建嵌入,存储在LanceDB中
🔍 search_documents(查询,限制=10)
跨所有文档的语义搜索
- 按含义查找相关代码/文本,而不仅仅是关键字
📋 list_documents(限制=100,偏移=0)
列出所有带页码的索引文档
- 浏览知识库中的内容
ℹ️ get_document_info(文件路径)
获取特定文档的元数据
- 检查添加时是否索引了内容哈希、块计数
🗑️ remove_document(文件路径)
从知识库中删除特定文档
- 清理过时或不需要的文件
🧹 clear_knowledge_base()
删除所有文档并重新开始
- 需要时完成重置
对话示例
添加GitHub存储库:
You: Add the FastAPI repository to my knowledge base
LLM: I'll add the FastAPI repository for you.
[Calls add_document("https://github.com/tiangolo/fastapi")]
Cloning repository...
Processing 247 Python files...
✓ Added 247 files from repository with 1,543 chunks
You can now ask questions about FastAPI's implementation!
You: How does FastAPI handle dependency injection?
LLM: [Searches the FastAPI codebase...]
Based on the FastAPI source code, dependency injection works through...
添加本地文件:
You: Add all Python files in my src/ directory to the knowledge base
LLM: I'll add those files for you.
[Calls add_document for each .py file]
✓ Added 15 Python files to the knowledge base
You: How do we handle authentication in this codebase?
LLM: Let me search for authentication-related code.
[Calls search_documents with query "authentication handling"]
Based on the code, you use JWT tokens with OAuth2. Here's what I found:
- src/auth/jwt.py: Token generation and validation
- src/auth/oauth.py: OAuth2 flow implementation
- src/middleware/auth.py: Authentication middleware
The main authentication flow is...
快速入门示例
示例1:添加您的项目
You: Add all Python files from /Users/me/my-project/src to the knowledge base
LLM: [Processes all .py files in the directory]
✓ Added 23 Python files with 156 chunks
示例2:从开源中学习
You: Add the FastAPI repository so I can learn how it works
LLM: [Calls add_document("https://github.com/tiangolo/fastapi")]
Cloning repository...
✓ Added 247 files from repository with 1,543 chunks
You: How does FastAPI handle dependency injection?
LLM: [Searches and explains based on actual FastAPI source code]
示例3:研究特定特征
You: Add just the authentication module from Django
LLM: [Calls add_document("https://github.com/django/django/tree/main/django/contrib/auth")]
✓ Added 45 files from django/contrib/auth with 312 chunks
You: Show me how Django implements password hashing
LLM: [Provides detailed explanation with code references]
查询示例
添加文档后,您可以提出以下强大的查询:
🔍 理解代码
"How does this codebase handle database connections?"
"Explain the authentication flow in this project"
"What design patterns are used in this repository?"
"How is error handling implemented?"
"Show me how the API endpoints are structured"
🐛 调试和问题解决
"Find examples of how to handle file uploads"
"Where is the rate limiting logic implemented?"
"Show me similar error handling patterns"
"How do other files handle this exception?"
"Find all places where we validate user input"
📚 学习与研究
"How does FastAPI implement dependency injection?"
"Compare how Django and Flask handle routing"
"What's the difference between these two implementations?"
"Show me examples of async/await usage in this codebase"
"How does this library handle backwards compatibility?"
♻️ 重构和代码审查
"Find all files that use the old authentication method"
"Where else do we use this deprecated function?"
"Show me similar code that might have the same bug"
"Find duplicate logic that could be refactored"
"What files would be affected if I change this interface?"
🎯 具体实施问题
"How do I use the caching system in this project?"
"Show me examples of writing tests for API endpoints"
"How is configuration managed in this codebase?"
"Find examples of custom middleware implementation"
"How do I add a new database model?"
🌟 开源探索
"How does React implement hooks internally?"
"Show me how Django's ORM builds SQL queries"
"How does FastAPI achieve such high performance?"
"Explain how pytest's fixture system works"
"How does requests handle HTTP retries?"
💡 更好查询的提示
✅ 好问题:
- 具体来说:“FastAPI如何验证请求体?”
- 询问概念:“解释此代码中的中间件模式”
- 请求示例:“显示异步数据库查询的示例”
- 比较:“这与旧的实现有何不同?”
❌ 避免:
- 太模糊了:“告诉我代码”
- 过于宽泛:“解释一切”
- 范围之外:关于不在知识库中的代码的问题
高级配置
地方发展(尚未出版)
如果您正在本地开发Context Lens:
克劳德桌面:
{
"mcpServers": {
"context-lens": {
"command": "context-lens"
}
}
}
开发 IDE :
{
"mcpServers": {
"context-lens": {
"command": "python",
"args": ["-m", "context_lens.main"],
"disabled": false,
"autoApprove": ["list_documents", "search_documents"]
}
}
}
MCP检查员(测试和开发)
MCP Inspector是一个基于网络的工具,用于在开发过程中测试MCP服务器。
快速入门:
# Test with MCP Inspector
DANGEROUSLY_OMIT_AUTH=true npx @modelcontextprotocol/inspector python -m context_lens.server
发生了什么:
- 服务器在<1秒内启动(延迟初始化)
- 检查器在浏览器中打开,显示所有6个工具
- 第一次工具调用加载嵌入模型(5-10秒,一次性)
- 后续通话速度很快(<1秒)
测试工作流程:
- 使用检查器的UI调用具有不同参数的工具
- 实时查看请求/响应JSON
- 检查登录
./logs/context-lens.log详细信息 - 使用无效输入处理测试错误
注: 服务器使用延迟初始化,因此启动很快,但第一次工具调用将需要更长的时间来加载嵌入模型。这是预期的行为,每个会话只发生一次。
运作原理
幕后的魔法
1. 📄 Document Ingestion
├─ Read file content with encoding detection
├─ Generate content hash for deduplication
├─ Extract metadata (size, timestamps, type)
└─ Split into overlapping chunks (~1000 chars)
2. 🧮 Vector Embedding
├─ Load sentence-transformers model (all-MiniLM-L6-v2)
├─ Convert each chunk to 384-dimensional vector
├─ Batch processing for efficiency
└─ Store vectors in LanceDB
3. 🔍 Semantic Search
├─ Convert search query to vector
├─ Find similar vectors using ANN (Approximate Nearest Neighbor)
├─ Rank results by cosine similarity
└─ Return relevant chunks with metadata
4. 💾 Storage
├─ LanceDB: Fast columnar vector database
├─ Two tables: documents + chunks
├─ Efficient updates and deletes
└─ All data stays local
首次运行
在第一次使用时, uvx 自动:
- 下载并安装软件包
- 安装所有依赖项
- 下载嵌入模型(~100MB,一次性)
- 启动服务器
然后服务器:
- 创建
knowledge_base.db在当前目录中 - 存储登录
./logs - 支持
.py和.txt默认情况下的文件
无需配置!
为什么使用这个?
传统关键字搜索
You: "Find authentication code"
Result: Files containing the word "authentication"
Problem: Misses related concepts like "login", "auth", "credentials"
使用此MCP进行语义搜索
You: "Find authentication code"
Result: All auth-related code including:
✓ Files about "login" and "sign in"
✓ Code handling "credentials" and "tokens"
✓ "Authorization" and "access control"
✓ Related security implementations
Why: Understands meaning, not just words
真实世界用例
- 🔍 代码发现 -“我们如何处理数据库连接?”
- 📚 入职 -新团队成员更快地理解代码库
- 🐛 调试 -“查找类似的错误处理模式”
- ♻️ 重构 -“我们在哪里使用这种已弃用的模式?”
- 📖 文档 -“解释身份验证系统的工作原理”
- 🎯 代码审查 -“查找可能受影响的相关代码”
- 🌟 学习OSS -“添加React仓库并解释钩子是如何工作的”
- 📦 图书馆研究 -“添加此库并演示如何使用功能X”
故障排除
常见问题
服务器未启动?
# Check installation
context-lens --version
# View detailed logs
tail -f logs/context-lens.log
# Check for errors
tail -f logs/errors.log
首轮跑得慢吗? 首次使用时下载嵌入模型(~100MB)。这种情况只发生一次。后续的跑步速度很快。
第一次工具调用很慢? 服务器使用延迟初始化——它启动很快,但在第一次工具调用时加载嵌入模型。这需要5-10秒,每次会话只发生一次。这是为了为MCP Inspector和其他工具提供快速启动时间。
MCP检查员未连接?
# Make sure you're using the correct command
npx @modelcontextprotocol/inspector python -m context_lens.server
# NOT this (incorrect):
# npx @modelcontextprotocol/inspector fastmcp run context_lens.server:app
# Check that Python can find the module
python -m context_lens.server --help
LLM客户端中未显示工具?
- 验证服务器是否在客户端的MCP设置中正确配置
- 配置更改后重新启动LLM客户端
- 检查客户端日志中的连接错误
- 对于Kiro IDE:使用命令面板→ “MCP:重新加载服务器”
数据库错误?
# Check database location
ls -la knowledge_base.db
# If corrupted, you can reset it
rm -rf knowledge_base.db
# The server will create a new database on next run
导入错误或缺少依赖项?
# Reinstall dependencies
pip install -r requirements.txt
# For development installation
pip install -e .
日志显示“stdio传输”错误? 这通常意味着有东西在不应该写入stdout的时候写入了stdout。服务器被配置为只记录到文件,以保持MCP协议通信的stdio干净。如果你看到这个:
- 检查是否有
print()代码中的语句 - 验证日志记录配置是否正确(应仅写入文件)
- 检查第三方库是否未写入stdout
性能问题?
- 首次添加文档:缓慢(模型加载)
- 后续操作:应快速(<1秒)
- 大文件(>10MB):自动跳过
- 多个文件:分批处理
配置问题?
# Check environment variables
env | grep MCP_KB
# Use config file for complex setups
cp config.example.yaml config.yaml
# Edit config.yaml with your settings
context-lens --config config.yaml
还有问题吗?
技术细节
堆栈
- MCP框架:FastMCP-现代Python MCP实现
- 向量数据库:LanceDB-快速嵌入式矢量数据库
- 嵌入:句子转换器/全MiniLM-L6-v2(384尺寸)
- 搜索:具有余弦相似性的近似最近邻(ANN)
- 存储:使用Apache Arrow的列格式
演出
- 嵌入速度:CPU上约1000个令牌/秒
- 搜索延迟:对于大多数查询,<100ms
- 存储:每块约1KB(文本+向量+元数据)
- 记忆:~500MB(型号)+数据库大小
支持的文件类型
支持的文件类型:
.py-Python源代码.txt-纯文本文件.md-Markdown.js,.ts-JavaScript/TypeScript.java,.cpp,.c,.h-C/C++/Java.go,.rs-Go/Rust- 以及更多基于文本的格式
贡献
贡献或从源代码运行:
git clone https://github.com/yourusername/context-lens.git
cd context-lens
pip install -e .
pytest tests/
环境变量
通过MCP客户端中的环境变量进行配置:
{
"env": {
"LANCE_DB_PATH": "./context-lens.db",
"LOG_LEVEL": "INFO"
}
}
贡献
欢迎投稿!这是一个开源项目。
- 通过以下方式报告错误和请求功能 GitHub问题
- 提交改进请求
- 如果你觉得回购有用,请为其加星! ⭐
许可证
MIT许可证
