computer icon indicating copy to clipboard operation
computer copied to clipboard

Add example showing how to setup an MCP server for a workspace

Open scuffi opened this issue 4 months ago • 2 comments

Codemode already runs model-written JavaScript in an isolated Dynamic Worker, and Computer already provides bounded tools for working with a durable workspace. This example shows how to compose those pieces without adding another Computer backend or executor. All implementation code stays in examples/codemode.

The example creates an internal MCP server from Computer's read, ls, write, edit, and exec tools. Codemode's codeMcpServer() discovers those tools, generates their JavaScript types, and exposes them to MCP clients as one code tool.

MCP client
  → code
  → Codemode DynamicWorkerExecutor
  → Computer read / ls / write / edit / exec
  → durable Workspace

The main composition is intentionally small:

const computer = createComputerMcpServer(workspace);
const server = await codeMcpServer({
  server: computer,
  executor: new DynamicWorkerExecutor({
    loader: env.LOADER,
    globalOutbound: null,
  }),
});

Code running in the Codemode sandbox can then use the native Computer tools together:

async () => {
  await codemode.write({
    path: "/workspace/message.txt",
    content: "hello from codemode"
  });
  const file = await codemode.read({ path: "/workspace/message.txt" });
  const shell = await codemode.exec({ command: "pwd" });
  return { content: file.content, cwd: shell.stdout.trim() };
}

To try it locally, run:

npm install
npm run dev --workspace @example/computer-codemode

Then connect a Streamable HTTP MCP client to http://127.0.0.1:8787/mcp. The local harness is stateless and accepts POST only; it does not open a session-bound event stream.

The integration test connects with a real MCP client, checks that only code is public, exercises all five Computer tools through Codemode's Dynamic Worker, and confirms that files persist across calls. The example README documents the architecture, local setup, and trust boundary. A public deployment would still need tenant-derived Workspace identity, authentication, authorization, limits, and storage quotas.

scuffi avatar Jul 31 '26 11:07 scuffi

Open in StackBlitz

npm i https://pkg.pr.new/@cloudflare/computer@28

commit: 652d3bd

pkg-pr-new[bot] avatar Jul 31 '26 11:07 pkg-pr-new[bot]

Trying to figure out if we need this on top of the existing backend. I'm not sure what it would be used for. What are your thoughts?

aron-cf avatar Aug 01 '26 16:08 aron-cf