Something went wrong. Try again.
A Kubernetes operator that gives a pod its own browser as an MCP server
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960// The experiments' workload: an MCP client that runs a list of tool// calls against the sidecar and prints each result. The image ships it// beside the sidecar so that one image serves both containers, and the// pod runs it as another user.//// node workload.mjs '[["browser_navigate", {"url": "https://example.com"}], ["browser_snapshot"]]'//// A call named "sleep" waits {"seconds": n}. A target that starts with @// is the ref on the first line of the last result that contains the rest// of it, so a script can name a field it has not seen yet:// {"target": "@textbox \"Card number\""}.import { Client } from '@modelcontextprotocol/sdk/client/index.js';import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';import { writeFileSync } from 'node:fs';
const url = process.env.GOFER_MCP_URL ?? 'http://127.0.0.1:8931/mcp';const calls = JSON.parse(process.argv[2] ?? '[]');
const client = new Client({ name: 'workload', version: '0' });await client.connect(new StreamableHTTPClientTransport(new URL(url)));
const { tools } = await client.listTools();console.log(`## tools\n${tools.map(t => t.name).join('\n')}`);
let last = '';for (const [name, args = {}] of calls) { if (name === 'sleep') { await new Promise(resolve => setTimeout(resolve, args.seconds * 1000)); continue; } if (args.target?.startsWith('@')) { const ref = refOf(last, args.target.slice(1)); if (!ref) { console.log(`## ${name}: no ref for ${args.target} in the last result`); continue; } args.target = ref; } const result = await client.callTool({ name, arguments: args }); last = (result.content ?? []).filter(c => c.type === 'text').map(c => c.text).join('\n'); console.log(`## ${name} ${JSON.stringify(args)}${result.isError ? ' (error)' : ''}`); for (const c of result.content ?? []) { if (c.type === 'text') { console.log(c.text); continue; } // GOFER_IMAGES names a directory to keep screenshots in, so a person // can look at what the workload saw. const file = process.env.GOFER_IMAGES && `${process.env.GOFER_IMAGES}/${Date.now()}.jpg`; if (file) writeFileSync(file, Buffer.from(c.data, 'base64')); console.log(`[${c.type} ${c.mimeType}, ${c.data.length} base64 bytes${file ? `, saved to ${file}` : ''}]`); }}await client.close();
function refOf(text, needle) { const line = text.split('\n').find(l => l.includes(needle)); return line?.match(/\[ref=([a-z0-9]+)\]/)?.[1];}