@finalai/agent-provider 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -35
- package/dist/modules/channel.d.mts +53 -12
- package/dist/modules/channel.mjs +1 -0
- package/dist/modules/channel.mjs.map +2 -2
- package/dist/modules/credential-routes.d.mts +1 -3
- package/dist/modules/credential-routes.mjs +0 -2
- package/dist/modules/credential-routes.mjs.map +2 -2
- package/dist/modules/document.d.mts +0 -18
- package/dist/modules/document.mjs +1 -16
- package/dist/modules/document.mjs.map +3 -3
- package/dist/modules/index.d.mts +0 -1
- package/dist/modules/index.mjs +0 -1
- package/dist/modules/index.mjs.map +2 -2
- package/dist/modules/local-channel-context.d.mts +1 -1
- package/dist/modules/local-channel-context.mjs +1 -1
- package/dist/modules/local-channel-context.mjs.map +2 -2
- package/dist/modules/sandbox/client.mjs +0 -3
- package/dist/modules/sandbox/client.mjs.map +2 -2
- package/dist/modules/sandbox/execution-guard.d.mts +0 -1
- package/dist/modules/sandbox/execution-guard.mjs +0 -9
- package/dist/modules/sandbox/execution-guard.mjs.map +2 -2
- package/dist/modules/sandbox/grant.d.mts +0 -1
- package/dist/modules/sandbox/grant.mjs +0 -4
- package/dist/modules/sandbox/grant.mjs.map +2 -2
- package/dist/modules/sandbox/index.d.mts +0 -2
- package/dist/modules/sandbox/index.mjs +1 -10
- package/dist/modules/sandbox/index.mjs.map +2 -2
- package/dist/modules/sandbox/mcp-hosted.d.mts +0 -1
- package/dist/modules/sandbox/mcp-hosted.mjs +1 -8
- package/dist/modules/sandbox/mcp-hosted.mjs.map +2 -2
- package/dist/modules/sandbox/mcp.d.mts +2 -4
- package/dist/modules/sandbox/mcp.mjs +6 -52
- package/dist/modules/sandbox/mcp.mjs.map +2 -2
- package/dist/modules/sandbox/remote/body.d.mts +1 -0
- package/dist/modules/sandbox/remote/body.mjs +27 -0
- package/dist/modules/sandbox/remote/body.mjs.map +7 -0
- package/dist/modules/sandbox/remote/serve.mjs +3 -2
- package/dist/modules/sandbox/remote/serve.mjs.map +2 -2
- package/dist/modules/sandbox/server.mjs +0 -14
- package/dist/modules/sandbox/server.mjs.map +2 -2
- package/dist/modules/sandbox/setup/apply.d.mts +1 -2
- package/dist/modules/sandbox/setup/apply.mjs +6 -20
- package/dist/modules/sandbox/setup/apply.mjs.map +2 -2
- package/dist/modules/sandbox/setup/describe.mjs +1 -1
- package/dist/modules/sandbox/setup/describe.mjs.map +1 -1
- package/dist/modules/sandbox/setup/schema.d.mts +2 -1
- package/dist/modules/sandbox/tool-policy.mjs +1 -19
- package/dist/modules/sandbox/tool-policy.mjs.map +2 -2
- package/dist/modules/sandbox/types.d.mts +2 -16
- package/dist/modules/sandbox/types.mjs +4 -13
- package/dist/modules/sandbox/types.mjs.map +3 -3
- package/dist/modules/sandbox/wire.d.mts +0 -17
- package/dist/modules/sandbox/wire.mjs +0 -12
- package/dist/modules/sandbox/wire.mjs.map +2 -2
- package/package.json +3 -8
- package/dist/modules/room-surface.d.mts +0 -350
- package/dist/modules/room-surface.mjs +0 -156
- package/dist/modules/room-surface.mjs.map +0 -7
- package/dist/modules/sandbox/networking.d.mts +0 -73
- package/dist/modules/sandbox/networking.mjs +0 -157
- package/dist/modules/sandbox/networking.mjs.map +0 -7
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../../src/sandbox/mcp.ts"],
|
|
4
|
-
"sourcesContent": ["import { resolveSandboxToolPolicy } from \"./tool-policy.mjs\";\nimport type { FetchLike } from '@modelcontextprotocol/client';\nimport {\n createMcpHandler,\n fromJsonSchema,\n McpServer,\n ResourceTemplate,\n type CallToolResult,\n type McpHttpHandler,\n type ReadResourceResult,\n} from '@modelcontextprotocol/server';\nimport * as z from 'zod';\nimport {\n WorkspaceEgressAction,\n workspaceEgressRequestSchema,\n workspaceEgressStatusSchema,\n} from '@finalai/agent-protocol/device-egress';\nimport type { JsonObject, JsonSchema, JsonValue } from \"../json.mjs\";\nimport { Capability, FilesAccess, ScreenAccess, ShellAccess } from '@finalai/agent-protocol';\nimport { ToolApprovalMode as ToolApproval } from \"../document.mjs\";\nimport type { ToolDescriptorIface } from \"../tool-descriptor.mjs\";\nimport { FULL_SANDBOX_GRANT, grantAllows, type SandboxGrantIface } from \"./grant.mjs\";\nimport { bytesToBase64, utf8Decode } from \"./bytes.mjs\";\nimport {\n createHostedMcpConnections,\n EMPTY_MCP_CATALOG,\n hostedTools,\n SandboxHostedServerKind,\n type HostedToolIface,\n type SandboxHostedServer,\n} from \"./mcp-hosted.mjs\";\nimport { globFiles, grepFiles } from \"./mcp-search.mjs\";\nimport { createSandboxShell, SandboxShellMode } from \"./mcp-shell.mjs\";\nimport { resolvePosixPath } from \"./paths.mjs\";\nimport {\n PortProtocol,\n SandboxCapability,\n SandboxEntryType,\n type NetworkSandboxSessionIface,\n type SandboxMcpCatalogIface,\n type SandboxProcessIface,\n} from \"./types.mjs\";\n\nexport {\n COMPUTER_USE_SERVER_NAME,\n SandboxHostedServerKind,\n withHostedMcpEndpoints,\n type SandboxHostedServer,\n} from \"./mcp-hosted.mjs\";\nexport { SandboxShellMode } from \"./mcp-shell.mjs\";\n\n// The standard sandbox MCP projection, generated from a\n// handle for harnesses that do not speak the handle RPC. `tools/list` is\n// answered from static descriptors, so enumerating tools never starts a\n// sandbox; the handle is obtained on the first tool call.\n//\n// The sandbox's hosted MCP servers (computer use, local MCP) are projected as\n// their own tools. Their catalogs come from the provider when it knows them\n// before anything starts (`catalog`); otherwise the projection lists what it\n// knows, learns the rest from the servers once the sandbox is attached, and\n// announces it with `notifications/tools/list_changed`.\n\nexport enum SandboxMcpTool {\n Run = 'run',\n Spawn = 'spawn',\n ProcessOutput = 'process_output',\n ProcessInput = 'process_input',\n ProcessTerminate = 'process_terminate',\n ReadFile = 'read_file',\n WriteFile = 'write_file',\n EditFile = 'edit_file',\n ListDir = 'list_dir',\n Stat = 'stat',\n Grep = 'grep',\n Glob = 'glob',\n GetHost = 'get_host',\n Egress = 'egress',\n}\n\n// What one projection serves: everything, only the hosted servers (for\n// harnesses whose native tools use the handle RPC), the computer-use server,\n// or one local MCP server (the per-server addresses the handle RPC hands out).\nexport enum SandboxMcpScopeKind {\n All = 'all',\n Hosted = 'hosted',\n ComputerUse = 'computerUse',\n LocalServer = 'localServer',\n}\n\nexport type SandboxMcpScope =\n | { kind: SandboxMcpScopeKind.All | SandboxMcpScopeKind.Hosted | SandboxMcpScopeKind.ComputerUse }\n | { kind: SandboxMcpScopeKind.LocalServer; server: string };\n\nexport const SANDBOX_FILE_RESOURCE_SCHEME = 'sandbox-file';\n\nexport interface SandboxMcpDescriptorOptionsIface {\n capabilities: readonly SandboxCapability[];\n // What the projection's caller may do; tools it does not allow are not listed. Default: everything.\n grant?: SandboxGrantIface;\n lifecycle?: boolean;\n // The hosted servers' tools known so far (the provider's `mcpCatalog`, or\n // learned on an earlier attach). Default: none known.\n catalog?: SandboxMcpCatalogIface;\n scope?: SandboxMcpScope;\n}\n\ninterface ToolSpecIface {\n descriptor: ToolDescriptorIface;\n capability: SandboxCapability;\n write: boolean;\n // toolPolicy keys after the tool name, most specific first.\n policyKeys: string[];\n // A tool of a hosted MCP server, proxied to it.\n hosted?: HostedToolIface;\n}\n\nconst objectSchema = (properties: JsonObject, required: string[]): JsonSchema => ({\n type: 'object',\n properties,\n required,\n additionalProperties: false,\n});\n\nconst str = (description: string): JsonObject => ({ type: 'string', description });\nconst int = (description: string, minimum = 0): JsonObject => ({\n type: 'integer',\n minimum,\n description,\n});\nconst envSchema: JsonObject = {\n type: 'object',\n additionalProperties: { type: 'string' },\n description: 'Extra environment variables for this command only.',\n};\n\ntype ToolAnnotationsIface = NonNullable<ToolDescriptorIface['annotations']>;\n\nconst readOnly = {\n readOnlyHint: true,\n destructiveHint: false,\n idempotentHint: true,\n openWorldHint: false,\n};\nconst mutating = {\n readOnlyHint: false,\n destructiveHint: true,\n idempotentHint: false,\n openWorldHint: false,\n};\n\nconst tool = (args: {\n name: string;\n title: string;\n description: string;\n inputSchema: JsonSchema;\n capability: SandboxCapability;\n write: boolean;\n policyKeys: string[];\n idempotent?: boolean;\n destructive?: boolean;\n}): ToolSpecIface => {\n const annotations: ToolAnnotationsIface = {\n title: args.title,\n ...(args.write ? mutating : readOnly),\n };\n if (args.destructive != null) annotations.destructiveHint = args.destructive;\n if (args.idempotent != null) annotations.idempotentHint = args.idempotent;\n return {\n descriptor: {\n name: args.name,\n description: args.description,\n inputSchema: args.inputSchema,\n annotations,\n },\n capability: args.capability,\n write: args.write,\n policyKeys: args.policyKeys,\n };\n};\n\nconst BUILT_IN_TOOLS: readonly ToolSpecIface[] = [\n tool({\n name: SandboxMcpTool.Run,\n title: 'Run command',\n description:\n 'Run a shell command in the sandbox and wait for it. The shell persists between calls (cd and exported variables carry over). Returns exit_code, stdout and stderr.',\n inputSchema: objectSchema(\n {\n command: str('The bash command to run.'),\n cwd: str('Directory to run in (absolute, or relative to the working directory).'),\n timeout_ms: int('Kill the command after this many milliseconds.', 1),\n env: envSchema,\n },\n ['command'],\n ),\n capability: SandboxCapability.Process,\n write: true,\n policyKeys: ['shell', 'process'],\n }),\n tool({\n name: SandboxMcpTool.Spawn,\n title: 'Start background process',\n description:\n 'Start a long-running command (a server, a watcher) without waiting. Returns process_id; read its output with process_output.',\n inputSchema: objectSchema(\n {\n command: str('The bash command to start.'),\n cwd: str('Directory to run in.'),\n env: envSchema,\n },\n ['command'],\n ),\n capability: SandboxCapability.Process,\n write: true,\n policyKeys: ['shell', 'process'],\n }),\n tool({\n name: SandboxMcpTool.ProcessOutput,\n title: 'Read process output',\n description:\n 'Read output of a process started with spawn, from byte offsets (pass the next_offset values from the previous call). Optionally waits for new output.',\n inputSchema: objectSchema(\n {\n process_id: str('The id returned by spawn.'),\n stdout_offset: int('Byte offset into stdout.'),\n stderr_offset: int('Byte offset into stderr.'),\n max_bytes: int('Maximum bytes per stream to return (default 65536).', 1),\n wait_ms: int('Wait up to this long for new output or exit (max 30000).'),\n },\n ['process_id'],\n ),\n capability: SandboxCapability.Process,\n write: false,\n policyKeys: ['shell', 'process'],\n idempotent: false,\n }),\n tool({\n name: SandboxMcpTool.ProcessInput,\n title: 'Write process input',\n description: 'Write text to the stdin of a process started with spawn; set end to close stdin.',\n inputSchema: objectSchema(\n {\n process_id: str('The id returned by spawn.'),\n data: str('Text to write.'),\n end: { type: 'boolean' },\n },\n ['process_id'],\n ),\n capability: SandboxCapability.Process,\n write: true,\n policyKeys: ['shell', 'process'],\n destructive: false,\n }),\n tool({\n name: SandboxMcpTool.ProcessTerminate,\n title: 'Terminate process',\n description: 'Stop a process started with spawn.',\n inputSchema: objectSchema(\n {\n process_id: str('The id returned by spawn.'),\n signal: str('Signal name (default SIGTERM).'),\n },\n ['process_id'],\n ),\n capability: SandboxCapability.Process,\n write: true,\n policyKeys: ['shell', 'process'],\n }),\n tool({\n name: SandboxMcpTool.ReadFile,\n title: 'Read file',\n description:\n 'Read a text file. Use offset (1-based line) and limit (line count) for large files; large or binary files also come back as a resource link.',\n inputSchema: objectSchema(\n {\n path: str('File path (absolute, or relative to the working directory).'),\n offset: int('First line to read (1-based).', 1),\n limit: int('Number of lines to read.', 1),\n },\n ['path'],\n ),\n capability: SandboxCapability.Files,\n write: false,\n policyKeys: ['files.read', 'files'],\n }),\n tool({\n name: SandboxMcpTool.WriteFile,\n title: 'Write file',\n description: 'Create or overwrite a text file. Parent directories are created.',\n inputSchema: objectSchema({ path: str('File path.'), content: str('Full file content.') }, [\n 'path',\n 'content',\n ]),\n capability: SandboxCapability.Files,\n write: true,\n policyKeys: ['files.write', 'files'],\n }),\n tool({\n name: SandboxMcpTool.EditFile,\n title: 'Edit file',\n description:\n 'Replace an exact string in a file. old_string must occur exactly once unless replace_all is true; include surrounding lines to make it unique.',\n inputSchema: objectSchema(\n {\n path: str('File path.'),\n old_string: str('Exact text to replace.'),\n new_string: str('Replacement text.'),\n replace_all: { type: 'boolean', description: 'Replace every occurrence.' },\n },\n ['path', 'old_string', 'new_string'],\n ),\n capability: SandboxCapability.Files,\n write: true,\n policyKeys: ['files.write', 'files'],\n }),\n tool({\n name: SandboxMcpTool.ListDir,\n title: 'List directory',\n description: 'List the entries of a directory (name, type, size).',\n inputSchema: objectSchema(\n { path: str('Directory path (default: the working directory).') },\n [],\n ),\n capability: SandboxCapability.Files,\n write: false,\n policyKeys: ['files.read', 'files'],\n }),\n tool({\n name: SandboxMcpTool.Stat,\n title: 'Stat path',\n description: 'Type, size, modification time and mode of a path, or exists: false.',\n inputSchema: objectSchema({ path: str('Path to inspect.') }, ['path']),\n capability: SandboxCapability.Files,\n write: false,\n policyKeys: ['files.read', 'files'],\n }),\n tool({\n name: SandboxMcpTool.Grep,\n title: 'Search file contents',\n description:\n 'Search files under a path for a regular expression. Returns path:line:text matches. Skips .git and node_modules.',\n inputSchema: objectSchema(\n {\n pattern: str('JavaScript regular expression.'),\n path: str('File or directory to search (default: the working directory).'),\n glob: str('Only search files matching this glob (e.g. \"*.ts\").'),\n ignore_case: { type: 'boolean' },\n max_results: int('Maximum matches (default 200).', 1),\n },\n ['pattern'],\n ),\n capability: SandboxCapability.Files,\n write: false,\n policyKeys: ['files.read', 'files'],\n }),\n tool({\n name: SandboxMcpTool.Glob,\n title: 'Find files',\n description: 'Find files whose path (relative to path) matches a glob such as \"**/*.ts\".',\n inputSchema: objectSchema(\n {\n pattern: str('Glob pattern.'),\n path: str('Directory to search (default: the working directory).'),\n max_results: int('Maximum paths (default 500).', 1),\n },\n ['pattern'],\n ),\n capability: SandboxCapability.Files,\n write: false,\n policyKeys: ['files.read', 'files'],\n }),\n tool({\n name: SandboxMcpTool.GetHost,\n title: 'Get port URL',\n description:\n 'The URL (and any required headers) at which a port served inside the sandbox is reachable.',\n inputSchema: objectSchema(\n {\n port: int('Port number.', 1),\n protocol: {\n type: 'string',\n enum: Object.values(PortProtocol),\n description: 'Default http.',\n },\n },\n ['port'],\n ),\n capability: SandboxCapability.Ports,\n write: false,\n policyKeys: ['ports'],\n }),\n tool({\n name: SandboxMcpTool.Egress,\n title: 'Workspace egress',\n description:\n 'Route this Linux workspace\u2019s TCP and UDP traffic through your Final Devices. Configure an ordered list of owned Device IDs, query status, or explicitly disable routing. Losing all exits blocks traffic. IPv6 is blocked while enabled. Configuring or disabling requires approval by default: traffic uses the selected Device\u2019s network access, including private destinations; sandbox destination allowlists do not apply inside the tunnel. Disabling restores the saved direct-access policy. Remote hosted tools have their own network origin.',\n inputSchema: objectSchema(\n {\n action: { type: 'string', enum: Object.values(WorkspaceEgressAction) },\n deviceIds: { type: 'array', items: { type: 'string' }, minItems: 1, maxItems: 5 },\n dnsResolvers: { type: 'array', items: { type: 'string' }, minItems: 1, maxItems: 3 },\n },\n ['action'],\n ),\n capability: SandboxCapability.Egress,\n write: true,\n policyKeys: ['network.egress', 'lifecycle', SandboxCapability.NetworkPolicy, 'network'],\n }),\n];\n\nconst scopeOf = (options: SandboxMcpDescriptorOptionsIface): SandboxMcpScope =>\n options.scope ?? { kind: SandboxMcpScopeKind.All };\n\ninterface HostedServersIface {\n computerUse: boolean;\n localMcp: boolean;\n // Only this local server (a per-server address).\n localServer?: string;\n}\n\nconst grantOf = (options: SandboxMcpDescriptorOptionsIface): SandboxGrantIface =>\n options.grant ?? FULL_SANDBOX_GRANT;\n\n// Whether the grant allows a built-in tool, by the handle methods it uses\n// (grant.ts): file tools read or write files, process and port tools run\n// commands, egress also needs lifecycle (checked by the caller).\nconst grantAllowsSpec = (grant: SandboxGrantIface, spec: ToolSpecIface): boolean => {\n if (spec.capability === SandboxCapability.Files)\n return grantAllows(grant, Capability.Files, spec.write ? FilesAccess.Write : FilesAccess.Read);\n return grantAllows(grant, Capability.Shell, ShellAccess.Execute);\n};\n\n// Which hosted servers a projection serves, given its capabilities, scope and grant.\nconst hostedServersOf = (options: SandboxMcpDescriptorOptionsIface): HostedServersIface => {\n const grant = grantOf(options);\n const capabilities = new Set(options.capabilities);\n const scope = scopeOf(options);\n const computerUse =\n capabilities.has(SandboxCapability.ComputerUse) &&\n scope.kind !== SandboxMcpScopeKind.LocalServer &&\n grantAllows(grant, Capability.ComputerUse, ScreenAccess.Control);\n // Local servers run as processes on the machine.\n const localMcp =\n capabilities.has(SandboxCapability.LocalMcp) &&\n scope.kind !== SandboxMcpScopeKind.ComputerUse &&\n grantAllows(grant, Capability.Shell, ShellAccess.Execute);\n return scope.kind === SandboxMcpScopeKind.LocalServer\n ? { computerUse, localMcp, localServer: scope.server }\n : { computerUse, localMcp };\n};\n\nconst hostedSpec = (hosted: HostedToolIface): ToolSpecIface => {\n const annotations: ToolAnnotationsIface = { ...mutating, ...hosted.tool.annotations };\n return {\n descriptor: { ...hosted.tool, name: hosted.name, annotations },\n capability:\n hosted.server.kind === SandboxHostedServerKind.ComputerUse\n ? SandboxCapability.ComputerUse\n : SandboxCapability.LocalMcp,\n write: true,\n policyKeys: hosted.policyKeys,\n hosted,\n };\n};\n\nconst toolSpecs = (options: SandboxMcpDescriptorOptionsIface): ToolSpecIface[] => {\n const capabilities = new Set(options.capabilities);\n // Shell-like capabilities imply processes for the projection.\n const builtIn =\n scopeOf(options).kind === SandboxMcpScopeKind.All\n ? BUILT_IN_TOOLS.filter(\n (spec) =>\n capabilities.has(spec.capability) &&\n (spec.capability !== SandboxCapability.Egress || options.lifecycle === true),\n )\n : [];\n const grant = grantOf(options);\n const visible = builtIn.filter((spec) => grantAllowsSpec(grant, spec));\n const hosted = hostedTools({\n catalog: options.catalog ?? EMPTY_MCP_CATALOG,\n ...hostedServersOf(options),\n taken: new Set(builtIn.map((spec) => spec.descriptor.name)),\n reserveEgress: visible.some((spec) => spec.capability === SandboxCapability.Egress),\n });\n return [...visible, ...hosted.map(hostedSpec)];\n};\n\n// The projection's tools for a sandbox with these capabilities and the hosted\n// catalogs known so far, without a handle: what `tools/list` answers.\nexport const sandboxMcpToolDescriptors = (\n options: SandboxMcpDescriptorOptionsIface,\n): ToolDescriptorIface[] => toolSpecs(options).map((spec) => spec.descriptor);\n\n// The toolPolicy keys a projection tool is decided by, most specific first\n// (its name, then capability keys such as `shell` or `files.write`), as the\n// projection checks them; null for a tool these options do not list. Lets the\n// MCP host raise an `ask` approval before it calls the projection. Egress mutations\n// require resolveSandboxToolPolicy: a relevant Ask wins over a specific Allow.\nconst policyKeysFor = (spec: ToolSpecIface, name: string, input?: JsonObject): string[] => {\n return spec.hosted == null &&\n name === SandboxMcpTool.Egress &&\n input?.action === WorkspaceEgressAction.Status\n ? ['network.egress.status', name]\n : spec.hosted == null && name === SandboxMcpTool.Egress\n ? [\n input?.action === WorkspaceEgressAction.Disable\n ? 'network.egress.disable'\n : 'network.egress.configure',\n name,\n ...spec.policyKeys,\n ]\n : [name, ...spec.policyKeys];\n};\n\nexport const sandboxMcpToolPolicyKeys = (\n options: SandboxMcpDescriptorOptionsIface,\n name: string,\n input?: JsonObject,\n): string[] | null => {\n const spec = toolSpecs(options).find((candidate) => candidate.descriptor.name === name);\n if (spec == null) return null;\n return policyKeysFor(spec, name, input);\n};\n\nexport interface CreateSandboxMcpServerArgsIface extends SandboxMcpDescriptorOptionsIface {\n // Obtains (and on the first call starts) the sandbox. Called lazily.\n getSandbox: () => Promise<NetworkSandboxSessionIface>;\n // Keys are tool names or capability keys (`shell`, `files.write`, ...).\n toolPolicy?: Record<string, ToolApproval>;\n // Asked for tools whose policy is `ask`; no hook means deny.\n approve?: (toolName: string, args: JsonObject) => Promise<boolean>;\n shell?: SandboxShellMode;\n name?: string;\n version?: string;\n // Fetch for sandbox-hosted MCP servers reached over HTTP (tests, service bindings).\n fetch?: FetchLike;\n limits?: Partial<SandboxMcpLimitsIface>;\n onError?: (error: Error) => void;\n // The sandbox when it is already attached (no start): a `tools/list` that\n // finds a hosted catalog unknown reads it from the live sandbox before\n // answering. Without a live sandbox the list answers at once and the catalog\n // is learned in the background.\n peekSandbox?: () => NetworkSandboxSessionIface | null;\n // Called with the hosted catalogs whenever more of them become known.\n onCatalog?: (catalog: SandboxMcpCatalogIface) => void;\n // A background process started with `spawn` exited on its own and no client\n // has read its exit (`process_output`) within `exitReportGraceMs`: the host\n // reports it as a sandbox result. Never called for a process\n // ended with `process_terminate` or by `close`.\n onProcessExit?: (exit: SandboxProcessExitIface) => void;\n}\n\nexport interface SandboxProcessExitIface {\n processId: string;\n command: string;\n // Null when the sandbox lost the process without an exit code.\n exitCode: number | null;\n // The end of each stream, at most `exitOutputChars`.\n stdout: string;\n stderr: string;\n}\n\nexport interface SandboxMcpLimitsIface {\n // Characters of stdout/stderr returned per run (head and tail kept).\n maxOutputChars: number;\n // Files above this size are not inlined without a line range.\n maxInlineBytes: number;\n // Bytes of background-process output kept per stream.\n processBufferBytes: number;\n maxProcesses: number;\n // How long an exit may go unread before onProcessExit reports it.\n exitReportGraceMs: number;\n // Characters of each stream's tail an exit report carries.\n exitOutputChars: number;\n}\n\nconst DEFAULT_LIMITS: SandboxMcpLimitsIface = {\n maxOutputChars: 64_000,\n maxInlineBytes: 256 * 1024,\n processBufferBytes: 4 * 1024 * 1024,\n maxProcesses: 32,\n exitReportGraceMs: 2_000,\n exitOutputChars: 4_000,\n};\n\nexport interface SandboxMcpServerIface {\n readonly descriptors: readonly ToolDescriptorIface[];\n // Streamable HTTP MCP endpoint (2026-07-28 and stateless 2025 clients).\n fetch(request: Request): Promise<Response>;\n // Direct entry points for hosts that relay MCP themselves (MCP over ACP).\n listTools(): ToolDescriptorIface[];\n // `listTools` after what a `tools/list` does first (reading hosted catalogs\n // from a live sandbox, or starting their discovery).\n refreshTools(): Promise<ToolDescriptorIface[]>;\n // Catalogs learned elsewhere (another projection of the same sandbox).\n updateCatalog(catalog: SandboxMcpCatalogIface): void;\n updateCapabilities(capabilities: readonly SandboxCapability[]): void;\n callTool(name: string, args: JsonObject, signal?: AbortSignal): Promise<CallToolResult>;\n close(): Promise<void>;\n}\n\ninterface OutputBufferIface {\n chunks: Uint8Array[];\n // Absolute offset of chunks[0].\n base: number;\n size: number;\n}\n\ninterface BackgroundProcessIface {\n process: SandboxProcessIface;\n stdout: OutputBufferIface;\n stderr: OutputBufferIface;\n exitCode: number | null;\n exited: boolean;\n changed: Set<() => void>;\n command: string;\n // A client read the exit, or ended the process itself.\n exitSeen: boolean;\n}\n\nconst textResult = (text: string): CallToolResult => ({ content: [{ type: 'text', text }] });\nconst jsonResult = (value: JsonValue): CallToolResult => textResult(JSON.stringify(value, null, 2));\nconst errorResult = (message: string): CallToolResult => ({\n content: [{ type: 'text', text: message }],\n isError: true,\n});\n\nconst truncateMiddle = (text: string, max: number): string => {\n if (text.length <= max) return text;\n const half = Math.floor(max / 2);\n return `${text.slice(0, half)}\\n[... ${text.length - max} characters omitted ...]\\n${text.slice(text.length - half)}`;\n};\n\nconst listRequestSchema = z.object({ method: z.literal('tools/list') });\n\nconst runArgsSchema = z.object({\n command: z.string().min(1),\n cwd: z.string().optional(),\n timeout_ms: z.number().int().positive().optional(),\n env: z.record(z.string(), z.string()).optional(),\n});\nconst spawnArgsSchema = runArgsSchema.omit({ timeout_ms: true });\nconst processOutputArgsSchema = z.object({\n process_id: z.string().min(1),\n stdout_offset: z.number().int().nonnegative().optional(),\n stderr_offset: z.number().int().nonnegative().optional(),\n max_bytes: z.number().int().positive().optional(),\n wait_ms: z.number().int().nonnegative().optional(),\n});\nconst processInputArgsSchema = z.object({\n process_id: z.string().min(1),\n data: z.string().optional(),\n end: z.boolean().optional(),\n});\nconst processTerminateArgsSchema = z.object({\n process_id: z.string().min(1),\n signal: z.string().optional(),\n});\nconst readFileArgsSchema = z.object({\n path: z.string().min(1),\n offset: z.number().int().positive().optional(),\n limit: z.number().int().positive().optional(),\n});\nconst writeFileArgsSchema = z.object({ path: z.string().min(1), content: z.string() });\nconst editFileArgsSchema = z.object({\n path: z.string().min(1),\n old_string: z.string().min(1),\n new_string: z.string(),\n replace_all: z.boolean().optional(),\n});\nconst pathArgsSchema = z.object({ path: z.string().min(1) });\nconst listDirArgsSchema = z.object({ path: z.string().optional() });\nconst grepArgsSchema = z.object({\n pattern: z.string().min(1),\n path: z.string().optional(),\n glob: z.string().optional(),\n ignore_case: z.boolean().optional(),\n max_results: z.number().int().positive().optional(),\n});\nconst globArgsSchema = z.object({\n pattern: z.string().min(1),\n path: z.string().optional(),\n max_results: z.number().int().positive().optional(),\n});\nconst getHostArgsSchema = z.object({\n port: z.number().int().min(1).max(65535),\n protocol: z.nativeEnum(PortProtocol).optional(),\n});\n\nconst appendOutput = (buffer: OutputBufferIface, chunk: Uint8Array, cap: number) => {\n buffer.chunks.push(chunk);\n buffer.size += chunk.byteLength;\n while (buffer.size > cap && buffer.chunks.length > 1) {\n const dropped = buffer.chunks.shift();\n if (dropped == null) break;\n buffer.size -= dropped.byteLength;\n buffer.base += dropped.byteLength;\n }\n};\n\nconst readOutput = (buffer: OutputBufferIface, offset: number, maxBytes: number) => {\n const start = Math.max(offset, buffer.base);\n const end = buffer.base + buffer.size;\n const out = new Uint8Array(Math.max(0, Math.min(end - start, maxBytes)));\n let position = buffer.base;\n let written = 0;\n for (const chunk of buffer.chunks) {\n const chunkEnd = position + chunk.byteLength;\n if (chunkEnd > start && written < out.byteLength) {\n const from = Math.max(0, start - position);\n const take = Math.min(chunk.byteLength - from, out.byteLength - written);\n out.set(chunk.subarray(from, from + take), written);\n written += take;\n }\n position = chunkEnd;\n }\n return {\n data: utf8Decode(out),\n offset: start,\n next_offset: start + written,\n skipped: start - offset,\n available: end,\n };\n};\n\nexport const createSandboxMcpServer = (\n args: CreateSandboxMcpServerArgsIface,\n): SandboxMcpServerIface => {\n const limits: SandboxMcpLimitsIface = { ...DEFAULT_LIMITS, ...args.limits };\n let catalog: SandboxMcpCatalogIface = args.catalog ?? EMPTY_MCP_CATALOG;\n let capabilities = [...args.capabilities];\n let specs = toolSpecs({ ...args, capabilities, catalog });\n let byName = new Map(specs.map((spec) => [spec.descriptor.name, spec]));\n const processes = new Map<string, BackgroundProcessIface>();\n let served = hostedServersOf({ ...args, capabilities });\n let handler: McpHttpHandler | null = null;\n let learning: Promise<void> | null = null;\n let discovering = false;\n // Hosted servers whose known catalog was checked against the live server.\n const checked = new Set<string>();\n\n const asError = (cause: unknown): Error =>\n cause instanceof Error ? cause : new Error(String(cause));\n const reportError = (error: Error) => args.onError?.(error);\n\n // Merges catalogs as they become known; a change relists the tools and tells\n // `subscriptions/listen` clients (`notifications/tools/list_changed`).\n const mergeCatalog = (next: Partial<SandboxMcpCatalogIface>): boolean => {\n const merged: SandboxMcpCatalogIface = {\n computerUse: next.computerUse ?? catalog.computerUse,\n localMcp: next.localMcp ?? catalog.localMcp,\n localMcpIncomplete:\n next.localMcp == null ? catalog.localMcpIncomplete : (next.localMcpIncomplete ?? false),\n };\n if (\n JSON.stringify({ ...merged, localMcpIncomplete: merged.localMcpIncomplete ?? false }) ===\n JSON.stringify({ ...catalog, localMcpIncomplete: catalog.localMcpIncomplete ?? false })\n )\n return false;\n catalog = merged;\n specs = toolSpecs({ ...args, capabilities, catalog });\n byName = new Map(specs.map((spec) => [spec.descriptor.name, spec]));\n handler?.notify.toolsChanged();\n return true;\n };\n\n const unknownCatalogs = () => ({\n computerUse: served.computerUse && catalog.computerUse == null,\n localMcp: served.localMcp && (catalog.localMcp == null || catalog.localMcpIncomplete === true),\n });\n\n const connections = createHostedMcpConnections({\n getSandbox: () => sandboxFor(),\n fetch: args.fetch,\n name: args.name,\n version: args.version,\n catalog: () => catalog,\n });\n\n // Reads the unknown hosted catalogs from an attached sandbox, once at a time.\n let nextAutomaticLearningAt: number | null = null;\n const learnFrom = (sandbox: NetworkSandboxSessionIface, force = false): Promise<void> => {\n if (!force && nextAutomaticLearningAt != null && performance.now() < nextAutomaticLearningAt)\n return learning ?? Promise.resolve();\n const needed = unknownCatalogs();\n if (!needed.computerUse && !needed.localMcp) return Promise.resolve();\n learning ??= (async () => {\n try {\n const learned = await connections.learn(sandbox, needed);\n nextAutomaticLearningAt =\n learned.localMcpIncomplete === true || (needed.computerUse && learned.computerUse == null)\n ? performance.now() + 1000\n : null;\n if (!closed && mergeCatalog(learned)) args.onCatalog?.(catalog);\n } catch (cause) {\n nextAutomaticLearningAt = performance.now() + 1000;\n throw cause;\n } finally {\n learning = null;\n }\n })();\n return learning;\n };\n\n // Every attach through this projection also learns what it does not know yet.\n const sandboxFor = async (): Promise<NetworkSandboxSessionIface> => {\n const sandbox = await args.getSandbox();\n learnFrom(sandbox).catch((cause: unknown) => reportError(asError(cause)));\n return sandbox;\n };\n\n // Before answering `tools/list`: a live sandbox is asked for the catalogs it\n // can give now; without one, the first list attaches in the background (the\n // only way to learn a catalog the provider cannot give) and the tools follow\n // with `notifications/tools/list_changed`.\n const beforeList = async (waitForDiscovery = false): Promise<void> => {\n const needed = unknownCatalogs();\n if (!needed.computerUse && !needed.localMcp) return;\n const live = args.peekSandbox?.() ?? null;\n if (live != null) {\n // The combined projection stays responsive; dedicated endpoints need\n // their live catalog before a harness can use them.\n const discovered = learnFrom(live, waitForDiscovery).catch((cause: unknown) =>\n reportError(asError(cause)),\n );\n if (waitForDiscovery || scopeOf(args).kind !== SandboxMcpScopeKind.All) await discovered;\n return;\n }\n if (discovering) return;\n discovering = true;\n sandboxFor()\n .then((sandbox) => learnFrom(sandbox))\n .catch((cause: unknown) => {\n discovering = false;\n reportError(asError(cause));\n });\n };\n\n // A known catalog may be stale (the server changed since it was recorded).\n // It is checked against the live server the first time a call opens that\n // server, never by opening servers the session does not use; a change is\n // announced like a new catalog.\n const checkCatalog = (server: SandboxHostedServer): void => {\n const key = server.kind === SandboxHostedServerKind.Local ? `local:${server.name}` : 'cu';\n if (checked.has(key)) return;\n checked.add(key);\n void (async () => {\n const tools = await connections.tools(server);\n const next: Partial<SandboxMcpCatalogIface> =\n server.kind === SandboxHostedServerKind.ComputerUse\n ? { computerUse: tools }\n : catalog.localMcp == null\n ? {}\n : {\n localMcp: { ...catalog.localMcp, [server.name]: tools },\n localMcpIncomplete: catalog.localMcpIncomplete,\n };\n if (!closed && mergeCatalog(next)) args.onCatalog?.(catalog);\n })().catch((cause: unknown) => {\n checked.delete(key);\n reportError(asError(cause));\n });\n };\n\n const shell = createSandboxShell({ getSandbox: sandboxFor, mode: args.shell });\n const isAllowed = async (\n spec: ToolSpecIface,\n name: string,\n input: JsonObject,\n ): Promise<boolean> => {\n const keys = policyKeysFor(spec, name, input);\n const decision = resolveSandboxToolPolicy(args.toolPolicy ?? {}, keys);\n return (\n decision?.decision !== ToolApproval.Ask ||\n (args.approve != null && (await args.approve(name, input)))\n );\n };\n\n const backgroundProcess = (id: string): BackgroundProcessIface => {\n const entry = processes.get(id);\n if (entry == null) throw new Error(`no process ${id}; start one with spawn`);\n return entry;\n };\n\n const notifyProcess = (entry: BackgroundProcessIface) => {\n for (const listener of [...entry.changed]) listener();\n };\n\n // Up to four UTF-8 bytes per character are read back from the end.\n const tailOf = (buffer: OutputBufferIface): string => {\n const end = buffer.base + buffer.size;\n const text = readOutput(\n buffer,\n end - limits.exitOutputChars * 4,\n limits.exitOutputChars * 4,\n ).data;\n return text.length <= limits.exitOutputChars\n ? text\n : text.slice(text.length - limits.exitOutputChars);\n };\n\n let closed = false;\n const reportExit = (id: string, entry: BackgroundProcessIface) => {\n const report = args.onProcessExit;\n if (report == null) return;\n setTimeout(() => {\n if (closed || entry.exitSeen) return;\n entry.exitSeen = true;\n report({\n processId: id,\n command: entry.command,\n exitCode: entry.exitCode,\n stdout: tailOf(entry.stdout),\n stderr: tailOf(entry.stderr),\n });\n }, limits.exitReportGraceMs);\n };\n\n const pumpOutput = async (\n entry: BackgroundProcessIface,\n stream: ReadableStream<Uint8Array>,\n buffer: OutputBufferIface,\n ) => {\n const reader = stream.getReader();\n try {\n for (;;) {\n const { done, value } = await reader.read();\n if (done) break;\n appendOutput(buffer, value, limits.processBufferBytes);\n notifyProcess(entry);\n }\n } catch {\n // Reported through `exited`.\n }\n };\n\n const run = async (\n name: string,\n rawArgs: JsonObject,\n signal: AbortSignal | undefined,\n ): Promise<CallToolResult> => {\n switch (name) {\n case SandboxMcpTool.Run: {\n const input = runArgsSchema.parse(rawArgs);\n const result = await shell.run({\n command: input.command,\n cwd: input.cwd,\n env: input.env,\n timeoutMs: input.timeout_ms ?? 120_000,\n signal,\n });\n return jsonResult({\n exit_code: result.exitCode,\n stdout: truncateMiddle(result.stdout, limits.maxOutputChars),\n stderr: truncateMiddle(result.stderr, limits.maxOutputChars),\n });\n }\n case SandboxMcpTool.Spawn: {\n const input = spawnArgsSchema.parse(rawArgs);\n if (processes.size >= limits.maxProcesses) {\n for (const [id, entry] of processes)\n if (entry.exited && entry.exitSeen) processes.delete(id);\n }\n if (processes.size >= limits.maxProcesses) {\n return errorResult(\n `too many background processes (${limits.maxProcesses}); terminate one first`,\n );\n }\n const sandbox = await sandboxFor();\n const child = await sandbox.spawn({\n command: input.command,\n shell: true,\n cwd: input.cwd,\n env: input.env,\n });\n const id = `p${crypto.randomUUID().slice(0, 8)}`;\n const entry: BackgroundProcessIface = {\n process: child,\n stdout: { chunks: [], base: 0, size: 0 },\n stderr: { chunks: [], base: 0, size: 0 },\n exitCode: null,\n exited: false,\n changed: new Set(),\n command: input.command,\n exitSeen: false,\n };\n processes.set(id, entry);\n void pumpOutput(entry, child.stdout, entry.stdout);\n void pumpOutput(entry, child.stderr, entry.stderr);\n child.wait().then(\n (result) => {\n entry.exited = true;\n entry.exitCode = result.exitCode;\n notifyProcess(entry);\n reportExit(id, entry);\n },\n () => {\n entry.exited = true;\n notifyProcess(entry);\n reportExit(id, entry);\n },\n );\n return jsonResult({ process_id: id, pid: child.pid });\n }\n case SandboxMcpTool.ProcessOutput: {\n const input = processOutputArgsSchema.parse(rawArgs);\n const entry = backgroundProcess(input.process_id);\n const stdoutOffset = input.stdout_offset ?? 0;\n const stderrOffset = input.stderr_offset ?? 0;\n const hasNews = () =>\n entry.exited ||\n entry.stdout.base + entry.stdout.size > stdoutOffset ||\n entry.stderr.base + entry.stderr.size > stderrOffset;\n const waitMs = Math.min(input.wait_ms ?? 0, 30_000);\n if (waitMs > 0 && !hasNews()) {\n await new Promise<void>((resolve) => {\n const done = () => {\n clearTimeout(timer);\n entry.changed.delete(listener);\n signal?.removeEventListener('abort', done);\n resolve();\n };\n const listener = () => {\n if (hasNews()) done();\n };\n const timer = setTimeout(done, waitMs);\n entry.changed.add(listener);\n signal?.addEventListener('abort', done, { once: true });\n });\n }\n const maxBytes = input.max_bytes ?? 65_536;\n if (entry.exited) entry.exitSeen = true;\n return jsonResult({\n stdout: readOutput(entry.stdout, stdoutOffset, maxBytes),\n stderr: readOutput(entry.stderr, stderrOffset, maxBytes),\n running: !entry.exited,\n exit_code: entry.exitCode,\n });\n }\n case SandboxMcpTool.ProcessInput: {\n const input = processInputArgsSchema.parse(rawArgs);\n const entry = backgroundProcess(input.process_id);\n await entry.process.write(input.data ?? '', { end: input.end === true });\n return jsonResult({ written: input.data?.length ?? 0, stdin_closed: input.end === true });\n }\n case SandboxMcpTool.ProcessTerminate: {\n const input = processTerminateArgsSchema.parse(rawArgs);\n const entry = backgroundProcess(input.process_id);\n entry.exitSeen = true;\n await entry.process.kill(input.signal);\n const result = await entry.process.wait().catch(() => ({ exitCode: entry.exitCode ?? -1 }));\n processes.delete(input.process_id);\n return jsonResult({ process_id: input.process_id, exit_code: result.exitCode });\n }\n case SandboxMcpTool.ReadFile: {\n const input = readFileArgsSchema.parse(rawArgs);\n const sandbox = await sandboxFor();\n const path = resolvePosixPath(sandbox.defaultWorkingDirectory, input.path);\n const stat = await sandbox.stat({ path, abortSignal: signal });\n if (stat == null) return errorResult(`${path}: no such file`);\n if (stat.type === SandboxEntryType.Directory)\n return errorResult(`${path} is a directory; use list_dir`);\n const link = {\n type: 'resource_link' as const,\n uri: `${SANDBOX_FILE_RESOURCE_SCHEME}://${encodeURI(path)}`,\n name: path.split('/').pop() ?? path,\n size: stat.size,\n };\n const ranged = input.offset != null || input.limit != null;\n const startLine = input.offset ?? 1;\n const endLine = input.limit == null ? undefined : startLine + input.limit - 1;\n if (!ranged && stat.size > limits.maxInlineBytes) {\n const head =\n (await sandbox.readTextFile({\n path,\n startLine: 1,\n endLine: 200,\n abortSignal: signal,\n })) ?? '';\n return {\n content: [\n {\n type: 'text',\n text: `${path} is ${stat.size} bytes, too large to return whole. First 200 lines below; read more with offset and limit, or fetch the resource.\\n\\n${truncateMiddle(head, limits.maxOutputChars)}`,\n },\n link,\n ],\n };\n }\n const text = await sandbox.readTextFile({\n path,\n startLine: ranged ? startLine : undefined,\n endLine,\n abortSignal: signal,\n });\n if (text == null) return errorResult(`${path}: no such file`);\n if (text.includes('\\u0000')) {\n return {\n content: [\n { type: 'text', text: `${path} is a binary file (${stat.size} bytes).` },\n link,\n ],\n };\n }\n return textResult(\n truncateMiddle(text, Math.max(limits.maxOutputChars, limits.maxInlineBytes)),\n );\n }\n case SandboxMcpTool.WriteFile: {\n const input = writeFileArgsSchema.parse(rawArgs);\n const sandbox = await sandboxFor();\n const path = resolvePosixPath(sandbox.defaultWorkingDirectory, input.path);\n await sandbox.writeTextFile({ path, content: input.content, abortSignal: signal });\n return textResult(\n `Wrote ${new TextEncoder().encode(input.content).byteLength} bytes to ${path}`,\n );\n }\n case SandboxMcpTool.EditFile: {\n const input = editFileArgsSchema.parse(rawArgs);\n const sandbox = await sandboxFor();\n const path = resolvePosixPath(sandbox.defaultWorkingDirectory, input.path);\n const text = await sandbox.readTextFile({ path, abortSignal: signal });\n if (text == null) return errorResult(`${path}: no such file`);\n const count = text.split(input.old_string).length - 1;\n if (count === 0) return errorResult(`old_string was not found in ${path}`);\n if (count > 1 && input.replace_all !== true) {\n return errorResult(\n `old_string occurs ${count} times in ${path}; add surrounding context or set replace_all`,\n );\n }\n const next =\n input.replace_all === true\n ? text.split(input.old_string).join(input.new_string)\n : text.replace(input.old_string, () => input.new_string);\n await sandbox.writeTextFile({ path, content: next, abortSignal: signal });\n return textResult(\n `Replaced ${input.replace_all === true ? count : 1} occurrence(s) in ${path}`,\n );\n }\n case SandboxMcpTool.ListDir: {\n const input = listDirArgsSchema.parse(rawArgs);\n const sandbox = await sandboxFor();\n const path = resolvePosixPath(sandbox.defaultWorkingDirectory, input.path ?? '.');\n const entries = await sandbox.readDir({ path, abortSignal: signal });\n return jsonResult({\n path,\n entries: entries.map((entry) => ({\n name: entry.name,\n type: entry.type,\n size: entry.size,\n })),\n });\n }\n case SandboxMcpTool.Stat: {\n const input = pathArgsSchema.parse(rawArgs);\n const sandbox = await sandboxFor();\n const path = resolvePosixPath(sandbox.defaultWorkingDirectory, input.path);\n const stat = await sandbox.stat({ path, abortSignal: signal });\n return jsonResult(stat == null ? { path, exists: false } : { exists: true, ...stat });\n }\n case SandboxMcpTool.Grep: {\n const input = grepArgsSchema.parse(rawArgs);\n const sandbox = await sandboxFor();\n const result = await grepFiles({\n sandbox,\n cwd: sandbox.defaultWorkingDirectory,\n pattern: input.pattern,\n path: input.path,\n glob: input.glob,\n ignoreCase: input.ignore_case,\n maxResults: input.max_results ?? 200,\n signal,\n });\n const lines = result.matches.map((match) => `${match.path}:${match.line}:${match.text}`);\n if (result.truncated) lines.push('[results truncated]');\n return textResult(lines.length === 0 ? 'No matches.' : lines.join('\\n'));\n }\n case SandboxMcpTool.Glob: {\n const input = globArgsSchema.parse(rawArgs);\n const sandbox = await sandboxFor();\n const result = await globFiles({\n sandbox,\n cwd: sandbox.defaultWorkingDirectory,\n pattern: input.pattern,\n path: input.path,\n maxResults: input.max_results ?? 500,\n signal,\n });\n const lines = [...result.paths];\n if (result.truncated) lines.push('[results truncated]');\n return textResult(lines.length === 0 ? 'No files matched.' : lines.join('\\n'));\n }\n case SandboxMcpTool.GetHost: {\n const input = getHostArgsSchema.parse(rawArgs);\n const sandbox = await sandboxFor();\n const endpoint = await sandbox.getPortEndpoint({\n port: input.port,\n protocol: input.protocol,\n });\n return jsonResult({ url: endpoint.url, headers: endpoint.headers ?? {} });\n }\n case SandboxMcpTool.Egress: {\n const sandbox = await sandboxFor();\n if (sandbox.egress == null)\n return errorResult('This sandbox provider does not support Device egress');\n return jsonResult(\n workspaceEgressStatusSchema.parse(\n await sandbox.egress(workspaceEgressRequestSchema.parse(rawArgs)),\n ),\n );\n }\n default:\n return errorResult(`unknown tool ${name}`);\n }\n };\n\n const callTool = async (\n name: string,\n rawArgs: JsonObject,\n signal?: AbortSignal,\n ): Promise<CallToolResult> => {\n const spec = byName.get(name);\n if (spec == null) return errorResult(`unknown tool ${name}`);\n if (spec.hosted == null && name === SandboxMcpTool.Egress) {\n const parsed = workspaceEgressRequestSchema.safeParse(rawArgs);\n if (!parsed.success)\n return errorResult(`invalid arguments: ${z.prettifyError(parsed.error)}`);\n }\n if (!(await isAllowed(spec, name, rawArgs))) return errorResult(`${name} was not approved`);\n try {\n signal?.throwIfAborted();\n if (spec.hosted != null) {\n try {\n return await connections.call(spec.hosted.server, spec.hosted.tool.name, rawArgs, {\n signal,\n });\n } finally {\n // Also after a failure: a stale tool name is how a changed catalog shows.\n checkCatalog(spec.hosted.server);\n }\n }\n return await run(name, rawArgs, signal);\n } catch (error) {\n if (error instanceof z.ZodError)\n return errorResult(`invalid arguments: ${z.prettifyError(error)}`);\n return errorResult(error instanceof Error ? error.message : String(error));\n }\n };\n\n const readResource = async (uri: URL): Promise<ReadResourceResult> => {\n const path = decodeURI(uri.pathname);\n const spec = byName.get(SandboxMcpTool.ReadFile);\n if (spec == null || !(await isAllowed(spec, SandboxMcpTool.ReadFile, { path })))\n throw new Error(`${path}: file read was not approved`);\n const sandbox = await sandboxFor();\n const bytes = await sandbox.readBinaryFile({ path });\n if (bytes == null) throw new Error(`${path}: no such file`);\n const text = new TextDecoder('utf-8', { fatal: false }).decode(bytes);\n if (!text.includes('\\u0000') && !text.includes('\uFFFD')) {\n return { contents: [{ uri: uri.href, mimeType: 'text/plain', text }] };\n }\n return {\n contents: [\n { uri: uri.href, mimeType: 'application/octet-stream', blob: bytesToBase64(bytes) },\n ],\n };\n };\n\n const buildServer = (): McpServer => {\n const server = new McpServer(\n { name: args.name ?? 'final-sandbox', version: args.version ?? '1' },\n // The hosted servers' tools can follow the first list (see beforeList).\n {\n capabilities: {\n tools: { listChanged: true },\n resources: {},\n },\n },\n );\n for (const spec of specs) {\n server.registerTool(\n spec.descriptor.name,\n {\n title: spec.descriptor.annotations?.title,\n description: spec.descriptor.description,\n inputSchema: fromJsonSchema<JsonObject>(spec.descriptor.inputSchema),\n annotations: spec.descriptor.annotations,\n },\n (input, ctx) => callTool(spec.descriptor.name, input, ctx.mcpReq.signal),\n );\n }\n if (capabilities.includes(SandboxCapability.Files)) {\n server.registerResource(\n 'sandbox-file',\n new ResourceTemplate(`${SANDBOX_FILE_RESOURCE_SCHEME}://{+path}`, { list: undefined }),\n { description: 'A file in the sandbox, by absolute path.' },\n (uri) => readResource(uri),\n );\n }\n return server;\n };\n\n const isListRequest = async (request: Request): Promise<boolean> => {\n if (request.method !== 'POST') return false;\n try {\n const body: unknown = await request.clone().json();\n return (Array.isArray(body) ? body : [body]).some(\n (message) => listRequestSchema.safeParse(message).success,\n );\n } catch {\n return false;\n }\n };\n\n const getHandler = () => {\n handler ??= createMcpHandler(() => buildServer(), { onerror: args.onError });\n return handler;\n };\n\n return {\n get descriptors() {\n return specs.map((spec) => spec.descriptor);\n },\n fetch: async (request) => {\n if (await isListRequest(request)) await beforeList();\n return getHandler().fetch(request);\n },\n listTools: () => specs.map((spec) => spec.descriptor),\n refreshTools: async () => {\n await beforeList(true);\n return specs.map((spec) => spec.descriptor);\n },\n updateCatalog: (next) => {\n mergeCatalog(next);\n },\n updateCapabilities: (next) => {\n if (closed || JSON.stringify(next) === JSON.stringify(capabilities)) return;\n capabilities = [...next];\n served = hostedServersOf({ ...args, capabilities });\n specs = toolSpecs({ ...args, capabilities, catalog });\n byName = new Map(specs.map((spec) => [spec.descriptor.name, spec]));\n handler?.notify.toolsChanged();\n },\n callTool,\n close: async () => {\n closed = true;\n await shell.close();\n for (const entry of processes.values()) await entry.process.kill().catch(() => undefined);\n processes.clear();\n await connections.close();\n await handler?.close();\n },\n };\n};\n"],
|
|
5
|
-
"mappings": "AAAA,SAAS,gCAAgC;AAEzC;AAAA,EAC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IAC/B;AAAA,IACA;AAAA,IACA,OAAO,YAAY;AACjB,eAAS;AACT,YAAM,MAAM,MAAM;AAClB,iBAAW,SAAS,UAAU,OAAO,EAAG,OAAM,MAAM,QAAQ,KAAK,EAAE,MAAM,MAAM,MAAS;AACxF,gBAAU,MAAM;AAChB,YAAM,YAAY,MAAM;AACxB,YAAM,SAAS,MAAM;AAAA,IACvB;AAAA,EACF;AACF;",
|
|
4
|
+
"sourcesContent": ["import { resolveSandboxToolPolicy } from \"./tool-policy.mjs\";\nimport type { FetchLike } from '@modelcontextprotocol/client';\nimport {\n createMcpHandler,\n fromJsonSchema,\n McpServer,\n ResourceTemplate,\n type CallToolResult,\n type McpHttpHandler,\n type ReadResourceResult,\n} from '@modelcontextprotocol/server';\nimport * as z from 'zod';\nimport type { JsonObject, JsonSchema, JsonValue } from \"../json.mjs\";\nimport { Capability, FilesAccess, ScreenAccess, ShellAccess } from '@finalai/agent-protocol';\nimport { ToolApprovalMode as ToolApproval } from \"../document.mjs\";\nimport type { ToolDescriptorIface } from \"../tool-descriptor.mjs\";\nimport { FULL_SANDBOX_GRANT, grantAllows, type SandboxGrantIface } from \"./grant.mjs\";\nimport { bytesToBase64, utf8Decode } from \"./bytes.mjs\";\nimport {\n createHostedMcpConnections,\n EMPTY_MCP_CATALOG,\n hostedTools,\n SandboxHostedServerKind,\n type HostedToolIface,\n type SandboxHostedServer,\n} from \"./mcp-hosted.mjs\";\nimport { globFiles, grepFiles } from \"./mcp-search.mjs\";\nimport { createSandboxShell, SandboxShellMode } from \"./mcp-shell.mjs\";\nimport { resolvePosixPath } from \"./paths.mjs\";\nimport {\n PortProtocol,\n SandboxCapability,\n SandboxEntryType,\n type NetworkSandboxSessionIface,\n type SandboxMcpCatalogIface,\n type SandboxProcessIface,\n} from \"./types.mjs\";\n\nexport {\n COMPUTER_USE_SERVER_NAME,\n SandboxHostedServerKind,\n withHostedMcpEndpoints,\n type SandboxHostedServer,\n} from \"./mcp-hosted.mjs\";\nexport { SandboxShellMode } from \"./mcp-shell.mjs\";\n\n// The standard sandbox MCP projection, generated from a\n// handle for harnesses that do not speak the handle RPC. `tools/list` is\n// answered from static descriptors, so enumerating tools never starts a\n// sandbox; the handle is obtained on the first tool call.\n//\n// The sandbox's hosted MCP servers (computer use, local MCP) are projected as\n// their own tools. Their catalogs come from the provider when it knows them\n// before anything starts (`catalog`); otherwise the projection lists what it\n// knows, learns the rest from the servers once the sandbox is attached, and\n// announces it with `notifications/tools/list_changed`.\n\nexport enum SandboxMcpTool {\n Run = 'run',\n Spawn = 'spawn',\n ProcessOutput = 'process_output',\n ProcessInput = 'process_input',\n ProcessTerminate = 'process_terminate',\n ReadFile = 'read_file',\n WriteFile = 'write_file',\n EditFile = 'edit_file',\n ListDir = 'list_dir',\n Stat = 'stat',\n Grep = 'grep',\n Glob = 'glob',\n GetHost = 'get_host',\n}\n\n// What one projection serves: everything, only the hosted servers (for\n// harnesses whose native tools use the handle RPC), the computer-use server,\n// or one local MCP server (the per-server addresses the handle RPC hands out).\nexport enum SandboxMcpScopeKind {\n All = 'all',\n Hosted = 'hosted',\n ComputerUse = 'computerUse',\n LocalServer = 'localServer',\n}\n\nexport type SandboxMcpScope =\n | { kind: SandboxMcpScopeKind.All | SandboxMcpScopeKind.Hosted | SandboxMcpScopeKind.ComputerUse }\n | { kind: SandboxMcpScopeKind.LocalServer; server: string };\n\nexport const SANDBOX_FILE_RESOURCE_SCHEME = 'sandbox-file';\n\nexport interface SandboxMcpDescriptorOptionsIface {\n capabilities: readonly SandboxCapability[];\n // What the projection's caller may do; tools it does not allow are not listed. Default: everything.\n grant?: SandboxGrantIface;\n // The hosted servers' tools known so far (the provider's `mcpCatalog`, or\n // learned on an earlier attach). Default: none known.\n catalog?: SandboxMcpCatalogIface;\n scope?: SandboxMcpScope;\n}\n\ninterface ToolSpecIface {\n descriptor: ToolDescriptorIface;\n capability: SandboxCapability;\n write: boolean;\n // toolPolicy keys after the tool name, most specific first.\n policyKeys: string[];\n // A tool of a hosted MCP server, proxied to it.\n hosted?: HostedToolIface;\n}\n\nconst objectSchema = (properties: JsonObject, required: string[]): JsonSchema => ({\n type: 'object',\n properties,\n required,\n additionalProperties: false,\n});\n\nconst str = (description: string): JsonObject => ({ type: 'string', description });\nconst int = (description: string, minimum = 0): JsonObject => ({\n type: 'integer',\n minimum,\n description,\n});\nconst envSchema: JsonObject = {\n type: 'object',\n additionalProperties: { type: 'string' },\n description: 'Extra environment variables for this command only.',\n};\n\ntype ToolAnnotationsIface = NonNullable<ToolDescriptorIface['annotations']>;\n\nconst readOnly = {\n readOnlyHint: true,\n destructiveHint: false,\n idempotentHint: true,\n openWorldHint: false,\n};\nconst mutating = {\n readOnlyHint: false,\n destructiveHint: true,\n idempotentHint: false,\n openWorldHint: false,\n};\n\nconst tool = (args: {\n name: string;\n title: string;\n description: string;\n inputSchema: JsonSchema;\n capability: SandboxCapability;\n write: boolean;\n policyKeys: string[];\n idempotent?: boolean;\n destructive?: boolean;\n}): ToolSpecIface => {\n const annotations: ToolAnnotationsIface = {\n title: args.title,\n ...(args.write ? mutating : readOnly),\n };\n if (args.destructive != null) annotations.destructiveHint = args.destructive;\n if (args.idempotent != null) annotations.idempotentHint = args.idempotent;\n return {\n descriptor: {\n name: args.name,\n description: args.description,\n inputSchema: args.inputSchema,\n annotations,\n },\n capability: args.capability,\n write: args.write,\n policyKeys: args.policyKeys,\n };\n};\n\nconst BUILT_IN_TOOLS: readonly ToolSpecIface[] = [\n tool({\n name: SandboxMcpTool.Run,\n title: 'Run command',\n description:\n 'Run a shell command in the sandbox and wait for it. The shell persists between calls (cd and exported variables carry over). Returns exit_code, stdout and stderr.',\n inputSchema: objectSchema(\n {\n command: str('The bash command to run.'),\n cwd: str('Directory to run in (absolute, or relative to the working directory).'),\n timeout_ms: int('Kill the command after this many milliseconds.', 1),\n env: envSchema,\n },\n ['command'],\n ),\n capability: SandboxCapability.Process,\n write: true,\n policyKeys: ['shell', 'process'],\n }),\n tool({\n name: SandboxMcpTool.Spawn,\n title: 'Start background process',\n description:\n 'Start a long-running command (a server, a watcher) without waiting. Returns process_id; read its output with process_output.',\n inputSchema: objectSchema(\n {\n command: str('The bash command to start.'),\n cwd: str('Directory to run in.'),\n env: envSchema,\n },\n ['command'],\n ),\n capability: SandboxCapability.Process,\n write: true,\n policyKeys: ['shell', 'process'],\n }),\n tool({\n name: SandboxMcpTool.ProcessOutput,\n title: 'Read process output',\n description:\n 'Read output of a process started with spawn, from byte offsets (pass the next_offset values from the previous call). Optionally waits for new output.',\n inputSchema: objectSchema(\n {\n process_id: str('The id returned by spawn.'),\n stdout_offset: int('Byte offset into stdout.'),\n stderr_offset: int('Byte offset into stderr.'),\n max_bytes: int('Maximum bytes per stream to return (default 65536).', 1),\n wait_ms: int('Wait up to this long for new output or exit (max 30000).'),\n },\n ['process_id'],\n ),\n capability: SandboxCapability.Process,\n write: false,\n policyKeys: ['shell', 'process'],\n idempotent: false,\n }),\n tool({\n name: SandboxMcpTool.ProcessInput,\n title: 'Write process input',\n description: 'Write text to the stdin of a process started with spawn; set end to close stdin.',\n inputSchema: objectSchema(\n {\n process_id: str('The id returned by spawn.'),\n data: str('Text to write.'),\n end: { type: 'boolean' },\n },\n ['process_id'],\n ),\n capability: SandboxCapability.Process,\n write: true,\n policyKeys: ['shell', 'process'],\n destructive: false,\n }),\n tool({\n name: SandboxMcpTool.ProcessTerminate,\n title: 'Terminate process',\n description: 'Stop a process started with spawn.',\n inputSchema: objectSchema(\n {\n process_id: str('The id returned by spawn.'),\n signal: str('Signal name (default SIGTERM).'),\n },\n ['process_id'],\n ),\n capability: SandboxCapability.Process,\n write: true,\n policyKeys: ['shell', 'process'],\n }),\n tool({\n name: SandboxMcpTool.ReadFile,\n title: 'Read file',\n description:\n 'Read a text file. Use offset (1-based line) and limit (line count) for large files; large or binary files also come back as a resource link.',\n inputSchema: objectSchema(\n {\n path: str('File path (absolute, or relative to the working directory).'),\n offset: int('First line to read (1-based).', 1),\n limit: int('Number of lines to read.', 1),\n },\n ['path'],\n ),\n capability: SandboxCapability.Files,\n write: false,\n policyKeys: ['files.read', 'files'],\n }),\n tool({\n name: SandboxMcpTool.WriteFile,\n title: 'Write file',\n description: 'Create or overwrite a text file. Parent directories are created.',\n inputSchema: objectSchema({ path: str('File path.'), content: str('Full file content.') }, [\n 'path',\n 'content',\n ]),\n capability: SandboxCapability.Files,\n write: true,\n policyKeys: ['files.write', 'files'],\n }),\n tool({\n name: SandboxMcpTool.EditFile,\n title: 'Edit file',\n description:\n 'Replace an exact string in a file. old_string must occur exactly once unless replace_all is true; include surrounding lines to make it unique.',\n inputSchema: objectSchema(\n {\n path: str('File path.'),\n old_string: str('Exact text to replace.'),\n new_string: str('Replacement text.'),\n replace_all: { type: 'boolean', description: 'Replace every occurrence.' },\n },\n ['path', 'old_string', 'new_string'],\n ),\n capability: SandboxCapability.Files,\n write: true,\n policyKeys: ['files.write', 'files'],\n }),\n tool({\n name: SandboxMcpTool.ListDir,\n title: 'List directory',\n description: 'List the entries of a directory (name, type, size).',\n inputSchema: objectSchema(\n { path: str('Directory path (default: the working directory).') },\n [],\n ),\n capability: SandboxCapability.Files,\n write: false,\n policyKeys: ['files.read', 'files'],\n }),\n tool({\n name: SandboxMcpTool.Stat,\n title: 'Stat path',\n description: 'Type, size, modification time and mode of a path, or exists: false.',\n inputSchema: objectSchema({ path: str('Path to inspect.') }, ['path']),\n capability: SandboxCapability.Files,\n write: false,\n policyKeys: ['files.read', 'files'],\n }),\n tool({\n name: SandboxMcpTool.Grep,\n title: 'Search file contents',\n description:\n 'Search files under a path for a regular expression. Returns path:line:text matches. Skips .git and node_modules.',\n inputSchema: objectSchema(\n {\n pattern: str('JavaScript regular expression.'),\n path: str('File or directory to search (default: the working directory).'),\n glob: str('Only search files matching this glob (e.g. \"*.ts\").'),\n ignore_case: { type: 'boolean' },\n max_results: int('Maximum matches (default 200).', 1),\n },\n ['pattern'],\n ),\n capability: SandboxCapability.Files,\n write: false,\n policyKeys: ['files.read', 'files'],\n }),\n tool({\n name: SandboxMcpTool.Glob,\n title: 'Find files',\n description: 'Find files whose path (relative to path) matches a glob such as \"**/*.ts\".',\n inputSchema: objectSchema(\n {\n pattern: str('Glob pattern.'),\n path: str('Directory to search (default: the working directory).'),\n max_results: int('Maximum paths (default 500).', 1),\n },\n ['pattern'],\n ),\n capability: SandboxCapability.Files,\n write: false,\n policyKeys: ['files.read', 'files'],\n }),\n tool({\n name: SandboxMcpTool.GetHost,\n title: 'Get port URL',\n description:\n 'The URL (and any required headers) at which a port served inside the sandbox is reachable.',\n inputSchema: objectSchema(\n {\n port: int('Port number.', 1),\n protocol: {\n type: 'string',\n enum: Object.values(PortProtocol),\n description: 'Default http.',\n },\n },\n ['port'],\n ),\n capability: SandboxCapability.Ports,\n write: false,\n policyKeys: ['ports'],\n }),\n];\n\nconst scopeOf = (options: SandboxMcpDescriptorOptionsIface): SandboxMcpScope =>\n options.scope ?? { kind: SandboxMcpScopeKind.All };\n\ninterface HostedServersIface {\n computerUse: boolean;\n localMcp: boolean;\n // Only this local server (a per-server address).\n localServer?: string;\n}\n\nconst grantOf = (options: SandboxMcpDescriptorOptionsIface): SandboxGrantIface =>\n options.grant ?? FULL_SANDBOX_GRANT;\n\n// Whether the grant allows a built-in tool, by the handle methods it uses\n// (grant.ts): file tools read or write files, process and port tools run\n// commands.\nconst grantAllowsSpec = (grant: SandboxGrantIface, spec: ToolSpecIface): boolean => {\n if (spec.capability === SandboxCapability.Files)\n return grantAllows(grant, Capability.Files, spec.write ? FilesAccess.Write : FilesAccess.Read);\n return grantAllows(grant, Capability.Shell, ShellAccess.Execute);\n};\n\n// Which hosted servers a projection serves, given its capabilities, scope and grant.\nconst hostedServersOf = (options: SandboxMcpDescriptorOptionsIface): HostedServersIface => {\n const grant = grantOf(options);\n const capabilities = new Set(options.capabilities);\n const scope = scopeOf(options);\n const computerUse =\n capabilities.has(SandboxCapability.ComputerUse) &&\n scope.kind !== SandboxMcpScopeKind.LocalServer &&\n grantAllows(grant, Capability.ComputerUse, ScreenAccess.Control);\n // Local servers run as processes on the machine.\n const localMcp =\n capabilities.has(SandboxCapability.LocalMcp) &&\n scope.kind !== SandboxMcpScopeKind.ComputerUse &&\n grantAllows(grant, Capability.Shell, ShellAccess.Execute);\n return scope.kind === SandboxMcpScopeKind.LocalServer\n ? { computerUse, localMcp, localServer: scope.server }\n : { computerUse, localMcp };\n};\n\nconst hostedSpec = (hosted: HostedToolIface): ToolSpecIface => {\n const annotations: ToolAnnotationsIface = { ...mutating, ...hosted.tool.annotations };\n return {\n descriptor: { ...hosted.tool, name: hosted.name, annotations },\n capability:\n hosted.server.kind === SandboxHostedServerKind.ComputerUse\n ? SandboxCapability.ComputerUse\n : SandboxCapability.LocalMcp,\n write: true,\n policyKeys: hosted.policyKeys,\n hosted,\n };\n};\n\nconst toolSpecs = (options: SandboxMcpDescriptorOptionsIface): ToolSpecIface[] => {\n const capabilities = new Set(options.capabilities);\n // Shell-like capabilities imply processes for the projection.\n const builtIn =\n scopeOf(options).kind === SandboxMcpScopeKind.All\n ? BUILT_IN_TOOLS.filter((spec) => capabilities.has(spec.capability))\n : [];\n const grant = grantOf(options);\n const visible = builtIn.filter((spec) => grantAllowsSpec(grant, spec));\n const hosted = hostedTools({\n catalog: options.catalog ?? EMPTY_MCP_CATALOG,\n ...hostedServersOf(options),\n taken: new Set(builtIn.map((spec) => spec.descriptor.name)),\n });\n return [...visible, ...hosted.map(hostedSpec)];\n};\n\n// The projection's tools for a sandbox with these capabilities and the hosted\n// catalogs known so far, without a handle: what `tools/list` answers.\nexport const sandboxMcpToolDescriptors = (\n options: SandboxMcpDescriptorOptionsIface,\n): ToolDescriptorIface[] => toolSpecs(options).map((spec) => spec.descriptor);\n\n// The toolPolicy keys a projection tool is decided by, most specific first\n// (its name, then capability keys such as `shell` or `files.write`), as the\n// projection checks them; null for a tool these options do not list. Lets the\n// MCP host raise an `ask` approval before it calls the projection.\nconst policyKeysFor = (spec: ToolSpecIface, name: string): string[] => [name, ...spec.policyKeys];\n\nexport const sandboxMcpToolPolicyKeys = (\n options: SandboxMcpDescriptorOptionsIface,\n name: string,\n): string[] | null => {\n const spec = toolSpecs(options).find((candidate) => candidate.descriptor.name === name);\n if (spec == null) return null;\n return policyKeysFor(spec, name);\n};\n\nexport interface CreateSandboxMcpServerArgsIface extends SandboxMcpDescriptorOptionsIface {\n // Obtains (and on the first call starts) the sandbox. Called lazily.\n getSandbox: () => Promise<NetworkSandboxSessionIface>;\n // Keys are tool names or capability keys (`shell`, `files.write`, ...).\n toolPolicy?: Record<string, ToolApproval>;\n // Asked for tools whose policy is `ask`; no hook means deny.\n approve?: (toolName: string, args: JsonObject) => Promise<boolean>;\n shell?: SandboxShellMode;\n name?: string;\n version?: string;\n // Fetch for sandbox-hosted MCP servers reached over HTTP (tests, service bindings).\n fetch?: FetchLike;\n limits?: Partial<SandboxMcpLimitsIface>;\n onError?: (error: Error) => void;\n // The sandbox when it is already attached (no start): a `tools/list` that\n // finds a hosted catalog unknown reads it from the live sandbox before\n // answering. Without a live sandbox the list answers at once and the catalog\n // is learned in the background.\n peekSandbox?: () => NetworkSandboxSessionIface | null;\n // Called with the hosted catalogs whenever more of them become known.\n onCatalog?: (catalog: SandboxMcpCatalogIface) => void;\n // A background process started with `spawn` exited on its own and no client\n // has read its exit (`process_output`) within `exitReportGraceMs`: the host\n // reports it as a sandbox result. Never called for a process\n // ended with `process_terminate` or by `close`.\n onProcessExit?: (exit: SandboxProcessExitIface) => void;\n}\n\nexport interface SandboxProcessExitIface {\n processId: string;\n command: string;\n // Null when the sandbox lost the process without an exit code.\n exitCode: number | null;\n // The end of each stream, at most `exitOutputChars`.\n stdout: string;\n stderr: string;\n}\n\nexport interface SandboxMcpLimitsIface {\n // Characters of stdout/stderr returned per run (head and tail kept).\n maxOutputChars: number;\n // Files above this size are not inlined without a line range.\n maxInlineBytes: number;\n // Bytes of background-process output kept per stream.\n processBufferBytes: number;\n maxProcesses: number;\n // How long an exit may go unread before onProcessExit reports it.\n exitReportGraceMs: number;\n // Characters of each stream's tail an exit report carries.\n exitOutputChars: number;\n}\n\nconst DEFAULT_LIMITS: SandboxMcpLimitsIface = {\n maxOutputChars: 64_000,\n maxInlineBytes: 256 * 1024,\n processBufferBytes: 4 * 1024 * 1024,\n maxProcesses: 32,\n exitReportGraceMs: 2_000,\n exitOutputChars: 4_000,\n};\n\nexport interface SandboxMcpServerIface {\n readonly descriptors: readonly ToolDescriptorIface[];\n // Streamable HTTP MCP endpoint (2026-07-28 and stateless 2025 clients).\n fetch(request: Request): Promise<Response>;\n // Direct entry points for hosts that relay MCP themselves (MCP over ACP).\n listTools(): ToolDescriptorIface[];\n // `listTools` after what a `tools/list` does first (reading hosted catalogs\n // from a live sandbox, or starting their discovery).\n refreshTools(): Promise<ToolDescriptorIface[]>;\n // Catalogs learned elsewhere (another projection of the same sandbox).\n updateCatalog(catalog: SandboxMcpCatalogIface): void;\n updateCapabilities(capabilities: readonly SandboxCapability[]): void;\n callTool(name: string, args: JsonObject, signal?: AbortSignal): Promise<CallToolResult>;\n close(): Promise<void>;\n}\n\ninterface OutputBufferIface {\n chunks: Uint8Array[];\n // Absolute offset of chunks[0].\n base: number;\n size: number;\n}\n\ninterface BackgroundProcessIface {\n process: SandboxProcessIface;\n stdout: OutputBufferIface;\n stderr: OutputBufferIface;\n exitCode: number | null;\n exited: boolean;\n changed: Set<() => void>;\n command: string;\n // A client read the exit, or ended the process itself.\n exitSeen: boolean;\n}\n\nconst textResult = (text: string): CallToolResult => ({ content: [{ type: 'text', text }] });\nconst jsonResult = (value: JsonValue): CallToolResult => textResult(JSON.stringify(value, null, 2));\nconst errorResult = (message: string): CallToolResult => ({\n content: [{ type: 'text', text: message }],\n isError: true,\n});\n\nconst truncateMiddle = (text: string, max: number): string => {\n if (text.length <= max) return text;\n const half = Math.floor(max / 2);\n return `${text.slice(0, half)}\\n[... ${text.length - max} characters omitted ...]\\n${text.slice(text.length - half)}`;\n};\n\nconst listRequestSchema = z.object({ method: z.literal('tools/list') });\n\nconst runArgsSchema = z.object({\n command: z.string().min(1),\n cwd: z.string().optional(),\n timeout_ms: z.number().int().positive().optional(),\n env: z.record(z.string(), z.string()).optional(),\n});\nconst spawnArgsSchema = runArgsSchema.omit({ timeout_ms: true });\nconst processOutputArgsSchema = z.object({\n process_id: z.string().min(1),\n stdout_offset: z.number().int().nonnegative().optional(),\n stderr_offset: z.number().int().nonnegative().optional(),\n max_bytes: z.number().int().positive().optional(),\n wait_ms: z.number().int().nonnegative().optional(),\n});\nconst processInputArgsSchema = z.object({\n process_id: z.string().min(1),\n data: z.string().optional(),\n end: z.boolean().optional(),\n});\nconst processTerminateArgsSchema = z.object({\n process_id: z.string().min(1),\n signal: z.string().optional(),\n});\nconst readFileArgsSchema = z.object({\n path: z.string().min(1),\n offset: z.number().int().positive().optional(),\n limit: z.number().int().positive().optional(),\n});\nconst writeFileArgsSchema = z.object({ path: z.string().min(1), content: z.string() });\nconst editFileArgsSchema = z.object({\n path: z.string().min(1),\n old_string: z.string().min(1),\n new_string: z.string(),\n replace_all: z.boolean().optional(),\n});\nconst pathArgsSchema = z.object({ path: z.string().min(1) });\nconst listDirArgsSchema = z.object({ path: z.string().optional() });\nconst grepArgsSchema = z.object({\n pattern: z.string().min(1),\n path: z.string().optional(),\n glob: z.string().optional(),\n ignore_case: z.boolean().optional(),\n max_results: z.number().int().positive().optional(),\n});\nconst globArgsSchema = z.object({\n pattern: z.string().min(1),\n path: z.string().optional(),\n max_results: z.number().int().positive().optional(),\n});\nconst getHostArgsSchema = z.object({\n port: z.number().int().min(1).max(65535),\n protocol: z.nativeEnum(PortProtocol).optional(),\n});\n\nconst appendOutput = (buffer: OutputBufferIface, chunk: Uint8Array, cap: number) => {\n buffer.chunks.push(chunk);\n buffer.size += chunk.byteLength;\n while (buffer.size > cap && buffer.chunks.length > 1) {\n const dropped = buffer.chunks.shift();\n if (dropped == null) break;\n buffer.size -= dropped.byteLength;\n buffer.base += dropped.byteLength;\n }\n};\n\nconst readOutput = (buffer: OutputBufferIface, offset: number, maxBytes: number) => {\n const start = Math.max(offset, buffer.base);\n const end = buffer.base + buffer.size;\n const out = new Uint8Array(Math.max(0, Math.min(end - start, maxBytes)));\n let position = buffer.base;\n let written = 0;\n for (const chunk of buffer.chunks) {\n const chunkEnd = position + chunk.byteLength;\n if (chunkEnd > start && written < out.byteLength) {\n const from = Math.max(0, start - position);\n const take = Math.min(chunk.byteLength - from, out.byteLength - written);\n out.set(chunk.subarray(from, from + take), written);\n written += take;\n }\n position = chunkEnd;\n }\n return {\n data: utf8Decode(out),\n offset: start,\n next_offset: start + written,\n skipped: start - offset,\n available: end,\n };\n};\n\nexport const createSandboxMcpServer = (\n args: CreateSandboxMcpServerArgsIface,\n): SandboxMcpServerIface => {\n const limits: SandboxMcpLimitsIface = { ...DEFAULT_LIMITS, ...args.limits };\n let catalog: SandboxMcpCatalogIface = args.catalog ?? EMPTY_MCP_CATALOG;\n let capabilities = [...args.capabilities];\n let specs = toolSpecs({ ...args, capabilities, catalog });\n let byName = new Map(specs.map((spec) => [spec.descriptor.name, spec]));\n const processes = new Map<string, BackgroundProcessIface>();\n let served = hostedServersOf({ ...args, capabilities });\n let handler: McpHttpHandler | null = null;\n let learning: Promise<void> | null = null;\n let discovering = false;\n // Hosted servers whose known catalog was checked against the live server.\n const checked = new Set<string>();\n\n const asError = (cause: unknown): Error =>\n cause instanceof Error ? cause : new Error(String(cause));\n const reportError = (error: Error) => args.onError?.(error);\n\n // Merges catalogs as they become known; a change relists the tools and tells\n // `subscriptions/listen` clients (`notifications/tools/list_changed`).\n const mergeCatalog = (next: Partial<SandboxMcpCatalogIface>): boolean => {\n const merged: SandboxMcpCatalogIface = {\n computerUse: next.computerUse ?? catalog.computerUse,\n localMcp: next.localMcp ?? catalog.localMcp,\n localMcpIncomplete:\n next.localMcp == null ? catalog.localMcpIncomplete : (next.localMcpIncomplete ?? false),\n };\n if (\n JSON.stringify({ ...merged, localMcpIncomplete: merged.localMcpIncomplete ?? false }) ===\n JSON.stringify({ ...catalog, localMcpIncomplete: catalog.localMcpIncomplete ?? false })\n )\n return false;\n catalog = merged;\n specs = toolSpecs({ ...args, capabilities, catalog });\n byName = new Map(specs.map((spec) => [spec.descriptor.name, spec]));\n handler?.notify.toolsChanged();\n return true;\n };\n\n const unknownCatalogs = () => ({\n computerUse: served.computerUse && catalog.computerUse == null,\n localMcp: served.localMcp && (catalog.localMcp == null || catalog.localMcpIncomplete === true),\n });\n\n const connections = createHostedMcpConnections({\n getSandbox: () => sandboxFor(),\n fetch: args.fetch,\n name: args.name,\n version: args.version,\n catalog: () => catalog,\n });\n\n // Reads the unknown hosted catalogs from an attached sandbox, once at a time.\n let nextAutomaticLearningAt: number | null = null;\n const learnFrom = (sandbox: NetworkSandboxSessionIface, force = false): Promise<void> => {\n if (!force && nextAutomaticLearningAt != null && performance.now() < nextAutomaticLearningAt)\n return learning ?? Promise.resolve();\n const needed = unknownCatalogs();\n if (!needed.computerUse && !needed.localMcp) return Promise.resolve();\n learning ??= (async () => {\n try {\n const learned = await connections.learn(sandbox, needed);\n nextAutomaticLearningAt =\n learned.localMcpIncomplete === true || (needed.computerUse && learned.computerUse == null)\n ? performance.now() + 1000\n : null;\n if (!closed && mergeCatalog(learned)) args.onCatalog?.(catalog);\n } catch (cause) {\n nextAutomaticLearningAt = performance.now() + 1000;\n throw cause;\n } finally {\n learning = null;\n }\n })();\n return learning;\n };\n\n // Every attach through this projection also learns what it does not know yet.\n const sandboxFor = async (): Promise<NetworkSandboxSessionIface> => {\n const sandbox = await args.getSandbox();\n learnFrom(sandbox).catch((cause: unknown) => reportError(asError(cause)));\n return sandbox;\n };\n\n // Before answering `tools/list`: a live sandbox is asked for the catalogs it\n // can give now; without one, the first list attaches in the background (the\n // only way to learn a catalog the provider cannot give) and the tools follow\n // with `notifications/tools/list_changed`.\n const beforeList = async (waitForDiscovery = false): Promise<void> => {\n const needed = unknownCatalogs();\n if (!needed.computerUse && !needed.localMcp) return;\n const live = args.peekSandbox?.() ?? null;\n if (live != null) {\n // The combined projection stays responsive; dedicated endpoints need\n // their live catalog before a harness can use them.\n const discovered = learnFrom(live, waitForDiscovery).catch((cause: unknown) =>\n reportError(asError(cause)),\n );\n if (waitForDiscovery || scopeOf(args).kind !== SandboxMcpScopeKind.All) await discovered;\n return;\n }\n if (discovering) return;\n discovering = true;\n sandboxFor()\n .then((sandbox) => learnFrom(sandbox))\n .catch((cause: unknown) => {\n discovering = false;\n reportError(asError(cause));\n });\n };\n\n // A known catalog may be stale (the server changed since it was recorded).\n // It is checked against the live server the first time a call opens that\n // server, never by opening servers the session does not use; a change is\n // announced like a new catalog.\n const checkCatalog = (server: SandboxHostedServer): void => {\n const key = server.kind === SandboxHostedServerKind.Local ? `local:${server.name}` : 'cu';\n if (checked.has(key)) return;\n checked.add(key);\n void (async () => {\n const tools = await connections.tools(server);\n const next: Partial<SandboxMcpCatalogIface> =\n server.kind === SandboxHostedServerKind.ComputerUse\n ? { computerUse: tools }\n : catalog.localMcp == null\n ? {}\n : {\n localMcp: { ...catalog.localMcp, [server.name]: tools },\n localMcpIncomplete: catalog.localMcpIncomplete,\n };\n if (!closed && mergeCatalog(next)) args.onCatalog?.(catalog);\n })().catch((cause: unknown) => {\n checked.delete(key);\n reportError(asError(cause));\n });\n };\n\n const shell = createSandboxShell({ getSandbox: sandboxFor, mode: args.shell });\n const isAllowed = async (\n spec: ToolSpecIface,\n name: string,\n input: JsonObject,\n ): Promise<boolean> => {\n const keys = policyKeysFor(spec, name);\n const decision = resolveSandboxToolPolicy(args.toolPolicy ?? {}, keys);\n return (\n decision?.decision !== ToolApproval.Ask ||\n (args.approve != null && (await args.approve(name, input)))\n );\n };\n\n const backgroundProcess = (id: string): BackgroundProcessIface => {\n const entry = processes.get(id);\n if (entry == null) throw new Error(`no process ${id}; start one with spawn`);\n return entry;\n };\n\n const notifyProcess = (entry: BackgroundProcessIface) => {\n for (const listener of [...entry.changed]) listener();\n };\n\n // Up to four UTF-8 bytes per character are read back from the end.\n const tailOf = (buffer: OutputBufferIface): string => {\n const end = buffer.base + buffer.size;\n const text = readOutput(\n buffer,\n end - limits.exitOutputChars * 4,\n limits.exitOutputChars * 4,\n ).data;\n return text.length <= limits.exitOutputChars\n ? text\n : text.slice(text.length - limits.exitOutputChars);\n };\n\n let closed = false;\n const reportExit = (id: string, entry: BackgroundProcessIface) => {\n const report = args.onProcessExit;\n if (report == null) return;\n setTimeout(() => {\n if (closed || entry.exitSeen) return;\n entry.exitSeen = true;\n report({\n processId: id,\n command: entry.command,\n exitCode: entry.exitCode,\n stdout: tailOf(entry.stdout),\n stderr: tailOf(entry.stderr),\n });\n }, limits.exitReportGraceMs);\n };\n\n const pumpOutput = async (\n entry: BackgroundProcessIface,\n stream: ReadableStream<Uint8Array>,\n buffer: OutputBufferIface,\n ) => {\n const reader = stream.getReader();\n try {\n for (;;) {\n const { done, value } = await reader.read();\n if (done) break;\n appendOutput(buffer, value, limits.processBufferBytes);\n notifyProcess(entry);\n }\n } catch {\n // Reported through `exited`.\n }\n };\n\n const run = async (\n name: string,\n rawArgs: JsonObject,\n signal: AbortSignal | undefined,\n ): Promise<CallToolResult> => {\n switch (name) {\n case SandboxMcpTool.Run: {\n const input = runArgsSchema.parse(rawArgs);\n const result = await shell.run({\n command: input.command,\n cwd: input.cwd,\n env: input.env,\n timeoutMs: input.timeout_ms ?? 120_000,\n signal,\n });\n return jsonResult({\n exit_code: result.exitCode,\n stdout: truncateMiddle(result.stdout, limits.maxOutputChars),\n stderr: truncateMiddle(result.stderr, limits.maxOutputChars),\n });\n }\n case SandboxMcpTool.Spawn: {\n const input = spawnArgsSchema.parse(rawArgs);\n if (processes.size >= limits.maxProcesses) {\n for (const [id, entry] of processes)\n if (entry.exited && entry.exitSeen) processes.delete(id);\n }\n if (processes.size >= limits.maxProcesses) {\n return errorResult(\n `too many background processes (${limits.maxProcesses}); terminate one first`,\n );\n }\n const sandbox = await sandboxFor();\n const child = await sandbox.spawn({\n command: input.command,\n shell: true,\n cwd: input.cwd,\n env: input.env,\n });\n const id = `p${crypto.randomUUID().slice(0, 8)}`;\n const entry: BackgroundProcessIface = {\n process: child,\n stdout: { chunks: [], base: 0, size: 0 },\n stderr: { chunks: [], base: 0, size: 0 },\n exitCode: null,\n exited: false,\n changed: new Set(),\n command: input.command,\n exitSeen: false,\n };\n processes.set(id, entry);\n void pumpOutput(entry, child.stdout, entry.stdout);\n void pumpOutput(entry, child.stderr, entry.stderr);\n child.wait().then(\n (result) => {\n entry.exited = true;\n entry.exitCode = result.exitCode;\n notifyProcess(entry);\n reportExit(id, entry);\n },\n () => {\n entry.exited = true;\n notifyProcess(entry);\n reportExit(id, entry);\n },\n );\n return jsonResult({ process_id: id, pid: child.pid });\n }\n case SandboxMcpTool.ProcessOutput: {\n const input = processOutputArgsSchema.parse(rawArgs);\n const entry = backgroundProcess(input.process_id);\n const stdoutOffset = input.stdout_offset ?? 0;\n const stderrOffset = input.stderr_offset ?? 0;\n const hasNews = () =>\n entry.exited ||\n entry.stdout.base + entry.stdout.size > stdoutOffset ||\n entry.stderr.base + entry.stderr.size > stderrOffset;\n const waitMs = Math.min(input.wait_ms ?? 0, 30_000);\n if (waitMs > 0 && !hasNews()) {\n await new Promise<void>((resolve) => {\n const done = () => {\n clearTimeout(timer);\n entry.changed.delete(listener);\n signal?.removeEventListener('abort', done);\n resolve();\n };\n const listener = () => {\n if (hasNews()) done();\n };\n const timer = setTimeout(done, waitMs);\n entry.changed.add(listener);\n signal?.addEventListener('abort', done, { once: true });\n });\n }\n const maxBytes = input.max_bytes ?? 65_536;\n if (entry.exited) entry.exitSeen = true;\n return jsonResult({\n stdout: readOutput(entry.stdout, stdoutOffset, maxBytes),\n stderr: readOutput(entry.stderr, stderrOffset, maxBytes),\n running: !entry.exited,\n exit_code: entry.exitCode,\n });\n }\n case SandboxMcpTool.ProcessInput: {\n const input = processInputArgsSchema.parse(rawArgs);\n const entry = backgroundProcess(input.process_id);\n await entry.process.write(input.data ?? '', { end: input.end === true });\n return jsonResult({ written: input.data?.length ?? 0, stdin_closed: input.end === true });\n }\n case SandboxMcpTool.ProcessTerminate: {\n const input = processTerminateArgsSchema.parse(rawArgs);\n const entry = backgroundProcess(input.process_id);\n entry.exitSeen = true;\n await entry.process.kill(input.signal);\n const result = await entry.process.wait().catch(() => ({ exitCode: entry.exitCode ?? -1 }));\n processes.delete(input.process_id);\n return jsonResult({ process_id: input.process_id, exit_code: result.exitCode });\n }\n case SandboxMcpTool.ReadFile: {\n const input = readFileArgsSchema.parse(rawArgs);\n const sandbox = await sandboxFor();\n const path = resolvePosixPath(sandbox.defaultWorkingDirectory, input.path);\n const stat = await sandbox.stat({ path, abortSignal: signal });\n if (stat == null) return errorResult(`${path}: no such file`);\n if (stat.type === SandboxEntryType.Directory)\n return errorResult(`${path} is a directory; use list_dir`);\n const link = {\n type: 'resource_link' as const,\n uri: `${SANDBOX_FILE_RESOURCE_SCHEME}://${encodeURI(path)}`,\n name: path.split('/').pop() ?? path,\n size: stat.size,\n };\n const ranged = input.offset != null || input.limit != null;\n const startLine = input.offset ?? 1;\n const endLine = input.limit == null ? undefined : startLine + input.limit - 1;\n if (!ranged && stat.size > limits.maxInlineBytes) {\n const head =\n (await sandbox.readTextFile({\n path,\n startLine: 1,\n endLine: 200,\n abortSignal: signal,\n })) ?? '';\n return {\n content: [\n {\n type: 'text',\n text: `${path} is ${stat.size} bytes, too large to return whole. First 200 lines below; read more with offset and limit, or fetch the resource.\\n\\n${truncateMiddle(head, limits.maxOutputChars)}`,\n },\n link,\n ],\n };\n }\n const text = await sandbox.readTextFile({\n path,\n startLine: ranged ? startLine : undefined,\n endLine,\n abortSignal: signal,\n });\n if (text == null) return errorResult(`${path}: no such file`);\n if (text.includes('\\u0000')) {\n return {\n content: [\n { type: 'text', text: `${path} is a binary file (${stat.size} bytes).` },\n link,\n ],\n };\n }\n return textResult(\n truncateMiddle(text, Math.max(limits.maxOutputChars, limits.maxInlineBytes)),\n );\n }\n case SandboxMcpTool.WriteFile: {\n const input = writeFileArgsSchema.parse(rawArgs);\n const sandbox = await sandboxFor();\n const path = resolvePosixPath(sandbox.defaultWorkingDirectory, input.path);\n await sandbox.writeTextFile({ path, content: input.content, abortSignal: signal });\n return textResult(\n `Wrote ${new TextEncoder().encode(input.content).byteLength} bytes to ${path}`,\n );\n }\n case SandboxMcpTool.EditFile: {\n const input = editFileArgsSchema.parse(rawArgs);\n const sandbox = await sandboxFor();\n const path = resolvePosixPath(sandbox.defaultWorkingDirectory, input.path);\n const text = await sandbox.readTextFile({ path, abortSignal: signal });\n if (text == null) return errorResult(`${path}: no such file`);\n const count = text.split(input.old_string).length - 1;\n if (count === 0) return errorResult(`old_string was not found in ${path}`);\n if (count > 1 && input.replace_all !== true) {\n return errorResult(\n `old_string occurs ${count} times in ${path}; add surrounding context or set replace_all`,\n );\n }\n const next =\n input.replace_all === true\n ? text.split(input.old_string).join(input.new_string)\n : text.replace(input.old_string, () => input.new_string);\n await sandbox.writeTextFile({ path, content: next, abortSignal: signal });\n return textResult(\n `Replaced ${input.replace_all === true ? count : 1} occurrence(s) in ${path}`,\n );\n }\n case SandboxMcpTool.ListDir: {\n const input = listDirArgsSchema.parse(rawArgs);\n const sandbox = await sandboxFor();\n const path = resolvePosixPath(sandbox.defaultWorkingDirectory, input.path ?? '.');\n const entries = await sandbox.readDir({ path, abortSignal: signal });\n return jsonResult({\n path,\n entries: entries.map((entry) => ({\n name: entry.name,\n type: entry.type,\n size: entry.size,\n })),\n });\n }\n case SandboxMcpTool.Stat: {\n const input = pathArgsSchema.parse(rawArgs);\n const sandbox = await sandboxFor();\n const path = resolvePosixPath(sandbox.defaultWorkingDirectory, input.path);\n const stat = await sandbox.stat({ path, abortSignal: signal });\n return jsonResult(stat == null ? { path, exists: false } : { exists: true, ...stat });\n }\n case SandboxMcpTool.Grep: {\n const input = grepArgsSchema.parse(rawArgs);\n const sandbox = await sandboxFor();\n const result = await grepFiles({\n sandbox,\n cwd: sandbox.defaultWorkingDirectory,\n pattern: input.pattern,\n path: input.path,\n glob: input.glob,\n ignoreCase: input.ignore_case,\n maxResults: input.max_results ?? 200,\n signal,\n });\n const lines = result.matches.map((match) => `${match.path}:${match.line}:${match.text}`);\n if (result.truncated) lines.push('[results truncated]');\n return textResult(lines.length === 0 ? 'No matches.' : lines.join('\\n'));\n }\n case SandboxMcpTool.Glob: {\n const input = globArgsSchema.parse(rawArgs);\n const sandbox = await sandboxFor();\n const result = await globFiles({\n sandbox,\n cwd: sandbox.defaultWorkingDirectory,\n pattern: input.pattern,\n path: input.path,\n maxResults: input.max_results ?? 500,\n signal,\n });\n const lines = [...result.paths];\n if (result.truncated) lines.push('[results truncated]');\n return textResult(lines.length === 0 ? 'No files matched.' : lines.join('\\n'));\n }\n case SandboxMcpTool.GetHost: {\n const input = getHostArgsSchema.parse(rawArgs);\n const sandbox = await sandboxFor();\n const endpoint = await sandbox.getPortEndpoint({\n port: input.port,\n protocol: input.protocol,\n });\n return jsonResult({ url: endpoint.url, headers: endpoint.headers ?? {} });\n }\n default:\n return errorResult(`unknown tool ${name}`);\n }\n };\n\n const callTool = async (\n name: string,\n rawArgs: JsonObject,\n signal?: AbortSignal,\n ): Promise<CallToolResult> => {\n const spec = byName.get(name);\n if (spec == null) return errorResult(`unknown tool ${name}`);\n if (!(await isAllowed(spec, name, rawArgs))) return errorResult(`${name} was not approved`);\n try {\n signal?.throwIfAborted();\n if (spec.hosted != null) {\n try {\n return await connections.call(spec.hosted.server, spec.hosted.tool.name, rawArgs, {\n signal,\n });\n } finally {\n // Also after a failure: a stale tool name is how a changed catalog shows.\n checkCatalog(spec.hosted.server);\n }\n }\n return await run(name, rawArgs, signal);\n } catch (error) {\n if (error instanceof z.ZodError)\n return errorResult(`invalid arguments: ${z.prettifyError(error)}`);\n return errorResult(error instanceof Error ? error.message : String(error));\n }\n };\n\n const readResource = async (uri: URL): Promise<ReadResourceResult> => {\n const path = decodeURI(uri.pathname);\n const spec = byName.get(SandboxMcpTool.ReadFile);\n if (spec == null || !(await isAllowed(spec, SandboxMcpTool.ReadFile, { path })))\n throw new Error(`${path}: file read was not approved`);\n const sandbox = await sandboxFor();\n const bytes = await sandbox.readBinaryFile({ path });\n if (bytes == null) throw new Error(`${path}: no such file`);\n const text = new TextDecoder('utf-8', { fatal: false }).decode(bytes);\n if (!text.includes('\\u0000') && !text.includes('\uFFFD')) {\n return { contents: [{ uri: uri.href, mimeType: 'text/plain', text }] };\n }\n return {\n contents: [\n { uri: uri.href, mimeType: 'application/octet-stream', blob: bytesToBase64(bytes) },\n ],\n };\n };\n\n const buildServer = (): McpServer => {\n const server = new McpServer(\n { name: args.name ?? 'final-sandbox', version: args.version ?? '1' },\n // The hosted servers' tools can follow the first list (see beforeList).\n {\n capabilities: {\n tools: { listChanged: true },\n resources: {},\n },\n },\n );\n for (const spec of specs) {\n server.registerTool(\n spec.descriptor.name,\n {\n title: spec.descriptor.annotations?.title,\n description: spec.descriptor.description,\n inputSchema: fromJsonSchema<JsonObject>(spec.descriptor.inputSchema),\n annotations: spec.descriptor.annotations,\n },\n (input, ctx) => callTool(spec.descriptor.name, input, ctx.mcpReq.signal),\n );\n }\n if (capabilities.includes(SandboxCapability.Files)) {\n server.registerResource(\n 'sandbox-file',\n new ResourceTemplate(`${SANDBOX_FILE_RESOURCE_SCHEME}://{+path}`, { list: undefined }),\n { description: 'A file in the sandbox, by absolute path.' },\n (uri) => readResource(uri),\n );\n }\n return server;\n };\n\n const isListRequest = async (request: Request): Promise<boolean> => {\n if (request.method !== 'POST') return false;\n try {\n const body: unknown = await request.clone().json();\n return (Array.isArray(body) ? body : [body]).some(\n (message) => listRequestSchema.safeParse(message).success,\n );\n } catch {\n return false;\n }\n };\n\n const getHandler = () => {\n handler ??= createMcpHandler(() => buildServer(), { onerror: args.onError });\n return handler;\n };\n\n return {\n get descriptors() {\n return specs.map((spec) => spec.descriptor);\n },\n fetch: async (request) => {\n if (await isListRequest(request)) await beforeList();\n return getHandler().fetch(request);\n },\n listTools: () => specs.map((spec) => spec.descriptor),\n refreshTools: async () => {\n await beforeList(true);\n return specs.map((spec) => spec.descriptor);\n },\n updateCatalog: (next) => {\n mergeCatalog(next);\n },\n updateCapabilities: (next) => {\n if (closed || JSON.stringify(next) === JSON.stringify(capabilities)) return;\n capabilities = [...next];\n served = hostedServersOf({ ...args, capabilities });\n specs = toolSpecs({ ...args, capabilities, catalog });\n byName = new Map(specs.map((spec) => [spec.descriptor.name, spec]));\n handler?.notify.toolsChanged();\n },\n callTool,\n close: async () => {\n closed = true;\n await shell.close();\n for (const entry of processes.values()) await entry.process.kill().catch(() => undefined);\n processes.clear();\n await connections.close();\n await handler?.close();\n },\n };\n};\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,gCAAgC;AAEzC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAIK;AACP,YAAY,OAAO;AAEnB,SAAS,YAAY,aAAa,cAAc,mBAAmB;AACnE,SAAS,oBAAoB,oBAAoB;AAEjD,SAAS,oBAAoB,mBAA2C;AACxE,SAAS,eAAe,kBAAkB;AAC1C;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAGK;AACP,SAAS,WAAW,iBAAiB;AACrC,SAAS,oBAAoB,wBAAwB;AACrD,SAAS,wBAAwB;AACjC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAIK;AAEP;AAAA,EACE;AAAA,EACA,2BAAAA;AAAA,EACA;AAAA,OAEK;AACP,SAAS,oBAAAC,yBAAwB;AAa1B,IAAK,iBAAL,kBAAKC,oBAAL;AACL,EAAAA,gBAAA,SAAM;AACN,EAAAA,gBAAA,WAAQ;AACR,EAAAA,gBAAA,mBAAgB;AAChB,EAAAA,gBAAA,kBAAe;AACf,EAAAA,gBAAA,sBAAmB;AACnB,EAAAA,gBAAA,cAAW;AACX,EAAAA,gBAAA,eAAY;AACZ,EAAAA,gBAAA,cAAW;AACX,EAAAA,gBAAA,aAAU;AACV,EAAAA,gBAAA,UAAO;AACP,EAAAA,gBAAA,UAAO;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,IAAI,MAAM,KAAK,UAAU,YAAY,EAAG;AACrE,qBAAe,CAAC,GAAG,IAAI;AACvB,eAAS,gBAAgB,EAAE,GAAG,MAAM,aAAa,CAAC;AAClD,cAAQ,UAAU,EAAE,GAAG,MAAM,cAAc,QAAQ,CAAC;AACpD,eAAS,IAAI,IAAI,MAAM,IAAI,CAAC,SAAS,CAAC,KAAK,WAAW,MAAM,IAAI,CAAC,CAAC;AAClE,eAAS,OAAO,aAAa;AAAA,IAC/B;AAAA,IACA;AAAA,IACA,OAAO,YAAY;AACjB,eAAS;AACT,YAAM,MAAM,MAAM;AAClB,iBAAW,SAAS,UAAU,OAAO,EAAG,OAAM,MAAM,QAAQ,KAAK,EAAE,MAAM,MAAM,MAAS;AACxF,gBAAU,MAAM;AAChB,YAAM,YAAY,MAAM;AACxB,YAAM,SAAS,MAAM;AAAA,IACvB;AAAA,EACF;AACF;",
|
|
6
6
|
"names": ["SandboxHostedServerKind", "SandboxShellMode", "SandboxMcpTool", "SandboxMcpScopeKind", "id", "entry"]
|
|
7
7
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const readBodyText: (request: Request, limit: number) => Promise<string | null>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const readBodyText = async (request, limit) => {
|
|
2
|
+
const reader = request.body?.getReader();
|
|
3
|
+
if (reader == null) return "";
|
|
4
|
+
const chunks = [];
|
|
5
|
+
let size = 0;
|
|
6
|
+
for (; ; ) {
|
|
7
|
+
const next = await reader.read();
|
|
8
|
+
if (next.done) break;
|
|
9
|
+
size += next.value.byteLength;
|
|
10
|
+
if (size > limit) {
|
|
11
|
+
await reader.cancel().catch(() => void 0);
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
chunks.push(next.value);
|
|
15
|
+
}
|
|
16
|
+
const bytes = new Uint8Array(size);
|
|
17
|
+
let offset = 0;
|
|
18
|
+
for (const chunk of chunks) {
|
|
19
|
+
bytes.set(chunk, offset);
|
|
20
|
+
offset += chunk.byteLength;
|
|
21
|
+
}
|
|
22
|
+
return new TextDecoder().decode(bytes);
|
|
23
|
+
};
|
|
24
|
+
export {
|
|
25
|
+
readBodyText
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=body.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../../../src/sandbox/remote/body.ts"],
|
|
4
|
+
"sourcesContent": ["// A request body as text, read incrementally: a body over `limit` bytes stops the\n// read as soon as it passes the limit, whatever the request declared. Null when\n// it is over.\nexport const readBodyText = async (request: Request, limit: number): Promise<string | null> => {\n const reader = request.body?.getReader();\n if (reader == null) return '';\n const chunks: Uint8Array[] = [];\n let size = 0;\n for (;;) {\n const next = await reader.read();\n if (next.done) break;\n size += next.value.byteLength;\n if (size > limit) {\n await reader.cancel().catch(() => undefined);\n return null;\n }\n chunks.push(next.value);\n }\n const bytes = new Uint8Array(size);\n let offset = 0;\n for (const chunk of chunks) {\n bytes.set(chunk, offset);\n offset += chunk.byteLength;\n }\n return new TextDecoder().decode(bytes);\n};\n"],
|
|
5
|
+
"mappings": "AAGO,MAAM,eAAe,OAAO,SAAkB,UAA0C;AAC7F,QAAM,SAAS,QAAQ,MAAM,UAAU;AACvC,MAAI,UAAU,KAAM,QAAO;AAC3B,QAAM,SAAuB,CAAC;AAC9B,MAAI,OAAO;AACX,aAAS;AACP,UAAM,OAAO,MAAM,OAAO,KAAK;AAC/B,QAAI,KAAK,KAAM;AACf,YAAQ,KAAK,MAAM;AACnB,QAAI,OAAO,OAAO;AAChB,YAAM,OAAO,OAAO,EAAE,MAAM,MAAM,MAAS;AAC3C,aAAO;AAAA,IACT;AACA,WAAO,KAAK,KAAK,KAAK;AAAA,EACxB;AACA,QAAM,QAAQ,IAAI,WAAW,IAAI;AACjC,MAAI,SAAS;AACb,aAAW,SAAS,QAAQ;AAC1B,UAAM,IAAI,OAAO,MAAM;AACvB,cAAU,MAAM;AAAA,EAClB;AACA,SAAO,IAAI,YAAY,EAAE,OAAO,KAAK;AACvC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -39,6 +39,7 @@ import {
|
|
|
39
39
|
handleRpcTokenFromRequest
|
|
40
40
|
} from "../wire.mjs";
|
|
41
41
|
import { SANDBOX_PROVIDER_ERROR_STATUS, sandboxProviderWireError, wireErrorOf } from "./errors.mjs";
|
|
42
|
+
import { readBodyText } from "./body.mjs";
|
|
42
43
|
import { ndjsonResponse } from "./ndjson.mjs";
|
|
43
44
|
import {
|
|
44
45
|
platformIdentityExchangeUrl,
|
|
@@ -179,8 +180,8 @@ const serveSandboxProvider = (args) => {
|
|
|
179
180
|
const declared = Number(request.headers.get("content-length") ?? "0");
|
|
180
181
|
if (declared > SANDBOX_PROVIDER_MAX_BODY_BYTES)
|
|
181
182
|
throw sandboxProviderWireError(invalid("the request body is over the size limit"));
|
|
182
|
-
const text = await request
|
|
183
|
-
if (
|
|
183
|
+
const text = await readBodyText(request, SANDBOX_PROVIDER_MAX_BODY_BYTES);
|
|
184
|
+
if (text == null)
|
|
184
185
|
throw sandboxProviderWireError(invalid("the request body is over the size limit"));
|
|
185
186
|
try {
|
|
186
187
|
return JSON.parse(text);
|