huseyinkaracif/file-operations-mcp
If you are the rightful owner of file-operations-mcp and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
The File Operations MCP Server is a secure tool for AI assistants to perform file operations within specified directories.
read_file
Read the contents of a file with optional encoding.
write_file
Write content to a file with encoding options.
list_directory
List files and directories in a given path with optional hidden file inclusion.
file_exists
Check if a file or directory exists.
File Operations MCP Server
A Model Context Protocol (MCP) server that provides secure file operation tools for AI assistants. This server allows safe reading, writing, and listing of files within configured directories.
Features
- Read Files: Read file contents with customizable encoding
- Write Files: Write content to files with encoding options
- List Directories: Browse directory contents with optional hidden file inclusion
- File Existence Check: Verify if files or directories exist
- Security: Path validation to prevent directory traversal attacks
- TypeScript: Fully typed for better development experience
Available Tools
read_file
Read the contents of a file.
Parameters:
path
(string, required): Path to the file to readencoding
(string, optional): File encoding (default: 'utf8')
write_file
Write content to a file.
Parameters:
path
(string, required): Path to the file to writecontent
(string, required): Content to write to the fileencoding
(string, optional): File encoding (default: 'utf8')
list_directory
List files and directories in a given path.
Parameters:
path
(string, required): Path to the directory to listincludeHidden
(boolean, optional): Include hidden files (default: false)
file_exists
Check if a file or directory exists.
Parameters:
path
(string, required): Path to check for existence
Installation
- Clone or download this project
- Install dependencies:
npm install
- Build the TypeScript code:
npm run build
Usage
Running the Server Directly
npm start
Development Mode
npm run dev
Adding to Cursor
To use this MCP server with Cursor, add the following configuration to your MCP settings:
- Open Cursor Settings (Cmd/Ctrl + ,)
- Go to "Features" β "Model Context Protocol"
- Add a new server configuration:
{
"file-operations": {
"command": "node",
"args": ["/absolute/path/to/file-operations-mcp/build/index.js"],
"env": {}
}
}
Or add to your ~/.cursor/mcp.json
file:
{
"mcpServers": {
"file-operations": {
"command": "node",
"args": ["/absolute/path/to/file-operations-mcp/build/index.js"]
}
}
}
Security
This server implements several security measures:
- Path Validation: All file paths are validated to prevent directory traversal attacks
- Allowed Directories: Only files within the current working directory are accessible by default
- Safe Path Resolution: Uses Node.js path utilities to safely resolve file paths
Customizing Allowed Directories
To modify which directories the server can access, edit the ALLOWED_DIRECTORIES
array in src/tools/file-tools.ts
:
const ALLOWED_DIRECTORIES = [
process.cwd(), // Current working directory
'/path/to/your/safe/directory',
// Add more allowed directories as needed
];
Example Responses
Successful File Read
{
"success": true,
"message": "File read successfully: /path/to/file.txt",
"data": {
"content": "Hello, World!",
"path": "/path/to/file.txt"
}
}
Directory Listing
{
"success": true,
"message": "Directory listed successfully: /path/to/directory",
"data": {
"files": [
{
"name": "example.txt",
"path": "/path/to/directory/example.txt",
"isDirectory": false,
"isFile": true,
"size": 1024,
"lastModified": "2024-01-15T10:30:00.000Z"
}
],
"count": 1
}
}
Error Response
{
"success": false,
"message": "Failed to read file: ENOENT: no such file or directory",
"data": null
}
Development
Scripts
npm run build
: Compile TypeScript to JavaScriptnpm start
: Run the compiled servernpm run dev
: Build and run in one commandnpm run watch
: Watch for changes and recompile
Project Structure
file-operations-mcp/
βββ src/
β βββ index.ts # Main server implementation
β βββ tools/
β β βββ file-tools.ts # File operation implementations
β βββ types/
β βββ index.ts # TypeScript type definitions
βββ build/ # Compiled JavaScript (after build)
βββ package.json
βββ tsconfig.json
βββ README.md
Requirements
- Node.js >= 18.0.0
- npm or yarn package manager
License
MIT License - see package.json for details. by HΓΌseyin Karacif
Contributing
Feel free to open issues or submit pull requests to improve this MCP server!