小贴士:按下Ctrl+D 或 ⌘+D,一键收藏本站,方便下次快速访问!
MIT License
8
最近更新:2个月前

将 llms-txt 暴露给 IDE 进行开发

MCP LLMS-TXT 文档服务

概览

llms.txt 是一个用于大型语言模型(LLMs)的网站索引,提供背景信息、指导以及链接到详细的 Markdown 文件。IDE 如 Cursor 和 Windsurf 或应用程序如 Claude Code/Desktop 可以使用 llms.txt 来获取任务上下文。然而,这些应用程序使用不同的内置工具来读取和处理像 llms.txt 这样的文件。检索过程可能不透明,不一定总有办法审计工具调用或返回的上下文。

MCP 为开发人员提供了对这些应用使用的工具的 完全控制。在这里,我们创建了一个开源 MCP 服务器来为 MCP 托管应用(例如,Cursor、Windsurf、Claude Code/Desktop)提供 (1) 用户自定义的 llms.txt 文件列表和 (2) 一个简单的 fetch_docs 工具,用于读取任何提供文件中的 URL。这使得用户可以审计每个工具调用以及返回的上下文。

mcpdoc

快速入门

安装 uv

bash 复制代码
curl -LsSf https://astral.sh/uv/install.sh | sh

选择要使用的 llms.txt 文件

  • 例如,这里是 LangGraph 的 llms.txt 文件。

(可选)使用你的选择的 llms.txt 文件,在本地测试 MCP 服务器:

bash 复制代码
uvx --from mcpdoc mcpdoc \
    --urls LangGraph:https://langchain-ai.github.io/langgraph/llms.txt \
    --transport sse \
    --port 8082 \
    --host localhost
截图 2025-03-18 下午3点29分30秒
bash 复制代码
npx @modelcontextprotocol/inspector
截图 2025-03-18 下午3点30分30秒
  • 在这里,你可以测试工具调用。

连接到 Cursor

  • 打开 Cursor 设置 和 MCP 选项卡。
  • 这将打开 ~/.cursor/mcp.json 文件。
截图 2025-03-19 上午11点01分31秒
  • 将以下内容粘贴到文件中(我们使用了名称 langgraph-docs-mcp 并链接到 LangGraph 的 llms.txt)。
复制代码
{
  "mcpServers": {
    "langgraph-docs-mcp": {
      "command": "uvx",
      "args": [
        "--from",
        "mcpdoc",
        "mcpdoc",
        "--urls",
        "LangGraph:https://langchain-ai.github.io/langgraph/llms.txt",
        "--transport",
        "stdio",
        "--port",
        "8081",
        "--host",
        "localhost"
      ]
    }
  }
}
  • 确认服务器在你的 Cursor 设置/MCP 标签下正在运行。
  • 使用 CMD+L(在 MacOS 上)打开聊天窗口。
  • 确保选择了机器人助手。
截图 2025-03-18 下午1点56分54秒

然后,尝试以下示例提示语:

复制代码
使用 llmg-docs-mcp 服务器回答与 LangGraph 相关的问题 ——
+ 调用 list_doc_sources 工具以获取可用的 llms.txt 文件
+ 调用 fetch_docs 工具进行阅读
+ 反思 llms.txt 文件中的 url
+ 反思输入问题
+ 对与问题相关的 url 调用 fetch_docs 工具
+ 使用所得结果作答

LangGraph 中的内存类型有哪些?
截图 2025-03-18 下午1点58分38秒

连接到 Windsurf

  • 按下 CMD+L(Mac)打开 Cascade。
  • 点击 Configure MCP 打开心配置文件 ~/.codeium/windsurf/mcp_config.json
  • 根据上面所注明更新 langgraph-docs-mcp 配置。
截图 2025-03-19 上午 11点02分52秒
  • CMD + L (在Mac上)打开Cascade,并刷新MCP服务器。
  • 显示可连的 MCP 服务器,显示连接的 langgraph - docs - mcp
截图 2025年3月18日 2时02分12秒

然后,尝试下列问题示例:

  • 这将执行你的工具调用。
截图 2025-03-18下午2点03分7秒

连接到 Claude Desktop

  • 打开设置/开发者部分更新~/Library/Application\ Support /Claude/claude_desktop_config.json
  • 按照以上所列更新langgraph - docs - mcp
  • 重启 Claude Desktop 应用程序。
截图 2025年3月18日下午2点05分54秒
  • 你会在聊天窗口底部右侧看到你的工具选项。
截图 2025-03-18 下午2时05分左右

然后,尝试示例提示语:

  • 执行请求时会要求你批准工具调用。
截图 2025-03-18下午2点06分54秒

连接到 Claude Code

  • 在安装完成后,Claude Code 终端中运行以下命令,将 MCP 服务器添加到你的项目中:
复制代码
claude mcp add-json langgraph-docs '{"type":"stdio","command":"uvx" ,"args":["--from", "mcpdoc", "mcpdoc", "--urls", "langgraph:https://langchain-ai.github.io/langgraph/llms.txt"]}' -s local
  • 你会看到更新的 ~/.claude.json 文件内容。
  • 测试:通过启动 Claude Code 查看工具:
复制代码
Claude 
/mpc
截图 2025-0 March 18日下午2点13分49秒

然后,尝试示例提示语:

  • 这将要求你批准工具调用。
截图 2025-03-18下午2点14分37秒

命令行界面

mcpdoc 命令提供了用于启动文档服务器的简单命令行界面(CLI)。

您可以以三种方式指定文档资源,这些方式可以组合使用:

  1. 使用 YAML 配置文件:
  • 这将以示例 sample_config.yaml 中的 LangGraph Python 数据开始。
bash 复制代码
mcpdoc --yaml sample_config.yaml
  1. 使用 JSON 配置文件:
  • 这将以示例 sample_config.json 配置文中的 LangGraph 数据开始。
bash 复制代码
mcpdoc --json sample_config.json
  1. 直接指定 llms.txt 的 URL 和可选名称:
  • URL 可以以普通 URL 或命名的方式指定,格式为 name:url
  • 该方法是上方的 MCP 服务器加载 llms.txt 的方式。
bash 复制代码
mcpdoc --urls LangGraph:https://langchain-ai.github.io/langgraph/llms.txt

您还可以结合这些方法来合并文档来源数据:

bash 复制代码
mcpdoc --yaml sample_config.yaml --json sample_config.json --urls https://langchain-ai.github.io/langgraph/llms.txt

额外选项

  • --follow-redirects :跟随HTTP 重定向(默认设置为 False)
  • -timeout SECONDS :以秒为单位设置HTTP 请求超时时间(默认值为 10.0)

带有额外选项的例子:

bash 复制代码
mcpdoc --yaml sample_config.yaml --follow-redirects --timeout 15

该指令将以 15 秒超时加载 LangGraph Python 文档,并根据需要遵循任何 HTTP 重定向。

配置格式

YAML 和 JSON 配置文件应包含文档来源列表。

每个来源必须包括 llms_txt 参数 URL,而 name 则为可选的附加参数:

YAML 配置示例 (sample_config.xml)

yaml 复制代码
# Sample configuration for mcp-mcpdoc server
# 每个条目都必须有一个 llms_txt URL,并且可以包含一个 name 
- name: Langraph Python
  llms_txt: https://langchain-ai.github.io/langraph/llms.txt

JSON 配置示例 (sample_config.xml)

json 复制代码
[
  {
    "name": "Langgraf Python",
    "llms_txt": "https://langchain-ai.github.io/langraph/llms.txt"
  }
]

编程使用

python 复制代码
from mcpdoc.main import create_server

# 通过数据创建服务器
server = create_server(
    [
        {
            "name": "LangGraph Python",
            "llms_txt": "https://langchain-ai.github.io/langgraph/llms.txt",
        },
        # 可以添加多个文档来源
        # {
        #     "name": "Another Documentation",
        #     "llms_txt": "https://example.com/llms.txt",
        # },
    ],
    follow_redirects=True,
    timeout=15.0,
)

# 执行服务器
server.run(transport="stdio")