-
Notifications
You must be signed in to change notification settings - Fork 8
Expose browser filesystem through MCP #144
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
Open
masnwilliams
wants to merge
4
commits into
main
Choose a base branch
from
hypeship/expose-browser-files
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+667
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7e50277
Expose browser filesystem through MCP
masnwilliams 34756cd
Handle root filesystem archive downloads
masnwilliams d5a2072
Merge remote-tracking branch 'origin/main' into hypeship/expose-brows…
masnwilliams 24ae9c8
Align manage_browser_files with MCP tool conventions
masnwilliams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,317 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { connectTestMcp } from "@/lib/mcp/mcp-test-fixtures"; | ||
| import { registerBrowserFileTools } from "@/lib/mcp/tools/browser-files"; | ||
|
|
||
| async function callBrowserFiles(fs: unknown, args: Record<string, unknown>) { | ||
| const { client, close } = await connectTestMcp(registerBrowserFileTools, { | ||
| browsers: { fs }, | ||
| }); | ||
| try { | ||
| return await client.callTool({ | ||
| name: "manage_browser_files", | ||
| arguments: args, | ||
| }); | ||
| } finally { | ||
| await close(); | ||
| } | ||
| } | ||
|
|
||
| function text(result: Awaited<ReturnType<typeof callBrowserFiles>>) { | ||
| const [content] = result.content as Array<{ type: string; text?: string }>; | ||
| return content.type === "text" ? content.text : undefined; | ||
| } | ||
|
|
||
| describe("manage_browser_files", () => { | ||
| test("lists files", async () => { | ||
| const entries = [ | ||
| { | ||
| is_dir: false, | ||
| mod_time: "2026-01-01T00:00:00Z", | ||
| mode: "-rw-r--r--", | ||
| name: "report.txt", | ||
| path: "/tmp/report.txt", | ||
| size_bytes: 6, | ||
| }, | ||
| ]; | ||
| const fs = { | ||
| listFiles: async (sessionId: string, params: { path: string }) => { | ||
| expect(sessionId).toBe("session-1"); | ||
| expect(params).toEqual({ path: "/tmp" }); | ||
| return entries; | ||
| }, | ||
| } as any; | ||
|
|
||
| const result = await callBrowserFiles(fs, { | ||
| action: "list", | ||
| session_id: "session-1", | ||
| path: "/tmp", | ||
| }); | ||
|
|
||
| expect(JSON.parse(text(result)!)).toEqual({ items: entries }); | ||
| }); | ||
|
|
||
| test("reads text without wrapping the contents", async () => { | ||
| const fs = { | ||
| readFile: async () => new Response("hello\nworld\n"), | ||
| } as any; | ||
|
|
||
| const result = await callBrowserFiles(fs, { | ||
| action: "read", | ||
| session_id: "session-1", | ||
| path: "/tmp/hello.txt", | ||
| }); | ||
|
|
||
| expect(text(result)).toBe("hello\nworld\n"); | ||
| }); | ||
|
|
||
| test("returns binary downloads as embedded resources", async () => { | ||
| const fs = { | ||
| readFile: async () => | ||
| new Response(new Uint8Array([0, 1, 2]), { | ||
| headers: { "content-type": "image/png" }, | ||
| }), | ||
| } as any; | ||
|
|
||
| const result = await callBrowserFiles(fs, { | ||
| action: "download", | ||
| session_id: "session-1", | ||
| path: "/tmp/a file.png", | ||
| }); | ||
|
|
||
| expect(result.content).toEqual([ | ||
| { | ||
| type: "resource", | ||
| resource: { | ||
| uri: "kernel-browser-file://session-1/tmp/a%20file.png", | ||
| blob: "AAEC", | ||
| mimeType: "image/png", | ||
| }, | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| test("decodes base64 writes", async () => { | ||
| let written: Uint8Array | undefined; | ||
| const fs = { | ||
| writeFile: async ( | ||
| sessionId: string, | ||
| contents: Uint8Array, | ||
| params: { path: string; mode?: string }, | ||
| ) => { | ||
| expect(sessionId).toBe("session-1"); | ||
| expect(params).toEqual({ path: "/tmp/file.bin", mode: "0600" }); | ||
| written = contents; | ||
| }, | ||
| } as any; | ||
|
|
||
| const result = await callBrowserFiles(fs, { | ||
| action: "write", | ||
| session_id: "session-1", | ||
| path: "/tmp/file.bin", | ||
| content: "AAEC", | ||
| encoding: "base64", | ||
| mode: "0600", | ||
| }); | ||
|
|
||
| expect([...written!]).toEqual([0, 1, 2]); | ||
| expect(text(result)).toBe("Wrote file /tmp/file.bin"); | ||
| }); | ||
|
|
||
| test("rejects malformed base64 before writing", async () => { | ||
| let called = false; | ||
| const fs = { | ||
| writeFile: async () => { | ||
| called = true; | ||
| }, | ||
| } as any; | ||
|
|
||
| const result = await callBrowserFiles(fs, { | ||
| action: "write", | ||
| session_id: "session-1", | ||
| path: "/tmp/file.bin", | ||
| content: "not base64!", | ||
| encoding: "base64", | ||
| }); | ||
|
|
||
| expect(called).toBe(false); | ||
| expect(result.isError).toBe(true); | ||
| expect(text(result)).toBe("Error: content is not valid base64."); | ||
| }); | ||
|
|
||
| test("uploads multiple files", async () => { | ||
| let uploaded: any; | ||
| const fs = { | ||
| upload: async (sessionId: string, params: any) => { | ||
| expect(sessionId).toBe("session-1"); | ||
| uploaded = params; | ||
| }, | ||
| } as any; | ||
|
|
||
| const result = await callBrowserFiles(fs, { | ||
| action: "upload", | ||
| session_id: "session-1", | ||
| files: [ | ||
| { dest_path: "/tmp/one.txt", content: "one" }, | ||
| { | ||
| dest_path: "/tmp/two.bin", | ||
| content: "dHdv", | ||
| encoding: "base64", | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| expect(uploaded.files.map((file: any) => file.dest_path)).toEqual([ | ||
| "/tmp/one.txt", | ||
| "/tmp/two.bin", | ||
| ]); | ||
| expect(await uploaded.files[0].file.text()).toBe("one"); | ||
| expect(await uploaded.files[1].file.text()).toBe("two"); | ||
| expect(text(result)).toBe("Uploaded 2 file(s)"); | ||
| }); | ||
|
|
||
| test("downloads directories as embedded zip resources", async () => { | ||
| const fs = { | ||
| downloadDirZip: async () => new Response(new Uint8Array([80, 75])), | ||
| } as any; | ||
|
|
||
| const result = await callBrowserFiles(fs, { | ||
| action: "download_dir_zip", | ||
| session_id: "session-1", | ||
| path: "/tmp/reports/", | ||
| }); | ||
|
|
||
| expect((result.content as unknown[])[0]).toEqual({ | ||
| type: "resource", | ||
| resource: { | ||
| uri: "kernel-browser-file://session-1/tmp/reports.zip", | ||
| blob: "UEs=", | ||
| mimeType: "application/zip", | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test("uses an absolute resource path when downloading the root directory", async () => { | ||
| const fs = { | ||
| downloadDirZip: async () => new Response(new Uint8Array([80, 75])), | ||
| } as any; | ||
|
|
||
| const result = await callBrowserFiles(fs, { | ||
| action: "download_dir_zip", | ||
| session_id: "session-1", | ||
| path: "/", | ||
| }); | ||
|
|
||
| expect((result.content as unknown[])[0]).toEqual({ | ||
| type: "resource", | ||
| resource: { | ||
| uri: "kernel-browser-file://session-1/browser-files.zip", | ||
| blob: "UEs=", | ||
| mimeType: "application/zip", | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| test("routes filesystem mutations to the SDK", async () => { | ||
| const calls: Array<[string, unknown]> = []; | ||
| const fs = { | ||
| createDirectory: async (_id: string, params: unknown) => | ||
| calls.push(["createDirectory", params]), | ||
| move: async (_id: string, params: unknown) => | ||
| calls.push(["move", params]), | ||
| deleteFile: async (_id: string, params: unknown) => | ||
| calls.push(["deleteFile", params]), | ||
| deleteDirectory: async (_id: string, params: unknown) => | ||
| calls.push(["deleteDirectory", params]), | ||
| setFilePermissions: async (_id: string, params: unknown) => | ||
| calls.push(["setFilePermissions", params]), | ||
| } as any; | ||
|
|
||
| await callBrowserFiles(fs, { | ||
| action: "create_directory", | ||
| session_id: "session-1", | ||
| path: "/tmp/new", | ||
| mode: "0755", | ||
| }); | ||
| await callBrowserFiles(fs, { | ||
| action: "move", | ||
| session_id: "session-1", | ||
| src_path: "/tmp/old", | ||
| dest_path: "/tmp/new", | ||
| }); | ||
| await callBrowserFiles(fs, { | ||
| action: "delete_file", | ||
| session_id: "session-1", | ||
| path: "/tmp/file", | ||
| }); | ||
| await callBrowserFiles(fs, { | ||
| action: "delete_directory", | ||
| session_id: "session-1", | ||
| path: "/tmp/dir", | ||
| }); | ||
| await callBrowserFiles(fs, { | ||
| action: "set_permissions", | ||
| session_id: "session-1", | ||
| path: "/tmp/file", | ||
| mode: "0640", | ||
| owner: "1000", | ||
| group: "1000", | ||
| }); | ||
|
|
||
| expect(calls).toEqual([ | ||
| ["createDirectory", { path: "/tmp/new", mode: "0755" }], | ||
| ["move", { src_path: "/tmp/old", dest_path: "/tmp/new" }], | ||
| ["deleteFile", { path: "/tmp/file" }], | ||
| ["deleteDirectory", { path: "/tmp/dir" }], | ||
| [ | ||
| "setFilePermissions", | ||
| { path: "/tmp/file", mode: "0640", owner: "1000", group: "1000" }, | ||
| ], | ||
| ]); | ||
| }); | ||
|
|
||
| test("reports missing action parameters without calling the SDK", async () => { | ||
| const fs = new Proxy( | ||
| {}, | ||
| { | ||
| get: () => { | ||
| throw new Error("unexpected SDK call"); | ||
| }, | ||
| }, | ||
| ) as any; | ||
|
|
||
| const result = await callBrowserFiles(fs, { | ||
| action: "move", | ||
| session_id: "session-1", | ||
| src_path: "/tmp/source", | ||
| }); | ||
|
|
||
| expect(result.isError).toBe(true); | ||
| expect(text(result)).toBe("Error: dest_path is required for move."); | ||
| }); | ||
|
|
||
| test("rejects empty session IDs and paths before calling the SDK", async () => { | ||
| const fs = new Proxy( | ||
| {}, | ||
| { | ||
| get: () => { | ||
| throw new Error("unexpected SDK call"); | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| for (const args of [ | ||
| { action: "list", session_id: "", path: "/tmp" }, | ||
| { action: "list", session_id: "session-1", path: "" }, | ||
| { | ||
| action: "move", | ||
| session_id: "session-1", | ||
| src_path: "", | ||
| dest_path: "/b", | ||
| }, | ||
| ]) { | ||
| const result = await callBrowserFiles(fs, args); | ||
| expect(result.isError).toBe(true); | ||
| expect(text(result)).not.toContain("unexpected SDK call"); | ||
| } | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.