-
-
Notifications
You must be signed in to change notification settings - Fork 505
feat: export workspace files as MCP resources #308
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
svomro
wants to merge
1
commit into
Waishnav:main
Choose a base branch
from
svomro:feat/export-artifact-resource
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.
Open
Changes from all commits
Commits
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,200 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { mkdir, mkdtemp, rm, symlink, writeFile } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join, resolve } from "node:path"; | ||
| import { test } from "node:test"; | ||
| import { Client } from "@modelcontextprotocol/sdk/client/index.js"; | ||
| import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"; | ||
| import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
| import { | ||
| ARTIFACT_RESOURCE_MAX_BYTES, | ||
| clearExportedArtifactsForTests, | ||
| exportWorkspaceArtifact, | ||
| readExportedArtifactResource, | ||
| registerArtifactExportTool, | ||
| } from "./artifact-export.js"; | ||
| import type { ServerConfig } from "./config.js"; | ||
| import type { WorkspaceRegistry } from "./workspaces.js"; | ||
|
|
||
| async function fixture(t: { after(callback: () => void | Promise<void>): void }) { | ||
| const root = await mkdtemp(join(tmpdir(), "devspace-artifact-export-")); | ||
| const workspace = join(root, "workspace"); | ||
| const outside = join(root, "outside"); | ||
| await Promise.all([mkdir(workspace), mkdir(outside)]); | ||
| t.after(async () => { | ||
| await clearExportedArtifactsForTests(); | ||
| await rm(root, { recursive: true, force: true }); | ||
| }); | ||
| return { workspace, outside }; | ||
| } | ||
|
|
||
| function workspaceRegistry(root: string): WorkspaceRegistry { | ||
| return { | ||
| getWorkspace(id: string) { | ||
| assert.equal(id, "ws_test"); | ||
| return { id, root }; | ||
| }, | ||
| resolvePath(_workspace: unknown, path: string) { | ||
| return resolve(root, path); | ||
| }, | ||
| } as unknown as WorkspaceRegistry; | ||
| } | ||
|
|
||
| async function connectedServer(root: string) { | ||
| const server = new McpServer({ name: "artifact-export-test", version: "1.0.0" }); | ||
| registerArtifactExportTool(server, { | ||
| config: { | ||
| artifactMaxFileBytes: ARTIFACT_RESOURCE_MAX_BYTES, | ||
| logging: { toolCalls: false }, | ||
| } as unknown as ServerConfig, | ||
| workspaces: workspaceRegistry(root), | ||
| }); | ||
| const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair(); | ||
| const client = new Client({ name: "artifact-export-test-client", version: "1.0.0" }); | ||
| await Promise.all([ | ||
| client.connect(clientTransport), | ||
| server.connect(serverTransport), | ||
| ]); | ||
| return { | ||
| client, | ||
| close: async () => { | ||
| await client.close(); | ||
| await server.close(); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| test("export_artifact materializes through resources/read across MCP sessions", async (t) => { | ||
| const { workspace } = await fixture(t); | ||
| const bytes = Buffer.from("artifact-diagnostic-marker\n", "utf8"); | ||
| await writeFile(join(workspace, "note.txt"), bytes); | ||
|
|
||
| const first = await connectedServer(workspace); | ||
| const exported = await first.client.callTool({ | ||
| name: "export_artifact", | ||
| arguments: { workspaceId: "ws_test", path: "note.txt" }, | ||
| }); | ||
| await first.close(); | ||
|
|
||
| const content = exported.content as Array<{ | ||
| type: string; | ||
| uri?: string; | ||
| name?: string; | ||
| mimeType?: string; | ||
| size?: number; | ||
| }>; | ||
| const link = content.find((item) => item.type === "resource_link"); | ||
| assert.ok(link?.uri); | ||
| assert.equal(link.name, "note.txt"); | ||
| assert.equal(link.mimeType, "text/plain; charset=utf-8"); | ||
| assert.equal(link.size, bytes.length); | ||
| assert.match(link.uri, /^artifact:\/\/devspace\/[A-Za-z0-9_-]{43}$/); | ||
| assert.equal(JSON.stringify(exported).includes(bytes.toString("base64")), false); | ||
|
|
||
| const second = await connectedServer(workspace); | ||
| const read = await second.client.readResource({ uri: link.uri }); | ||
| await second.close(); | ||
| assert.deepEqual(read.contents, [{ | ||
| uri: link.uri, | ||
| mimeType: "text/plain; charset=utf-8", | ||
| text: bytes.toString("utf8"), | ||
| }]); | ||
| }); | ||
|
|
||
| test("binary resources use MCP blob content", async (t) => { | ||
| const { workspace } = await fixture(t); | ||
| const bytes = Buffer.from([0, 1, 2, 255, 10]); | ||
| const filePath = join(workspace, "payload.bin"); | ||
| await writeFile(filePath, bytes); | ||
|
|
||
| const exported = await exportWorkspaceArtifact({ workspaceRoot: workspace, filePath }); | ||
| const token = exported.uri.split("/").at(-1) ?? ""; | ||
| const read = await readExportedArtifactResource(token, exported.uri); | ||
| assert.deepEqual(read.contents, [{ | ||
| uri: exported.uri, | ||
| mimeType: "application/octet-stream", | ||
| blob: bytes.toString("base64"), | ||
| }]); | ||
| }); | ||
|
|
||
| test("the exact 8 MiB resource boundary remains exportable", async (t) => { | ||
| const { workspace } = await fixture(t); | ||
| const filePath = join(workspace, "boundary.bin"); | ||
| await writeFile(filePath, Buffer.alloc(ARTIFACT_RESOURCE_MAX_BYTES, 0x5a)); | ||
|
|
||
| const exported = await exportWorkspaceArtifact({ workspaceRoot: workspace, filePath }); | ||
| assert.equal(exported.size, ARTIFACT_RESOURCE_MAX_BYTES); | ||
| }); | ||
|
|
||
| test("files larger than the MCP resource limit are rejected", async (t) => { | ||
| const { workspace } = await fixture(t); | ||
| const filePath = join(workspace, "too-large.bin"); | ||
| await writeFile(filePath, Buffer.alloc(ARTIFACT_RESOURCE_MAX_BYTES + 1, 0x5a)); | ||
|
|
||
| await assert.rejects( | ||
| exportWorkspaceArtifact({ workspaceRoot: workspace, filePath }), | ||
| /configured MCP resource materialization limit/, | ||
| ); | ||
| }); | ||
|
|
||
| test("a lower configured per-file limit is enforced", async (t) => { | ||
| const { workspace } = await fixture(t); | ||
| const filePath = join(workspace, "configured-limit.bin"); | ||
| await writeFile(filePath, Buffer.alloc(5)); | ||
|
|
||
| await assert.rejects( | ||
| exportWorkspaceArtifact({ | ||
| workspaceRoot: workspace, | ||
| filePath, | ||
| maxFileBytes: 4, | ||
| }), | ||
| /configured MCP resource materialization limit/, | ||
| ); | ||
| }); | ||
|
|
||
| test("missing sources fail without exposing their absolute path", async (t) => { | ||
| const { workspace } = await fixture(t); | ||
| const filePath = join(workspace, "does-not-exist.txt"); | ||
|
|
||
| await assert.rejects( | ||
| exportWorkspaceArtifact({ workspaceRoot: workspace, filePath }), | ||
| (error: unknown) => { | ||
| assert.ok(error instanceof Error); | ||
| assert.match(error.message, /existing regular file inside the selected workspace/); | ||
| assert.equal(error.message.includes(workspace), false); | ||
| return true; | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| test("symlinks resolving outside the workspace are rejected", async (t) => { | ||
| if (process.platform === "win32") t.skip("symlink fixture differs on Windows"); | ||
| const { workspace, outside } = await fixture(t); | ||
| const outsideFile = join(outside, "secret.txt"); | ||
| const linkedFile = join(workspace, "linked.txt"); | ||
| await writeFile(outsideFile, "secret"); | ||
| await symlink(outsideFile, linkedFile); | ||
|
|
||
| await assert.rejects( | ||
| exportWorkspaceArtifact({ workspaceRoot: workspace, filePath: linkedFile }), | ||
| /must resolve to a file inside the selected workspace/, | ||
| ); | ||
| }); | ||
|
|
||
| test("expired resources cannot be read", async (t) => { | ||
| const { workspace } = await fixture(t); | ||
| const filePath = join(workspace, "short-lived.txt"); | ||
| await writeFile(filePath, "short-lived"); | ||
| const exported = await exportWorkspaceArtifact({ | ||
| workspaceRoot: workspace, | ||
| filePath, | ||
| ttlMs: 5, | ||
| }); | ||
| const token = exported.uri.split("/").at(-1) ?? ""; | ||
| await new Promise((resolvePromise) => setTimeout(resolvePromise, 15)); | ||
|
|
||
| await assert.rejects( | ||
| readExportedArtifactResource(token, exported.uri), | ||
| /no longer available/, | ||
| ); | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
sed -n '1,210p' src/artifact-export.test.tsRepository: Waishnav/devspace
Length of output: 7221
🏁 Script executed:
rg -n -A18 -B8 'resolvePath' src/workspaces.tsRepository: Waishnav/devspace
Length of output: 2621
Path Traversal (CWE-22): Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
Reachability: External · Exploitability: Trivial
Cover path escapes through
callTool.The existing test invokes
exportWorkspaceArtifactdirectly. Add acallToolcase for../outside/secret.txtand assert an error result for the host-facing contract.🤖 Prompt for AI Agents