mcpserver-local-file

TIZ36/mcpserver-local-file

3.2

If you are the rightful owner of mcpserver-local-file and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.

This document provides a comprehensive overview of a local file system management server implemented in Golang, based on the Model Context Protocol (MCP) standard.

Tools
4
Resources
0
Prompts
0

File MCP Server

基于 MCP (Model Context Protocol) 标准的本地文件系统管理服务器,使用 Golang 实现。

功能特性

提供以下文件系统管理工具:

  1. read_file - 读取文件内容
  2. write_file - 写入或创建文件
  3. delete_file - 删除文件或目录
  4. list_directory - 列出目录内容

快速开始

安装依赖

go mod download

运行服务器

go run main.go

服务器将在 http://localhost:18062/mcp 启动。

MCP 协议实现

本服务器遵循 MCP StreamableHTTP Transport 规范:

  • 单一端点: 所有请求都发送到 /mcp
  • JSON-RPC 2.0: 使用标准 JSON-RPC 协议
  • Session 管理: 通过 mcp-session-id 头维护会话
  • 标准方法:
    • initialize - 初始化连接
    • notifications/initialized - 初始化通知
    • tools/list - 列出可用工具
    • tools/call - 调用工具

工具说明

read_file

读取指定路径的文件内容。

参数:

  • path (string, 必需): 文件的本地路径

示例:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "read_file",
    "arguments": {
      "path": "/path/to/file.txt"
    }
  }
}

write_file

写入或创建文件。如果文件不存在则创建,存在则覆盖。

参数:

  • path (string, 必需): 文件的本地路径
  • content (string, 必需): 要写入的文件内容

示例:

{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/call",
  "params": {
    "name": "write_file",
    "arguments": {
      "path": "/path/to/file.txt",
      "content": "Hello, World!"
    }
  }
}

delete_file

删除文件或目录(递归删除)。

参数:

  • path (string, 必需): 要删除的文件或目录路径

示例:

{
  "jsonrpc": "2.0",
  "id": 3,
  "method": "tools/call",
  "params": {
    "name": "delete_file",
    "arguments": {
      "path": "/path/to/file.txt"
    }
  }
}

list_directory

列出指定目录的内容。

参数:

  • path (string, 必需): 要列出的目录路径

示例:

{
  "jsonrpc": "2.0",
  "id": 4,
  "method": "tools/call",
  "params": {
    "name": "list_directory",
    "arguments": {
      "path": "/path/to/directory"
    }
  }
}

安全说明

  • 路径安全检查:防止路径遍历攻击(..
  • 支持绝对路径和相对路径
  • 建议在生产环境中添加更严格的路径白名单限制

测试

可以使用 curl 或任何支持 HTTP POST 的工具测试服务器:

# 初始化连接
curl -X POST http://localhost:18062/mcp \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "initialize",
    "params": {
      "protocolVersion": "2024-11-05",
      "capabilities": {},
      "clientInfo": {
        "name": "test-client",
        "version": "1.0.0"
      }
    }
  }'

参考