MCP Tools

local-only server

The server can only run on the client’s local machine because it depends on local resources.

Integrations

  • Referenced as a related project through langchain-mcp-adapters, enabling the use of MCP tools with LangChain.

  • Referenced indirectly through MCP-Bridge which maps MCP tools to OpenAI's format, suggesting compatibility with OpenAI models.

  • Allows execution of Python scripts through the shell command tool, enabling AI agents to run Python code and analyze data.

MCP 工具

自定义模型上下文协议 (MCP) 服务器实现,为 Claude Desktop 和其他 LLM 客户端提供文件系统和命令执行工具。

什么是模型上下文协议?

模型上下文协议 (MCP) 是一种开放协议,它规范了应用程序向大型语言模型 (LLM) 提供上下文的方式。就像 USB-C 端口提供了一种将设备连接到各种外设的标准化方式一样,MCP 也提供了一种将 AI 模型连接到不同数据源和工具的标准化方式。

该项目实现了一个 FastMCP 服务器,其中包含一些实用工具,使 Claude 和其他 LLM 能够与本地文件系统交互并执行命令。它通过定义明确的工具接口,以可控的方式扩展了 LLM 的本地系统访问能力。

MCP 的主要优势

  • 标准化集成:MCP 提供了越来越多的预构建集成,您的 LLM 可以直接插入
  • 供应商灵活性:轻松在 LLM 提供商和供应商(Claude、GPT-4o、Gemini 等)之间切换
  • 安全性:保护基础设施内数据的最佳实践
  • 工具公开:封装现有工具,使任何兼容 MCP 的 LLM 客户端都可以访问它们

特征

MCP 服务器提供以下文件系统和命令执行工具:

  • execute_shell_command :执行 shell 命令并获取 stdout/stderr 结果
  • show_file :使用可选的行范围规范查看文件内容
  • search_in_file :使用正则表达式在文件中搜索模式
  • edit_file :使用字符串替换和行操作对文件进行精确更改
  • write_file :将内容写入或附加到文件

MCP 架构

MCP 遵循客户端-服务器架构:

  • 主机:发起连接的 LLM 应用程序(如 Claude Desktop 或 IDE)
  • 客户端:在主机应用程序内部与服务器保持 1:1 连接
  • 服务器:向客户端提供上下文、工具和提示(该项目实现了一个服务器)

先决条件

  • Python 3.10 或更高版本
  • 与 MCP 兼容的客户端(Claude Desktop 或任何其他支持 MCP 的客户端)

安装

  1. 安装 uv
  2. 克隆此存储库或下载源代码
  3. 运行uv run mcp install来安装 MCP 服务器
  4. 运行which uv以获取uv可执行文件的绝对路径
  5. 在 Claude Desktop 中更新 MCP 服务器配置以使用uv可执行文件的绝对路径

我的 MCP 服务器配置如下:

{ "globalShortcut": "", "mcpServers": { "zbigniew-mcp": { "command": "/Users/zbigniewtomanek/.local/bin/uv", "args": [ "run", "--with", "mcp[cli]", "--with", "marker-pdf", "mcp", "run", "/Users/zbigniewtomanek/PycharmProjects/my-mcp-tools/server.py" ] } } }

用法

从 Claude Desktop 连接

  1. 打开 Claude 桌面
  2. 使用标识符“zbigniew-mcp”连接到 MCP 服务器

注意:虽然此实现侧重于 Claude Desktop,但 MCP 旨在与任何 MCP 兼容工具或 LLM 客户端兼容,从而提供实现和集成的灵活性。

可用工具

执行shell命令

使用参数列表安全地执行 shell 命令:

execute_shell_command(["ls", "-la"]) execute_shell_command(["grep", "-r", "TODO", "./src"]) execute_shell_command(["python", "analysis.py", "--input", "data.csv"]) execute_shell_command(["uname", "-a"])

显示文件

使用可选的行范围规范查看文件内容:

show_file("/path/to/file.txt") show_file("/path/to/file.txt", num_lines=10) show_file("/path/to/file.txt", start_line=5, num_lines=10)

在文件中搜索

使用正则表达式在文件中搜索模式:

search_in_file("/path/to/script.py", r"def\s+\w+\s*\(") search_in_file("/path/to/code.py", r"#\s*TODO", case_sensitive=False)

编辑文件

对文件进行精确的更改:

# Replace text edit_file("config.json", replacements={"\"debug\": false": "\"debug\": true"}) # Insert at line 5 edit_file("script.py", line_operations=[{"operation": "insert", "line": 5, "content": "# New comment"}]) # Delete lines 10-15 edit_file("file.txt", line_operations=[{"operation": "delete", "start_line": 10, "end_line": 15}]) # Replace line 20 edit_file("file.txt", line_operations=[{"operation": "replace", "line": 20, "content": "Updated content"}])

写入文件

向文件写入或附加内容:

# Overwrite file write_file("/path/to/file.txt", "New content") # Append to file write_file("/path/to/log.txt", "Log entry", mode="a")

获取页面

将网页内容提取为 PDF(需要安装 Chromium),然后使用本地 LLM 将其解析为 markdown:

fetch_page("https://example.com")

运输机制

MCP 支持客户端与服务器之间多种传输方式的通信:

  • 标准输入/输出(stdio) :使用标准输入/输出进行通信,非常适合本地进程
  • 服务器发送事件 (SSE) :通过 HTTP POST 请求实现服务器到客户端的流式传输,实现客户端到服务器的通信

此实现使用通过文本输入/输出进行通信的本地 MCP 服务器。

使用您自己的工具进行扩展

您可以使用@mcp.tool装饰器轻松扩展此 MCP 服务器,添加新工具。请按照 server.py 中的模式创建新工具,以向 LLM 客户端公开更多功能。

相关项目

安全注意事项

MCP 服务器允许 Claude 访问您的本地系统。请注意以下事项:

  • 服务器以你的用户身份执行shell命令
  • 它可以读取、写入和修改系统上的文件
  • 如果担心安全问题,请考虑限制对特定目录的访问
-
security - not tested
A
license - permissive license
-
quality - not tested

自定义模型上下文协议服务器,使 Claude Desktop 和其他 LLM 能够通过标准化工具接口访问文件系统操作和命令执行功能。

  1. What is the Model Context Protocol?
    1. Key Benefits of MCP
      1. Features
        1. MCP Architecture
          1. Prerequisites
            1. Installation
              1. Usage
                1. Connecting from Claude Desktop
              2. Available Tools
                1. execute_shell_command
                2. show_file
                3. search_in_file
                4. edit_file
                5. write_file
                6. fetch_page
              3. Transport Mechanisms
                1. Extending with Your Own Tools
                  1. Related Projects
                    1. Security Considerations
                      ID: fx1oclw99q