通过MCP协议在Cursor中实时查询技术文档,避免AI幻觉API问题,减少上下文切换。
如果你使用 Cursor、Windsurf 或 Claude Code 来开发软件,你一定遇到过「幻觉 API」问题:
你让模型用现代框架(如 Next.js 14 App Router、LangChain v0.3 或 Pydantic v2)实现一个功能。模型写出了 150 行自信而优雅的代码。你运行它,它立刻崩溃了:
TypeError: Cannot read properties of undefined (reading 'call')
ImportError: cannot import name 'ChatOpenAI' from 'langchain'
你意识到模型幻觉出了一个两年前就已废弃的 API 方法,或者发明了一个根本不存在的函数签名。
常见的解决方案令人沮丧:你切换标签页,找到官方文档网站,把三页 markdown 复制粘贴到 Cursor 聊天提示框,看着上下文窗口膨胀了 12,000 个 token,而你还没写一行应用逻辑。
有一种好得多的方式:Model Context Protocol(MCP)。
在这篇指南中,我们将介绍如何构建并对外暴露一个零注册、公开的 Documentation MCP Server:https://docs.memorysync.io/mcp,它允许 Cursor 和 Claude Desktop 在 50ms 以内自主搜索、索引并读取实时技术文档,全程无需任何认证。
Cursor 内置了 @Docs 爬虫,但当处理快速演进的 AI 库时,它有三个结构性缺陷:
我们没有让开发者为了查阅一个文档页面而必须在本地下载笨重的 Python 或 Node.js 包,而是直接在 https://docs.memorysync.io/mcp 托管了一个 edge JSON-RPC 2.0 服务器。
以下是确切的运行时流程:
+-------------------------------------------------------------+
| Cursor Composer |
| (User types: "How do I store...") |
+------------------------------+------------------------------+
|
| 1. Auto-calls tool: search_docs("store chat turns")
v
+-------------------------------------------------------------+
| MemorySync Public Docs MCP Server |
| (https://docs.memorysync.io/mcp) |
+------------------------------+------------------------------+
|
| 2. Returns scored markdown headings & slugs
v
+-------------------------------------------------------------+
| Cursor Composer |
| 2. Auto-calls tool: read_doc("/quickstart") |
+------------------------------+------------------------------+
|
| 3. Returns exact markdown snippet (< 200 tokens)
v
+-------------------------------------------------------------+
| Model Writes Bug-Free Code Matching Exact Live API |
+-------------------------------------------------------------+
我们的公开文档服务器实现了严格的 MCP 2025-06-18 规范,暴露了三个只读工具:
执行 BM25 和关键词搜索,跨越所有已索引的文档章节。
{
"name": "search_docs",
"arguments": {
"query": "authentication bearer token"
}
}
返回值:经排序的 URL、标题和章节标题列表。
获取任意文档页面的干净、纯 markdown 对应版本,无 HTML 样板、无脚本、无导航横幅。
{
"name": "read_doc",
"arguments": {
"path": "/guides/cursor"
}
}
返回值:可供 LLM 检查的精确 markdown 内容。
Tool 3: list_doc_sections
返回整个文档层次的结构化地图,包括指向原始 llms.txt 和 llms-full.txt 端点的指针。
你不需要账号、API 密钥或信用卡就能在本地项目中使用它。
Step 1: 创建或打开 .cursor/mcp.json
在项目根目录下创建 .cursor 文件夹并添加 mcp.json 文件:
{
"mcpServers": {
"memorysync-docs": {
"url": "https://docs.memorysync.io/mcp"
}
}
}
(如果你使用的是 Claude Desktop,使用 npx -y mcp-remote https://docs.memorysync.io/mcp 作为你的 stdio-to-SSE 桥接器。)
Step 2: 在 Cursor 设置中验证
按 Cmd + ,(macOS)或 Ctrl + ,(Windows/Linux)。
进入 Features -> MCP。
你会看到 memorysync-docs 旁边有一个绿色状态点,显示 3 个活跃工具!
为了让 Cursor 在你提问时自动查询文档(这样你甚至不需要手动输入 @docs),把这个代码片段添加到根目录的 .cursorrules 或 .cursor/rules/mcp.mdc 文件中:
# Documentation Query Rule
When writing code that integrates with MemorySync or external APIs:
1. NEVER assume or guess method names, SDK signatures, or endpoint parameters.
2. If you are unsure of an API contract, call `search_docs` with the relevant keywords.
3. Inspect the returned slug with `read_doc` before generating code.
4. Always implement code strictly matching the signatures in the returned markdown documentation.
当你向 Cursor Composer 提示时,会发生以下事情:
"Show me how to store conversation turns in MemorySync using Python."
模型不再从过时的 2023 年训练权重中猜测,你会看到 Cursor 在时间线中执行两次工具调用:
memorysync-docs: search_docs({"query": "python store turns"})
memorysync-docs: read_doc({"path": "/sdks/python"})
生成的代码使用的是精确的当前 SDK:
from memorysync import MemorySyncClient
client = MemorySyncClient(api_key="ms_live_...")
# Correct, verified live SDK method:
memory = client.memories.add(
text="User prefers PostgreSQL over MongoDB for transactional data",
metadata={"source": "composer", "importance": 0.9}
)
print(f"Memory recorded: {memory.id}")
零弃用警告。零幻觉。零手动复制粘贴。
我们基准测试了一个 50 轮对话的 Agent 编程会话,对比传统上下文填充 vs. Docs-over-MCP:
通过让 IDE 在需要时精确获取所需内容,你的 LLM 始终保持在快速、高准确性的上下文最佳区间。
如果你想不经过手动设置立即测试,我们发布了一个开箱即用的模板:
Cursor 起始模板:github.com/memorysyncio/memorysync-cursor-starter
Claude Desktop 5 行快速上手:gist.github.com/mdhaseeb343q-pixel
实时文档:docs.memorysync.io
快乐构建,愿你的 AI Agent 不再幻觉出 API 签名!