From 67f5fa76f24bfe7b46c5bb0f0ebc48ebea1b0473 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Wed, 21 Oct 2026 19:30:36 -0700 Subject: [PATCH] Remove Local agents and Agent Relay Retire the local-agent catalog, pairing and transport routes, installers, UI, and build dependency now that The Knife provides this functionality. Reject token exchanges for retired catalogs while preserving stored account data. Assisted-by: gpt-6-astra --- dashboard/.gitignore | 1 - dashboard/agent/install.ps1 | 41 - dashboard/agent/install.sh | 53 -- dashboard/agent/setup.mjs | 192 ----- dashboard/agent/source.json | 4 - dashboard/src/main.rs | 4 - dashboard/src/mcp.rs | 90 +-- dashboard/src/relay.rs | 1111 ---------------------------- dashboard/web/api.contract.ts | 6 - dashboard/web/pages/MCP.css | 11 - dashboard/web/pages/MCP.tsx | 77 +- dashboard/web/pages/MCPAccess.tsx | 4 +- dashboard/web/pages/MCPConsent.tsx | 5 +- dashboard/web/types/mcp.ts | 3 +- nixos/dashboard.nix | 2 - tools/build-agent.py | 29 - tools/dashboard-agent-test.py | 175 ----- tools/deploy.py | 3 - 18 files changed, 49 insertions(+), 1762 deletions(-) delete mode 100644 dashboard/agent/install.ps1 delete mode 100644 dashboard/agent/install.sh delete mode 100644 dashboard/agent/setup.mjs delete mode 100644 dashboard/agent/source.json delete mode 100644 dashboard/src/relay.rs delete mode 100644 tools/build-agent.py delete mode 100644 tools/dashboard-agent-test.py diff --git a/dashboard/.gitignore b/dashboard/.gitignore index 2925148a23320aa72f16daa5d6e8ee24b2a5a3ad..c9b60c56f8344e5229a9ef30ed024babbcbbc819 100644 --- a/dashboard/.gitignore +++ b/dashboard/.gitignore @@ -3,4 +3,3 @@ dist/ .cache/ data/ target/ -agent/relay.mjs diff --git a/dashboard/agent/install.ps1 b/dashboard/agent/install.ps1 deleted file mode 100644 index 41f1caaa7c69037074ba196b0e3241e81621d59e..0000000000000000000000000000000000000000 --- a/dashboard/agent/install.ps1 +++ /dev/null @@ -1,41 +0,0 @@ -$ErrorActionPreference = 'Stop' -$Server = __SERVER__ - -function Install-Agent { - $Identity = [Security.Principal.WindowsIdentity]::GetCurrent() - $Principal = New-Object Security.Principal.WindowsPrincipal($Identity) - if ($Principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { - throw 'Run this installer from your usual PowerShell window, without administrator privileges.' - } - $Base = Join-Path $env:LOCALAPPDATA 'AgentRelay' - $Stage = Join-Path $Base ('.install.' + [guid]::NewGuid().ToString('N')) - New-Item -ItemType Directory -Force $Base | Out-Null - & icacls.exe $Base /inheritance:r /grant:r ('*' + $Identity.User.Value + ':(OI)(CI)F') '*S-1-5-18:(OI)(CI)F' | Out-Null - if ($LASTEXITCODE -ne 0) { throw 'Unable to protect the agent folder. Check its permissions and retry.' } - try { - New-Item -ItemType Directory -Force $Stage | Out-Null - $Node = Join-Path $Base 'node.exe' - if (!(Test-Path $Node)) { - Write-Host 'Downloading the agent runtime…' - $Arch = if ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64' -or $env:PROCESSOR_ARCHITEW6432 -eq 'ARM64') { 'arm64' } else { 'x64' } - $Checksums = (Invoke-WebRequest -UseBasicParsing -TimeoutSec 30 'https://nodejs.org/dist/latest-v24.x/SHASUMS256.txt').Content - $Match = [regex]::Match($Checksums, "(?m)^([a-f0-9]{64})\s+(node-(v24\.[0-9]+\.[0-9]+)-win-$Arch\.zip)\s*$") - if (!$Match.Success) { throw 'No runtime download is available for this machine.' } - $Archive = Join-Path $Stage $Match.Groups[2].Value - Invoke-WebRequest -UseBasicParsing -TimeoutSec 120 ("https://nodejs.org/dist/" + $Match.Groups[3].Value + '/' + $Match.Groups[2].Value) -OutFile $Archive - if ((Get-FileHash $Archive -Algorithm SHA256).Hash.ToLowerInvariant() -ne $Match.Groups[1].Value) { - throw 'The runtime checksum did not match. Run the installer again.' - } - Expand-Archive $Archive $Stage - Copy-Item (Join-Path $Stage ($Match.Groups[2].Value.Replace('.zip', '') + '\node.exe')) $Node - } - Invoke-WebRequest -UseBasicParsing -TimeoutSec 30 "$Server/agent/relay.mjs" -OutFile (Join-Path $Stage 'relay.mjs') - Invoke-WebRequest -UseBasicParsing -TimeoutSec 30 "$Server/agent/setup.mjs" -OutFile (Join-Path $Stage 'setup.mjs') - & $Node (Join-Path $Stage 'setup.mjs') install $Server $Base - if ($LASTEXITCODE -ne 0) { throw 'Installation stopped. Correct the problem above, then run the installer again.' } - } finally { - Remove-Item -Recurse -Force $Stage - } -} - -Install-Agent diff --git a/dashboard/agent/install.sh b/dashboard/agent/install.sh deleted file mode 100644 index 3e9b3d13cf804b24fca2ce45ce9150e1a61ed2e3..0000000000000000000000000000000000000000 --- a/dashboard/agent/install.sh +++ /dev/null @@ -1,53 +0,0 @@ -#!/bin/sh -set -eu -umask 077 -server=__SERVER__ - -install_agent() { - [ "$(id -u)" != 0 ] || { echo 'Run this installer as your login user, without sudo.' >&2; return 1; } - case $(uname -s) in - Linux) os=linux; base=${XDG_DATA_HOME:-"$HOME/.local/share"}/agent-relay - command -v systemctl >/dev/null || { echo 'This installer needs a systemd user session.' >&2; return 1; } - systemctl --user show-environment >/dev/null || { echo 'Sign in to a systemd user session, then run the installer again.' >&2; return 1; } ;; - Darwin) os=darwin; base="$HOME/Library/Application Support/AgentRelay" ;; - *) echo "Use $server/agent/install.ps1 from Windows PowerShell." >&2; return 1 ;; - esac - case $(uname -m) in - x86_64|amd64) arch=x64 ;; - aarch64|arm64) arch=arm64 ;; - *) echo 'This installer supports x64 and arm64 machines.' >&2; return 1 ;; - esac - command -v curl >/dev/null || { echo 'Install curl, then run this installer again.' >&2; return 1; } - mkdir -p "$base" - chmod 700 "$base" - stage=$(mktemp -d "$base/.install.XXXXXX") - trap 'rm -rf "$stage"' EXIT HUP INT TERM - if [ ! -x "$base/node" ] && [ -f /etc/NIXOS ]; then - echo 'Installing the agent runtime…' - nix --extra-experimental-features 'nix-command flakes' build nixpkgs#nodejs_24 --out-link "$base/node-runtime" - ln -sf "$base/node-runtime/bin/node" "$base/node" - elif [ ! -x "$base/node" ]; then - echo 'Downloading the agent runtime…' - curl -fsSL https://nodejs.org/dist/latest-v24.x/SHASUMS256.txt -o "$stage/checksums" - archive=$(awk -v suffix="-$os-$arch.tar.gz" '$2 ~ /^node-v24\./ && substr($2, length($2)-length(suffix)+1) == suffix {print $2}' "$stage/checksums") - [ -n "$archive" ] || { echo 'No runtime download is available for this machine.' >&2; return 1; } - version=${archive#node-}; version=${version%%-$os-*} - curl -fsSL "https://nodejs.org/dist/$version/$archive" -o "$stage/$archive" - expected=$(awk -v file="$archive" '$2 == file {print $1}' "$stage/checksums") - if command -v sha256sum >/dev/null; then - actual=$(sha256sum "$stage/$archive"); actual=${actual%% *} - else - actual=$(shasum -a 256 "$stage/$archive"); actual=${actual%% *} - fi - [ "$actual" = "$expected" ] || { echo 'The runtime checksum did not match. Run the installer again.' >&2; return 1; } - tar -xzf "$stage/$archive" -C "$stage" - cp "$stage/${archive%.tar.gz}/bin/node" "$base/node" - chmod 700 "$base/node" - fi - curl -fsSL "$server/agent/relay.mjs" -o "$stage/relay.mjs" - curl -fsSL "$server/agent/setup.mjs" -o "$stage/setup.mjs" - "$base/node" "$stage/setup.mjs" install "$server" "$base" execFileSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-EncodedCommand', Buffer.from(script, 'utf16le').toString('base64')], { stdio: 'inherit' }); -const psQuote = (text) => "'" + text.replaceAll("'", "''") + "'"; -const shellQuote = (text) => "'" + text.replaceAll("'", "'\\''") + "'"; - -async function stop() { - if (windows) ps(`$ErrorActionPreference = 'Stop' -if (Get-ScheduledTask -TaskName ${psQuote(task)} -ErrorAction SilentlyContinue) { Stop-ScheduledTask -TaskName ${psQuote(task)} } -Get-CimInstance Win32_Process -Filter "Name = 'node.exe'" | Where-Object { - $_.ExecutablePath -eq ${psQuote(join(base, 'node.exe'))} -and $_.CommandLine.Contains(${psQuote(join(base, 'relay.mjs'))}) -} | ForEach-Object { - & taskkill.exe /PID $_.ProcessId /T /F | Out-Null - if ($LASTEXITCODE -ne 0 -and (Get-Process -Id $_.ProcessId -ErrorAction SilentlyContinue)) { throw 'Unable to stop the previous agent. Close it and run the installer again.' } -}`); - else if (mac) { - if (spawnSync('launchctl', ['print', `${domain}/net.paperclover.agent-relay`], { stdio: 'ignore' }).status !== 0) return; - execFileSync('launchctl', ['bootout', `${domain}/net.paperclover.agent-relay`]); - for (let attempt = 0; attempt < 40; attempt++) { - try { execFileSync('launchctl', ['print', `${domain}/net.paperclover.agent-relay`], { stdio: 'ignore' }); } - catch { return; } - await sleep(250); - } - throw new Error('The previous agent is still stopping. Wait and run the installer again.'); - } - else if (spawnSync('systemctl', ['--user', 'show', '-P', 'LoadState', 'agent-relay.service'], { encoding: 'utf8' }).stdout.trim() === 'loaded') { - execFileSync('systemctl', ['--user', 'stop', 'agent-relay.service']); - } -} - -async function main() { - if (action === 'stop') { await stop(); return; } - if (action === 'start') { - if (windows) ps(`$ErrorActionPreference = 'Stop'; Start-ScheduledTask -TaskName ${psQuote(task)}`); - else if (mac) { - if (spawnSync('launchctl', ['print', `${domain}/net.paperclover.agent-relay`], { stdio: 'ignore' }).status === 0) { - execFileSync('launchctl', ['kickstart', `${domain}/net.paperclover.agent-relay`], { stdio: 'inherit' }); - } else execFileSync('launchctl', ['bootstrap', domain, unit], { stdio: 'inherit' }); - } - else execFileSync('systemctl', ['--user', 'start', 'agent-relay.service'], { stdio: 'inherit' }); - return; - } - if (action === 'status') { - if (windows) ps(`$ErrorActionPreference = 'Stop'; Get-ScheduledTask -TaskName ${psQuote(task)} | Select-Object TaskName,State`); - else if (mac) execFileSync('launchctl', ['print', `${domain}/net.paperclover.agent-relay`], { stdio: 'inherit' }); - else execFileSync('systemctl', ['--user', 'status', '--no-pager', 'agent-relay.service'], { stdio: 'inherit' }); - return; - } - if (action === 'uninstall') { - await stop(); - if (windows) ps(`$ErrorActionPreference = 'Stop'; Unregister-ScheduledTask -TaskName ${psQuote(task)} -Confirm:$false`); - else { - if (!mac) execFileSync('systemctl', ['--user', 'disable', 'agent-relay.service'], { stdio: 'inherit' }); - await rm(unit, { force: true }); - if (!mac) execFileSync('systemctl', ['--user', 'daemon-reload']); - } - console.log(`Startup removed. Pairing and files remain in ${base} and ${data}.`); - return; - } - if (action !== 'install' || !server || !installDir) throw new Error('Use install, status, start, stop, or uninstall.'); - const origin = new URL(server); - if (origin.origin !== server || (origin.protocol !== 'https:' && !(origin.protocol === 'http:' && ['localhost', '127.0.0.1', '[::1]'].includes(origin.hostname)))) { - throw new Error('Use an HTTPS dashboard origin, or localhost for a preview.'); - } - const staged = dirname(process.argv[1]); - let previous; - try { previous = JSON.parse(await readFile(join(data, 'agent.json'), 'utf8')); } - catch (error) { if (error.code !== 'ENOENT') throw error; } - if (previous && previous.server !== server) throw new Error(`This machine is paired to ${previous.server}. Unlink it there before changing dashboards.`); - const input = createInterface({ input: process.stdin, output: process.stdout }); - const closed = new AbortController(); - input.once('close', () => closed.abort()); - const ask = (text) => input.question(text, { signal: closed.signal }); - let name, desktopWrite; - const roots = []; - try { - console.log(`Agent Relay · ${server}\nCodex and Claude Code must already be installed and signed in.`); - console.log('Linked clients can read saved Codex and Claude Code chats on this machine.'); - name = previous ? 'this machine' : (await ask(`Machine name [${hostname()}]: `)).trim() || hostname(); - if (previous) console.log('The existing machine pairing will be kept.'); - if (name.length > 100) throw new Error('Enter a machine name up to 100 characters.'); - console.log('Allowed folders apply to new chats. Existing chats keep their own permissions.'); - if (previous?.roots?.length) console.log(`Current folders: ${previous.roots.join(', ')}`); - console.log('Enter one project folder at a time. Leave blank to finish.'); - if (previous) console.log('Leave the first answer blank to keep the current folders. Enter - to clear them.'); - while (true) { - const answer = (await ask('Project folder: ')).trim(); - if (!answer) { if (!roots.length && previous) roots.push(...previous.roots); break; } - if (answer === '-' && !roots.length) break; - const path = await realpath(resolve(answer === '~' ? homedir() : answer.startsWith('~/') ? join(homedir(), answer.slice(2)) : answer)); - if (!(await stat(path)).isDirectory()) throw new Error('Choose an existing project folder.'); - if (!roots.includes(path)) roots.push(path); - } - if (!windows) { - console.log('Experimental Codex desktop control lets linked clients send messages and interrupt chats. App updates may break it.'); - const answer = (await ask(`Enable desktop control? [${previous?.desktopWrite ? 'Y/n' : 'y/N'}]: `)).trim().toLowerCase(); - if (answer && !['y', 'yes', 'n', 'no'].includes(answer)) throw new Error('Answer yes or no.'); - desktopWrite = answer ? ['y', 'yes'].includes(answer) : previous?.desktopWrite ?? false; - } else desktopWrite = false; - } catch (error) { - if (closed.signal.aborted) throw new Error('Keep the terminal open to answer the install prompts, then run the installer again.'); - throw error; - } finally { input.close(); } - await mkdir(data, { recursive: true, mode: 0o700 }); - if (previous) { - const response = await fetch(`${server}/pairing`, { headers: { Authorization: `Bearer ${previous.token}` }, signal: AbortSignal.timeout(10_000) }); - if (!response.ok) throw new Error(`The saved pairing is unavailable (${response.status}). Check the dashboard before reinstalling.`); - } else { - await new Promise((accept, reject) => { - const child = spawn(process.execPath, [join(staged, 'relay.mjs'), 'pair', '--server', server, '--name', name, '--data-dir', data, - ...roots.flatMap((root) => ['--allow-root', root]), ...(desktopWrite ? ['--codex-desktop-write'] : [])], { stdio: 'inherit' }); - child.on('error', reject); - child.on('exit', (code) => code === 0 ? accept() : reject(new Error('Pairing stopped. Run the installer again for a new code.'))); - }); - previous = JSON.parse(await readFile(join(data, 'agent.json'), 'utf8')); - } - const config = { ...previous, roots, desktopWrite }; - const binaries = {}; - for (const cli of ['codex', 'claude']) { - try { - const found = windows ? execFileSync('where.exe', [cli], { encoding: 'utf8' }).trim().split(/\r?\n/)[0] : - execFileSync('/bin/sh', ['-c', 'command -v "$1"', 'sh', cli], { encoding: 'utf8' }).trim(); - if (found) binaries[cli] = found; - } catch { console.log(`${cli} was not found. Install it and rerun this installer to enable its chats.`); } - } - await stop(); - await writeFile(join(data, 'agent.json.pending'), JSON.stringify(config) + '\n', { mode: 0o600 }); - await rename(join(data, 'agent.json.pending'), join(data, 'agent.json')); - for (const file of ['relay.mjs', 'setup.mjs']) await copyFile(join(staged, file), join(base, file)); - const args = [join(base, 'relay.mjs'), 'run', '--data-dir', data, - ...Object.entries(binaries).flatMap(([cli, path]) => [`--${cli}-bin`, path])]; - const environment = Object.fromEntries(['PATH', 'CODEX_HOME', 'CLAUDE_CONFIG_DIR'].flatMap((key) => process.env[key] ? [[key, process.env[key]]] : [])); - environment.PATH = [base, environment.PATH].filter(Boolean).join(delimiter); - if (windows) { - const runner = join(base, 'run.ps1'); - await writeFile(runner, "$ErrorActionPreference = 'Stop'\n" + Object.entries(environment).map(([key, value]) => `$env:${key} = ${psQuote(value)}`).join('\n') + - `\n& ${psQuote(process.execPath)} ${args.map(psQuote).join(' ')} *>> ${psQuote(join(base, 'agent.log'))}\nexit $LASTEXITCODE\n`); - ps(`$ErrorActionPreference = 'Stop' -$user = [Security.Principal.WindowsIdentity]::GetCurrent().Name -$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument ${psQuote(`-NoProfile -NonInteractive -WindowStyle Hidden -ExecutionPolicy Bypass -File "${runner}"`)} -$trigger = New-ScheduledTaskTrigger -AtLogOn -User $user -$principal = New-ScheduledTaskPrincipal -UserId $user -LogonType Interactive -RunLevel Limited -$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit ([TimeSpan]::Zero) -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 1) -MultipleInstances IgnoreNew -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -Register-ScheduledTask -TaskName ${psQuote(task)} -Action $action -Trigger $trigger -Principal $principal -Settings $settings -Force | Out-Null -Start-ScheduledTask -TaskName ${psQuote(task)} -if ((Get-ScheduledTask -TaskName ${psQuote(task)}).State -eq 'Disabled') { throw 'Enable the Agent Relay task and run the installer again.' }`); - await writeFile(join(base, 'agent-relay.cmd'), `@echo off\r\n"${process.execPath}" "${join(base, 'setup.mjs')}" %*\r\n`); - } else { - await mkdir(dirname(unit), { recursive: true }); - if (mac) { - const xml = (text) => text.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"').replaceAll("'", '''); - await writeFile(unit, `\n\n -Labelnet.paperclover.agent-relay -ProgramArguments${[process.execPath, ...args].map((arg) => `${xml(arg)}`).join('')} -EnvironmentVariables${Object.entries(environment).map(([key, value]) => `${key}${xml(value)}`).join('')} -RunAtLoadKeepAliveThrottleInterval30 -StandardOutPath${xml(join(base, 'agent.log'))} -StandardErrorPath${xml(join(base, 'agent.log'))} -\n`); - execFileSync('launchctl', ['bootstrap', domain, unit], { stdio: 'inherit' }); - execFileSync('launchctl', ['print', `${domain}/net.paperclover.agent-relay`], { stdio: 'ignore' }); - } else { - const quote = (text) => '"' + text.replaceAll('\\', '\\\\').replaceAll('"', '\\"').replaceAll('\n', '\\n').replaceAll('\r', '\\r').replaceAll('%', '%%') + '"'; - await writeFile(unit, `[Unit]\nDescription=Agent Relay\n\n[Service]\nExecStart=${[process.execPath, ...args].map((arg) => quote(arg).replaceAll('$', '$$')).join(' ')}\n` + - Object.entries(environment).map(([key, value]) => `Environment=${quote(`${key}=${value}`)}`).join('\n') + - '\nRestart=on-failure\nRestartSec=30\nUMask=0077\n\n[Install]\nWantedBy=default.target\n'); - execFileSync('systemctl', ['--user', 'daemon-reload']); - execFileSync('systemctl', ['--user', 'enable', '--now', 'agent-relay.service'], { stdio: 'inherit' }); - execFileSync('systemctl', ['--user', 'is-active', '--quiet', 'agent-relay.service']); - } - await writeFile(join(base, 'agent-relay'), `#!/bin/sh\nexec ${shellQuote(process.execPath)} ${shellQuote(join(base, 'setup.mjs'))} "$@"\n`, { mode: 0o700 }); - } - console.log(`Installed. Agent Relay starts at login.\nOpen ${server}/mcp/settings/agents and check that ${name} is online.`); - const manage = join(base, windows ? 'agent-relay.cmd' : 'agent-relay'); - console.log(`Check startup: ${windows ? '& ' + psQuote(manage) : shellQuote(manage)} status\nUse start, stop, or uninstall in place of status.`); -} - -main().catch((error) => { console.error(error.message); process.exitCode = 1; }); diff --git a/dashboard/agent/source.json b/dashboard/agent/source.json deleted file mode 100644 index 29bfe76f9b9f83bc636e5743338a74fa964c8f16..0000000000000000000000000000000000000000 --- a/dashboard/agent/source.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "source": "../../../agent-relay", - "entry": "src/agent.ts" -} diff --git a/dashboard/src/main.rs b/dashboard/src/main.rs index 8fa2d29c28d3041b60ff81ff1fbf1afe74e6853f..000fcd65aad75acc5c49626d195914aed20b1d83 100644 --- a/dashboard/src/main.rs +++ b/dashboard/src/main.rs @@ -14,7 +14,6 @@ mod index; mod mcp; mod observability; mod oidc; -mod relay; mod shale; mod shale_page; mod shares; @@ -94,7 +93,6 @@ impl Document { struct App { auth: auth::Store, mcp: mcp::Store, - relay: relay::Broker, shale: shale::Backend, http: reqwest::Client, shale_http: reqwest::Client, @@ -538,7 +536,6 @@ async fn main() -> std::result::Result<(), Box> { auth, mcp: mcp::Store::new(&PathBuf::from(env("SNOWGLOBE_DATA_DIR", "data")), &origin) .map_err(|error| std::io::Error::other(error.message))?, - relay: relay::Broker::default(), shale: shale::Backend::new( &env( "SNOWGLOBE_SHALE_URL", @@ -596,7 +593,6 @@ async fn main() -> std::result::Result<(), Box> { .merge(observability::router(app.clone())) .merge(computer::router(app.clone())) .merge(shale::router(app.clone())) - .merge(relay::router(app.clone())) .fallback_service( ServeDir::new(&dist).fallback(ServeFile::new(format!("{dist}/index.html"))), ) diff --git a/dashboard/src/mcp.rs b/dashboard/src/mcp.rs index 17665794e073f3d88d99a9baab72a3540b0b688c..2994dd2f9372ce7711043620207b522c576b4500 100644 --- a/dashboard/src/mcp.rs +++ b/dashboard/src/mcp.rs @@ -14,11 +14,6 @@ const CATALOGS: &[(&str, &str, &[&str])] = &[ "Logs and traces", &["observability:read", "offline_access"], ), - ( - "agents", - "Local agents", - &["sessions:read", "sessions:write", "offline_access"], - ), ( "shale", "Shale", @@ -329,6 +324,12 @@ impl Store { } _ => return Err(fail("unsupported_grant_type")), }; + if !CATALOGS + .iter() + .any(|(id, _, _)| grant["resource"] == self.resource(id)) + { + return Err(fail("invalid_grant")); + } Ok((key, grant)) } fn oauth( @@ -606,13 +607,6 @@ pub fn public(path: &str) -> bool { path == format!("/mcp/{id}") || path.starts_with(&format!("/mcp/{id}/")) }) || path.starts_with("/.well-known/oauth-") - || path == "/pairing" - || path == "/agent/connect" - || matches!( - path, - "/agent/install.sh" | "/agent/install.ps1" | "/agent/setup.mjs" | "/agent/relay.mjs" - ) - || path.starts_with("/api/v1/") } pub async fn oauth(State(app): State>, request: Request) -> Response { if request.uri().path().starts_with("/oauth/shale/") { @@ -720,19 +714,6 @@ pub async fn manage( if parts == ["shale"] { return shale::manage(app, method, &owner, &body).await; } - if parts == ["relay", "live"] && method == Method::GET { - let owner = owner_id.to_owned(); - let stream = WatchStream::new(app.relay.changes.subscribe()).map(move |_| { - let machines = relay::machines(&app.mcp.db.lock().unwrap(), &owner) - .map(|machines| app.relay.view(machines, None)); - machines - .map(|machines| Event::default().json_data(machines).unwrap()) - .map_err(|error| std::io::Error::other(error.message)) - }); - return Ok(Sse::new(stream) - .keep_alive(axum::response::sse::KeepAlive::default()) - .into_response()); - } let consent_resource = if let ["connections", id] = parts && method != Method::DELETE { @@ -762,21 +743,18 @@ pub async fn manage( } else { String::new() }; - let agent_consent = consent_resource == app.mcp.resource("agents"); let shale_consent = consent_resource == app.mcp.resource("shale"); let catalog = CATALOGS .iter() .find(|(id, _, _)| consent_resource == app.mcp.resource(id)) .map(|(id, _, _)| *id); + if !consent_resource.is_empty() && catalog.is_none() { + return Err(Error::new(404, "No connector here.")); + } let mut linked = true; let mut resource_error = None; let resources: Vec = if body["deny"] == true { Vec::new() - } else if agent_consent { - relay::machines(&app.mcp.db.lock().unwrap(), owner_id)? - .into_iter() - .map(|m| json!({"id":m["id"],"name":m["name"]})) - .collect() } else if shale_consent { let available = if body["resources"] == "all" { shale::verified_session(&app, owner_id) @@ -824,19 +802,18 @@ pub async fn manage( [] if method == Method::GET => { let mut grants = list(&tx, "grant:")?; grants.retain(|g| g["user"] == owner_id); - let machines = relay::machines(&tx, owner_id)?; - let connections = grants.iter().map(|grant| { - let name = if grant["client"].is_null() {grant["name"].clone()} else {get(&tx, &format!("client:{}", string(&grant["client"])))?["client_name"].clone()}; - let resources = if grant["resource"] == app.mcp.resource("shale") && grant["resources"] == "all" {json!("all")} else {json!(array(&grant["resources"]).iter().map(|id| if grant["resource"] == app.mcp.resource("agents") {machines.iter().find(|m| m["id"] == *id).map(|m| m["name"].clone()).unwrap_or_else(|| json!("Unlinked machine"))} else {id.clone()}).collect::>())}; - let catalog = CATALOGS.iter().find(|(id, _, _)| grant["resource"] == app.mcp.resource(id)).map(|(id, _, _)| *id); - Ok(json!({"id":grant["id"],"name":name,"catalog":catalog,"resources":resources,"scopes":grant["scopes"],"createdAt":grant["createdAt"]})) + let connections = grants.iter().filter_map(|grant| { + let (catalog, _, _) = CATALOGS.iter().find(|(id, _, _)| grant["resource"] == app.mcp.resource(id))?; + Some(get(&tx, &format!("client:{}", string(&grant["client"]))).map(|client| { + json!({"id":grant["id"],"name":client["client_name"],"catalog":catalog,"resources":grant["resources"],"scopes":grant["scopes"],"createdAt":grant["createdAt"]}) + })) }).collect::>>()?; let shale = get(&tx, &format!("shale-session:{owner_id}"))?; let catalogs: Vec<_> = CATALOGS .iter() .map(|(id, name, _)| json!({"id":id,"name":name,"endpoint":app.mcp.resource(id)})) .collect(); - json!({"catalogs":catalogs,"connections":connections,"machines":app.relay.view(machines,None),"shale":if shale["origin"] != app.shale.origin.as_str() {Value::Null} else {json!({"linkedAt":shale["linkedAt"]})}}) + json!({"catalogs":catalogs,"connections":connections,"shale":if shale["origin"] != app.shale.origin.as_str() {Value::Null} else {json!({"linkedAt":shale["linkedAt"]})}}) } ["consent", id] if method == Method::GET || method == Method::POST => { let key = format!("pending:{}", hash(id)); @@ -897,10 +874,7 @@ pub async fn manage( )); } let grant_id = uuid::Uuid::new_v4().to_string(); - let mut grant = json!({"id":grant_id,"user":owner_id,"client":pending["client"],"resource":pending["resource"],"scopes":pending["scopes"],"resources":chosen,"createdAt":now()}); - if agent_consent { - grant["targets"] = json!(chosen); - } + let grant = json!({"id":grant_id,"user":owner_id,"client":pending["client"],"resource":pending["resource"],"scopes":pending["scopes"],"resources":chosen,"createdAt":now()}); put(&tx, &format!("grant:{grant_id}"), &grant, 0)?; let code = secret(); pending["grant"] = json!(grant_id); @@ -917,7 +891,6 @@ pub async fn manage( json!({"redirect":target.as_str()}) } } - ["relay", rest @ ..] => relay::manage(&app, &tx, rest, method, owner_id, &body)?, ["connections", id] if method == Method::GET || method == Method::POST || method == Method::DELETE => { @@ -940,9 +913,6 @@ pub async fn manage( } let chosen = chosen_resources(&body, &resources, shale_consent)?; grant["resources"] = json!(chosen); - if agent_consent { - grant["targets"] = json!(chosen); - } tx.execute( "UPDATE records SET value=? WHERE key=?", rusqlite::params![grant.to_string(), key], @@ -953,9 +923,6 @@ pub async fn manage( _ => return Err(Error::new(404, "No endpoint here.")), }; tx.commit()?; - if matches!(parts, ["relay", "pair"] | ["relay", "machines", _]) { - app.relay.changes.send_replace(()); - } Ok(if value.is_null() { StatusCode::NO_CONTENT.into_response() } else { @@ -1069,7 +1036,7 @@ mod tests { json!({"client_name":"Codex","redirect_uris":[uri]})).unwrap()).await; let mut request = fields(json!({"response_type":"code","client_id":client["client_id"],"state":"fixture-state", "code_challenge":"AdJpfUsRa6XMg54f2khmgo2onMwePN3vmU9DOOoaTzo","code_challenge_method":"S256", - "redirect_uri":uri,"scope":"observability:read offline_access sessions:read sessions:write shale:read shale:write computer:read computer:control"})); + "redirect_uri":uri,"scope":"observability:read offline_access shale:read shale:write computer:read computer:control"})); for (catalog, _, allowed) in CATALOGS { request.insert("resource".into(), fixture.store.resource(catalog)); let response = fixture.store.oauth("authorize", &Method::GET, &request, &HeaderMap::new(), Value::Null).unwrap(); @@ -1102,6 +1069,29 @@ mod tests { request.insert("redirect_uri".into(), uri.replace("H9rAWI-8eQbv", "different")); assert_eq!(fixture.store.oauth("authorize", &Method::GET, &request, &HeaderMap::new(), Value::Null).unwrap_err().message, "invalid_redirect_uri"); } + #[test] + fn removed_catalogs_cannot_authorize_or_exchange_tokens() { + let fixture = Fixture::new(); + let mut input = fixture.code("one", "owned-code"); + let resource = fixture.store.resource("agents"); + let tokens = { + let db = fixture.store.db.lock().unwrap(); + let key = format!("code:{}", hash("owned-code")); + let mut code = get(&db, &key).unwrap(); + let mut grant = get(&db, &format!("grant:{}", string(&code["grant"]))).unwrap(); + code["resource"] = json!(resource); + grant["resource"] = json!(resource); + put(&db, &key, &code, 300).unwrap(); + put(&db, &format!("grant:{}", string(&grant["id"])), &grant, 0).unwrap(); + fixture.store.issue(&db, &grant).unwrap() + }; + input.insert("resource".into(), resource.clone()); + let refresh = fields(json!({"grant_type":"refresh_token","client_id":"one","refresh_token":tokens["refresh_token"]})); + for request in [&input, &refresh] { + assert_eq!(fixture.store.oauth("token", &Method::POST, request, &HeaderMap::new(), Value::Null).unwrap_err().message, "invalid_grant"); + } + assert_eq!(fixture.store.oauth("authorize", &Method::GET, &fields(json!({"resource":resource})), &HeaderMap::new(), Value::Null).unwrap_err().message, "invalid_target"); + } #[tokio::test] async fn code_binds_pkce_client_redirect_and_audience_without_consuming_on_failure() { let fixture = Fixture::new(); diff --git a/dashboard/src/relay.rs b/dashboard/src/relay.rs deleted file mode 100644 index 7df6640dc68eb705c46ccd6f4aaf29cdefd87dd4..0000000000000000000000000000000000000000 --- a/dashboard/src/relay.rs +++ /dev/null @@ -1,1111 +0,0 @@ -use crate::*; -use axum::extract::ws::{Message, WebSocket, WebSocketUpgrade}; -use futures::SinkExt; -use mcp::{delete, get, hash, list, put, secret}; -use rmcp::{ - ErrorData, RoleServer, ServerHandler, - model::{ - CacheScope, CallToolRequestParams, CallToolResponse, CallToolResult, ContentBlock, - ListToolsResult, PaginatedRequestParams, ServerCapabilities, ServerConfig, Tool, - ToolAnnotations, - }, - service::RequestContext, -}; -use tokio::sync::{mpsc, oneshot}; - -pub struct Broker { - connections: Mutex>>, - pending_slots: Semaphore, - pub(crate) changes: watch::Sender<()>, -} -impl Default for Broker { - fn default() -> Self { - Self { - connections: Mutex::new(HashMap::new()), - pending_slots: Semaphore::new(128), - changes: watch::channel(()).0, - } - } -} -struct Connection { - messages: mpsc::Sender, - pending: Mutex>>>, - closed: watch::Sender, -} -struct Pending { - connection: Arc, - id: String, -} -impl Drop for Pending { - fn drop(&mut self) { - self.connection.pending.lock().unwrap().remove(&self.id); - } -} -fn unknown() -> Error { - Error::new( - 409, - "Command outcome is unknown. Read the thread before retrying.", - ) -} -impl Broker { - pub fn disconnect(&self, id: &str) { - if let Some(connection) = self.connections.lock().unwrap().remove(id) { - self.changes.send_replace(()); - connection.closed.send_replace(true); - for (_, response) in connection.pending.lock().unwrap().drain() { - let _ = response.send(Err(unknown())); - } - } - } - fn remove(&self, id: &str, connection: &Arc) { - let mut connections = self.connections.lock().unwrap(); - if connections - .get(id) - .is_some_and(|current| Arc::ptr_eq(current, connection)) - { - connections.remove(id); - self.changes.send_replace(()); - } - connection.closed.send_replace(true); - for (_, response) in connection.pending.lock().unwrap().drain() { - let _ = response.send(Err(unknown())); - } - } - pub fn view(&self, machines: Vec, grant: Option<&Value>) -> Vec { - let connections = self.connections.lock().unwrap(); - machines - .into_iter() - .filter(|machine| grant.is_none_or(|g| array(&g["resources"]).contains(&machine["id"]))) - .map(|mut machine| { - machine.as_object_mut().unwrap().remove("tokenHash"); - machine.as_object_mut().unwrap().remove("user"); - machine["online"] = json!(connections.contains_key(string(&machine["id"]))); - if let Some(grant) = grant { - machine["selected"] = json!(array(&grant["targets"]).contains(&machine["id"])); - } - machine - }) - .collect() - } - async fn dispatch( - &self, - store: &mcp::Store, - grant_id: &str, - machine_id: &str, - method: &str, - params: Value, - deadline: Duration, - ) -> Result { - let params = command(method, params)?; - let (pending, receive, slot) = { - let db = store.db.lock().unwrap(); - let grant = get(&db, &format!("grant:{grant_id}"))?; - let machine = get(&db, &format!("machine:{machine_id}"))?; - if grant.is_null() - || grant["resource"] != store.resource("agents") - || machine.is_null() - || machine["user"] != grant["user"] - || !array(&grant["resources"]).iter().any(|id| id == machine_id) - { - return Err(Error::new( - 403, - "Choose a machine granted to this connection.", - )); - } - let scope = if matches!(method, "send_message" | "interrupt_thread" | "start_thread") { - "sessions:write" - } else { - "sessions:read" - }; - if !array(&grant["scopes"]).iter().any(|s| s == scope) { - return Err(Error::new( - 403, - "This connection has read access only. Connect again to request control.", - )); - } - let slot = self.pending_slots.try_acquire().map_err(|_| { - Error::new( - 429, - "The relay has too many pending commands. Try again shortly.", - ) - })?; - let connection = self - .connections - .lock() - .unwrap() - .get(machine_id) - .cloned() - .ok_or_else(|| Error::new(409, "Machine is offline. Start its local agent."))?; - let (send, receive) = oneshot::channel(); - let id = uuid::Uuid::new_v4().to_string(); - { - let mut pending = connection.pending.lock().unwrap(); - if *connection.closed.borrow() { - return Err(Error::new( - 409, - "Machine is offline. Start its local agent.", - )); - } - if pending.len() >= 8 { - return Err(Error::new( - 429, - "Machine has eight pending commands. Wait for one to finish.", - )); - } - pending.insert(id.clone(), send); - } - let pending = Pending { - connection: connection.clone(), - id: id.clone(), - }; - let frame = json!({"id":id,"method":method,"params":params}).to_string(); - if frame.len() > 256 * 1024 { - return Err(Error::new( - 413, - "Send a shorter message. Local agent commands are limited to 256 KB.", - )); - } - let frame = Message::Text(frame.into()); - if connection.messages.try_send(frame).is_err() { - return Err(unknown()); - } - (pending, receive, slot) - }; - let result = tokio::time::timeout(deadline, receive) - .await - .map_err(|_| unknown())? - .map_err(|_| unknown())?; - drop(pending); - drop(slot); - result - } -} -pub(crate) fn machines(db: &rusqlite::Connection, user: &str) -> Result> { - Ok(list(db, "machine:")? - .into_iter() - .filter(|m| m["user"] == user) - .collect()) -} -fn device(db: &rusqlite::Connection, headers: &HeaderMap) -> Result { - let token = headers - .get("authorization") - .and_then(|v| v.to_str().ok()) - .and_then(|s| s.strip_prefix("Bearer ")) - .filter(|s| !s.is_empty() && s.len() <= 256) - .ok_or_else(|| Error::new(401, "Pair this machine again."))?; - let fingerprint = hash(token); - list(db, "machine:")? - .into_iter() - .find(|machine| { - string(&machine["tokenHash"]) - .as_bytes() - .ct_eq(fingerprint.as_bytes()) - .unwrap_u8() - == 1 - }) - .ok_or_else(|| Error::new(401, "Pair this machine again.")) -} -fn name(value: &Value) -> Result<&str> { - value - .as_str() - .filter(|s| !s.trim().is_empty() && s.encode_utf16().count() <= 100) - .ok_or_else(|| Error::new(400, "Enter a name up to 100 characters.")) -} -pub(crate) fn manage( - app: &App, - db: &rusqlite::Connection, - parts: &[&str], - method: &Method, - owner: &str, - body: &Value, -) -> Result { - match parts { - ["pair"] if method == Method::POST => { - let code = body["code"] - .as_str() - .filter(|s| !s.is_empty() && s.len() <= 40) - .ok_or_else(|| Error::new(400, "Enter the code from your local agent."))?; - let code: String = code - .chars() - .filter(|c| *c != '-' && !c.is_whitespace()) - .flat_map(char::to_uppercase) - .collect(); - let key = format!("pair:{}", hash(&code)); - let pairing = get(db, &key)?; - if pairing.is_null() { - return Err(Error::new( - 410, - "This code expired or was used. Start pairing again.", - )); - } - if machines(db, owner)?.len() >= 128 { - return Err(Error::new( - 409, - "Unlink an unused machine before adding another.", - )); - } - let id = uuid::Uuid::new_v4().to_string(); - let machine = json!({"id":id,"user":owner,"name":pairing["name"],"platform":pairing["platform"],"tokenHash":pairing["tokenHash"]}); - put(db, &format!("machine:{id}"), &machine, 0)?; - delete(db, &key)?; - Ok(json!(app.relay.view(vec![machine], None).remove(0))) - } - ["machines", id] if method == Method::DELETE || method == Method::PATCH => { - let mut machine = get(db, &format!("machine:{id}"))?; - if machine["user"] != owner { - return Err(Error::new(404, "No linked machine with that ID.")); - } - if method == Method::DELETE { - delete(db, &format!("machine:{id}"))?; - app.relay.disconnect(id); - Ok(Value::Null) - } else { - machine["name"] = json!(name(&body["name"])?); - put(db, &format!("machine:{id}"), &machine, 0)?; - Ok(json!(app.relay.view(vec![machine], None).remove(0))) - } - } - ["keys"] if method == Method::POST => { - let available = machines(db, owner)?; - let resources = selection( - &body["resources"], - &available - .iter() - .map(|m| m["id"].clone()) - .collect::>(), - )?; - let name = name(&body["name"])?; - let write = body - .get("write") - .map(|v| { - v.as_bool() - .ok_or_else(|| Error::new(400, "Choose read access or control.")) - }) - .transpose()? - .unwrap_or(false); - if list(db, "grant:")? - .iter() - .filter(|g| g["user"] == owner) - .count() - >= 256 - { - return Err(Error::new( - 409, - "Revoke an unused connection before adding another.", - )); - } - let id = uuid::Uuid::new_v4().to_string(); - let grant = json!({"id":id,"user":owner,"name":name,"client":null,"resource":app.mcp.resource("agents"),"scopes":if write {json!(["sessions:read","sessions:write"])} else {json!(["sessions:read"])},"resources":resources,"targets":resources,"createdAt":now()}); - let key = format!("ar_{}", secret()); - put(db, &format!("grant:{id}"), &grant, 0)?; - put( - db, - &format!("access:{}", hash(&key)), - &json!({"grant":id,"resource":grant["resource"]}), - 0, - )?; - Ok(json!({"key":key,"id":id})) - } - _ => Err(Error::new(404, "No endpoint here.")), - } -} -fn selection(value: &Value, allowed: &[Value]) -> Result> { - let ids = value - .as_array() - .filter(|ids| !ids.is_empty() && ids.len() <= 128) - .ok_or_else(|| Error::new(400, "Choose at least one linked machine."))?; - if ids - .iter() - .enumerate() - .any(|(i, id)| !allowed.contains(id) || ids[..i].contains(id)) - { - return Err(Error::new( - 403, - "Choose machines granted to this connection once each.", - )); - } - Ok(ids.clone()) -} -fn view(app: &App, grant_id: &str, targets: Option<&Value>) -> Result> { - let mut db = app.mcp.db.lock().unwrap(); - let tx = db.transaction()?; - let mut grant = get(&tx, &format!("grant:{grant_id}"))?; - if grant.is_null() || grant["resource"] != app.mcp.resource("agents") { - return Err(Error::new( - 401, - "This connection was revoked. Connect again.", - )); - } - let machines = machines(&tx, string(&grant["user"]))?; - if let Some(targets) = targets { - let allowed: Vec<_> = machines - .iter() - .filter(|m| array(&grant["resources"]).contains(&m["id"])) - .map(|m| m["id"].clone()) - .collect(); - grant["targets"] = json!(selection(targets, &allowed)?); - put(&tx, &format!("grant:{grant_id}"), &grant, 0)?; - } - let result = app.relay.view(machines, Some(&grant)); - tx.commit()?; - Ok(result) -} -fn schema(method: &str) -> Option { - let provider = json!({"type":"string","enum":["codex","claude"]}); - let id = json!({"type":"string","format":"uuid"}); - let message = json!({"type":"string","minLength":1,"maxLength":100000}); - let (properties, required) = match method { - "list_threads" => ( - json!({"provider":provider,"limit":{"type":"integer","minimum":1,"maximum":100,"default":30}}), - json!([]), - ), - "read_thread" => ( - json!({"provider":provider,"thread_id":id,"cursor":{"type":"string","minLength":1,"maxLength":512},"limit":{"type":"integer","minimum":1,"maximum":100,"default":20}}), - json!(["provider", "thread_id"]), - ), - "send_message" => ( - json!({"provider":provider,"thread_id":id,"message":message,"expected_turn_id":id}), - json!(["provider", "thread_id", "message"]), - ), - "interrupt_thread" => ( - json!({"provider":provider,"thread_id":id,"expected_turn_id":id}), - json!(["provider", "thread_id"]), - ), - "start_thread" => ( - json!({"provider":provider,"cwd":{"type":"string","minLength":1,"maxLength":4096},"message":message}), - json!(["provider", "cwd", "message"]), - ), - _ => return None, - }; - Some( - json!({"type":"object","properties":properties,"required":required,"additionalProperties":false}), - ) -} -fn command(method: &str, value: Value) -> Result { - let schema = schema(method) - .ok_or_else(|| Error::new(404, "Choose a session tool listed by this connector."))?; - let mut params = value - .as_object() - .cloned() - .ok_or_else(|| Error::new(400, "Use the fields listed for this tool."))?; - let properties = schema["properties"].as_object().unwrap(); - if params.keys().any(|key| !properties.contains_key(key)) - || array(&schema["required"]) - .iter() - .any(|key| !params.contains_key(string(key))) - { - return Err(Error::new(400, "Use the fields listed for this tool.")); - } - for (key, rule) in properties { - if !params.contains_key(key) && rule.get("default").is_some() { - params.insert(key.clone(), rule["default"].clone()); - } - let Some(value) = params.get(key) else { - continue; - }; - let valid = match string(&rule["type"]) { - "integer" => value.as_i64().is_some_and(|n| { - n >= rule["minimum"].as_i64().unwrap() && n <= rule["maximum"].as_i64().unwrap() - }), - "string" => value.as_str().is_some_and(|s| { - let length = s.encode_utf16().count() as u64; - rule["minLength"].as_u64().is_none_or(|n| length >= n) - && rule["maxLength"].as_u64().is_none_or(|n| length <= n) - && rule - .get("enum") - .is_none_or(|values| array(values).contains(value)) - && (rule["format"] != "uuid" - || uuid::Uuid::parse_str(s) - .is_ok_and(|id| id.to_string().eq_ignore_ascii_case(s))) - }), - _ => false, - }; - if !valid { - return Err(Error::new( - 400, - format!("Check {key} against the tool's fields."), - )); - } - } - Ok(json!(params)) -} -async fn pairing(State(app): State>, request: Request) -> Result { - let method = request.method().clone(); - let headers = request.headers().clone(); - let bytes = axum::body::to_bytes(request.into_body(), 4096) - .await - .map_err(|_| Error::new(400, "Enter a shorter machine name."))?; - let mut db = app.mcp.db.lock().unwrap(); - let tx = db.transaction()?; - let response = if method == Method::POST { - if list(&tx, "pair:")?.len() >= 256 { - return Err(Error::new( - 429, - "Too many pairing requests. Try again in ten minutes.", - )); - } - let body: Value = serde_json::from_slice(&bytes) - .map_err(|_| Error::new(400, "Enter a machine name and platform."))?; - let name = name(&body["name"])?; - let platform = body["platform"] - .as_str() - .filter(|s| s.encode_utf16().count() <= 100) - .ok_or_else(|| Error::new(400, "Enter a platform up to 100 characters."))?; - let token = secret(); - let code: String = rand::random::<[u8; 5]>() - .iter() - .map(|b| format!("{b:02X}")) - .collect(); - put( - &tx, - &format!("pair:{}", hash(&code)), - &json!({"name":name,"platform":platform,"tokenHash":hash(&token)}), - 600, - )?; - (StatusCode::CREATED, axum::Json(json!({"code":format!("{}-{}", &code[..5], &code[5..]),"token":token,"expires_in":600}))).into_response() - } else if method == Method::GET { - match device(&tx, &headers) { - Ok(machine) => axum::Json(json!({"machine_id":machine["id"]})).into_response(), - Err(_) => { - let token = headers - .get("authorization") - .and_then(|v| v.to_str().ok()) - .and_then(|s| s.strip_prefix("Bearer ")) - .filter(|s| !s.is_empty() && s.len() <= 256) - .ok_or_else(|| Error::new(401, "Start pairing from your local agent."))?; - if !list(&tx, "pair:")? - .iter() - .any(|p| p["tokenHash"] == hash(token)) - { - return Err(Error::new( - 410, - "This pairing expired. Start pairing again.", - )); - } - (StatusCode::ACCEPTED, axum::Json(json!({"pending":true}))).into_response() - } - } - } else { - return Err(Error::new(405, "Use GET or POST for pairing.")); - }; - tx.commit()?; - Ok(response) -} -async fn connect( - State(app): State>, - headers: HeaderMap, - ws: WebSocketUpgrade, -) -> Result { - if headers.contains_key("origin") { - return Err(Error::new(401, "Connect from your local agent.")); - } - let machine = device(&app.mcp.db.lock().unwrap(), &headers)?; - let id = string(&machine["id"]).to_owned(); - let (send, receive) = mpsc::channel(16); - let (closed, _) = watch::channel(false); - let connection = Arc::new(Connection { - messages: send, - pending: Mutex::new(HashMap::new()), - closed, - }); - { - let mut connections = app.relay.connections.lock().unwrap(); - if connections.contains_key(&id) { - return Err(Error::new( - 409, - "Another agent is connected. Stop it before starting a second copy.", - )); - } - if connections.len() >= 256 { - return Err(Error::new( - 503, - "Too many agents are connected. Try again later.", - )); - } - connections.insert(id.clone(), connection.clone()); - app.relay.changes.send_replace(()); - } - let (failure_app, failure_id, failure_connection) = - (app.clone(), id.clone(), connection.clone()); - Ok(ws - .max_frame_size(4 * 1024 * 1024) - .max_message_size(4 * 1024 * 1024) - .read_buffer_size(16 * 1024) - .write_buffer_size(0) - .max_write_buffer_size(256 * 1024) - .on_failed_upgrade(move |_| failure_app.relay.remove(&failure_id, &failure_connection)) - .on_upgrade(move |socket| session(app, id, connection, receive, socket))) -} -async fn session( - app: Arc, - id: String, - connection: Arc, - mut messages: mpsc::Receiver, - mut socket: WebSocket, -) { - let mut closed = connection.closed.subscribe(); - let connected = Message::Text( - json!({"type":"connected","machine_id":id}) - .to_string() - .into(), - ); - if tokio::time::timeout(Duration::from_secs(5), socket.send(connected)) - .await - .is_ok_and(|r| r.is_ok()) - { - let mut heartbeat = tokio::time::interval_at( - tokio::time::Instant::now() + Duration::from_secs(20), - Duration::from_secs(20), - ); - heartbeat.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); - let mut alive = true; - loop { - if *closed.borrow() - || !get(&app.mcp.db.lock().unwrap(), &format!("machine:{id}")) - .is_ok_and(|m| !m.is_null()) - { - break; - } - let outgoing = tokio::select! { - _ = closed.changed() => break, - outgoing = messages.recv() => match outgoing {Some(frame) => frame, None => break}, - _ = heartbeat.tick() => { - if !alive {break;} - alive = false; - Message::Ping(Bytes::new()) - }, - incoming = socket.recv() => { - match incoming { - Some(Ok(Message::Pong(_))) => alive = true, - Some(Ok(Message::Ping(bytes))) => { - if !tokio::time::timeout(Duration::from_secs(5), socket.send(Message::Pong(bytes))).await.is_ok_and(|r| r.is_ok()) {break;} - }, - Some(Ok(Message::Text(text))) => { - let reply = serde_json::from_str::(&text); - let Ok(reply) = reply else {break}; - let valid = reply.as_object().is_some_and(|fields| fields.keys().all(|k| ["id","result","error"].contains(&k.as_str()))) - && reply["id"].as_str().is_some_and(|s| uuid::Uuid::parse_str(s).is_ok()) - && (reply.get("result").is_some() != reply.get("error").is_some()) - && reply.get("error").is_none_or(|error| error.as_str().is_some_and(|s| s.encode_utf16().count() <= 2000)); - if !valid {break;} - if let Some(send) = connection.pending.lock().unwrap().remove(string(&reply["id"])) { - let result = if let Some(error) = reply["error"].as_str() {Err(Error::new(400, error))} else {Ok(reply["result"].clone())}; - let _ = send.send(result); - } - }, - _ => break, - } - continue; - } - }; - if !tokio::time::timeout(Duration::from_secs(5), socket.send(outgoing)) - .await - .is_ok_and(|r| r.is_ok()) - { - break; - } - } - } - app.relay.remove(&id, &connection); - let _ = tokio::time::timeout(Duration::from_secs(1), socket.close()).await; -} -async fn rest(State(app): State>, request: Request) -> Response { - let result: Result = async { - if request.headers().get("origin").is_some_and(|v| { - v.to_str().ok() != Some(app.mcp.origin.origin().ascii_serialization().as_str()) - }) { - return Err(Error::new(403, "Use the relay from its own origin.")); - } - let grant = app - .mcp - .authenticate(request.headers(), &app.mcp.resource("agents"))?; - if !mcp::active_owner(&app, &grant)? { - return Err(Error::new(401, "This connection's account is no longer authorized.")); - } - let id = string(&grant["id"]); - let method = request.method().clone(); - let path = request - .uri() - .path() - .trim_start_matches("/api/v1/") - .to_owned(); - let bytes = axum::body::to_bytes(request.into_body(), 256 * 1024) - .await - .map_err(|_| Error::new(400, "Send a shorter command."))?; - let body: Value = if bytes.is_empty() { - Value::Null - } else { - serde_json::from_slice(&bytes).map_err(|_| Error::new(400, "Use JSON command fields."))? - }; - let value = match path.split('/').collect::>().as_slice() { - ["machines"] if method == Method::GET => json!(view(&app, id, None)?), - ["targets"] if method == Method::PUT => json!(view(&app, id, Some(&body["machine_ids"]))?), - ["machines", machine, "commands"] if method == Method::POST => { - let method = body["method"] - .as_str() - .ok_or_else(|| Error::new(400, "Choose a session command."))?; - json!({"result":app.relay.dispatch(&app.mcp, id, machine, method, body["params"].clone(), Duration::from_secs(30)).await?}) - } - _ => return Err(Error::new(404, "No relay endpoint here.")), - }; - Ok(axum::Json(value).into_response()) - }.await; - match result { - Ok(response) => response, - Err(error) => (StatusCode::from_u16(error.status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR), axum::Json(json!({"error":if error.status >= 500 {"The relay couldn't answer. Check its dashboard and retry."} else {&error.message}}))).into_response(), - } -} -#[derive(Clone)] -struct Agents(Arc); -impl ServerHandler for Agents { - fn get_info(&self) -> ServerConfig { - ServerConfig::new(ServerCapabilities::builder().enable_tools().build()) - } - async fn list_tools( - &self, - _: Option, - _: RequestContext, - ) -> std::result::Result { - let tools = [ - ("list_machines", "List granted machines and their connection state."), - ("set_target_machines", "Select default machines for this connection."), - ("list_threads", "List recent Codex and Claude Code threads on selected machines."), - ("read_thread", "Read a thread and its current status."), - ("send_message", "Submit a message. Desktop control requires local opt-in and reopens unloaded chats, switching the selected desktop chat. Submission acknowledges delivery, not task completion."), - ("interrupt_thread", "Interrupt an agent-owned session or an opted-in Codex desktop turn."), - ("start_thread", "Start an agent-owned session under a locally allowed directory."), - ].into_iter().map(|(name, description)| { - let mut schema = schema(name).unwrap_or_else(|| if name == "set_target_machines" {json!({"type":"object","properties":{"machine_ids":{"type":"array","items":{"type":"string","format":"uuid"},"minItems":1,"maxItems":128,"uniqueItems":true}},"required":["machine_ids"],"additionalProperties":false})} else {json!({"type":"object","properties":{},"additionalProperties":false})}); - if !["list_machines","set_target_machines"].contains(&name) {schema["properties"]["machine_id"] = json!({"type":"string","format":"uuid"});} - let read = ["list_machines","list_threads","read_thread"].contains(&name); - Tool::new(name, description, schema.as_object().unwrap().clone()).with_annotations(ToolAnnotations::new().read_only(read).destructive(name == "interrupt_thread").idempotent(read)) - }).collect(); - Ok(ListToolsResult { - tools, - ttl_ms: Some(0), - cache_scope: Some(CacheScope::Private), - ..Default::default() - }) - } - async fn call_tool( - &self, - request: CallToolRequestParams, - context: RequestContext, - ) -> std::result::Result { - let result: Result = async { - let grant = &context - .extensions - .get::() - .and_then(|parts| parts.extensions.get::()) - .ok_or_else(|| Error::new(401, "This connection expired. Connect again."))? - .0; - let mut arguments = request.arguments.unwrap_or_default(); - let id = string(&grant["id"]); - match request.name.as_ref() { - "list_machines" if arguments.is_empty() => Ok(CallToolResult::structured( - json!({"machines":view(&self.0, id, None)?}), - )), - "set_target_machines" - if arguments.len() == 1 && arguments.contains_key("machine_ids") => - { - Ok(CallToolResult::structured( - json!({"machines":view(&self.0, id, arguments.get("machine_ids"))?}), - )) - } - method => { - let explicit = arguments.remove("machine_id"); - let machines = view(&self.0, id, None)?; - let targets: Vec<_> = if let Some(explicit) = explicit { - let explicit = explicit - .as_str() - .ok_or_else(|| Error::new(400, "Choose a machine ID."))?; - if !machines.iter().any(|m| m["id"] == explicit) { - return Err(Error::new(403, "Choose a granted machine.")); - } - vec![explicit.to_owned()] - } else { - machines - .iter() - .filter(|m| m["selected"] == true) - .map(|m| string(&m["id"]).to_owned()) - .collect() - }; - if targets.is_empty() || method != "list_threads" && targets.len() != 1 { - return Err(Error::new( - 400, - "Select one machine or supply machine_id for this command.", - )); - } - let params = command(method, json!(arguments))?; - let mut replies: futures::stream::FuturesUnordered<_> = targets.into_iter().map(|machine| { - let params = params.clone(); - async move { - match self - .0 - .relay - .dispatch( - &self.0.mcp, - id, - &machine, - method, - params, - Duration::from_secs(30), - ) - .await - { - Ok(result) => json!({"machine_id":machine,"result":result}), - Err(error) => json!({"machine_id":machine,"error":error.message}), - } - } - }).collect(); - let mut results = Vec::new(); - let mut bytes = 0; - while let Some(reply) = futures::StreamExt::next(&mut replies).await { - bytes += reply.to_string().len(); - if bytes > 16*1024*1024 {return Err(Error::new(413, "Machine replies exceed 16 MB. Select fewer machines or request fewer messages."));} - results.push(reply); - } - let errors = results.iter().any(|r| r.get("error").is_some()); - let mut result = CallToolResult::structured(json!({"results":results})); - result.is_error = Some(errors); - Ok(result) - } - } - } - .await; - Ok(match result { - Ok(result) => result, - Err(error) => CallToolResult::error(vec![ContentBlock::text(if error.status >= 500 { - "The relay couldn't answer. Check its dashboard and retry.".to_owned() - } else { - error.message - })]), - } - .into()) - } -} -async fn installer(State(app): State>, request: Request) -> Response { - let origin = app.mcp.origin.origin().ascii_serialization(); - let source = if request.uri().path().ends_with(".ps1") { - include_str!("../agent/install.ps1") - .replace("__SERVER__", &format!("'{}'", origin.replace('\'', "''"))) - } else { - include_str!("../agent/install.sh").replace( - "__SERVER__", - &format!("'{}'", origin.replace('\'', "'\\''")), - ) - }; - ([("content-type", "text/plain; charset=utf-8")], source).into_response() -} -pub fn router(app: Arc) -> Router { - let state = app.clone(); - let expected_host = - app.mcp.origin[url::Position::BeforeHost..url::Position::AfterPort].to_owned(); - let agent = env("SNOWGLOBE_AGENT_DIR", "agent"); - Router::new() - .route("/pairing", any(pairing)) - .route("/agent/connect", any(connect)) - .route("/agent/install.sh", axum::routing::get(installer)) - .route("/agent/install.ps1", axum::routing::get(installer)) - .route_service( - "/agent/setup.mjs", - ServeFile::new(format!("{agent}/setup.mjs")), - ) - .route_service( - "/agent/relay.mjs", - ServeFile::new(format!("{agent}/relay.mjs")), - ) - .route("/api/v1/{*path}", any(rest)) - .with_state(app.clone()) - .merge(mcp::router(app, "agents", move || { - Ok(Agents(state.clone())) - })) - .layer(axum::middleware::from_fn( - move |request: Request, next: axum::middleware::Next| { - let host = expected_host.clone(); - async move { - if request.headers().get("host").and_then(|v| v.to_str().ok()) != Some(&host) { - return StatusCode::MISDIRECTED_REQUEST.into_response(); - } - next.run(request).await - } - }, - )) -} - -#[cfg(test)] -mod tests { - use super::*; - struct Fixture { - store: Arc, - broker: Arc, - path: PathBuf, - machine: String, - grant: String, - } - impl Fixture { - fn new(write: bool) -> Self { - let path = std::env::temp_dir() - .canonicalize() - .unwrap() - .join(format!("snowglobe-relay-test-{}", uuid::Uuid::new_v4())); - let store = Arc::new(mcp::Store::new(&path, "https://globe.snowglobe.test").unwrap()); - let machine = uuid::Uuid::new_v4().to_string(); - let grant = uuid::Uuid::new_v4().to_string(); - { - let db = store.db.lock().unwrap(); - put(&db, &format!("machine:{machine}"), &json!({"id":machine,"user":"owner","name":"Fixture","tokenHash":hash("device-secret")}), 0).unwrap(); - put(&db, &format!("grant:{grant}"), &json!({"id":grant,"user":"owner","resource":store.resource("agents"),"resources":[machine],"scopes":if write {json!(["sessions:read","sessions:write"])} else {json!(["sessions:read"])}}), 0).unwrap(); - } - Self { - store, - broker: Arc::new(Broker::default()), - path, - machine, - grant, - } - } - fn connect(&self) -> (Arc, mpsc::Receiver) { - let (messages, receive) = mpsc::channel(16); - let (closed, _) = watch::channel(false); - let connection = Arc::new(Connection { - messages, - closed, - pending: Mutex::new(HashMap::new()), - }); - self.broker - .connections - .lock() - .unwrap() - .insert(self.machine.clone(), connection.clone()); - (connection, receive) - } - fn dispatch(&self, duration: Duration) -> tokio::task::JoinHandle> { - let (store, broker, machine, grant) = ( - self.store.clone(), - self.broker.clone(), - self.machine.clone(), - self.grant.clone(), - ); - tokio::spawn(async move { - broker - .dispatch( - &store, - &grant, - &machine, - "list_threads", - json!({}), - duration, - ) - .await - }) - } - } - impl Drop for Fixture { - fn drop(&mut self) { - std::fs::remove_dir_all(&self.path).unwrap(); - } - } - #[test] - fn command_fields_defaults_and_provider_boundaries() { - assert_eq!( - command("list_threads", json!({})).unwrap(), - json!({"limit":30}) - ); - let id = uuid::Uuid::new_v4().to_string(); - assert_eq!( - command("read_thread", json!({"provider":"claude","thread_id":id})).unwrap()["limit"], - 20 - ); - assert_eq!( - command( - "read_thread", - json!({"provider":"codex","thread_id":id,"cursor":"page"}) - ) - .unwrap()["cursor"], - "page" - ); - for (method, params) in [ - ( - "read_thread", - json!({"provider":"codex","thread_id":id,"cursor":""}), - ), - ( - "read_thread", - json!({"provider":"codex","thread_id":id,"cursor":"x".repeat(513)}), - ), - ( - "read_thread", - json!({"provider":"codex","thread_id":"not-a-uuid"}), - ), - ( - "read_thread", - json!({"provider":"native-chat","thread_id":id}), - ), - ("list_threads", json!({"limit":101})), - ("list_threads", json!({"limit":1.5})), - ("list_threads", json!({"approval_response":true})), - ( - "send_message", - json!({"provider":"codex","thread_id":id,"message":""}), - ), - ("start_thread", json!({"provider":"claude","cwd":"/tmp"})), - ( - "interrupt_thread", - json!({"provider":"codex","thread_id":id,"expected_turn_id":1}), - ), - ("approve_tool", json!({})), - ] { - assert!(command(method, params).is_err(), "{method}"); - } - } - #[tokio::test] - async fn dispatch_uses_current_owner_grant_and_control_scope() { - let fixture = Fixture::new(false); - let (connection, mut receive) = fixture.connect(); - let task = fixture.dispatch(Duration::from_secs(2)); - let Message::Text(frame) = receive.recv().await.unwrap() else { - panic!() - }; - let frame: Value = serde_json::from_str(&frame).unwrap(); - assert_eq!(frame["params"], json!({"limit":30})); - connection - .pending - .lock() - .unwrap() - .remove(string(&frame["id"])) - .unwrap() - .send(Ok(json!({"threads":[]}))) - .unwrap(); - assert_eq!(task.await.unwrap().unwrap(), json!({"threads":[]})); - let refused = fixture - .broker - .dispatch( - &fixture.store, - &fixture.grant, - &fixture.machine, - "start_thread", - json!({"provider":"codex","cwd":"/tmp","message":"owned fixture"}), - Duration::from_secs(2), - ) - .await - .unwrap_err(); - assert_eq!(refused.status, 403); - assert!(receive.try_recv().is_err()); - let foreign = uuid::Uuid::new_v4().to_string(); - { - let db = fixture.store.db.lock().unwrap(); - let mut grant = get(&db, &format!("grant:{}", fixture.grant)).unwrap(); - grant["resources"] = json!([foreign]); - put(&db, &format!("grant:{}", fixture.grant), &grant, 0).unwrap(); - put( - &db, - &format!("machine:{foreign}"), - &json!({"id":foreign,"user":"someone-else"}), - 0, - ) - .unwrap(); - } - assert_eq!( - fixture - .broker - .dispatch( - &fixture.store, - &fixture.grant, - &foreign, - "list_threads", - json!({}), - Duration::from_secs(2) - ) - .await - .unwrap_err() - .status, - 403 - ); - delete( - &fixture.store.db.lock().unwrap(), - &format!("grant:{}", fixture.grant), - ) - .unwrap(); - assert_eq!( - fixture - .dispatch(Duration::from_secs(2)) - .await - .unwrap() - .unwrap_err() - .status, - 403 - ); - } - #[tokio::test] - async fn unicode_message_cannot_exceed_local_agent_frame_budget() { - let fixture = Fixture::new(true); - let (connection, mut receive) = fixture.connect(); - let error = fixture.broker.dispatch(&fixture.store, &fixture.grant, &fixture.machine, "send_message", json!({"provider":"codex","thread_id":uuid::Uuid::new_v4().to_string(),"message":"雪".repeat(100000)}), Duration::from_secs(2)).await.unwrap_err(); - assert_eq!(error.status, 413); - assert!(receive.try_recv().is_err()); - assert!(connection.pending.lock().unwrap().is_empty()); - assert_eq!(fixture.broker.pending_slots.available_permits(), 128); - } - #[tokio::test] - async fn eight_pending_limit_and_cancellation_release_capacity() { - let fixture = Fixture::new(true); - let (connection, mut receive) = fixture.connect(); - let mut tasks = Vec::new(); - for _ in 0..8 { - tasks.push(fixture.dispatch(Duration::from_secs(2))); - receive.recv().await.unwrap(); - } - assert_eq!( - fixture - .dispatch(Duration::from_secs(2)) - .await - .unwrap() - .unwrap_err() - .status, - 429 - ); - tasks.remove(0).abort(); - tokio::task::yield_now().await; - assert_eq!(connection.pending.lock().unwrap().len(), 7); - tasks.push(fixture.dispatch(Duration::from_secs(2))); - receive.recv().await.unwrap(); - assert_eq!(connection.pending.lock().unwrap().len(), 8); - fixture.broker.disconnect(&fixture.machine); - for task in tasks { - assert!(task.await.unwrap().unwrap_err().message.contains("unknown")); - } - assert!(connection.pending.lock().unwrap().is_empty()); - } - #[tokio::test] - async fn timeout_disconnect_and_reconnect_do_not_replay() { - let fixture = Fixture::new(true); - let (old, mut receive) = fixture.connect(); - let task = fixture.dispatch(Duration::from_millis(5)); - receive.recv().await.unwrap(); - assert!(task.await.unwrap().unwrap_err().message.contains("unknown")); - assert!(old.pending.lock().unwrap().is_empty()); - assert!(receive.try_recv().is_err()); - let task = fixture.dispatch(Duration::from_secs(2)); - receive.recv().await.unwrap(); - fixture.broker.disconnect(&fixture.machine); - assert!(task.await.unwrap().unwrap_err().message.contains("unknown")); - let (new, mut receive) = fixture.connect(); - fixture.broker.remove(&fixture.machine, &old); - assert!(Arc::ptr_eq( - fixture - .broker - .connections - .lock() - .unwrap() - .get(&fixture.machine) - .unwrap(), - &new - )); - assert!(receive.try_recv().is_err()); - let task = fixture.dispatch(Duration::from_secs(2)); - receive.recv().await.unwrap(); - fixture.broker.disconnect(&fixture.machine); - assert!(task.await.unwrap().unwrap_err().message.contains("unknown")); - } -} diff --git a/dashboard/web/api.contract.ts b/dashboard/web/api.contract.ts index 8b727100e352371b30ff33a2ff518e8b9eb007a2..0eedf850d545391daf2f1a53668d99e9fa8b3282 100644 --- a/dashboard/web/api.contract.ts +++ b/dashboard/web/api.contract.ts @@ -81,12 +81,6 @@ export type Api = Hono<{}, { $get: Endpoint; $post: Endpoint<{ redirect: string }, { param: { id: string }; json: { resources: Access } | { deny: true } }>; }; - "/mcp/relay/pair": { $post: Endpoint; }; - "/mcp/relay/machines/:id": { - $patch: Endpoint; - $delete: Endpoint; - }; - "/mcp/relay/keys": { $post: Endpoint<{ key: string; id: string }, { json: { name: string; resources: string[]; write: boolean } }>; }; "/mcp/connections/:id": { $get: Endpoint<{ resources: Resources; selected: Access; linked: boolean; resourceError: string | null }, { param: { id: string } }>; $post: Endpoint; diff --git a/dashboard/web/pages/MCP.css b/dashboard/web/pages/MCP.css index b431bc5f9cb6e45c07c02599851b84d05608c986..4d06caf70289fa15aebb1d705295fc4d0c795889 100644 --- a/dashboard/web/pages/MCP.css +++ b/dashboard/web/pages/MCP.css @@ -12,7 +12,6 @@ .mcp-endpoint .muted { font-size: 12px; } .mcp-repositories, .mcp-install { margin: 12px 0; } .mcp-repositories summary, .mcp-install summary { cursor: pointer; color: var(--text-2); } -.mcp-install h3 { margin: 12px 0 6px; } .mcp-page .mcp-access-modes > label { padding: 6px 0; border: 0; background: none; } .mcp-actions { display: flex; flex-wrap: wrap; gap: 8px; } .mcp-access { padding: 0; margin: 20px 0; border: 0; min-width: 0; } @@ -30,15 +29,7 @@ .mcp-client > .mcp-actions { align-self: start; } .mcp-client p { margin: 6px 0 0; overflow-wrap: anywhere; } .mcp-edit-access { grid-column: 1 / -1; } -.mcp-page table { margin: 16px 0; } -.mcp-page form { margin: 16px 0; } -.mcp-page form > label { display: flex; align-items: center; gap: 12px; } -.mcp-page form:not(.mcp-actions) > button { margin-top: 12px; } -.mcp-help { margin: 24px 0; } -.mcp-help summary { cursor: pointer; } -.mcp-help li { margin: 12px 0; } -.mcp-connector { padding: 16px 0; margin: 16px 0; border-top: 1px solid var(--line); } .mcp-authorization { min-height: 100%; display: grid; place-items: center; padding: 40px 24px; box-sizing: border-box; } .mcp-approval { width: min(100%, 600px); padding: 32px; box-sizing: border-box; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; } .mcp-approval-brand { color: var(--accent); font-size: 13px; font-weight: 600; margin-bottom: 28px; } @@ -58,8 +49,6 @@ .mcp-section-heading { flex-wrap: wrap; } .mcp-endpoint .copy { width: 100%; } .mcp-repository-list > div { grid-template-columns: 1fr; gap: 4px; } - .mcp-page form > label { flex-wrap: wrap; } - .mcp-page input { max-width: 100%; min-width: 0; } .mcp-authorization { padding: 20px 12px; align-items: start; } .mcp-approval { padding: 24px 20px; } } diff --git a/dashboard/web/pages/MCP.tsx b/dashboard/web/pages/MCP.tsx index 958df2f52c675173efda68c91d9a8ca5f7cb2fbb..d986a8041d5adfff4f235830b82c40ef9a18d339 100644 --- a/dashboard/web/pages/MCP.tsx +++ b/dashboard/web/pages/MCP.tsx @@ -1,9 +1,8 @@ import { A, useNavigate, useParams } from "@solidjs/router"; import { parseResponse } from "hono/client"; -import { createEffect, createResource, createSignal, For, onCleanup, Show } from "solid-js"; +import { createEffect, createResource, createSignal, For, Show } from "solid-js"; import { api, reason } from "../api.ts"; import { Ago } from "../components/Ago.tsx"; -import { Checkbox } from "../components/Checkbox.tsx"; import { Copy } from "../components/Copy.tsx"; import { showConfirmDialog } from "../components/Dialog.tsx"; import { Loaded } from "../components/Loaded.tsx"; @@ -18,7 +17,6 @@ import "./MCP.css"; const descriptions: Record = { computer: "Inspect and control selected virtual machines, run commands, and install guest software.", shale: "Read and edit issues across your Shale repositories.", - agents: "Connect to Codex and Claude Code on your machines.", observability: "Read logs and traces from your services.", }; @@ -27,7 +25,7 @@ export function MCP(props: { me: Me }) { const navigate = useNavigate(); const ai = () => props.me.sections.includes("ai"); createEffect(() => { if (!params.catalog || params.catalog === "ai" && !ai()) navigate(`/mcp/settings/${ai() ? "ai" : "observability"}`, { replace: true }); }); - const [overview, { refetch, mutate }] = createResource(() => parseResponse(api.mcp.$get())); + const [overview, { refetch }] = createResource(() => parseResponse(api.mcp.$get())); const [shale, { refetch: retryShale }] = createResource(() => params.catalog === "shale", () => parseResponse(api.mcp.shale.$get())); const [editing, setEditing] = createSignal(""); @@ -39,21 +37,7 @@ export function MCP(props: { me: Me }) { return result; }); const [busy, setBusy] = createSignal(false); - const [code, setCode] = createSignal(""); - const [keyName, setKeyName] = createSignal(""); - const [keyMachines, setKeyMachines] = createSignal([]); - const [control, setControl] = createSignal(false); - const [key, setKey] = createSignal(""); - createEffect(() => { params.catalog; setEditing(""); setKey(""); }); - createEffect(() => { - if (params.catalog !== "agents") return; - const events = new EventSource("/api/mcp/relay/live"); - events.onmessage = (event) => { - const machines: NonNullable>["machines"] = JSON.parse(event.data); - mutate((previous) => previous && { ...previous, machines }); - }; - onCleanup(() => events.close()); - }); + createEffect(() => { params.catalog; setEditing(""); }); const linkShale = async () => { setBusy(true); try { const result = await parseResponse(api.mcp.shale.$post({ json: {} })); window.location.assign(result.redirect); } @@ -94,59 +78,6 @@ export function MCP(props: { me: Me }) { - -
-

Local machines

-
Install an agent -

Install on each machine, then enter the pairing code below. The agent starts at login.

-

macOS or Linux

- -

Windows PowerShell

- -

Use your usual terminal, without administrator privileges. Codex or Claude Code must already be installed and signed in.

-
{ - event.preventDefault(); setBusy(true); - try { await parseResponse(api.mcp.relay.pair.$post({ json: { code: code() } })); setCode(""); await refetch(); } - catch (error) { toast(reason(error)); } - finally { setBusy(false); } - }}> -
- No machines linked yet. Run the installer on a machine to link it.

}> - - {(machine) => } -
MachinePlatformConnection
{machine.name}{machine.platform}{machine.online ? "Online" : "Offline"} - -
-

API key

-
{ - event.preventDefault(); setBusy(true); - try { const result = await parseResponse(api.mcp.relay.keys.$post({ json: { name: keyName(), resources: keyMachines(), write: control() } })); setKey(result.key); setKeyName(""); setKeyMachines([]); setControl(false); await refetch(); } - catch (error) { toast(reason(error)); } - finally { setBusy(false); } - }}> - -
{(machine) => setKeyMachines(checked ? [...keyMachines(), machine.id] : keyMachines().filter((id) => id !== machine.id))}>{machine.name}}
- Allow session control - -
-
-
How to use linked machines -
    -
  1. Add the Local agents endpoint above to your AI client's MCP connectors. Sign in and choose the machines it can access.
  2. -
  3. Ask the client to list machines, choose a target, and list or read its Codex and Claude Code chats.
  4. -
  5. To start a chat, name the machine, provider, and an existing project folder allowed during installation.
  6. -
-

Session control lets a client send messages, start chats, and interrupt turns. Desktop Codex control needs the installer's experimental option.

-

Linked clients can read saved chats on granted machines. Allowed project folders limit where new chats start; existing chats keep their own permissions.

-

For a script or another local tool, create an API key for selected machines. Enable session control only when it needs to write.

-

Rerun the installer to update the agent or change allowed folders. Unlink removes the machine's access immediately.

-
-

New API key

Copy this key now. It won't be shown again.

-
-

Connected clients

No clients connected. Add the connector URL to your AI client to get started.

}>
{(client) => { @@ -157,7 +88,7 @@ export function MCP(props: { me: Me }) { event.preventDefault(); event.stopPropagation(); cancel(); } }}> -

{client.name}

{client.resources === "all" ? "All Repositories" : client.resources.join(", ")}

{client.scopes.includes("computer:control") ? "VM control and commands" : client.scopes.includes("sessions:write") ? "Session control" : client.scopes.includes("shale:write") ? "Issue editing" : "Read only"} · Connected

+

{client.name}

{client.resources === "all" ? "All Repositories" : client.resources.join(", ")}

{client.scopes.includes("computer:control") ? "VM control and commands" : client.scopes.includes("shale:write") ? "Issue editing" : "Read only"} · Connected

}> diff --git a/dashboard/web/types/mcp.ts b/dashboard/web/types/mcp.ts index 9cb101300c08af0fed73614f87ace48b4edc99cb..e068e4b027170ecdad85429906e44dad0147d529 100644 --- a/dashboard/web/types/mcp.ts +++ b/dashboard/web/types/mcp.ts @@ -1,11 +1,10 @@ -export type Catalog = "shale" | "agents" | "observability" | "computer"; +export type Catalog = "shale" | "observability" | "computer"; export type Access = "all" | string[]; export type Resources = { id: string; name: string; description?: string }[]; export interface Connections { catalogs: { id: Catalog; name: string; endpoint: string }[]; connections: { id: string; name: string; catalog: Catalog; resources: Access; scopes: string[]; createdAt: number }[]; - machines: { id: string; name: string; platform: string; online: boolean }[]; shale: { linkedAt: number } | null; } export interface Consent { diff --git a/nixos/dashboard.nix b/nixos/dashboard.nix index 1257893d8777c1d8a68e038179ba15fcc0165737..6bd0dd01cff1e19eae6a1ccf81303fa3fdadc283 100644 --- a/nixos/dashboard.nix +++ b/nixos/dashboard.nix @@ -37,7 +37,6 @@ let root = ../dashboard; fileset = lib.fileset.unions [ ../dashboard/src ../dashboard/web/sso ../dashboard/ai ../dashboard/tests ../dashboard/Cargo.toml ../dashboard/Cargo.lock - ../dashboard/agent/install.sh ../dashboard/agent/install.ps1 ]; }; cargoLock.lockFile = ../dashboard/Cargo.lock; @@ -68,6 +67,5 @@ let ln -s ${server}/bin/home-dashboard $out/bin/home-dashboard ln -s ${web} $out/lib/home-dashboard/dist ln -s ${../dashboard/server} $out/lib/home-dashboard/server - ln -s ${../dashboard/agent} $out/lib/home-dashboard/agent ''; in dashboard diff --git a/tools/build-agent.py b/tools/build-agent.py deleted file mode 100644 index deffd2fe9e56012c60ae2f9e36bf8d5ddc9f6459..0000000000000000000000000000000000000000 --- a/tools/build-agent.py +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env python3 -import argparse -import json -from pathlib import Path -import subprocess - - -def build(repo, destination, manifest): - spec = json.loads(manifest.read_text()) - source = (repo / "dashboard/agent" / spec["source"]).resolve(strict=True) - entry = (source / spec["entry"]).resolve(strict=True) - if not source.is_relative_to(repo.parent) or not entry.is_relative_to(source): - raise ValueError("agent source must stay inside the workspace") - destination.parent.mkdir(parents=True, exist_ok=True) - subprocess.run([ - str(repo / "dashboard/node_modules/.bin/esbuild"), str(entry), - "--bundle", "--platform=node", "--format=esm", "--target=node24", - "--external:bufferutil", "--external:utf-8-validate", - "--banner:js=import { createRequire } from 'node:module'; const require = createRequire(import.meta.url);", - "--outfile=" + str(destination), - ], check=True) - - -if __name__ == "__main__": - parser = argparse.ArgumentParser() - parser.add_argument("--output", type=Path) - args = parser.parse_args() - repo = Path(__file__).resolve().parent.parent - build(repo, args.output or repo / "dashboard/agent/relay.mjs", repo / "dashboard/agent/source.json") diff --git a/tools/dashboard-agent-test.py b/tools/dashboard-agent-test.py deleted file mode 100644 index 5d50c097226d878bc9839582a933fbd3a461302f..0000000000000000000000000000000000000000 --- a/tools/dashboard-agent-test.py +++ /dev/null @@ -1,175 +0,0 @@ -#!/usr/bin/env python3 -import argparse -import fcntl -import json -import os -from pathlib import Path -import pty -import re -import select -import shlex -import sqlite3 -import subprocess -import tempfile -import termios -import time -import urllib.error -import urllib.request -import uuid - - -def main(): - parser = argparse.ArgumentParser() - parser.add_argument("--url", required=True) - parser.add_argument("--output", type=Path) - parser.add_argument("--home", type=Path) - args = parser.parse_args() - origin = args.url.rstrip("/") - if os.uname().sysname == "Darwin": - existing = subprocess.run(["launchctl", "print", f"gui/{os.getuid()}/net.paperclover.agent-relay"], capture_output=True) - assert existing.returncode != 0, "stop the installed agent before running this disposable fixture" - - def http(path, body=None, token=None, status=200): - request = urllib.request.Request(origin + path, data=json.dumps(body).encode() if body is not None else None, - headers={"Content-Type": "application/json", "Origin": origin, **({"Authorization": "Bearer " + token} if token else {})}) - try: - response = urllib.request.urlopen(request, timeout=15) - except urllib.error.HTTPError as error: - response = error - with response: - raw = response.read().decode() - assert response.status == status, (path, response.status, raw[:200]) - try: - return json.loads(raw) if raw else None - except ValueError: - return raw - - with tempfile.TemporaryDirectory(prefix="agent-relay-native-") as temporary: - root = args.home.resolve() if args.home else Path(temporary).resolve() / "home with spaces $dollar %percent" - root.mkdir(exist_ok=True) - assert not (root / ".config/agent-relay/agent.json").exists(), "use a disposable home without an agent pairing" - projects = root / "projects" - projects.mkdir() - codex = root / "codex" - codex.mkdir() - thread = str(uuid.uuid4()) - db = sqlite3.connect(codex / "state_5.sqlite") - db.execute("CREATE TABLE threads (id TEXT,title TEXT,cwd TEXT,updated_at INTEGER,rollout_path TEXT,archived INTEGER)") - db.execute("INSERT INTO threads VALUES (?,?,?,?,?,0)", (thread, "Installer fixture", str(projects), int(time.time()), str(root / "fixture.jsonl"))) - db.commit() - db.close() - env = {**os.environ, "HOME": str(root), "XDG_CONFIG_HOME": str(root / ".config"), "XDG_DATA_HOME": str(root / ".local/share"), "CODEX_HOME": str(codex), "CLAUDE_CONFIG_DIR": str(root / "claude")} - base = root / "Library/Application Support/AgentRelay" if os.uname().sysname == "Darwin" else root / ".local/share/agent-relay" - data = root / ".config/agent-relay" - machine = None - child = None - master = None - transcript = "" - - def install(fresh): - nonlocal child, master, transcript, machine - master, slave = pty.openpty() - - def terminal(): - os.setsid() - fcntl.ioctl(0, termios.TIOCSCTTY, 0) - - child = subprocess.Popen(["sh", "-c", f"curl -fsSL {shlex.quote(origin + '/agent/install.sh')} | sh"], stdin=slave, stdout=slave, stderr=slave, env=env, preexec_fn=terminal) - os.close(slave) - transcript = "" - cursor = 0 - - def expect(pattern, timeout=120): - nonlocal transcript, cursor - deadline = time.monotonic() + timeout - while True: - match = re.search(pattern, transcript[cursor:]) - if match: - cursor += match.end() - return match - assert time.monotonic() < deadline, transcript[-1800:] - if select.select([master], [], [], .2)[0]: - try: - chunk = os.read(master, 65536) - except OSError: - chunk = b"" - assert chunk, transcript[-1800:] - transcript += chunk.decode(errors="replace") - - if fresh: - expect(r"Machine name \[.*?\]: ") - os.write(master, b"Installer fixture\n") - expect(r"Project folder: ") - os.write(master, (str(projects) + "\n" if fresh else "\n").encode()) - if fresh: - expect(r"Project folder: ") - os.write(master, b"\n") - expect(r"Enable desktop control\?.*?: ") - os.write(master, b"n\n") - if fresh: - code = expect(r"link this machine with code ([A-Z0-9]+-[A-Z0-9]+)").group(1) - machine = http("/api/mcp/relay/pair", {"code": code}) - expect(r"Installed\. Agent Relay starts at login\.") - deadline = time.monotonic() + 20 - while child.poll() is None and time.monotonic() < deadline: - if select.select([master], [], [], .2)[0]: - try: - transcript += os.read(master, 65536).decode(errors="replace") - except OSError: - break - assert child.poll() is not None, transcript[-1800:] - assert child.returncode == 0, transcript[-1800:] - os.close(master) - master = None - - def online(expected): - deadline = time.monotonic() + 20 - while time.monotonic() < deadline: - machines = http("/api/mcp")["machines"] - found = next((item for item in machines if item["id"] == machine["id"]), None) - if found and found["online"] == expected: - return - time.sleep(.2) - raise AssertionError("agent connection did not change") - - try: - install(True) - online(True) - config = json.loads((data / "agent.json").read_text()) - assert config["roots"] == [str(projects)] and not config["desktopWrite"] - assert (data / "agent.json").stat().st_mode & 0o777 == 0o600 - key = http("/api/mcp/relay/keys", {"name": "Installer fixture", "resources": [machine["id"]], "write": True})["key"] - result = http(f"/api/v1/machines/{machine['id']}/commands", {"method": "list_threads", "params": {"provider": "codex"}}, key) - assert any(item["id"] == thread for item in result["result"]["threads"]), result - result = http(f"/api/v1/machines/{machine['id']}/commands", {"method": "start_thread", "params": {"provider": "codex", "cwd": str(root), "message": "fixture"}}, key, status=400) - assert "outside the roots" in str(result) - install(False) - online(True) - assert json.loads((data / "agent.json").read_text()) == config - manage = [str(base / "node"), str(base / "setup.mjs")] - subprocess.run([*manage, "stop"], env=env, check=True, capture_output=True) - online(False) - subprocess.run([*manage, "start"], env=env, check=True, capture_output=True) - online(True) - subprocess.run([*manage, "uninstall"], env=env, check=True, capture_output=True) - online(False) - assert (data / "agent.json").exists() - report = {"platform": os.uname().sysname, "curl_pipe_prompts": "passed", "pairing": "passed", "native_login_startup": "passed", "private_credentials": "passed", "captured_cli_environment": "passed", "allowed_folders": "passed", "reinstall_keeps_identity": "passed", "stop_start_uninstall": "passed", "spaces_dollars_percent_in_paths": "passed"} - if args.output: - args.output.write_text(json.dumps(report, indent=2) + "\n") - print(json.dumps(report)) - finally: - if (base / "setup.mjs").exists(): - subprocess.run([str(base / "node"), str(base / "setup.mjs"), "uninstall"], env=env, capture_output=True) - if child and child.poll() is None: - os.killpg(child.pid, 9) - child.wait(timeout=10) - if master is not None: - os.close(master) - if machine: - request = urllib.request.Request(origin + "/api/mcp/relay/machines/" + machine["id"], method="DELETE", headers={"Origin": origin}) - urllib.request.urlopen(request, timeout=10).close() - - -if __name__ == "__main__": - main() diff --git a/tools/deploy.py b/tools/deploy.py index d8f6738814ce6f1829f9c79a8d64a657198fe3c8..def85e8188bf6823b35c6bcd153d91d1e0d07dca 100644 --- a/tools/deploy.py +++ b/tools/deploy.py @@ -2,7 +2,6 @@ import argparse import json import os -from importlib import import_module from pathlib import Path import re import shlex @@ -131,8 +130,6 @@ def upload(main=False): shutil.copytree(origin, destination, symlinks=True) else: shutil.copy2(origin, destination) - if (snapshot / "dashboard/agent/source.json").exists(): - import_module("build-agent").build(REPO, snapshot / "dashboard/agent/relay.mjs", snapshot / "dashboard/agent/source.json") digest = tree_digest(snapshot) release = digest[:16] remote_release = REMOTE / "releases" / release -- 2.54.1