diff --git a/apps/host/server/agent/profile.mjs b/apps/host/server/agent/profile.mjs index 2eca7e9129..e22a28efed 100644 --- a/apps/host/server/agent/profile.mjs +++ b/apps/host/server/agent/profile.mjs @@ -90,10 +90,21 @@ export function mcpRows(servers, env, skipped = []) { env[variable] = String(value); return `@@JS:process.env.${variable}@@`; }; - const config = { serverName: name, ...(server.command - ? { transport: 'stdio', command: String(server.command), args: Array.isArray(server.args) ? server.args.map(String) : [], ...(server.cwd ? { cwd: String(server.cwd) } : {}), - ...(server.env && Object.keys(server.env).length ? { env: Object.fromEntries(Object.entries(server.env).map(([k, v]) => [k, token('E', k, v)])) } : {}) } - : { transport: 'streamable-http', url: String(server.url), ...(server.headers && Object.keys(server.headers).length ? { headers: Object.fromEntries(Object.entries(server.headers).map(([k, v]) => [k, token('H', k, v)])) } : {}) }) }; + const rawTimeoutSec = server.timeoutSec ?? server.timeout_s ?? server.timeout; + const envTimeoutMs = process.env.STRATA_MCP_TIMEOUT_MS && Number(process.env.STRATA_MCP_TIMEOUT_MS) > 0 + ? Math.round(Number(process.env.STRATA_MCP_TIMEOUT_MS)) + : undefined; + const timeoutMs = Number(rawTimeoutSec) > 0 + ? Math.round(Number(rawTimeoutSec) * 1000) + : (Number(server.toolCallTimeoutMs) > 0 ? Math.round(Number(server.toolCallTimeoutMs)) : envTimeoutMs); + const config = { + serverName: name, + ...(timeoutMs ? { toolCallTimeoutMs: timeoutMs } : {}), + ...(server.command + ? { transport: 'stdio', command: String(server.command), args: Array.isArray(server.args) ? server.args.map(String) : [], ...(server.cwd ? { cwd: String(server.cwd) } : {}), + ...(server.env && Object.keys(server.env).length ? { env: Object.fromEntries(Object.entries(server.env).map(([k, v]) => [k, token('E', k, v)])) } : {}) } + : { transport: 'streamable-http', url: String(server.url), ...(server.headers && Object.keys(server.headers).length ? { headers: Object.fromEntries(Object.entries(server.headers).map(([k, v]) => [k, token('H', k, v)])) } : {}) }), + }; rows.push({ id: `mcp-${slug(name)}`, name: '@deepseek-ai/dsh-mcp-client', config }); index += 1; } diff --git a/apps/host/server/agent/tools-settings.mjs b/apps/host/server/agent/tools-settings.mjs index 0adba80ce6..b998c6198d 100644 --- a/apps/host/server/agent/tools-settings.mjs +++ b/apps/host/server/agent/tools-settings.mjs @@ -77,6 +77,9 @@ function validateCustom(body, existing) { if (url.username || url.password) throw new Error('Use a web address without a user name or password in it.'); } out.args = (Array.isArray(body.args) ? body.args : []).slice(0, 50).map(a => clean(a, 1000)).filter(Boolean); + const before = existing.find(t => t.id === body.id && t.kind === 'server'); + if (body.timeoutSec !== undefined && !(Number(body.timeoutSec) >= 1 && Number(body.timeoutSec) <= 3600)) throw new Error('Give the time allowed in seconds, from 1 to 3600.'); + out.timeoutSec = body.timeoutSec !== undefined ? clampTimeoutSec(body.timeoutSec) : before?.timeoutSec ?? clampTimeoutSec(undefined); } if (existing.some(t => t.name.toLowerCase() === name.toLowerCase() && t.id !== body.id)) throw new Error('You already have a tool with that name.'); return out; @@ -126,7 +129,8 @@ export function createAgentTools({ read, write, dataDir, browser = () => ({ foun id: t.id, name: t.name, about: t.about, kind: t.kind, enabled: t.enabled, summary: t.kind === 'instructions' ? t.instructions.slice(0, 80) : t.kind === 'command' ? t.command : t.address, instructions: t.instructions, command: t.command, address: t.address, args: t.args, - ...(t.kind === 'command' ? { cwd: t.cwd, shell: t.shell, timeoutSec: t.timeoutSec } : {}), + ...(t.kind === 'command' || t.kind === 'server' ? { timeoutSec: t.timeoutSec } : {}), + ...(t.kind === 'command' ? { cwd: t.cwd, shell: t.shell } : {}), })), }; } @@ -141,7 +145,10 @@ export function createAgentTools({ read, write, dataDir, browser = () => ({ foun const servers = {}; for (const t of custom.filter(t => t.kind === 'server')) { const name = `custom-${t.id.replace(/_/g, "-")}`.slice(0, 32); - servers[name] = /^https?:\/\//i.test(t.address) ? { url: t.address } : { command: t.address, args: t.args }; + servers[name] = { + ...(/^https?:\/\//i.test(t.address) ? { url: t.address } : { command: t.address, args: t.args }), + timeoutSec: t.timeoutSec, + }; } return { off: new Set(s.off), diff --git a/apps/host/tests/agent-tools-settings.test.mjs b/apps/host/tests/agent-tools-settings.test.mjs index 667abf681b..de0d003336 100644 --- a/apps/host/tests/agent-tools-settings.test.mjs +++ b/apps/host/tests/agent-tools-settings.test.mjs @@ -79,7 +79,7 @@ test('Tools: the person\'s own tools are checked in plain words and saved with t assert.throws(() => tools.addCustom({ name: 'release notes', about: 'a', kind: 'instructions', instructions: 'y' }), /already have a tool/); const p = tools.profileView(); assert.equal(p.hasInstructionTools && p.hasCommandTools, true); - assert.deepEqual(p.servers, { 'custom-issue-tracker': { url: 'https://issues.example/mcp' } }); + assert.deepEqual(p.servers, { 'custom-issue-tracker': { url: 'https://issues.example/mcp', timeoutSec: 120 } }); const file = JSON.parse(readFileSync(tools.filePath, 'utf8')); assert.deepEqual(Object.keys(file.instructions), ['release_notes']); assert.deepEqual(Object.keys(file.commands), ['check_site']); v = tools.update({ custom: { check_site: false } }); @@ -143,6 +143,19 @@ test('profile: the person\'s instruction and command tools are the app\'s own to assert.deepEqual(rows.map(r => r.config.serverName).sort(), ['custom-remote', 'strata-commands', 'strata-instructions']); const own = rows.find(r => r.config.serverName === 'strata-commands').config; assert.deepEqual([own.command, own.args[0], own.args[1]], [process.execPath, CUSTOM_TOOLS_SCRIPT, 'commands']); + const remote = rows.find(r => r.config.serverName === 'custom-remote').config; + assert.equal(remote.toolCallTimeoutMs, 120000); + } finally { cleanup(dir); } +}); + +test('profile: MCP servers forward custom timeoutSec as toolCallTimeoutMs', () => { + const dir = tmp(); + try { + const { tools } = make(dir); + tools.addCustom({ name: 'Long Job', about: 'a', kind: 'server', address: 'https://r.example/mcp', timeoutSec: 600 }); + const e = entriesOf(buildProfile({ local: [], connections: [], tools: tools.profileView() })); + const row = e.flatMap(x => x.insert ?? []).find(r => r.name === '@deepseek-ai/dsh-mcp-client' && r.config.serverName === 'custom-long-job'); + assert.equal(row.config.toolCallTimeoutMs, 600000); } finally { cleanup(dir); } }); diff --git a/apps/host/tests/timeout-format-verification.test.mjs b/apps/host/tests/timeout-format-verification.test.mjs new file mode 100644 index 0000000000..c5fb71aed1 --- /dev/null +++ b/apps/host/tests/timeout-format-verification.test.mjs @@ -0,0 +1,152 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { mcpRows } from '../server/agent/profile.mjs'; + +test('Format 1: server with timeoutSec (e.g. from Strata custom tools settings)', () => { + const env = {}; + const servers = { + 'my-mcp': { + command: 'node', + args: ['server.js'], + timeoutSec: 300, + }, + }; + const rows = mcpRows(servers, env); + assert.equal(rows.length, 1); + assert.equal(rows[0].name, '@deepseek-ai/dsh-mcp-client'); + assert.equal(rows[0].config.serverName, 'my-mcp'); + assert.equal(rows[0].config.toolCallTimeoutMs, 300000); +}); + +test('Format 2: server with timeout_s (e.g. from strata-.json or mcp_servers.json)', () => { + const env = {}; + const servers = { + 'relay-mcp': { + command: 'node', + args: ['relay.js'], + timeout_s: 180, + }, + }; + const rows = mcpRows(servers, env); + assert.equal(rows.length, 1); + assert.equal(rows[0].config.serverName, 'relay-mcp'); + assert.equal(rows[0].config.toolCallTimeoutMs, 180000); +}); + +test('Format 3: server with timeout (e.g. Claude Desktop style)', () => { + const env = {}; + const servers = { + 'claude-style-mcp': { + command: 'node', + args: ['claude.js'], + timeout: 240, + }, + }; + const rows = mcpRows(servers, env); + assert.equal(rows.length, 1); + assert.equal(rows[0].config.serverName, 'claude-style-mcp'); + assert.equal(rows[0].config.toolCallTimeoutMs, 240000); +}); + +test('Format 4: server with direct toolCallTimeoutMs in milliseconds', () => { + const env = {}; + const servers = { + 'direct-ms-mcp': { + command: 'node', + args: ['direct.js'], + toolCallTimeoutMs: 450000, + }, + }; + const rows = mcpRows(servers, env); + assert.equal(rows.length, 1); + assert.equal(rows[0].config.serverName, 'direct-ms-mcp'); + assert.equal(rows[0].config.toolCallTimeoutMs, 450000); +}); + +test('Format 5: Streamable HTTP server with timeoutSec', () => { + const env = {}; + const servers = { + 'remote-mcp': { + url: 'https://mcp.example.com/sse', + timeoutSec: 600, + }, + }; + const rows = mcpRows(servers, env); + assert.equal(rows.length, 1); + assert.equal(rows[0].config.serverName, 'remote-mcp'); + assert.equal(rows[0].config.transport, 'streamable-http'); + assert.equal(rows[0].config.toolCallTimeoutMs, 600000); +}); + +test('Format 6: environment variable STRATA_MCP_TIMEOUT_MS fallback', () => { + const originalEnv = process.env.STRATA_MCP_TIMEOUT_MS; + try { + process.env.STRATA_MCP_TIMEOUT_MS = '500000'; + const env = {}; + const servers = { + 'fallback-mcp': { + command: 'node', + args: ['server.js'], + }, + }; + const rows = mcpRows(servers, env); + assert.equal(rows.length, 1); + assert.equal(rows[0].config.toolCallTimeoutMs, 500000); + } finally { + if (originalEnv === undefined) delete process.env.STRATA_MCP_TIMEOUT_MS; + else process.env.STRATA_MCP_TIMEOUT_MS = originalEnv; + } +}); + +test('Format 7: explicit server timeout overrides environment variable', () => { + const originalEnv = process.env.STRATA_MCP_TIMEOUT_MS; + try { + process.env.STRATA_MCP_TIMEOUT_MS = '500000'; + const env = {}; + const servers = { + 'override-mcp': { + command: 'node', + args: ['server.js'], + timeoutSec: 120, // 120s should win over 500s env var + }, + }; + const rows = mcpRows(servers, env); + assert.equal(rows.length, 1); + assert.equal(rows[0].config.toolCallTimeoutMs, 120000); + } finally { + if (originalEnv === undefined) delete process.env.STRATA_MCP_TIMEOUT_MS; + else process.env.STRATA_MCP_TIMEOUT_MS = originalEnv; + } +}); + +test('Format 8: string numeric values are safely parsed', () => { + const env = {}; + const servers = { + 'string-timeout': { + command: 'node', + args: ['server.js'], + timeoutSec: '150', + }, + }; + const rows = mcpRows(servers, env); + assert.equal(rows[0].config.toolCallTimeoutMs, 150000); +}); + +test('Format 9: omitted timeout preserves undefined (client schema default applies)', () => { + const originalEnv = process.env.STRATA_MCP_TIMEOUT_MS; + delete process.env.STRATA_MCP_TIMEOUT_MS; + try { + const env = {}; + const servers = { + 'default-mcp': { + command: 'node', + args: ['server.js'], + }, + }; + const rows = mcpRows(servers, env); + assert.equal(rows.length, 1); + assert.equal(rows[0].config.toolCallTimeoutMs, undefined); + } finally { + if (originalEnv !== undefined) process.env.STRATA_MCP_TIMEOUT_MS = originalEnv; + } +});