File Context MCP
File Context MCP (Model Context Processor)
Overview
File Context MCP is a TypeScript-based application that provides an API for querying Large Language Models (LLMs) with context from local files. It supports multiple LLM providers (Ollama and Together.ai) and can process various file types to generate context-aware responses.
Core Features
1. File System Navigation
- Dynamic file and directory traversal
- Support for multiple file types (
.txt
,.md
,.ts
,.json
, etc.) - Safe path handling with sanitization
2. Context Processing
- Intelligent context formatting for LLM queries
- Context truncation to handle large files
- File content aggregation for directory queries
3. Multi-Model Support
- Ollama (local) integration
- Together.ai (cloud) integration
- Extensible model interface design
Architecture
Core Components
- Server (server.ts)
- Express.js REST API implementation
- File upload/delete handling with multer
- Request validation and routing
- OpenAPI/Swagger integration
- FileSystemTools (core/fileSystem.ts)
- File and directory operations
- Content reading and parsing
- Directory traversal
- Secure file deletion
- Error handling for file operations
- ModelInterface (core/modelInterface.ts)
- Multiple LLM provider support (Ollama, Together.ai)
- Response formatting and error handling
- Configurable model parameters
- Unified query interface
- Utility Modules
fileUtils
: File type detection, path sanitization, size formattingpromptUtils
: Context formatting, intelligent truncationvalidators
: Path, query, and model validationlogger
: Structured logging with levels
- Configuration (config/config.ts)
- Environment variable management
- API keys and endpoints
- Model configuration
- Server settings
- API Specification (resources/file-context-api.yml)
- OpenAPI 3.0 documentation
- Request/response schemas
- Endpoint documentation
- Error response definitions
API Endpoints
1. List Files
2. Upload File
3. Delete File
4. Query with Context
Setup and Configuration
- Environment Variables
- Installation
- Running the Application
How It Works
- File Processing Flow
- Request received → Path validation → File reading → Content extraction
- Directory handling includes recursive file reading
- Content filtering based on file type
- File uploads are validated for type and size
- Secure file deletion with path validation
- Context Processing
- File contents are aggregated
- Context is formatted with clear boundaries
- Large contexts are intelligently truncated
- Prompt formatting adds structure for LLM understanding
- Model Integration
- Unified interface for different LLM providers
- Error handling and response normalization
- Configurable model parameters
Security Features
- Path Sanitization
- Prevention of directory traversal attacks
- Path validation and normalization
- Safe file type checking
- File Upload Security
- File type validation
- File size limits (5MB max)
- Secure file storage
- Safe file deletion
- Input Validation
- Query content validation
- Model type verification
- Path structure verification
- File content validation
Supported File Types
The application supports the following text-based file types:
- Documentation:
.txt
,.md
- Code files:
.js
,.ts
,.jsx
,.tsx
,.py
,.java
,.cpp
,.c
,.h
- Configuration:
.json
,.yaml
,.yml
,.env
- Web files:
.html
,.css
- Data files:
.csv
,.xml
,.log
File type validation is enforced during:
- File uploads
- Context processing
- File reading operations
Maximum file size: 5MB per file
Error Handling
The application implements comprehensive error handling:
- File system errors
- API response errors
- Invalid input errors
- Model-specific errors
- File upload/deletion errors
Development
Project Structure
Adding New Features
- New File Types
- Add extensions to
fileUtils.isTextFile()
- Implement specific handlers if needed
- Add extensions to
- New Model Providers
- Extend
ModelInterface
class - Add provider to
validators.isValidModel()
- Implement provider-specific error handling
- Extend
Testing
Postman Collection
The project includes a Postman collection (postman/File-Context-MCP.postman_collection.json
) for testing all API endpoints. To use it:
- Import the Collection
- Open Postman
- Click "Import" button
- Select or drag the
File-Context-MCP.postman_collection.json
file
- Available RequestsCopy
- Testing File Operations
- List Files: View contents of the storage directory
- Upload File: Use form-data with key "file" and select a text file
- Query File: Analyze single file contents with LLM
- Analyze Directory: Process multiple files with LLM
- Example QueriesCopy
- File Upload Guide
- Use the "File Upload" request
- Select "form-data" in the Body tab
- Add key "file" with type "File"
- Choose a supported text file (see Supported File Types)
- Maximum file size: 5MB
Manual Testing
- Use the provided test files in
/storage
- Test different file types and queries
- Verify model responses and error handling
- Test file size limits and type restrictions
Environment Setup
Make sure to:
- Have the server running (
npm run dev
) - Configure environment variables
- Have Ollama running locally (for Ollama model)
- Set Together.ai API key (for Together model)
Future Considerations
- How to handle large files efficiently
- Expanding supported file types
- Optimizing context processing
- Adding streaming support for responses
- Implementing rate limiting and caching
This project demonstrates modern TypeScript/Node.js practices with a focus on modularity, type safety, and error handling while providing a flexible interface for LLM interactions with file-based context.
This server cannot be installed
This server provides an API to query Large Language Models using context from local files, supporting various models and file types for context-aware responses.