File System MCP Server

File System MCP Server

Provides secure file read and write operations within a sandboxed directory, allowing AI assistants to safely create, modify, and access files without risk of accessing the broader file system.

Category
访问服务器

README

File System MCP Server

A minimal Model Context Protocol (MCP) server that provides secure file read and write operations within a sandbox directory. This server is designed to work with MCP clients and includes a Gradio web interface for interactive use.

Features

This MCP server provides two simple tools:

  • read_file - Read the contents of a file from the sandbox directory
  • write_file - Write content to a file in the sandbox directory (creates file if it doesn't exist)

Security Features

  • Sandbox Directory: All file operations are restricted to the sandbox/ directory
  • Path Resolution: Paths are automatically resolved relative to the sandbox, preventing access to files outside the sandbox
  • Automatic Directory Creation: Parent directories are automatically created when writing files

Installation

  1. Install Python 3.8 or higher

  2. Install the required dependencies:

pip install -r requirements.txt

The main dependencies are:

  • openai-agents - OpenAI Agents SDK (includes MCP support)
  • openai - OpenAI SDK (used for Gemini API compatibility)
  • gradio - Web interface framework
  • python-dotenv - Environment variable management
  1. Install the MCP Python SDK:
pip install git+https://github.com/modelcontextprotocol/python-sdk.git

Or if available on PyPI:

pip install mcp

Configuration

Environment Variables

Create a .env file in the project root:

GOOGLE_API_KEY=your_google_api_key_here

Get your API key from Google AI Studio.

MCP Client Configuration

To use this MCP server with an MCP client (like Cursor), add it to your MCP configuration file.

For Cursor, add to your MCP settings (typically in ~/.cursor/mcp.json or similar):

{
  "mcpServers": {
    "file-system": {
      "command": "python3",
      "args": ["/absolute/path/to/file-system-mcp-server/server.py"],
      "env": {}
    }
  }
}

Or if you've installed it in a virtual environment:

{
  "mcpServers": {
    "file-system": {
      "command": "/path/to/venv/bin/python",
      "args": ["/absolute/path/to/file-system-mcp-server/server.py"],
      "env": {}
    }
  }
}

See example_config.json for a reference configuration.

Usage

Running the MCP Server

The server uses stdio (standard input/output) for communication:

python server.py

Using the Gradio Web Interface

The project includes a Gradio web interface (app.py) that provides an interactive way to use the MCP server with Google's Gemini model:

python app.py

This will:

  1. Launch a web interface (typically at http://127.0.0.1:7860)
  2. Connect to the MCP server
  3. Use Gemini 2.0 Flash model to process natural language prompts
  4. Execute file operations within the sandbox directory

The interface allows you to enter prompts like:

  • "Write a story about a robot"
  • "Read the file test.md"
  • "Create a file called notes.txt with some content"

Example Tool Usage

Once connected via an MCP client, you can use the tools:

Read a file:

{
  "name": "read_file",
  "arguments": {
    "path": "test.md"
  }
}

Write a file:

{
  "name": "write_file",
  "arguments": {
    "path": "hello.txt",
    "content": "Hello, World!"
  }
}

Note: All paths are relative to the sandbox/ directory. The server automatically creates the sandbox directory if it doesn't exist.

Project Structure

file-system-mcp-server/
├── server.py              # MCP server implementation
├── app.py                 # Gradio web interface
├── requirements.txt       # Python dependencies
├── example_config.json    # Example MCP client configuration
├── README.md             # This file
└── sandbox/              # Sandbox directory for file operations
    ├── hello.txt
    ├── sample.txt
    └── test.md

How It Works

MCP Server (server.py)

  1. Initializes an MCP server named "basic-fileserver"
  2. Creates a sandbox/ directory for secure file operations
  3. Provides two tools:
    • read_file: Reads files from the sandbox directory
    • write_file: Writes files to the sandbox directory
  4. All paths are resolved relative to the sandbox directory for security

Gradio Interface (app.py)

  1. Loads environment variables (including GOOGLE_API_KEY)
  2. Creates a Gemini client using OpenAI-compatible API
  3. Connects to the MCP server via stdio
  4. Uses OpenAI Agents SDK to create an agent with MCP server access
  5. Processes natural language prompts and executes file operations

Security Notes

⚠️ Important Security Considerations:

  • All file operations are restricted to the sandbox/ directory
  • Absolute paths are sanitized to prevent directory traversal
  • The sandbox directory is automatically created if it doesn't exist
  • Files in the sandbox directory are excluded from git (see .gitignore)

Development

Testing

You can test the server directly:

# Test reading a file
python -c "
import asyncio
from server import read_file_tool
result = asyncio.run(read_file_tool({'path': 'hello.txt'}))
print(result.content[0].text)
"

Adding New Tools

To add new file operations, modify server.py:

  1. Create a new async function (e.g., async def delete_file_tool(...))
  2. Add the tool to the list_tools() function
  3. Add a handler in the call_tool() function

Remember to keep all operations within the sandbox directory for security.

License

This project is provided as-is for use with the Model Context Protocol.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选