Virtual filesystem service for AI agents with LangChain integration
Project description
AI Filesystem Service
A virtual filesystem service designed for AI agents, providing secure file storage and management through LangChain-compatible tools. Each user gets an isolated filesystem where they can create, read, update, and append to files.
Features
- 🔐 User Isolation: Each user has their own isolated filesystem using PostgreSQL Row Level Security
- 🛠️ LangChain Integration: Ready-to-use tools for AI agents
- 🔄 Version Control: Automatic versioning for all file changes
- 🐳 Docker Support: Easy local development with docker-compose
- 🔑 Flexible Authentication: Supports both Supabase auth and dev mode
Architecture
The system consists of three main components:
- FastAPI Server (
server/): REST API that handles file operations - PostgreSQL Database: Stores files with user isolation via RLS
- LangChain Tools (
ai_filesystem/): Python client and tools for AI agents
Quick Start
Local Development with Docker
- Clone the repository:
git clone https://github.com/yourusername/ai-filesystem.git
cd ai-filesystem
- Copy the example environment file:
cp .env.example .env
- Start the services:
docker-compose up
This will start:
- PostgreSQL database on port 5432
- FastAPI server on http://localhost:8000
Using the Filesystem Tools
from ai_filesystem import get_filesystem_tools
# Initialize tools
tools = get_filesystem_tools("http://localhost:8000")
# Use with LangChain
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4")
llm_with_tools = llm.bind_tools(tools)
# The tools will automatically handle authentication
# based on your configuration
Environment Variables
Required Variables
| Variable | Description | Example |
|---|---|---|
DATABASE_URL |
PostgreSQL connection string | postgresql://user:pass@localhost:5432/dbname |
SUPABASE_URL |
Your Supabase project URL | https://your-project.supabase.co |
SUPABASE_ANON_KEY |
Supabase anonymous key for auth | eyJhbGc... |
Optional Variables
| Variable | Description | Default |
|---|---|---|
DEV_MODE |
Skip authentication (development only) | false |
MAX_FILE_SIZE |
Maximum file size in bytes | 10485760 (10MB) |
RATE_LIMIT_PER_MINUTE |
API rate limit per user | 100 |
Authentication
The service supports two authentication modes:
Production Mode (DEV_MODE=false)
Uses Supabase JWT tokens for authentication. Each request must include a valid Bearer token:
curl -H "Authorization: Bearer YOUR_JWT_TOKEN" \
http://localhost:8000/v1/files
In LangChain/LangGraph, the token is automatically extracted from the config:
config = {
"configurable": {
"langgraph_auth_user": {
"supabase_token": "your_jwt_token"
}
}
}
Development Mode (DEV_MODE=true)
Bypasses authentication for local testing. Any Bearer token is accepted, and all users get ID 00000000-0000-0000-0000-000000000000.
⚠️ Warning: Never use DEV_MODE in production!
NOTE If you are testing an agent locally, you need to set DEV_MODE=true in your agent's environment as well. This will enable the tools to work without custom authentication being set up.
API Endpoints
All endpoints require authentication (unless in dev mode):
| Method | Endpoint | Description |
|---|---|---|
| GET | /v1/files |
List all files for the authenticated user |
| GET | /v1/files/{path} |
Read a specific file |
| POST | /v1/files |
Create a new file |
| PUT | /v1/files/{path} |
Update file (requires version) |
| POST | /v1/files/{path}/append |
Append to existing file |
Database Schema
The service uses a single user_files table with Row Level Security:
CREATE TABLE user_files (
id UUID PRIMARY KEY,
user_id UUID NOT NULL,
path TEXT NOT NULL,
content TEXT,
size BIGINT,
version INTEGER DEFAULT 1,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
UNIQUE(user_id, path)
);
Deployment
Railway Deployment
- Create a new Railway project
- Add a PostgreSQL service
- Add your GitHub repository as a service
- Set environment variables:
DATABASE_URL(auto-set by Railway)SUPABASE_URLandSUPABASE_ANON_KEYDEV_MODE=falsefor production
- Run database migrations using Railway CLI:
railway link railway connect postgres # Paste contents of server/migrations/001_create_user_files.sql
Other Platforms
The service includes a Dockerfile and can be deployed to any platform that supports containers:
- Render
- Fly.io
- Google Cloud Run
- AWS App Runner
Security Considerations
- Row Level Security: The database uses RLS to ensure users can only access their own files
- Authentication: In production, all requests are validated against Supabase
- Version Control: Prevents race conditions with optimistic locking
- Rate Limiting: Configurable per-user rate limits
- File Size Limits: Configurable maximum file size
Development
Project Structure
.
├── server/ # FastAPI backend
│ ├── main.py # Application entry point
│ ├── routers/ # API endpoints
│ ├── auth.py # Authentication logic
│ ├── database.py # Database connection
│ └── migrations/ # SQL migrations
├── ai_filesystem/ # LangChain tools
│ ├── tools.py # Tool implementations
│ ├── client.py # HTTP client
│ └── models.py # Pydantic models
├── docker-compose.yml # Local development setup
└── Dockerfile # Container definition
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ai_filesystem-0.1.0.tar.gz.
File metadata
- Download URL: ai_filesystem-0.1.0.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ea84246fb2a626db1bf01ad5831bfbf7b1c90d97711ad1732cb0dbf3aa872a3
|
|
| MD5 |
544ac25c628413053af38514a54dc99a
|
|
| BLAKE2b-256 |
51d4309645b78e9f5a0b21a11ae5591e41e644e55f4f5fecc6a053fadd8139e6
|
File details
Details for the file ai_filesystem-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ai_filesystem-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4d361697b71b514d984e0df319c79add61cc44c926ad821db14196ccee28b79
|
|
| MD5 |
7dcc945ddff76de4580b42d19ff11793
|
|
| BLAKE2b-256 |
e6cfdeb87fd0476b58545acea8b5685afc9c1e40b4c6a6d4b906f4d728c3bb55
|