Skip to content

Add line limits and env variable configs #2115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/filesystem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ The server's directory access control follows this flow:



## Environment Variables

- `MCP_FILESYSTEM_MAX_LINES`: Maximum number of lines for head/tail operations (default: 5000)
- `MCP_FILESYSTEM_CHUNK_SIZE`: Chunk size in bytes for file reading operations (default: 1024)

### Example Usage with Environment Variables

```bash
# Set custom limits
export MCP_FILESYSTEM_MAX_LINES=10000
export MCP_FILESYSTEM_CHUNK_SIZE=2048

# Run the server
npx @modelcontextprotocol/server-filesystem /path/to/directory
```

## API

### Resources
Expand Down
29 changes: 21 additions & 8 deletions src/filesystem/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,21 @@ import os from 'os';
import { randomBytes } from 'crypto';
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
import { diffLines, createTwoFilesPatch } from 'diff';
import { createTwoFilesPatch } from 'diff';
import { minimatch } from 'minimatch';
import { isPathWithinAllowedDirectories } from './path-validation.js';
import { getValidRootDirectories } from './roots-utils.js';

// Constants and config
const DEFAULT_MAX_LINES = 5000;
const DEFAULT_CHUNK_SIZE = 1024;
const config = {
maxLines: process.env.MCP_FILESYSTEM_MAX_LINES ?
parseInt(process.env.MCP_FILESYSTEM_MAX_LINES, 10) : DEFAULT_MAX_LINES,
chunkSize: process.env.MCP_FILESYSTEM_CHUNK_SIZE ?
parseInt(process.env.MCP_FILESYSTEM_CHUNK_SIZE, 10) : DEFAULT_CHUNK_SIZE
};

// Command line argument parsing
const args = process.argv.slice(2);
if (args.length === 0) {
Expand Down Expand Up @@ -119,8 +129,12 @@ async function validatePath(requestedPath: string): Promise<string> {
// Schema definitions
const ReadTextFileArgsSchema = z.object({
path: z.string(),
tail: z.number().optional().describe('If provided, returns only the last N lines of the file'),
head: z.number().optional().describe('If provided, returns only the first N lines of the file')
tail: z.number().optional()
.describe('If provided, returns only the last N lines of the file')
.refine(n => n === undefined || n <= config.maxLines, `Maximum of ${config.maxLines} lines allowed`),
head: z.number().optional()
.describe('If provided, returns only the first N lines of the file')
.refine(n => n === undefined || n <= config.maxLines, `Maximum of ${config.maxLines} lines allowed`)
});

const ReadMediaFileArgsSchema = z.object({
Expand Down Expand Up @@ -388,7 +402,6 @@ function formatSize(bytes: number): string {

// Memory-efficient implementation to get the last N lines of a file
async function tailFile(filePath: string, numLines: number): Promise<string> {
const CHUNK_SIZE = 1024; // Read 1KB at a time
const stats = await fs.stat(filePath);
const fileSize = stats.size;

Expand All @@ -399,13 +412,13 @@ async function tailFile(filePath: string, numLines: number): Promise<string> {
try {
const lines: string[] = [];
let position = fileSize;
let chunk = Buffer.alloc(CHUNK_SIZE);
let chunk = Buffer.alloc(config.chunkSize);
let linesFound = 0;
let remainingText = '';

// Read chunks from the end of the file until we have enough lines
while (position > 0 && linesFound < numLines) {
const size = Math.min(CHUNK_SIZE, position);
const size = Math.min(config.chunkSize, position);
position -= size;

const { bytesRead } = await fileHandle.read(chunk, 0, size, position);
Expand Down Expand Up @@ -445,8 +458,8 @@ async function headFile(filePath: string, numLines: number): Promise<string> {
const lines: string[] = [];
let buffer = '';
let bytesRead = 0;
const chunk = Buffer.alloc(1024); // 1KB buffer

const chunk = Buffer.alloc(config.chunkSize);
// Read chunks and count lines until we have enough or reach EOF
while (lines.length < numLines) {
const result = await fileHandle.read(chunk, 0, chunk.length, bytesRead);
Expand Down