io.github.lu-zhengda/virtual-fs
数据与存储by lu-zhengda
PostgreSQL-backed virtual filesystem for AI agents with persistent, session-isolated storage.
什么是 io.github.lu-zhengda/virtual-fs?
PostgreSQL-backed virtual filesystem for AI agents with persistent, session-isolated storage.
README
mcp-virtual-fs
An MCP server that provides AI agents with a persistent, PostgreSQL-backed virtual filesystem. Supports session-isolated file operations, cross-session shared stores, glob/grep search, and Row Level Security — all exposed as standard Model Context Protocol tools.
Works with any MCP client: Claude Desktop, Claude Code, Cursor, Windsurf, Cline, and others.
Features
- Persistent file storage — files are stored in PostgreSQL and survive process restarts, container recycling, and redeployments
- Session isolation — each agent session gets its own namespace automatically, no configuration needed
- Cross-session stores — named persistent stores for sharing data between agents or for long-term agent memory
- 11 POSIX-style tools —
read,write,append,stat,ls,mkdir,rm,mv,glob,grep,stores - Glob and grep search — find files by pattern (
**/*.ts) or search content by regex, powered by PostgreSQL trigram indexes - Row Level Security — optional database-enforced isolation between sessions for multi-tenant deployments
- Zero config — auto-creates tables on first run with
VFS_AUTO_INIT=true
Use Cases
- Agent scratchpad — give LLM agents a persistent workspace to read/write files across tool calls
- Long-term agent memory — store notes, context, and knowledge across sessions using named stores
- Multi-agent collaboration — multiple agents share files through cross-session stores
- Sandboxed file operations — agents interact with a virtual filesystem instead of the host OS
- CI/CD artifact storage — persist build outputs, logs, and reports in a queryable filesystem
Why
Agents work well with filesystems for context management, but coupling storage to the agent runtime means data is lost when pods restart or containers are recycled. This MCP server decouples storage from runtime by moving file operations to PostgreSQL — giving agents persistent, isolated, and searchable file storage without touching the host filesystem.
Prerequisites
- Node.js 20 or later
- PostgreSQL 14 or later (with
pg_trgmextension — included in most distributions)
Quick Start
1. Set up PostgreSQL
# Using Docker
docker run -d --name vfs-postgres \
-e POSTGRES_DB=vfs \
-e POSTGRES_PASSWORD=postgres \
-p 5432:5432 \
postgres:16-alpine
2. Configure your MCP client
Add to your MCP client config (e.g., Claude Desktop claude_desktop_config.json or Claude Code .mcp.json):
{
"mcpServers": {
"virtual-fs": {
"command": "npx",
"args": ["-y", "mcp-virtual-fs"],
"env": {
"DATABASE_URL": "postgresql://postgres:postgres@localhost:5432/vfs",
"VFS_AUTO_INIT": "true"
}
}
}
}
That's it. VFS_AUTO_INIT=true creates the tables on first run.
3. Use the tools
Tool names are short POSIX-style names:
write({ path: "/notes/todo.md", content: "# My Tasks\n- Ship feature" })
read({ path: "/notes/todo.md" })
ls({ path: "/notes" })
glob({ pattern: "**/*.md" })
grep({ pattern: "TODO" })
All tools return structured JSON responses.
Tools
| Tool | Parameters | Returns | Description |
|---|---|---|---|
read | path | {content, size} | Read file contents |
write | path, content | {path, size, has_parents} | Write file (creates parents automatically) |
append | path, content | {path, appended_bytes} | Append to file (creates if missing) |
stat | path | {exists, type?, size?, children?} | Check existence and get metadata |
ls | path | {entries: [{name, type}]} | List directory (dirs first, then alphabetical) |
mkdir | path | {path, already_existed} | Create directory and parents (mkdir -p) |
rm | path | {path, deleted} | Remove file or directory recursively |
mv | source, destination | {source, destination} | Move/rename file or directory |
glob | pattern | {files, count} | Find files by glob (e.g., **/*.ts, **/*.{js,ts}) |
grep | pattern, path_filter? | {matches, count} | Search file contents by regex |
stores | (none) | {stores, count} | List all persistent store names |
All tools (except stores) accept an optional store parameter for cross-session persistent storage.
Session Management
Sessions are handled automatically — no session ID in tool parameters.
How it works:
| Transport | Session identity | Behavior |
|---|---|---|
| stdio | Auto-generated UUID per process | Each MCP connection = unique session |
| HTTP/SSE | Transport-provided sessionId | MCP protocol handles it |
| Any | VFS_SESSION_ID env var | Deterministic/resumable sessions |
Priority: transport sessionId > VFS_SESSION_ID env var > auto-generated UUID.
Resumable sessions
To resume a previous session across process restarts, set a deterministic session ID:
{
"env": {
"DATABASE_URL": "postgresql://...",
"VFS_SESSION_ID": "my-agent-session-1"
}
}
Cross-Session Stores
Named stores persist across sessions. Any session can read/write to a store by passing the store parameter:
// Session A writes to a store
write({ path: "/context.md", content: "project notes", store: "agent-memory" })
// Session B (days later) reads from the same store
read({ path: "/context.md", store: "agent-memory" })
// Without `store`, operations target the session's own namespace
write({ path: "/scratch.txt", content: "session-only data" })
// List all available stores
stores()
Stores are auto-created on first use.
Environment Variables
| Variable | Required | Default | Description |
|---|---|---|---|
DATABASE_URL | Yes | — | PostgreSQL connection string |
VFS_AUTO_INIT | No | false | Auto-create tables on startup |
VFS_SESSION_ID | No | random UUID | Deterministic session ID |
VFS_ENABLE_RLS | No | false | Enable Row Level Security |
VFS_STORAGE_BACKEND | No | postgres | Storage backend type |
Manual Database Setup
If you prefer to manage the schema yourself instead of using VFS_AUTO_INIT:
psql $DATABASE_URL -f sql/schema.sql
Row Level Security (optional)
RLS provides database-enforced session isolation. Even if application code has a bug that omits a WHERE session_id = clause, PostgreSQL itself prevents cross-session access.
# Run after schema.sql
psql $DATABASE_URL -f sql/rls.sql
# Update the vfs_app password
psql $DATABASE_URL -c "ALTER ROLE vfs_app PASSWORD 'your-secure-password'"
Then configure the MCP server to connect as vfs_app:
{
"env": {
"DATABASE_URL": "postgresql://vfs_app:your-secure-password@localhost:5432/vfs",
"VFS_ENABLE_RLS": "true"
}
}
Development
Requirements
- Node.js 20+
- Docker (for integration tests — runs PostgreSQL via testcontainers)
git clone https://github.com/lu-zhengda/mcp-virtual-fs.git
cd mcp-virtual-fs
npm install
npm run build
Commands
| Command | Description |
|---|---|
npm run build | Compile TypeScript |
npm test | Run all tests (requires Docker) |
npm run test:unit | Run unit tests only |
npm run test:integration | Run integration tests only |
npm run lint | Run ESLint |
npm run lint:fix | Auto-fix lint issues |
npm run dev | Run with tsx (no build step) |
Testing
Tests use testcontainers to spin up real PostgreSQL instances in Docker. No mocks — the integration tests exercise actual SQL queries, trigram indexes, and RLS policies.
# Requires Docker running
npm test
Session Cleanup
Ephemeral sessions can be cleaned up periodically:
DELETE FROM vfs_sessions
WHERE is_persistent = false
AND created_at < now() - interval '7 days';
The ON DELETE CASCADE on vfs_nodes handles file cleanup automatically. Persistent stores (created via the store parameter) are never affected.
License
MIT
常见问题
io.github.lu-zhengda/virtual-fs 是什么?
PostgreSQL-backed virtual filesystem for AI agents with persistent, session-isolated storage.
相关 Skills
迁移架构师
by alirezarezvani
为数据库、API 与基础设施迁移制定分阶段零停机方案,提前校验兼容性与风险,生成回滚策略、验证关卡和时间线,适合复杂系统平滑切换。
✎ 做数据库与存储迁移时,用它统一梳理表结构和数据搬迁流程,架构视角更完整,复杂迁移也更稳。
数据库建模
by alirezarezvani
把需求梳理成关系型数据库表结构,自动生成迁移脚本、TypeScript/Python 类型、种子数据、RLS 策略和索引方案,适合多租户、审计追踪、软删除等后端建模与 Schema 评审场景。
✎ 把数据库结构设计、ER图梳理和SQL建模放到一处,复杂业务也能快速统一数据模式,少走不少返工弯路。
资深数据工程师
by alirezarezvani
聚焦生产级数据工程,覆盖 ETL/ELT、批处理与流式管道、数据建模、Airflow/dbt/Spark 优化和数据质量治理,适合设计数据架构、搭建现代数据栈与排查性能问题。
✎ 复杂数据管道、ETL/ELT 和治理难题交给它,凭 Spark、Airflow、dbt 等现代数据栈经验,能更稳地搭起可扩展的数据基础设施。
相关 MCP Server
PostgreSQL 数据库
编辑精选by Anthropic
PostgreSQL 是让 Claude 直接查询和管理你的数据库的 MCP 服务器。
✎ 这个服务器解决了开发者需要手动编写 SQL 查询的痛点,特别适合数据分析师或后端开发者快速探索数据库结构。不过,由于是参考实现,生产环境使用前务必评估安全风险,别指望它能处理复杂事务。
SQLite 数据库
编辑精选by Anthropic
SQLite 是让 AI 直接查询本地数据库进行数据分析的 MCP 服务器。
✎ 这个服务器解决了 AI 无法直接访问 SQLite 数据库的问题,适合需要快速分析本地数据集的开发者。不过,作为参考实现,它可能缺乏生产级的安全特性,建议在受控环境中使用。
Firecrawl 智能爬虫
编辑精选by Firecrawl
Firecrawl 是让 AI 直接抓取网页并提取结构化数据的 MCP 服务器。
✎ 它解决了手动写爬虫的麻烦,让 Claude 能直接访问动态网页内容。最适合需要实时数据的研究者或开发者,比如监控竞品价格或抓取新闻。但要注意,它依赖第三方 API,可能涉及隐私和成本问题。