Node.js Virtual File System backend for DeepAgents.
This package provides an in-memory VFS implementation that enables agents to work with files in an isolated environment without touching the real filesystem. It uses node-vfs-polyfill which implements the upcoming Node.js VFS feature (nodejs/node#61478).
npm install @langchain/node-vfs deepagents
# or
pnpm add @langchain/node-vfs deepagents
import { VfsBackend } from "@langchain/node-vfs";
import { createDeepAgent } from "deepagents";
import { ChatAnthropic } from "@langchain/anthropic";
// Create and initialize a VFS backend
const backend = await VfsBackend.create({
initialFiles: {
"/src/index.js": "console.log('Hello from VFS!')",
},
});
try {
const agent = createDeepAgent({
model: new ChatAnthropic({ model: "claude-sonnet-4-20250514" }),
systemPrompt: "You are a coding assistant with VFS access.",
backend,
});
const result = await agent.invoke({
messages: [{ role: "user", content: "Run the index.js file" }],
});
} finally {
await backend.stop();
}
read, ls, grep, and glob run directly against VFS dataBackendProtocolV2)The main class for creating and managing the in-memory VFS backend.
VfsBackend.create(options?)Create and initialize a new VFS backend in one step.
const backend = await VfsBackend.create({
mountPath: "/vfs", // Mount path for the VFS (default: "/vfs")
initialFiles: {
// Initial files to populate
"/README.md": "# Hello",
"/src/index.js": "console.log('Hello')",
},
});
backend.uploadFiles(files)Upload files to the backend.
const encoder = new TextEncoder();
await backend.uploadFiles([
["src/app.js", encoder.encode("console.log('Hi')")],
["package.json", encoder.encode('{"name": "test"}')],
]);
backend.downloadFiles(paths)Download files from the backend.
const results = await backend.downloadFiles(["src/app.js"]);
for (const result of results) {
if (result.content) {
console.log(new TextDecoder().decode(result.content));
}
}
backend.stop()Stop the backend and clean up resources.
await backend.stop();
createVfsBackendFactory(options?)Create an async factory that creates new backend instances per invocation.
const factory = createVfsBackendFactory({
initialFiles: { "/README.md": "# Hello" },
});
const backend = await factory();
createVfsBackendFactoryFromBackend(backend)Create a factory that reuses an existing backend.
const backend = await VfsBackend.create();
const factory = createVfsBackendFactoryFromBackend(backend);
| Option | Type | Default | Description |
|---|---|---|---|
mountPath |
string |
"/vfs" |
Mount path for the virtual file system |
initialFiles |
Record<string, string \| Uint8Array> |
undefined |
Initial files to populate the VFS |
The package exports a VfsSandboxError class for typed error handling:
import { VfsSandboxError } from "@langchain/node-vfs";
try {
const result = await backend.read("/src/index.js");
if (result.error) {
throw new Error(result.error);
}
} catch (error) {
if (error instanceof VfsSandboxError) {
switch (error.code) {
case "NOT_INITIALIZED":
// Handle uninitialized backend
break;
case "FILE_OPERATION_FAILED":
// Handle file operation failures
break;
}
}
}
NOT_INITIALIZED - Backend not initializedALREADY_INITIALIZED - Backend already initializedINITIALIZATION_FAILED - Failed to initialize VFSFILE_OPERATION_FAILED - File operation failedNOT_SUPPORTED - VFS not supported in environmentThe VFS backend is fully in-memory:
VirtualFileSystem from node-vfs-polyfillread, ls, grep, and glob operate directly on VFS pathsThis approach keeps filesystem operations isolated and avoids host shell execution from this provider.
This package uses node-vfs-polyfill which implements the upcoming Node.js VFS feature being developed in nodejs/node#61478.
When the official node:vfs module lands in Node.js, this package will be updated to use the native implementation for better performance and compatibility.
MIT
Node.js VFS backend for deepagents.
Node.js VFS backend for deepagents.
Custom error class for VFS Sandbox operations.
In-memory file system provider.
Real file system provider (pass-through to Node.js fs).
Virtual File System implementation.
Base class for VFS providers.
Virtual write stream class.
Custom error class for VFS Sandbox operations.
Create a backend factory that creates a new VFS backend per invocation.
Create a backend factory that reuses an existing VFS backend.
Create a backend factory that creates a new VFS backend per invocation.
Create a backend factory that reuses an existing VFS backend.
Create a new VirtualFileSystem instance.