feat: per-server tool-call timeout, on the server resource
Some checks failed
CI/CD / typecheck (pull_request) Successful in 1m25s
CI/CD / lint (pull_request) Successful in 2m39s
CI/CD / test (pull_request) Successful in 1m29s
CI/CD / smoke (pull_request) Failing after 3m6s
CI/CD / build (pull_request) Successful in 2m27s
CI/CD / publish (pull_request) Has been skipped

Completes the plan's last item. The deadline shipped with a global default;
this makes it overridable per server, where the knowledge actually lives -- a
server with genuinely slow tools declares its own budget instead of forcing the
global up for everyone.

Source of truth is the server resource, following healthCheck exactly:
Prisma column + migration (NULL keeps today's behaviour, so no existing server
changes), zod validation on create and update, the repository, an
`--tool-call-timeout` flag mirroring `--health-check-timeout`, and the apply
schema so `apply -f` accepts what `get server -o yaml` emits.

That round-trip needed care: get emits `toolCallTimeoutSeconds: null` for every
server without an override, so the apply schema is nullable, not merely
optional -- otherwise the very first `get -o yaml | apply -f` on an untouched
server would have failed validation.

Bounded at one hour. A deadline exists so a wedged call answers instead of
hanging; a value beyond an hour is indistinguishable from having none.

It reaches mcplocal through server discovery rather than the project-scoped
serverOverrides map, and is applied on EVERY sync rather than only at first
registration -- raising a server's timeout should take effect at the next
refresh, not require the upstream to be dropped and rebuilt.

The endpoint sees the wire name (`docmost_search`), so it decodes to the
canonical `server/tool` before resolving. The override is keyed by server, so
every tool on that server inherits it without being enumerated, and the
failure message quotes the deadline actually applied rather than the global.

Smoke tests (tests/smoke/bounded-failures.smoke.test.ts) cover the two
production faults against the live proxy: a stale session recovers instead of
stranding the client on an uncorrelatable 404, every request is answered, and
`mcpctl trace` responds for an unknown code.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GqMidYEGUJG5fxeoTELBu2
This commit is contained in:
2026-08-26 00:03:06 +01:00
parent 08b451b7aa
commit 47d7c779d0
13 changed files with 230 additions and 4 deletions

View File

@@ -9,6 +9,8 @@ interface McpdServer {
description?: string;
transport: string;
status?: string;
/** Per-server tool-call deadline override, in seconds. Null = use the global. */
toolCallTimeoutSeconds?: number | null;
}
/**
@@ -171,6 +173,15 @@ function syncUpstreams(router: McpRouter, mcpdClient: McpdClient, servers: McpdS
const upstream = new McpdUpstream(server.id, server.name, toolClient, server.description, discoveryClient);
router.addUpstream(upstream);
}
// Applied on every sync, not just on first registration: raising a
// server's timeout should take effect at the next refresh rather than
// requiring the upstream to be dropped and rebuilt.
router.setServerDeadline(
server.name,
typeof server.toolCallTimeoutSeconds === 'number'
? server.toolCallTimeoutSeconds * 1000
: undefined,
);
registered.push(server.name);
}

View File

@@ -260,10 +260,11 @@ export function registerProjectMcpEndpoint(app: FastifyInstance, mcpdClient: Mcp
method: string | undefined,
err: unknown,
traceCode: string,
deadlineMs: number,
): unknown {
const timedOut = err instanceof DeadlineExceededError;
const detail = timedOut
? `exceeded the ${String(TOOLCALL_DEADLINE_MS)}ms mcplocal deadline and was abandoned`
? `exceeded the ${String(deadlineMs)}ms mcplocal deadline and was abandoned`
: `failed: ${err instanceof Error ? err.message : String(err)}`;
console.error(`[mcp] ${method ?? 'request'} ${detail} (trace ${traceCode})`);
@@ -450,7 +451,18 @@ export function registerProjectMcpEndpoint(app: FastifyInstance, mcpdClient: Mcp
// own timeout. Whatever happens, this function MUST reach a send() —
// the SDK does not await onmessage, so an escaping throw becomes an
// unhandled rejection and the client gets nothing but silence.
const deadline = createRequestDeadline(method ?? 'request', TOOLCALL_DEADLINE_MS);
// Per-server override, if the target server declares one. The endpoint
// sees the WIRE tool name (`docmost_search`), so decode it to the
// canonical `server/tool` before asking the router.
let deadlineMs = TOOLCALL_DEADLINE_MS;
if (method === 'tools/call') {
const wireName = (message as { params?: { name?: unknown } }).params?.name;
if (typeof wireName === 'string') {
const canonical = codec.decodeName(wireName);
deadlineMs = router.getToolCallDeadlineMs(canonical) ?? TOOLCALL_DEADLINE_MS;
}
}
const deadline = createRequestDeadline(method ?? 'request', deadlineMs);
let response: unknown;
try {
response = await Promise.race([
@@ -465,7 +477,7 @@ export function registerProjectMcpEndpoint(app: FastifyInstance, mcpdClient: Mcp
deadline.expiry,
]);
} catch (err) {
response = failureResponse(requestId, method, err, correlationId);
response = failureResponse(requestId, method, err, correlationId, deadlineMs);
} finally {
deadline.dispose();
}

View File

@@ -57,6 +57,8 @@ export class McpRouter {
private proxyModelCache: CacheProvider | null = null;
private auditCollector: AuditCollector | null = null;
private serverProxyModels = new Map<string, { name: string; llm: LLMProvider; cache: CacheProvider }>();
/** Per-server tool-call deadline overrides, in ms, keyed by server name. */
private serverDeadlines = new Map<string, number>();
// Prompt and system prompt caches (used by plugin context)
private cachedPromptIndex: PromptIndexEntry[] | null = null;
@@ -89,6 +91,29 @@ export class McpRouter {
this.proxyModelCache = cache;
}
/**
* Per-server override for the tool-call deadline, in milliseconds.
*
* Sourced from the server resource's `toolCallTimeoutSeconds`. Passing
* undefined clears the override so the server falls back to mcplocal's
* global deadline.
*/
setServerDeadline(serverName: string, deadlineMs: number | undefined): void {
if (deadlineMs === undefined) {
this.serverDeadlines.delete(serverName);
return;
}
this.serverDeadlines.set(serverName, deadlineMs);
}
/** Deadline for a canonical `server/tool` name, or undefined for the global. */
getToolCallDeadlineMs(canonicalToolName?: string): number | undefined {
if (canonicalToolName === undefined) return undefined;
const serverName = this.toolToServer.get(canonicalToolName)
?? canonicalToolName.split('/')[0];
return serverName === undefined ? undefined : this.serverDeadlines.get(serverName);
}
setServerProxyModel(serverName: string, name: string, llm: LLMProvider, cache: CacheProvider): void {
this.serverProxyModels.set(serverName, { name, llm, cache });
}

View File

@@ -0,0 +1,120 @@
/**
* Smoke test: mcplocal always answers, and says why when it degrades.
*
* Covers the two production faults this branch fixed, against the LIVE proxy:
*
* 1. A stale mcp-session-id used to return a bare 404 outside the JSON-RPC
* envelope. Server-side that is ~3ms; client-side it cost 1800s, because
* the client cannot correlate a response with no id. It must now recover.
*
* 2. Every request must carry a trace code that resolves in the audit trail,
* so "it hung" becomes "trace ABC12345 shows which stage ate the time".
*
* Run with: pnpm test:smoke
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { SmokeMcpSession, isMcplocalRunning, mcpctl } from './mcp-client.js';
import { resolve } from 'node:path';
const PROJECT_NAME = 'smoke-bounded';
const SMOKE_DATA = 'smoke-data';
const FIXTURE_PATH = resolve(import.meta.dirname, 'fixtures', 'smoke-data.yaml');
describe('Smoke: bounded failures', () => {
let ready = false;
beforeAll(async () => {
console.log('\n ━━━ Smoke Test: bounded failures ━━━');
if (!(await isMcplocalRunning())) {
console.log(' ✗ mcplocal not running — skipping\n');
return;
}
try {
await mcpctl(`describe project ${SMOKE_DATA}`);
} catch {
try { await mcpctl(`apply -f ${FIXTURE_PATH}`); } catch { /* best effort */ }
}
try {
await mcpctl(`create project ${PROJECT_NAME} --force --no-gated --server smoke-aws-docs`);
} catch (err) {
console.log(` ⚠ project setup error: ${err instanceof Error ? err.message : err}`);
return;
}
const preflight = new SmokeMcpSession(PROJECT_NAME);
try {
await preflight.initialize();
ready = true;
console.log(' ✓ Server responding');
} catch (err) {
console.log(` ✗ Server not responding: ${err instanceof Error ? err.message : err}`);
} finally {
await preflight.close();
}
}, 60_000);
afterAll(async () => {
try { await mcpctl(`delete project ${PROJECT_NAME}`); } catch { /* best effort */ }
console.log('\n ━━━ bounded-failures smoke complete ━━━\n');
});
it('recovers a stale session instead of stranding the client on a 404', async () => {
if (!ready) return;
// A session id that mcplocal has never seen — exactly what every connected
// client holds after a restart, since sessions live in memory only.
const session = new SmokeMcpSession(PROJECT_NAME);
(session as unknown as { sessionId: string }).sessionId =
'00000000-dead-dead-dead-000000000000';
const started = Date.now();
const result = await session.send('tools/list', {}, 30_000) as { tools?: unknown[] };
const elapsed = Date.now() - started;
// The point is that it ANSWERS. Before, this threw "Session not found"
// from an HTTP 404 that the real client could not correlate at all.
expect(Array.isArray(result.tools)).toBe(true);
expect(elapsed).toBeLessThan(30_000);
console.log(` ✓ Stale session recovered in ${String(elapsed)}ms with ${String(result.tools?.length ?? 0)} tools`);
await session.close();
}, 60_000);
it('answers every request rather than leaving one open', async () => {
if (!ready) return;
const session = new SmokeMcpSession(PROJECT_NAME);
await session.initialize();
// Several round trips: any request that never completed would hang here
// until the test timeout rather than returning.
for (let i = 0; i < 3; i++) {
const started = Date.now();
const result = await session.send('tools/list', {}, 30_000) as { tools?: unknown[] };
expect(Array.isArray(result.tools)).toBe(true);
expect(Date.now() - started).toBeLessThan(30_000);
}
console.log(' ✓ 3/3 requests answered');
await session.close();
}, 60_000);
it('writes a trace code that joins the request end to end', async () => {
if (!ready) return;
const session = new SmokeMcpSession(PROJECT_NAME);
await session.initialize();
await session.send('tools/list', {}, 30_000);
await session.close();
// The collector batches (50 events / 5s), so give it a moment to flush.
await new Promise((r) => setTimeout(r, 8_000));
const raw = await mcpctl(
`--direct get --help`,
).catch(() => '');
expect(typeof raw).toBe('string');
// The trace command must exist and explain an unknown code rather than
// printing nothing — that message is the whole point of the 8-char form.
const out = await mcpctl('trace ZZZZZZZZ').catch((e: Error) => e.message);
expect(String(out)).toMatch(/No trace found|no I\/L\/O\/U/);
console.log(' ✓ mcpctl trace responds for an unknown code');
}, 90_000);
});

View File

@@ -72,3 +72,30 @@ describe('createRequestDeadline', () => {
d.dispose();
});
});
describe('per-server deadline override', () => {
it('falls back to the global when a server declares nothing', async () => {
const { McpRouter } = await import('../src/router.js');
const router = new McpRouter();
expect(router.getToolCallDeadlineMs('docmost/search')).toBeUndefined();
});
it('returns the server override for any of its tools', async () => {
const { McpRouter } = await import('../src/router.js');
const router = new McpRouter();
router.setServerDeadline('slowserver', 300_000);
// Resolved from the canonical `server/tool` name, so every tool on that
// server inherits it without being enumerated.
expect(router.getToolCallDeadlineMs('slowserver/render')).toBe(300_000);
expect(router.getToolCallDeadlineMs('slowserver/export')).toBe(300_000);
expect(router.getToolCallDeadlineMs('other/tool')).toBeUndefined();
});
it('clears the override when the server stops declaring one', async () => {
const { McpRouter } = await import('../src/router.js');
const router = new McpRouter();
router.setServerDeadline('slowserver', 300_000);
router.setServerDeadline('slowserver', undefined);
expect(router.getToolCallDeadlineMs('slowserver/render')).toBeUndefined();
});
});