A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Resolve secret references in stdio MCP arguments
Fei24115ec30c81867b
diff --git a/ds-bot/src/host/capabilities.js b/ds-bot/src/host/capabilities.jsindex e8f97661..5eda1c3b 100644--- a/ds-bot/src/host/capabilities.js+++ b/ds-bot/src/host/capabilities.js@@ -260,9 +260,10 @@ export function install(rt) {return resolved}const resolveUrl = (url, botId) => String(url).replace(SECRET_REFERENCE, (_match, name) => encodeUrlComponent(secretFor(name, botId)))+ const resolveList = (list, botId) => (list ?? []).map(raw => String(raw).replace(SECRET_REFERENCE, (_match, name) => secretFor(name, botId)))const configFor = (item, botId) => {if (item.kind === 'skill') return { name: item.name, description: item.description, content: item.content }- if (item.transport === 'stdio') return { transport: 'stdio', serverName: item.name, command: item.command, args: item.args ?? [], env: resolveValues(item.env, botId), failOnStartupError: true }+ if (item.transport === 'stdio') return { transport: 'stdio', serverName: item.name, command: item.command, args: resolveList(item.args, botId), env: resolveValues(item.env, botId), failOnStartupError: true }return { transport: 'streamable-http', serverName: item.name, url: resolveUrl(item.url, botId), headers: resolveValues(item.headers, botId), failOnStartupError: true }}const disposeMount = async mount => {diff --git a/ds-bot/test/capability-stdio-secrets.test.js b/ds-bot/test/capability-stdio-secrets.test.jsnew file mode 100644index 00000000..e93c9048--- /dev/null+++ b/ds-bot/test/capability-stdio-secrets.test.js@@ -0,0 +1,151 @@+import assert from 'node:assert/strict'+import { execFileSync } from 'node:child_process'+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'+import { tmpdir } from 'node:os'+import { join } from 'node:path'+import { test } from 'node:test'+import { fileURLToPath } from 'node:url'+import { createRuntime } from '../index.js'+import { fakeContext } from './fake-ctx.js'++// The fixture stands in for an MCP server: it reports back the argv and env it was+// started with, so the test sees exactly what the plugin passed to the child process.+const FIXTURE = fileURLToPath(new URL('./fixtures/echo-argv-env.mjs', import.meta.url))++const team = () => ({+ version: 1,+ mainBotIds: ['chief'],+ bots: {+ chief: { id: 'chief', sessionId: 'chief', name: 'Chief', capabilities: [], createdAt: 1 },+ worker: { id: 'worker', sessionId: 'worker', name: 'Worker', capabilities: [], createdAt: 2 },+ },+ rooms: {},+ agents: {},+ capabilities: { items: {} },+ exchanges: [],+ exchangeTurns: {},+ works: {},+ questions: {},+ prefs: {},+ revision: 1,+ whaleStored: true,+})++const secret = (name, value, scope = 'all') => ({ name, value, purpose: '', scope, requestedBy: null, createdAt: 1, updatedAt: 1 })++// An Agent whose plugin() really starts the configured command, like the mounted MCP+// client would, so the resolved args and env can be checked at the process boundary.+function spawningAgent(id, spawned) {+ const disposers = []+ return {+ id,+ session: { id },+ ctx: {+ effect(execute) {+ const dispose = execute()+ if (typeof dispose === 'function') disposers.push(dispose)+ },+ plugin(plugin, config) {+ const record = { plugin, config, sessionId: id, disposed: false }+ spawned.push(record)+ if (config?.transport === 'stdio') {+ record.output = JSON.parse(execFileSync(config.command, config.args, {+ env: { ...process.env, ...config.env },+ encoding: 'utf8',+ }))+ }+ return { await: async () => {}, dispose: () => { record.disposed = true } }+ },+ dispose() { for (const dispose of disposers.reverse()) dispose() },+ },+ }+}++async function setup({ state = team(), secrets = [] } = {}) {+ const home = mkdtempSync(join(tmpdir(), 'dsb-stdio-secrets-'))+ writeFileSync(join(home, 'state.json'), JSON.stringify(state))+ if (secrets.length > 0) writeFileSync(join(home, 'secrets.json'), JSON.stringify({ secrets }))+ const spawned = []+ const mcpPlugin = { name: 'mcp-client', apply() {} }+ const loader = { import: async () => ({ default: mcpPlugin }), unwrapExports: exports => exports.default }+ const ctx = fakeContext({ services: { loader } })+ const rt = createRuntime(ctx, { home, github: false, diagnosticsFile: false })+ ctx.sessionTitle = { rename: async () => {} }+ rt.resolveAgent = async sessionId => ({ session: { id: sessionId } })+ await rt.load()+ return {+ home,+ ctx,+ rt,+ spawned,+ mcpPlugin,+ async dispose() {+ ctx.dispose()+ await rt.scheduleCapabilitiesReconcile()+ await new Promise(resolve => setImmediate(resolve))+ rmSync(home, { recursive: true, force: true, maxRetries: 5 })+ },+ }+}++// Saves a stdio MCP capability, attaches it to `botId`, and mounts it on that Bot's Agent.+async function mountStdio(env, { name, args, env: envMap, botId = 'chief' }) {+ const { ctx, rt, spawned } = env+ await rt.capabilitySave({ name, kind: 'mcp', description: '', transport: 'stdio', command: process.execPath, args, env: envMap }, { botId })+ await rt.capabilityEndpoint('capability-approve', { name })+ await rt.changeCapabilityAttachment('attach', { name }, botId)+ ctx.emit('agent/created', { agent: spawningAgent(botId, spawned) })+ await rt.scheduleCapabilitiesReconcile()+ return spawned.findLast(mount => mount.config?.serverName === name && mount.sessionId === botId)+}++test('stdio MCP args resolve secret references like env does, and the child process sees the values', async (t) => {+ const env = await setup({ secrets: [secret('MCP_TOKEN', 'fake-dummy-secret-125'), secret('MCP_REGION', 'fake-region-125')] })+ t.after(() => env.dispose())+ const mount = await mountStdio(env, {+ name: 'echo',+ args: [FIXTURE, '--token', '$DSH_SECRET_MCP_TOKEN', 'pre$DSH_SECRET_MCP_TOKEN/$DSH_SECRET_MCP_REGION-post', 'plain'],+ env: { ECHO_ARGV_ENV_NAMES: 'MCP_ENV', MCP_ENV: '$DSH_SECRET_MCP_REGION' },+ })+ assert.ok(mount, 'the capability was not mounted')+ assert.deepEqual(mount.config.args, [FIXTURE, '--token', 'fake-dummy-secret-125', 'prefake-dummy-secret-125/fake-region-125-post', 'plain'])+ // The child sees argv after its own program and script names: everything we passed.+ assert.deepEqual(mount.output.argv, mount.config.args.slice(1))+ assert.equal(mount.output.env.MCP_ENV, 'fake-region-125')+ // state.json and the views keep the references, never the resolved values.+ const saved = readFileSync(join(env.home, 'state.json'), 'utf8')+ assert.equal(saved.includes('fake-dummy-secret-125'), false)+ assert.equal(saved.includes('fake-region-125'), false)+ assert.equal(JSON.stringify(env.rt.capabilityLibraryView()).includes('fake-dummy-secret-125'), false)+ assert.equal(env.rt.capabilityStatusesView().chief.echo.state, 'ready')+})++test('a missing or forbidden secret in stdio args keeps the server from starting, without leaking values', async (t) => {+ const env = await setup({ secrets: [secret('MCP_TOKEN', 'fake-dummy-secret-125', ['chief']), secret('OTHER_KEY', 'other-secret-125')] })+ t.after(() => env.dispose())+ // MCP_TOKEN is scoped to chief, so worker's mount must fail before the process starts.+ const denied = await mountStdio(env, {+ name: 'denied',+ args: [FIXTURE, '$DSH_SECRET_MCP_TOKEN'],+ env: {},+ botId: 'worker',+ })+ assert.equal(denied, undefined)+ const deniedStatus = env.rt.capabilityStatusesView().worker.denied+ assert.equal(deniedStatus.state, 'error')+ assert.match(deniedStatus.error, /MCP_TOKEN/)+ assert.equal(deniedStatus.error.includes('fake-dummy-secret-125'), false)+ assert.equal(deniedStatus.error.includes('other-secret-125'), false)+ // A name that is not saved at all fails the same way.+ const missing = await mountStdio(env, {+ name: 'missing',+ args: [FIXTURE, 'x$DSH_SECRET_NO_SUCH_KEY'],+ env: {},+ botId: 'chief',+ })+ assert.equal(missing, undefined)+ const missingStatus = env.rt.capabilityStatusesView().chief.missing+ assert.equal(missingStatus.state, 'error')+ assert.match(missingStatus.error, /NO_SUCH_KEY/)+ assert.equal(missingStatus.error.includes('other-secret-125'), false)+})diff --git a/ds-bot/test/fixtures/echo-argv-env.mjs b/ds-bot/test/fixtures/echo-argv-env.mjsnew file mode 100644index 00000000..c0788076--- /dev/null+++ b/ds-bot/test/fixtures/echo-argv-env.mjs@@ -0,0 +1,8 @@+// A stand-in stdio server: it prints the arguments and the environment variables it+// was started with as one JSON line, so a test can check what reached the process.+// ECHO_ARGV_ENV_NAMES picks which environment variables to report (comma-separated).+const names = (process.env.ECHO_ARGV_ENV_NAMES ?? '').split(',').filter(Boolean)+process.stdout.write(JSON.stringify({+ argv: process.argv.slice(2),+ env: Object.fromEntries(names.map(name => [name, process.env[name] ?? null])),+}))