Sponsored by Deepsite.site

Filesystem MCP Server

Created By
cyanheads10 months ago
A Model Context Protocol (MCP) server for platform-agnostic file capabilities, including advanced search/replace and directory tree traversal
Content

Filesystem MCP Server

TypeScript Model Context Protocol Version License Status GitHub

Empower your AI agents with robust, platform-agnostic file system capabilities.

This Model Context Protocol (MCP) server provides a secure and reliable interface for AI agents to interact with the local filesystem. It enables reading, writing, updating, and managing files and directories, backed by a production-ready TypeScript foundation featuring comprehensive logging, error handling, and security measures.

Table of Contents

Overview

The Model Context Protocol (MCP) is a standard framework allowing AI models to securely interact with external tools and data sources (resources). This server implements the MCP standard to expose essential filesystem operations as tools, enabling AI agents to:

  • Read and analyze file contents.
  • Create, modify, or overwrite files.
  • Manage directories and file paths.
  • Perform targeted updates within files.

Built with TypeScript, the server emphasizes type safety, modularity, and robust error handling, making it suitable for reliable integration into AI workflows.

Architecture

The server employs a layered architecture for clarity and maintainability:

flowchart TB
    subgraph API["API Layer"]
        direction LR
        MCP["MCP Protocol Interface"]
        Val["Input Validation (Zod)"]
        San["Path Sanitization"]

        MCP --> Val --> San
    end

    subgraph Core["Core Services"]
        direction LR
        Config["Configuration"]
        Logger["Logging (Winston)"]
        Error["Error Handling"]
        Server["MCP Server Logic"]
        State["Session State"]

        Config --> Server
        Logger --> Server & Error
        Error --> Server
        State --> Server
    end

    subgraph Implementation["Tool Implementation"]
        direction LR
        ToolLogic["Filesystem Tools"]
        Utils["Core Utilities"]

        ToolLogic --> Server
        Utils -- Used by --> ToolLogic
        Utils -- Used by --> Core
    end

    San --> ToolLogic

    classDef layer fill:#2d3748,stroke:#4299e1,stroke-width:3px,rx:5,color:#fff
    classDef component fill:#1a202c,stroke:#a0aec0,stroke-width:2px,rx:3,color:#fff
    class API,Core,Implementation layer
    class MCP,Val,San,Config,Logger,Error,Server,State,ToolLogic,Utils component
  • API Layer: Handles MCP communication, validates inputs using Zod, and sanitizes paths.
  • Core Services: Manages configuration, logging, error reporting, session state (like the default working directory), and the main MCP server instance.
  • Tool Implementation: Contains the specific logic for each filesystem tool, leveraging shared utilities.

Features

  • Comprehensive File Operations: Tools for reading, writing, listing, deleting, moving, and copying files and directories.
  • Targeted Updates: update_file tool allows precise search-and-replace operations within files, supporting plain text and regex.
  • Session-Aware Path Management: set_filesystem_default tool establishes a default working directory for resolving relative paths during a session.
  • Security First: Built-in path sanitization prevents directory traversal attacks. Optional base directory restriction enhances security.
  • Robust Foundation: Includes production-grade utilities for:
    • Structured, context-aware logging.
    • Standardized error handling with specific error codes.
    • Unique ID generation for request tracing.
    • Input sanitization.
    • Optional rate limiting (util file created but not yet integrated).
  • Type Safety: Fully implemented in TypeScript for improved reliability and maintainability.

Installation

Steps

  1. Clone the repository:
    git clone https://github.com/cyanheads/filesystem-mcp-server.git
    cd filesystem-mcp-server
    
  2. Install dependencies:
    npm install
    
  3. Build the project:
    npm run build
    
    This compiles the TypeScript code to JavaScript in the dist/ directory and makes the main script executable. The executable will be located at dist/index.js.

Configuration

Configure the server using environment variables:

  • FS_BASE_DIRECTORY (Optional, Recommended for Security):
    • If set to an absolute path, all file operations performed by the server will be strictly confined within this directory and its subdirectories. This prevents the AI agent from accessing files outside the intended scope.
    • Example: FS_BASE_DIRECTORY=/Users/casey/safe-agent-files
  • LOG_LEVEL (Optional):
    • Controls the verbosity of logs. Options: error, warn, info, http, verbose, debug, silly.
    • Defaults to info.
  • LOG_DIR (Optional):
    • Specifies the directory where log files (combined.log, error.log) will be stored.
    • Defaults to ./logs relative to the server's execution directory.

Usage with MCP Clients

To allow an MCP client (like an AI assistant) to use this server:

  1. Run the Server: Start the server from your terminal:

    node dist/index.js
    # Or if you are in the project root:
    # npm start
    
  2. Configure the Client: Add the server to your MCP client's configuration. The exact method depends on the client, but typically involves specifying:

    • Command: node
    • Arguments: The absolute path to the built server executable (e.g., /path/to/filesystem-mcp-server/dist/index.js).
    • Environment Variables (Optional): Set FS_BASE_DIRECTORY, LOG_LEVEL, or LOG_DIR as needed.

    Example MCP Settings (Conceptual):

    {
      "mcpServers": {
        "filesystem": {
          "command": "node",
          "args": ["/path/to/filesystem-mcp-server/dist/index.js"],
          "env": {
            "FS_BASE_DIRECTORY": "/path/to/base/directory",
            "LOG_LEVEL": "debug"
          },
          "disabled": false,
          "autoApprove": []
        }
        // ... other servers
      }
    }
    

Once configured and running, the client will detect the server and its available tools.

Available Tools

The server exposes the following tools for filesystem interaction:

ToolDescription
set_filesystem_defaultSets a default absolute path for the current session. Relative paths used in subsequent tool calls will be resolved against this default. Resets on server restart.
read_fileReads the entire content of a specified file as UTF-8 text. Accepts relative (resolved against default) or absolute paths.
write_fileWrites content to a specified file. Creates the file (and necessary parent directories) if it doesn't exist, or overwrites it if it does. Accepts relative or absolute paths.
update_filePerforms targeted search-and-replace operations within an existing file using an array of {search, replace} blocks. Ideal for localized changes. Supports plain text or regex search (useRegex: true) and replacing all occurrences (replaceAll: true). Accepts relative or absolute paths. File must exist.
list_filesLists files and directories within a specified path. Options include recursive listing (includeNested: true) and limiting the number of entries (maxEntries). Returns a formatted tree structure. Accepts relative or absolute paths.
delete_filePermanently removes a specific file. Accepts relative or absolute paths.
delete_directoryPermanently removes a directory. Use recursive: true to remove non-empty directories and their contents (use with caution!). Accepts relative or absolute paths.
create_directoryCreates a new directory at the specified path. By default (create_parents: true), it also creates any necessary parent directories. Accepts relative or absolute paths.
move_pathMoves or renames a file or directory from a source path to a destination path. Accepts relative or absolute paths for both.
copy_pathCopies a file or directory from a source path to a destination path. For directories, it copies recursively by default (recursive: true). Accepts relative or absolute paths.

Refer to the tool registration files (src/mcp-server/tools/*/registration.ts) for detailed input/output schemas (Zod/JSON Schema).

Project Structure

The codebase is organized for clarity and maintainability:

filesystem-mcp-server/
├── dist/                 # Compiled JavaScript output (after npm run build)
├── logs/                 # Log files (created at runtime)
├── node_modules/         # Project dependencies
├── src/                  # TypeScript source code
│   ├── config/           # Configuration loading (index.ts)
│   ├── mcp-server/       # Core MCP server logic
│   │   ├── server.ts     # Server initialization and tool registration
│   │   ├── state.ts      # Session state management (e.g., default path)
│   │   └── tools/        # Individual tool implementations (one subdir per tool)
│   │       ├── readFile/
│   │       │   ├── index.ts
│   │       │   ├── readFileLogic.ts
│   │       │   └── registration.ts
│   │       └── ...       # Other tools (writeFile, updateFile, etc.)
│   ├── types-global/     # Shared TypeScript types and interfaces
│   │   ├── errors.ts     # Custom error classes and codes
│   │   ├── mcp.ts        # MCP related types
│   │   └── tool.ts       # Tool definition types
│   ├── utils/            # Reusable utility modules
│   │   ├── errorHandler.ts
│   │   ├── idGenerator.ts
│   │   ├── index.ts
│   │   ├── logger.ts
│   │   ├── rateLimiter.ts
│   │   ├── requestContext.ts
│   │   └── sanitization.ts
│   └── index.ts          # Main application entry point
├── .clinerules           # Cheatsheet for LLM assistants
├── .gitignore
├── LICENSE
├── package.json
├── package-lock.json
├── README.md             # This file
└── tsconfig.json         # TypeScript compiler options

For a live, detailed view of the current structure, run: npm run tree

Developer Note: This repository includes a .clinerules file. This cheat sheet provides your LLM coding assistant with essential context about codebase patterns, file locations, and usage examples. Keep it updated as the server evolves!

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.


Built with ❤️ and the Model Context Protocol
Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Amap Maps高德地图官方 MCP Server
WindsurfThe new purpose-built IDE to harness magic
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
ChatWiseThe second fastest AI chatbot™
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
Playwright McpPlaywright MCP server
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
DeepChatYour AI Partner on Desktop
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Tavily Mcp
CursorThe AI Code Editor
Y GuiA web-based graphical interface for AI chat interactions with support for multiple AI models and MCP (Model Context Protocol) servers.
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Serper MCP ServerA Serper MCP Server
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code