Skip to content

Commit 6b87395

Browse files
committed
fix(gateway): own MCP app deadlines at the view boundary
1 parent 4eed1a6 commit 6b87395

6 files changed

Lines changed: 194 additions & 202 deletions

File tree

‎src/agents/agent-bundle-mcp-runtime.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,6 +1651,7 @@ process.on("SIGINT", shutdown);`,
16511651
volatile: {
16521652
command: process.execPath,
16531653
args: [serverPath],
1654+
requestTimeoutMs: 123_456,
16541655
},
16551656
},
16561657
},
@@ -1667,6 +1668,7 @@ process.on("SIGINT", shutdown);`,
16671668
"list_changed to invalidate the catalog",
16681669
LIST_TOOLS_SERVER_LOG_TIMEOUT_MS,
16691670
);
1671+
expect(runtime.getServerRequestTimeoutMs?.("volatile")).toBe(123_456);
16701672

16711673
const refreshedCatalog = await runtime.getCatalog();
16721674
expect(refreshedCatalog.tools).toEqual([]);
@@ -3798,6 +3800,7 @@ describe("requester-scoped MCP connection resolution", () => {
37983800
requesterScope: params.requesterScope,
37993801
peekCatalog: () => current,
38003802
getCatalog: async () => current,
3803+
getServerRequestTimeoutMs: () => (serverName === "user-mail" ? 90_000 : 60_000),
38013804
};
38023805
};
38033806
const manager = testing.createSessionMcpRuntimeManager({ createRuntime });
@@ -3824,6 +3827,7 @@ describe("requester-scoped MCP connection resolution", () => {
38243827
swapCatalogByServer.get("user-mail")?.("send_v2");
38253828
const after = await runtime.getCatalog();
38263829
expect(after.tools.map((tool) => tool.toolName).toSorted()).toEqual(["send_v2", "shared_tool"]);
3830+
expect(runtime.getServerRequestTimeoutMs?.("user-mail")).toBe(90_000);
38273831
expect(
38283832
runtime
38293833
.peekCatalog()

‎src/agents/mcp-transport-config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type ResolvedHttpMcpTransportConfig = ResolvedBaseMcpTransportConfig & {
5454
type ResolvedMcpTransportConfig = ResolvedStdioMcpTransportConfig | ResolvedHttpMcpTransportConfig;
5555

5656
const DEFAULT_CONNECTION_TIMEOUT_MS = 30_000;
57-
export const DEFAULT_REQUEST_TIMEOUT_MS = 60_000;
57+
const DEFAULT_REQUEST_TIMEOUT_MS = 60_000;
5858

5959
function getPositiveNumber(rawServer: unknown, keys: readonly string[]): number | undefined {
6060
if (!rawServer || typeof rawServer !== "object") {

‎src/agents/mcp-ui-resource.test.ts‎

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ describe("MCP App UI resources", () => {
162162
releases.slice(1).forEach((entry) => entry());
163163
});
164164

165-
it("normalizes CSP metadata before retaining the view", async () => {
165+
it("normalizes CSP and snapshots the session deadline before retaining the view", async () => {
166166
const sessionRuntime = runtime(async () => ({
167167
contents: [
168168
{
@@ -180,6 +180,10 @@ describe("MCP App UI resources", () => {
180180
},
181181
],
182182
}));
183+
const peekCatalog = vi.fn(() => null);
184+
sessionRuntime.peekCatalog = peekCatalog;
185+
sessionRuntime.getServerRequestTimeoutMs = (serverName) =>
186+
serverName === "demo" ? 120_000 : undefined;
183187
const result = await fetchMcpAppView({
184188
runtime: sessionRuntime,
185189
serverName: "demo",
@@ -194,6 +198,8 @@ describe("MCP App UI resources", () => {
194198
resourceDomains: ["https://cdn.example.com"],
195199
});
196200
expect(view?.html.startsWith("<!doctype html>")).toBe(true);
201+
expect(view?.requestTimeoutMs).toBe(120_000);
202+
expect(peekCatalog).not.toHaveBeenCalled();
197203
expect(buildMcpAppSandboxPath(view?.csp)).toContain("?csp=");
198204
});
199205

@@ -295,38 +301,6 @@ describe("MCP App UI resources", () => {
295301
expect(getMcpAppViewLease(viewIds[31] ?? "", sessionRuntime)).toBeDefined();
296302
});
297303

298-
it("reads the request timeout from the runtime session when the cached catalog is null", async () => {
299-
const sessionRuntime = runtime(async () => ({
300-
contents: [
301-
{
302-
uri: "ui://demo/app",
303-
mimeType: MCP_APP_RESOURCE_MIME_TYPE,
304-
text: "<html>demo</html>",
305-
_meta: { ui: { csp: { connectDomains: ["https://api.example.com"] } } },
306-
},
307-
],
308-
}));
309-
// Simulate catalog invalidation (peekCatalog returns null) while the session
310-
// still has the configured timeout.
311-
sessionRuntime.peekCatalog = () => null;
312-
sessionRuntime.getServerRequestTimeoutMs = (serverName) =>
313-
serverName === "demo" ? 120_000 : undefined;
314-
315-
const result = await fetchMcpAppView({
316-
runtime: sessionRuntime,
317-
serverName: "demo",
318-
toolName: "show",
319-
uiResourceUri: "ui://demo/app",
320-
toolInput: {},
321-
toolResult: { content: [] },
322-
});
323-
expect(result).toBeDefined();
324-
325-
const lease = getMcpAppViewLease(result!.viewId, sessionRuntime);
326-
expect(lease).toBeDefined();
327-
expect(lease!.requestTimeoutMs).toBe(120_000);
328-
});
329-
330304
it("replaces a reconstructed view id without leaking the previous runtime lease", async () => {
331305
const releases = [vi.fn(), vi.fn()];
332306
const sessionRuntime = runtime(async () => ({

‎src/agents/mcp-ui-resource.ts‎

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { completeDeferredSessionMcpRuntimeRetirement } from "./agent-bundle-mcp-
77
import type { SessionMcpRuntime } from "./agent-bundle-mcp-types.js";
88
import { clearMcpAppModelContextForView } from "./mcp-app-model-context.js";
99
import { type McpAppCsp, normalizeMcpAppCsp } from "./mcp-app-sandbox.js";
10-
import { DEFAULT_REQUEST_TIMEOUT_MS } from "./mcp-transport-config.js";
1110

1211
const MCP_APP_RESOURCE_MIME_TYPE = "text/html;profile=mcp-app";
1312
const MCP_APP_RESOURCE_MAX_BYTES = 2 * 1024 * 1024;
@@ -37,7 +36,7 @@ export type McpAppViewLease = {
3736
readOnly?: true;
3837
toolInput: unknown;
3938
toolResult: CallToolResult;
40-
requestTimeoutMs: number;
39+
requestTimeoutMs?: number;
4140
expiresAtMs: number;
4241
requestWindowStartedAtMs: number;
4342
requestCount: number;
@@ -266,17 +265,9 @@ export async function fetchMcpAppView(params: {
266265
const permissions = normalizePermissions(uiMeta?.permissions);
267266
const title = `${params.toolName} UI`;
268267
const viewId = params.viewId ?? `mcp-app-${randomUUID()}`;
269-
// Snapshot the configured request timeout when the view is created. The
270-
// cached catalog can be invalidated later (e.g. tools/list_changed), but the
271-
// browser-side deadline must stay consistent with the runtime deadline that
272-
// will be used for operations initiated from this view.
273-
// Read from the runtime session first — its timeout survives catalog
274-
// invalidation, so a tools/list_changed right before view creation does not
275-
// substitute the 60-second default for a configured 120-second MCP operation.
276-
const requestTimeoutMs =
277-
params.runtime.getServerRequestTimeoutMs?.(params.serverName) ??
278-
params.runtime.peekCatalog()?.servers[params.serverName]?.requestTimeoutMs ??
279-
DEFAULT_REQUEST_TIMEOUT_MS;
268+
// resources/read established the authoritative server session above. Carry
269+
// its deadline into the view so catalog invalidation cannot change it later.
270+
const requestTimeoutMs = params.runtime.getServerRequestTimeoutMs?.(params.serverName);
280271
releaseRuntimeLease = params.runtime.acquireLease?.();
281272
deleteView(viewId);
282273
pruneViewStore(byteSize, { reserveEntry: true });
@@ -300,7 +291,7 @@ export async function fetchMcpAppView(params: {
300291
...(params.readOnly ? { readOnly: true as const } : {}),
301292
toolInput: params.toolInput,
302293
toolResult: params.toolResult,
303-
requestTimeoutMs,
294+
...(requestTimeoutMs !== undefined ? { requestTimeoutMs } : {}),
304295
expiresAtMs: Date.now() + MCP_APP_VIEW_TTL_MS,
305296
requestWindowStartedAtMs: Date.now(),
306297
requestCount: 0,

0 commit comments

Comments
 (0)