TypeScriptADK-TS

MCP Filesystem

File system operations (read/write/list) using Anthropic's filesystem MCP server

  • Name: MCP Filesystem
  • Package: @modelcontextprotocol/server-filesystem
  • Provider: Anthropic

Overview

MCP Filesystem is a third-party Model Context Protocol server from Anthropic that provides file system operations. It allows agents to read, write, and list files in designated directories with secure path restrictions.

Security

Always specify ALLOWED_DIRECTORIES to restrict file access to specific paths. Without this, the server may have broad file system access.

Usage with ADK TypeScript

import { McpFilesystem } from "@iqai/adk";

const toolset = McpFilesystem({
  env: {
    ALLOWED_DIRECTORIES: "/workspace,/project/data",
  },
});

const tools = await toolset.getTools();
import { McpToolset } from "@iqai/adk";

const toolset = new McpToolset({
  name: "Filesystem MCP Client",
  description: "Client for filesystem operations",
  transport: {
    mode: "stdio",
    command: "npx",
    args: [
      "-y",
      "@modelcontextprotocol/server-filesystem",
      "/workspace",
    ],
    env: {
      PATH: process.env.PATH || "",
    },
  },
});

const tools = await toolset.getTools();

If you want to use this MCP with Claude Desktop, add this to your Claude Desktop configuration file:

MacOS: ~/Library/Application Support/Claude/claude_desktop_config.json

Windows: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-filesystem",
        "/workspace"
      ]
    }
  }
}

Environment Variables

VariableTypeDescription
ALLOWED_DIRECTORIESOptionalComma-separated list of allowed directory paths

Configuration Options

All MCP servers support these configuration options:

OptionTypeDescription
envobjectEnvironment variables (see table above)
debugbooleanEnable debug logging
descriptionstringCustom description
retryOptions.maxRetriesnumberMax retry attempts (default: 2)
retryOptions.initialDelaynumberInitial retry delay in ms (default: 200)
samplingHandlerSamplingHandlerHandler for MCP sampling requests

Available Tools

read_file
path:stringtail:numberhead:number

Description

Read the complete contents of a file as text. DEPRECATED: Use read_text_file instead.

read_text_file
path:stringtail:numberhead:number

Description

Read the complete contents of a file from the file system as text. Handles various text encodings and provides detailed error messages if the file cannot be read. Use this tool when you need to examine the contents of a single file. Use the 'head' parameter to read only the first N lines of a file, or the 'tail' parameter to read only the last N lines of a file. Operates on the file as text regardless of extension. Only works within allowed directories.

read_media_file
path:string

Description

Read an image or audio file. Returns the base64 encoded data and MIME type. Only works within allowed directories.

read_multiple_files
paths:array

Description

Read the contents of multiple files simultaneously. This is more efficient than reading files one by one when you need to analyze or compare multiple files. Each file's content is returned with its path as a reference. Failed reads for individual files won't stop the entire operation. Only works within allowed directories.

write_file
path:stringcontent:string

Description

Create a new file or completely overwrite an existing file with new content. Use with caution as it will overwrite existing files without warning. Handles text content with proper encoding. Only works within allowed directories.

edit_file
path:stringedits:arraydryRun:boolean

Description

Make line-based edits to a text file. Each edit replaces exact line sequences with new content. Returns a git-style diff showing the changes made. Only works within allowed directories.

create_directory
path:string

Description

Create a new directory or ensure a directory exists. Can create multiple nested directories in one operation. If the directory already exists, this operation will succeed silently. Perfect for setting up directory structures for projects or ensuring required paths exist. Only works within allowed directories.

list_directory
path:string

Description

Get a detailed listing of all files and directories in a specified path. Results clearly distinguish between files and directories with [FILE] and [DIR] prefixes. This tool is essential for understanding directory structure and finding specific files within a directory. Only works within allowed directories.

list_directory_with_sizes
path:stringsortBy:string

Description

Get a detailed listing of all files and directories in a specified path, including sizes. Results clearly distinguish between files and directories with [FILE] and [DIR] prefixes. This tool is useful for understanding directory structure and finding specific files within a directory. Only works within allowed directories.

directory_tree
path:stringexcludePatterns:array

Description

Get a recursive tree view of files and directories as a JSON structure. Each entry includes 'name', 'type' (file/directory), and 'children' for directories. Files have no children array, while directories always have a children array (which may be empty). The output is formatted with 2-space indentation for readability. Only works within allowed directories.

move_file
source:stringdestination:string

Description

Move or rename files and directories. Can move files between directories and rename them in a single operation. If the destination exists, the operation will fail. Works across different directories and can be used for simple renaming within the same directory. Both source and destination must be within allowed directories.

search_files
path:stringpattern:stringexcludePatterns:array

Description

Recursively search for files and directories matching a pattern. The patterns should be glob-style patterns that match paths relative to the working directory. Use pattern like '*.ext' to match files in current directory, and '**/*.ext' to match files in all subdirectories. Returns full paths to all matching items. Great for finding files when you don't know their exact location. Only searches within allowed directories.

get_file_info
path:string

Description

Retrieve detailed metadata about a file or directory. Returns comprehensive information including size, creation time, last modified time, permissions, and type. This tool is perfect for understanding file characteristics without reading the actual content. Only works within allowed directories.

list_allowed_directories

No parameters

Description

Returns the list of directories that this server is allowed to access. Subdirectories within these allowed directories are also accessible. Use this to understand which directories and their nested paths are available before trying to access files.

Complete Example

import { McpFilesystem, LlmAgent } from "@iqai/adk";

async function main() {
  // Initialize filesystem toolset with restricted access
  const filesystemToolset = McpFilesystem({
    env: {
      ALLOWED_DIRECTORIES: "/workspace,/project/data",
    },
    debug: true,
  });

  try {
    // Get available tools
    const tools = await filesystemToolset.getTools();

    // Create agent with filesystem tools
    const agent = new LlmAgent({
      name: "file_manager",
      model: "gemini-2.5-flash",
      description: "An agent that manages files and directories",
      instruction: `You can read, write, and manage files in the allowed directories.
        Always ask before modifying files. Be careful with destructive operations.`,
      tools,
    });

    // Use the agent
    const response = await agent.ask("List all markdown files in /workspace");
    console.log(response);
  } finally {
    // Always cleanup
    await filesystemToolset.close();
  }
}

main();

Best Practices

  • Restrict Access: Always specify ALLOWED_DIRECTORIES to limit file system access
  • Validate Paths: Ensure paths are absolute and within allowed directories
  • Error Handling: Implement proper error handling for file operations
  • Cleanup: Always call toolset.close() when done to free resources
  • Permissions: Ensure the process has appropriate file system permissions

Troubleshooting

IssueSolution
Permission deniedCheck file system permissions and allowed directories
Path not allowedAdd path to ALLOWED_DIRECTORIES environment variable
File not foundVerify the file path is correct and accessible
Connection timeoutCheck if npm/npx is installed and in PATH