Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🫗 fix: Bound MCP App Responses and In-Flight Work (#16300)

lia-by-librechat[bot]36ba2d3ce02f
diff --git a/api/app/clients/tools/util/handleTools.js b/api/app/clients/tools/util/handleTools.js
index cc7ef068af..a247f71323 100644
--- a/api/app/clients/tools/util/handleTools.js
+++ b/api/app/clients/tools/util/handleTools.js
@@ -634,6 +634,9 @@ const loadTools = async ({
admittedAppConfig?.mcpAppSandbox?.maxPersistedAppBytes,
admittedAppConfig?.mcpAppSandbox?.maxAdmissionRequestsPerMinute,
admittedAppConfig?.mcpAppSandbox?.url,
+ admittedAppConfig?.mcpAppSandbox?.maxActiveViews,
+ admittedAppConfig?.mcpAppSandbox?.maxActionPreviewChars,
+ admittedAppConfig?.mcpAppSandbox?.operationLimits,
);
const capabilityProfile = resolveMCPClientCapabilityProfile(admittedMCPAppsPolicy);
const requestScopedConnections =
diff --git a/api/server/controllers/mcp.js b/api/server/controllers/mcp.js
index e81d00aee4..8e96eafd3b 100644
--- a/api/server/controllers/mcp.js
+++ b/api/server/controllers/mcp.js
@@ -143,6 +143,9 @@ const getMCPTools = async (req, res) => {
req.config?.mcpAppSandbox?.maxPersistedAppBytes,
req.config?.mcpAppSandbox?.maxAdmissionRequestsPerMinute,
req.config?.mcpAppSandbox?.url,
+ req.config?.mcpAppSandbox?.maxActiveViews,
+ req.config?.mcpAppSandbox?.maxActionPreviewChars,
+ req.config?.mcpAppSandbox?.operationLimits,
);
/**
* A server whose normalized name is claimed by an earlier server produces
diff --git a/api/server/controllers/mcpApps.js b/api/server/controllers/mcpApps.js
index de43bf1f57..73cf11c6bf 100644
--- a/api/server/controllers/mcpApps.js
+++ b/api/server/controllers/mcpApps.js
@@ -29,6 +29,7 @@ module.exports = createMCPAppsController({
sandboxFrameAncestors: process.env.MCP_SANDBOX_FRAME_ANCESTORS,
readSandboxFile: fs.readFileSync,
getManager: getMCPManager,
+ getOperationBudget: () => getMCPManager().appOperationBudget,
getFlowManager: () => getFlowStateManager(getLogStores(CacheKeys.FLOWS)),
getAppConfig: (req) =>
getAppConfig({
diff --git a/api/server/controllers/mcpApps.test.js b/api/server/controllers/mcpApps.test.js
index 096d0f2a5b..ef66938c8a 100644
--- a/api/server/controllers/mcpApps.test.js
+++ b/api/server/controllers/mcpApps.test.js
@@ -116,6 +116,9 @@ describe('MCP Apps controller wiring', () => {
const response = {};
expect(dependencies.getManager).toBe(getMCPManager);
+ const appOperationBudget = {};
+ getMCPManager.mockReturnValueOnce({ appOperationBudget });
+ expect(dependencies.getOperationBudget()).toBe(appOperationBudget);
expect(dependencies.readSandboxFile).toBe(fs.readFileSync);
expect(dependencies.sandboxFrameAncestors).toBe('https://host.example.com');
expect(dependencies.getFlowManager()).toBe('flow-manager');
diff --git a/api/server/routes/mcp.js b/api/server/routes/mcp.js
index a96e3bbafa..5b06bde702 100644
--- a/api/server/routes/mcp.js
+++ b/api/server/routes/mcp.js
@@ -1006,6 +1006,9 @@ router.post(
req.config?.mcpAppSandbox?.maxPersistedAppBytes,
req.config?.mcpAppSandbox?.maxAdmissionRequestsPerMinute,
req.config?.mcpAppSandbox?.url,
+ req.config?.mcpAppSandbox?.maxActiveViews,
+ req.config?.mcpAppSandbox?.maxActionPreviewChars,
+ req.config?.mcpAppSandbox?.operationLimits,
),
});
diff --git a/api/server/services/MCP.js b/api/server/services/MCP.js
index dee92df897..bde54deb5a 100644
--- a/api/server/services/MCP.js
+++ b/api/server/services/MCP.js
@@ -397,6 +397,9 @@ async function getAssistantToolDefinitions({ req, res, tools }) {
appConfig?.mcpAppSandbox?.maxPersistedAppBytes,
appConfig?.mcpAppSandbox?.maxAdmissionRequestsPerMinute,
appConfig?.mcpAppSandbox?.url,
+ appConfig?.mcpAppSandbox?.maxActiveViews,
+ appConfig?.mcpAppSandbox?.maxActionPreviewChars,
+ appConfig?.mcpAppSandbox?.operationLimits,
);
const capabilityProfile = resolveMCPClientCapabilityProfile(mcpApps);
const oboIdentityContext = createAuthIdentityContext({
@@ -1514,6 +1517,9 @@ async function getMCPSetupData(userId, options = {}) {
appConfig?.mcpAppSandbox?.maxPersistedAppBytes,
appConfig?.mcpAppSandbox?.maxAdmissionRequestsPerMinute,
appConfig?.mcpAppSandbox?.url,
+ appConfig?.mcpAppSandbox?.maxActiveViews,
+ appConfig?.mcpAppSandbox?.maxActionPreviewChars,
+ appConfig?.mcpAppSandbox?.operationLimits,
),
);
const configServers = await registry.ensureConfigServers(appConfig?.mcpConfig || {});
diff --git a/api/server/services/ToolService.js b/api/server/services/ToolService.js
index 29d4cbc29c..5a85e7ff22 100644
--- a/api/server/services/ToolService.js
+++ b/api/server/services/ToolService.js
@@ -854,6 +854,9 @@ async function loadToolDefinitionsWrapper({
appConfig?.mcpAppSandbox?.maxPersistedAppBytes,
appConfig?.mcpAppSandbox?.maxAdmissionRequestsPerMinute,
appConfig?.mcpAppSandbox?.url,
+ appConfig?.mcpAppSandbox?.maxActiveViews,
+ appConfig?.mcpAppSandbox?.maxActionPreviewChars,
+ appConfig?.mcpAppSandbox?.operationLimits,
);
const capabilityProfile = resolveMCPClientCapabilityProfile(mcpApps);
const runtimeRequestBody = requestBody ?? req.body;
diff --git a/api/server/services/initializeMCPs.js b/api/server/services/initializeMCPs.js
index 4623037e3d..3019462190 100644
--- a/api/server/services/initializeMCPs.js
+++ b/api/server/services/initializeMCPs.js
@@ -42,6 +42,9 @@ async function resolveMCPAllowlists(ctx) {
appConfig?.mcpAppSandbox?.maxPersistedAppBytes,
appConfig?.mcpAppSandbox?.maxAdmissionRequestsPerMinute,
appConfig?.mcpAppSandbox?.url,
+ appConfig?.mcpAppSandbox?.maxActiveViews,
+ appConfig?.mcpAppSandbox?.maxActionPreviewChars,
+ appConfig?.mcpAppSandbox?.operationLimits,
),
};
}
@@ -128,6 +131,9 @@ async function initializeMCPs() {
appConfig?.mcpAppSandbox?.maxPersistedAppBytes,
appConfig?.mcpAppSandbox?.maxAdmissionRequestsPerMinute,
appConfig?.mcpAppSandbox?.url,
+ appConfig?.mcpAppSandbox?.maxActiveViews,
+ appConfig?.mcpAppSandbox?.maxActionPreviewChars,
+ appConfig?.mcpAppSandbox?.operationLimits,
),
);
} catch (error) {
diff --git a/docs/mcp-apps.md b/docs/mcp-apps.md
index cebc391555..a9b3011c45 100644
--- a/docs/mcp-apps.md
+++ b/docs/mcp-apps.md
@@ -85,6 +85,10 @@ mcpAppSandbox:
maxAdmissionRequestsPerMinute: 480
maxActiveViews: 3
maxActionPreviewChars: 16384
+ operationLimits:
+ maxBytes: 4194304
+ timeoutMs: 30000
+ maxActive: 16
```
These positive-integer settings come only from the base deployment configuration; role, group, and
@@ -121,6 +125,37 @@ MCP App browser routes use independent, per-user, one-minute limits. Configure p
values at `rateLimits.mcpApps.resourcesPerMinute` and
`rateLimits.mcpApps.toolCallsPerMinute`; their defaults are 120 and 60 respectively.
+## App operation budgets
+
+App-profile MCP sessions have a pre-parse upstream response cap of 4 MiB by default, independent
+of the standard-profile MCP transport settings, including the generic Streamable HTTP SSE line
+limit. Raising the validated App limit can admit a larger single SSE event without changing
+standard-profile limits. Streamable HTTP JSON responses, stdio frames and
+SSE events (both Streamable HTTP and standalone) are bounded before the SDK parses JSON. The SSE
+cap resets between events, so a healthy long-lived stream is not rejected for its lifetime bytes.
+An oversized standalone SSE event
+closes that App session after the SDK observes the error, preventing a sustained reconnect loop.
+The same cap is checked on serialized
+responses to the App bridge. Oversized optional initial `resources/read` results preserve the
+canonical tool result; a failed follow-up request returns a bounded error, never a truncated JSON
+success. The existing Express 3 MiB JSON ingress bound still applies before App route handlers.
+
+Configure `mcpAppSandbox.operationLimits` in `librechat.yaml` as shown above. The validated
+positive-integer limits default to the values shown and reject invalid or excessively high
+overrides. They are read from the admitted deployment config, not from process environment
+variables. The timeout is absolute across configuration, connection checkout,
+recovery and the SDK operation; progress notifications cannot extend the SDK call beyond it. The
+16-slot concurrency bound is **per LibreChat process**, shared across App resource, tool and binding
+validation routes and the optional initial document read after a tool completes. There is no queue:
+follow-up requests above it receive 503; an overloaded optional first read retains the bound URI
+for retry without changing the canonical tool result. An aborted or timed-out operation holds its
+slot until its underlying work actually settles, avoiding a false capacity
+release while upstream work remains active. This is not a distributed cluster-wide quota. Measure
+capacity per replica when sizing deployments. For safety, App-profile WebSocket connections are not
+supported: the installed SDK parses their messages before providing a configurable maximum payload.
+Standard-profile WebSocket sessions are unaffected. Larger legitimate Apps may require a higher
+byte limit; do not disable transport limits to work around large results.
+
## Required sandbox deployment
The chat application and Sandbox Proxy must use different URL origins. Set the proxy URL in the base
diff --git a/packages/api/src/mcp/MCPConnectionFactory.ts b/packages/api/src/mcp/MCPConnectionFactory.ts
index 0ea25b29a2..01f52f9349 100644
--- a/packages/api/src/mcp/MCPConnectionFactory.ts
+++ b/packages/api/src/mcp/MCPConnectionFactory.ts
@@ -94,6 +94,7 @@ export class MCPConnectionFactory {
protected readonly ephemeralConnection: boolean;
protected readonly directBearerRecoveryEnabled: boolean;
protected readonly capabilityProfile: t.BasicConnectionOptions['capabilityProfile'];
+ protected readonly operationLimits: t.BasicConnectionOptions['operationLimits'];
// OAuth-related properties (only set when useOAuth is true)
protected readonly userId?: string;
@@ -422,6 +423,7 @@ export class MCPConnectionFactory {
allowedAddresses: this.allowedAddresses,
ephemeralConnection: this.ephemeralConnection,
...(this.capabilityProfile && { capabilityProfile: this.capabilityProfile }),
+ ...(this.operationLimits && { operationLimits: this.operationLimits }),
...(this.directBearerRecoveryEnabled && { directBearerRecoveryEnabled: true }),
});
@@ -582,6 +584,7 @@ export class MCPConnectionFactory {
allowedAddresses: this.allowedAddresses,
ephemeralConnection: this.ephemeralConnection,
...(this.capabilityProfile && { capabilityProfile: this.capabilityProfile }),
+ ...(this.operationLimits && { operationLimits: this.operationLimits }),
});
unauthConnection.on('oauthRequired', () => {
@@ -634,6 +637,7 @@ export class MCPConnectionFactory {
basic.directBearerSourceConfig ?? basic.serverConfig,
);
this.capabilityProfile = basic.capabilityProfile;
+ this.operationLimits = basic.operationLimits;
this.connectionTimeout = options?.connectionTimeout;
this.deadlineMs = options?.deadlineMs;
this.onOAuthCredentialsChanged = options?.onOAuthCredentialsChanged;
@@ -777,6 +781,7 @@ export class MCPConnectionFactory {
allowedAddresses: this.allowedAddresses,
ephemeralConnection: this.ephemeralConnection,
...(this.capabilityProfile && { capabilityProfile: this.capabilityProfile }),
+ ...(this.operationLimits && { operationLimits: this.operationLimits }),
...(this.directBearerRecoveryEnabled && {
directBearerRecoveryEnabled: true,
}),
diff --git a/packages/api/src/mcp/MCPManager.ts b/packages/api/src/mcp/MCPManager.ts
index 51e723ce02..f63c90b092 100644
--- a/packages/api/src/mcp/MCPManager.ts
+++ b/packages/api/src/mcp/MCPManager.ts
@@ -50,6 +50,7 @@ import { mcpOptionsContainGraphTokenPlaceholder, preProcessGraphTokens } from '~
import { MCPAuthenticationRejectedError, isMCPTransportAuthenticationError } from './errors';
import { resolveDirectOpenIDBearerConfig, usesDirectOpenIDBearerRecovery } from './openid';
import { createLazyOboUpstreamTokenProvider, awaitOboOperation } from '~/mcp/oauth/obo';
+import { MCPAppOperationBudget, getMCPAppOperationLimits } from './apps/budget';
import { formatToolContent, selectResolvedAppResource } from './parsers';
import { MCPServersInitializer } from './registry/MCPServersInitializer';
import { OboTokenResolutionError, resolveOboToken } from '~/mcp/oauth';
@@ -149,6 +150,9 @@ export class MCPManager extends UserConnectionManager {
);
}
+ /** Per-process admission for follow-up requests and optional first document reads. */
+ public readonly appOperationBudget: MCPAppOperationBudget = new MCPAppOperationBudget();
+
private readonly resourceUriCache = new Map<string, Map<string, { uri: string }>>();
private readonly appHiddenToolCache = new Map<string, Set<string>>();
@@ -594,7 +598,7 @@ export class MCPManager extends UserConnectionManager {
return { tools: null, oauthRequired: false, oauthUrl: null };
}
- const { allowedDomains, allowedAddresses, useSSRFProtection } =
+ const { allowedDomains, allowedAddresses, useSSRFProtection, mcpApps } =
await registry.resolveAllowlists({ userId: user?.id, role: user?.role });
await this.assertResolvedRuntimeConfigAllowed({
config: catalogConfig,
@@ -618,6 +622,9 @@ export class MCPManager extends UserConnectionManager {
allowedDomains,
allowedAddresses,
capabilityProfile,
+ ...(capabilityProfile === MCP_APPS_CAPABILITY_PROFILE && {
+ operationLimits: mcpApps.operationLimits,
+ }),
};
const finalizeDiscoveryResult = async (
@@ -1988,12 +1995,23 @@ Please follow these instructions when using tools from the respective MCP server
}
let resolvedAppResource: t.ResourceContents | undefined;
- if (resourceMeta && !options?.signal?.aborted) {
+ const appConnection = connection;
+ if (resourceMeta && appConnection && !options?.signal?.aborted) {
const resourceUri = resourceMeta.uri;
try {
- const readResult = await connection.client.readResource(
- { uri: resourceUri },
- { timeout: connection.timeout, signal: options?.signal },
+ const limits = getMCPAppOperationLimits(mcpApps?.operationLimits);
+ const readResult = await this.appOperationBudget.run(
+ options?.signal ?? new AbortController().signal,
+ (signal) =>
+ appConnection.client.readResource(
+ { uri: resourceUri },
+ {
+ timeout: Math.min(appConnection.timeout ?? limits.timeoutMs, limits.timeoutMs),
+ maxTotalTimeout: limits.timeoutMs,
+ signal,
+ },
+ ),
+ limits,
);
if (!options?.signal?.aborted) {
resolvedAppResource = selectResolvedAppResource(readResult.contents, resourceUri);
@@ -2320,8 +2338,10 @@ Please follow these instructions when using tools from the respective MCP server
let result: TResult;
try {
+ const operationTimeout = getMCPAppOperationLimits(context.operationLimits).timeoutMs;
result = await operation(connection, {
- timeout: connection.timeout,
+ timeout: Math.min(connection.timeout ?? operationTimeout, operationTimeout),
+ maxTotalTimeout: operationTimeout,
...(signal ? { signal } : {}),
});
} catch (error) {
diff --git a/packages/api/src/mcp/UserConnectionManager.ts b/packages/api/src/mcp/UserConnectionManager.ts
index bff5fde455..5089e1f0be 100644
--- a/packages/api/src/mcp/UserConnectionManager.ts
+++ b/packages/api/src/mcp/UserConnectionManager.ts
@@ -900,7 +900,7 @@ export abstract class UserConnectionManager {
graphTokenResolver,
});
const registry = MCPServersRegistry.getInstance();
- const { allowedDomains, allowedAddresses, useSSRFProtection } =
+ const { allowedDomains, allowedAddresses, useSSRFProtection, mcpApps } =
await registry.resolveAllowlists({ userId: user?.id, role: user?.role });
await this.assertResolvedRuntimeConfigAllowed({
config: runtimeConfig,
@@ -926,6 +926,9 @@ export abstract class UserConnectionManager {
allowedAddresses,
ephemeralConnection,
capabilityProfile,
+ ...(capabilityProfile === MCP_APPS_CAPABILITY_PROFILE && {
+ operationLimits: mcpApps.operationLimits,
+ }),
};
/**
diff --git a/packages/api/src/mcp/__tests__/MCPConnectionFactory.test.ts b/packages/api/src/mcp/__tests__/MCPConnectionFactory.test.ts
index 0be1f5967f..f2eb103875 100644
--- a/packages/api/src/mcp/__tests__/MCPConnectionFactory.test.ts
+++ b/packages/api/src/mcp/__tests__/MCPConnectionFactory.test.ts
@@ -508,6 +508,22 @@ describe('MCPConnectionFactory', () => {
expect(mockConnectionInstance.connect).toHaveBeenCalled();
});
+ it('passes the validated App operation limits into App-profile transport construction', async () => {
+ const operationLimits = { maxBytes: 2 * 1024 * 1024, timeoutMs: 45_000, maxActive: 4 };
+ mockConnectionInstance.isConnected.mockResolvedValue(true);
+
+ await MCPConnectionFactory.create({
+ serverName: 'app-server',
+ serverConfig: mockServerConfig,
+ capabilityProfile: 'apps',
+ operationLimits,
+ });
+
+ expect(mockMCPConnection).toHaveBeenCalledWith(
+ expect.objectContaining({ capabilityProfile: 'apps', operationLimits }),
+ );
+ });
+
it('should merge requestHeaders before Graph pre-processing, overriding by case', async () => {
const graphTokenResolver = jest.fn();
const serverConfig = {
diff --git a/packages/api/src/mcp/__tests__/MCPConnectionSSRF.test.ts b/packages/api/src/mcp/__tests__/MCPConnectionSSRF.test.ts
index 83b90e47e2..a7ba516c72 100644
--- a/packages/api/src/mcp/__tests__/MCPConnectionSSRF.test.ts
+++ b/packages/api/src/mcp/__tests__/MCPConnectionSSRF.test.ts
@@ -2122,6 +2122,306 @@ describe('MCP SSRF protection – customFetch input shapes', () => {
}
});
+ it('enforces App-profile HTTP response bounds before JSON parsing even when the generic guard is disabled', async () => {
+ process.env.MCP_STREAMABLE_HTTP_MAX_RESPONSE_BYTES = '0';
+ const server = await createRawResponseServer((_req, res) => {
+ res.writeHead(200, { 'Content-Type': 'application/json' });
+ res.write('{"jsonrpc":"2.0","id":1,"result":{"text":"');
+ res.end('x'.repeat(100) + '"}}');
+ });
+ try {
+ conn = new MCPConnection({
+ serverName: 'app-profile-response-cap',
+ serverConfig: { type: 'streamable-http', url: server.url },
+ useSSRFProtection: false,
+ capabilityProfile: 'apps',
+ operationLimits: { maxBytes: 64, timeoutMs: 30_000, maxActive: 16 },
+ });
+ const customFetch = getGuardedStreamableHTTPCustomFetch(conn);
+ const response = await customFetch(server.url, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ jsonrpc: '2.0', method: 'resources/read', id: 1 }),
+ });
+ await expect(response.json()).rejects.toThrow(
+ /MCP response exceeded byte limit.*limit=64 bytes/,
+ );
+ } finally {
+ await server.close();
+ }
+ });
+
+ it('honors an operator-raised App cap for a valid response above the 4 MiB default', async () => {
+ const text = 'x'.repeat(4 * 1024 * 1024 + 1024);
+ const server = await createRawResponseServer((_req, res) => {
+ res.writeHead(200, { 'Content-Type': 'application/json' });
+ res.end(JSON.stringify({ jsonrpc: '2.0', id: 1, result: { text } }));
+ });
+ try {
+ conn = new MCPConnection({
+ serverName: 'app-profile-raised-byte-cap',
+ serverConfig: { type: 'streamable-http', url: server.url },
+ useSSRFProtection: false,
+ capabilityProfile: 'apps',
+ operationLimits: { maxBytes: 6 * 1024 * 1024, timeoutMs: 30_000, maxActive: 16 },
+ });
+ const response = await getGuardedStreamableHTTPCustomFetch(conn)(server.url, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ jsonrpc: '2.0', method: 'resources/read', id: 1 }),
+ });
+ const result = (await response.json()) as { result: { text: string } };
+ expect(result.result.text).toHaveLength(text.length);
+ } finally {
+ await server.close();
+ }
+ });
+
+ it('honors an operator-raised App cap for an SSE event above the generic 5 MiB line limit', async () => {
+ const event = `data: ${'x'.repeat(5 * 1024 * 1024 + 1024)}\n\n`;
+ const server = await createRawResponseServer((_req, res) => {
+ res.writeHead(200, { 'Content-Type': 'text/event-stream' });
+ res.write(event.slice(0, 2 * 1024 * 1024));
+ res.end(event.slice(2 * 1024 * 1024));
+ });
+ try {
+ conn = new MCPConnection({
+ serverName: 'app-profile-raised-sse-cap',
+ serverConfig: { type: 'streamable-http', url: server.url },
+ useSSRFProtection: false,
+ capabilityProfile: 'apps',
+ operationLimits: { maxBytes: 6 * 1024 * 1024, timeoutMs: 30_000, maxActive: 16 },
+ });
+ const response = await getGuardedStreamableHTTPCustomFetch(conn)(server.url, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ jsonrpc: '2.0', method: 'resources/read', id: 1 }),
+ });
+ expect((await response.text()).length).toBe(event.length);
+ } finally {
+ await server.close();
+ }
+ });
+
+ it('allows a long App SSE POST with cumulative bytes above the per-event cap', async () => {
+ const data = Array.from({ length: 4 }, (_, i) => `data: {"event":${i},"ok":true}\n\n`).join('');
+ expect(Buffer.byteLength(data)).toBeGreaterThan(64);
+ const server = await createRawResponseServer((_req, res) => {
+ res.writeHead(200, { 'Content-Type': 'text/event-stream' });
+ const half = Math.floor(data.length / 2);
+ res.write(data.slice(0, half));
+ res.end(data.slice(half));
+ });
+ try {
+ conn = new MCPConnection({
+ serverName: 'app-profile-sse-long-lived',
+ serverConfig: { type: 'streamable-http', url: server.url },
+ useSSRFProtection: false,
+ capabilityProfile: 'apps',
+ operationLimits: { maxBytes: 64, timeoutMs: 30_000, maxActive: 16 },
+ });
+ const response = await getGuardedStreamableHTTPCustomFetch(conn)(server.url, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ jsonrpc: '2.0', method: 'notifications/cancelled' }),
+ });
+ await expect(response.text()).resolves.toBe(data);
+ } finally {
+ await server.close();
+ }
+ });
+
+ it('rejects an App SSE POST event built from individually small data lines', async () => {
+ const server = await createRawResponseServer((_req, res) => {
+ res.writeHead(200, { 'Content-Type': 'text/event-stream' });
+ res.write(`data: ${'x'.repeat(30)}\n`);
+ res.end(`data: ${'y'.repeat(30)}\n\n`);
+ });
+ try {
+ conn = new MCPConnection({
+ serverName: 'app-profile-sse-multiline-cap',
+ serverConfig: { type: 'streamable-http', url: server.url },
+ useSSRFProtection: false,
+ capabilityProfile: 'apps',
+ operationLimits: { maxBytes: 64, timeoutMs: 30_000, maxActive: 16 },
+ });
+ const response = await getGuardedStreamableHTTPCustomFetch(conn)(server.url, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ jsonrpc: '2.0', method: 'notifications/cancelled' }),
+ });
+ await expect(response.text()).rejects.toThrow(
+ /MCP response exceeded byte limit.*limit=64 bytes/,
+ );
+ } finally {
+ await server.close();
+ }
+ });
+
+ it('blocks a chunked standalone SSE App event before the SDK delivers it', async () => {
+ let sentOversize = false;
+ let getRequests = 0;
+ const server = await createRawResponseServer((req, res) => {
+ if (req.method !== 'GET') {
+ res.writeHead(404);
+ res.end();
+ return;
+ }
+ getRequests++;
+ res.writeHead(200, { 'Content-Type': 'text/event-stream' });
+ res.write('retry: 10\nevent: endpoint\ndata: /messages\n\n');
+ setImmediate(() => {
+ sentOversize = true;
+ res.write('data: {"jsonrpc":"2.0","id":1,"result":{"text":"');
+ res.write('x'.repeat(256));
+ res.write('"}}\n\n');
+ });
+ });
+ conn = new MCPConnection({
+ serverName: 'app-profile-sse-event-cap',
+ serverConfig: { type: 'sse', url: server.url },
+ useSSRFProtection: false,
+ capabilityProfile: 'apps',
+ operationLimits: { maxBytes: 128, timeoutMs: 30_000, maxActive: 16 },
+ });
+ const transport = await (
+ conn as unknown as {
+ constructTransport(options: { type: 'sse'; url: string }): Promise<{
+ start(): Promise<void>;
+ close(): Promise<void>;
+ onmessage?: (message: unknown) => void;
+ onerror?: (error: Error) => void;
+ }>;
+ }
+ ).constructTransport({ type: 'sse', url: server.url });
+ const delivered: unknown[] = [];
+ transport.onmessage = (message) => delivered.push(message);
+ let timeout: ReturnType<typeof setTimeout> | undefined;
+ const originalClose = transport.close.bind(transport);
+ const closedOnOversize = new Promise<void>((resolve, reject) => {
+ timeout = setTimeout(
+ () => reject(new Error('SSE oversize did not close the SDK session')),
+ 3000,
+ );
+ transport.close = async () => {
+ resolve();
+ await originalClose();
+ };
+ });
+ try {
+ const startupSettled = transport.start().then(
+ () => 'connected',
+ () => 'rejected',
+ );
+ await Promise.all([startupSettled, closedOnOversize]);
+ expect(sentOversize).toBe(true);
+ expect(delivered).toEqual([]);
+ const requestsWhenClosed = getRequests;
+ await new Promise<void>((resolve) => setTimeout(resolve, 100));
+ expect(getRequests).toBe(requestsWhenClosed);
+ } finally {
+ clearTimeout(timeout);
+ await server.close();
+ }
+ });
+
+ it.each(['GET', 'POST'] as const)(
+ 'bounds SSE-typed non-2xx %s App error bodies cumulatively',
+ async (method) => {
+ const errorEvent = 'data: {"error":"temporary"}\n\n';
+ const server = await createRawResponseServer((_req, res) => {
+ res.writeHead(503, { 'Content-Type': 'text/event-stream' });
+ res.write(errorEvent);
+ res.write(errorEvent);
+ res.end(errorEvent);
+ });
+ try {
+ conn = new MCPConnection({
+ serverName: 'app-profile-sse-error-cap',
+ serverConfig: { type: 'streamable-http', url: server.url },
+ useSSRFProtection: false,
+ capabilityProfile: 'apps',
+ operationLimits: { maxBytes: 64, timeoutMs: 30_000, maxActive: 16 },
+ });
+ const response = await getGuardedStreamableHTTPCustomFetch(conn)(server.url, {
+ method,
+ ...(method === 'POST' && {
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ jsonrpc: '2.0', method: 'notifications/cancelled' }),
+ }),
+ });
+ await expect(response.text()).rejects.toThrow(
+ /MCP response exceeded byte limit.*limit=64 bytes/,
+ );
+ } finally {
+ await server.close();
+ }
+ },
+ );
+
+ it('bounds App-profile HTTP GET error bodies before the SDK reads them', async () => {
+ process.env.MCP_STREAMABLE_HTTP_MAX_RESPONSE_BYTES = '0';
+ const server = await createRawResponseServer((_req, res) => {
+ res.writeHead(503, { 'Content-Type': 'text/plain' });
+ res.end('x'.repeat(100));
+ });
+ try {
+ conn = new MCPConnection({
+ serverName: 'app-profile-get-error-cap',
+ serverConfig: { type: 'streamable-http', url: server.url },
+ useSSRFProtection: false,
+ capabilityProfile: 'apps',
+ operationLimits: { maxBytes: 64, timeoutMs: 30_000, maxActive: 16 },
+ });
+ const response = await getGuardedStreamableHTTPCustomFetch(conn)(server.url, {
+ method: 'GET',
+ });
+ await expect(response.text()).rejects.toThrow(
+ /MCP response exceeded byte limit.*limit=64 bytes/,
+ );
+ } finally {
+ await server.close();
+ }
+ });
+
+ it('refuses App-profile WebSocket sessions with no pre-parse SDK payload hook', async () => {
+ const config = { type: 'websocket' as const, url: 'wss://mcp.example.com/' };
+ conn = new MCPConnection({
+ serverName: 'app-profile-websocket-denied',
+ serverConfig: config,
+ useSSRFProtection: false,
+ capabilityProfile: 'apps',
+ });
+ await expect(
+ (
+ conn as unknown as { constructTransport(options: typeof config): Promise<unknown> }
+ ).constructTransport(config),
+ ).rejects.toThrow(/pre-parse response size limit/);
+ });
+
+ it('passes the App profile byte budget to the real SDK stdio ReadBuffer', async () => {
+ const config = { type: 'stdio' as const, command: 'node', args: ['-e', ''] };
+ conn = new MCPConnection({
+ serverName: 'app-profile-stdio-cap',
+ serverConfig: config,
+ useSSRFProtection: false,
+ capabilityProfile: 'apps',
+ operationLimits: { maxBytes: 64, timeoutMs: 30_000, maxActive: 16 },
+ });
+ const transport = await (
+ conn as unknown as {
+ constructTransport(options: {
+ type: 'stdio';
+ command: string;
+ args: string[];
+ }): Promise<{ _readBuffer: { append(chunk: Buffer): void } }>;
+ }
+ ).constructTransport(config);
+ expect(() => transport._readBuffer.append(Buffer.alloc(65))).toThrow(
+ /maximum size of 64 bytes/,
+ );
+ });
+
it('should reject oversized JSON POST responses with the streamable HTTP byte cap', async () => {
process.env.MCP_STREAMABLE_HTTP_MAX_RESPONSE_BYTES = '8';
const server = await createRawResponseServer((_req, res) => {
diff --git a/packages/api/src/mcp/__tests__/MCPManager.test.ts b/packages/api/src/mcp/__tests__/MCPManager.test.ts
index cf6e6beee2..b769ee993a 100644
--- a/packages/api/src/mcp/__tests__/MCPManager.test.ts
+++ b/packages/api/src/mcp/__tests__/MCPManager.test.ts
@@ -8,6 +8,7 @@ import {
McpError,
ErrorCode,
} from '@modelcontextprotocol/sdk/types.js';
+import type { TMCPAppsPolicy } from 'librechat-data-provider';
import type { IUser } from '@librechat/data-schemas';
import type { GraphTokenResolver } from '~/utils/graph';
import type * as t from '~/mcp/types';
@@ -109,7 +110,7 @@ const mockRegistryInstance = {
allowedDomains: mockGetAllowedDomains(),
allowedAddresses: mockGetAllowedAddresses(),
useSSRFProtection: mockShouldEnableSSRFProtection(),
- mcpApps: { enabled: true, legacyHtmlEnabled: true },
+ mcpApps: { enabled: true, legacyHtmlEnabled: true } as TMCPAppsPolicy,
})),
};
@@ -1145,6 +1146,47 @@ describe('MCPManager', () => {
).toHaveLength(1);
});
+ it('retains a bound URI without retrying the tool when the shared App budget is full', async () => {
+ const limits = { maxBytes: 4 * 1024 * 1024, timeoutMs: 30_000, maxActive: 1 };
+ const manager = new MCPManager(undefined, undefined, {
+ create: jest.fn(() => 'binding'),
+ verify: jest.fn(() => true),
+ });
+ const request = jest.fn(async ({ method }: { method: string }) => {
+ if (method === 'tools/call') return toolResult;
+ throw new Error('resource read must not start when capacity is full');
+ });
+ jest.spyOn(manager, 'getConnection').mockResolvedValue(connectionFor(request));
+ let finish!: () => void;
+ const occupied = manager.appOperationBudget.run(
+ new AbortController().signal,
+ () =>
+ new Promise<void>((resolve) => {
+ finish = resolve;
+ }),
+ limits,
+ );
+
+ const [text, artifacts] = await manager.callTool({
+ user,
+ serverName,
+ serverConfig,
+ toolName: 'app_tool',
+ provider: 'openai',
+ flowManager,
+ mcpApps: { enabled: true, legacyHtmlEnabled: true, operationLimits: limits },
+ });
+ expect(text).toContain('ordinary output');
+ expect(artifacts?.ui_resources?.data?.[0]).toMatchObject({
+ uri: resourceUri,
+ serverBinding: 'binding',
+ content: toolResult.content,
+ });
+ expect(request.mock.calls.map(([call]) => call.method)).toEqual(['tools/call']);
+ finish();
+ await occupied;
+ });
+
it('retains the bound URI if the read returns no usable matching App document', async () => {
const request = jest.fn(async ({ method }: { method: string }) =>
method === 'tools/call'
@@ -5413,6 +5455,68 @@ describe('MCPManager', () => {
(MCPConnectionFactory.discoverTools as jest.Mock) = jest.fn();
});
+ it.each([64, 6 * 1024 * 1024])(
+ 'passes the user-scoped %i byte limit to App-profile discovery',
+ async (maxBytes) => {
+ const operationLimits = { maxBytes, timeoutMs: 45_000, maxActive: 8 };
+ mockRegistryInstance.resolveAllowlists.mockResolvedValueOnce({
+ allowedDomains: null,
+ allowedAddresses: null,
+ useSSRFProtection: false,
+ mcpApps: { enabled: true, legacyHtmlEnabled: true, operationLimits },
+ });
+ (MCPConnectionFactory.discoverTools as jest.Mock).mockResolvedValue({
+ tools: mockTools,
+ connection: null,
+ oauthRequired: false,
+ oauthUrl: null,
+ });
+ const manager = await MCPManager.createInstance(newMCPServersConfig());
+ await manager.discoverServerTools({
+ serverName,
+ user: { id: 'discovery-user', role: 'ADMIN' } as IUser,
+ capabilityProfile: MCP_APPS_CAPABILITY_PROFILE,
+ });
+
+ expect(mockRegistryInstance.resolveAllowlists).toHaveBeenCalledWith({
+ userId: 'discovery-user',
+ role: 'ADMIN',
+ });
+ expect(MCPConnectionFactory.discoverTools).toHaveBeenCalledWith(
+ expect.objectContaining({
+ capabilityProfile: MCP_APPS_CAPABILITY_PROFILE,
+ operationLimits,
+ }),
+ expect.any(Object),
+ );
+ },
+ );
+
+ it('does not apply App limits to standard-profile discovery', async () => {
+ const operationLimits = { maxBytes: 64, timeoutMs: 45_000, maxActive: 8 };
+ mockAppConnections({ get: jest.fn().mockResolvedValue(null) });
+ mockRegistryInstance.resolveAllowlists.mockResolvedValueOnce({
+ allowedDomains: null,
+ allowedAddresses: null,
+ useSSRFProtection: false,
+ mcpApps: { enabled: true, legacyHtmlEnabled: true, operationLimits },
+ });
+ (MCPConnectionFactory.discoverTools as jest.Mock).mockResolvedValue({
+ tools: mockTools,
+ connection: null,
+ oauthRequired: false,
+ oauthUrl: null,
+ });
+ const manager = await MCPManager.createInstance(newMCPServersConfig());
+ await manager.discoverServerTools({
+ serverName,
+ capabilityProfile: STANDARD_MCP_CAPABILITY_PROFILE,
+ });
+ expect((MCPConnectionFactory.discoverTools as jest.Mock).mock.calls[0][0]).not.toHaveProperty(
+ 'operationLimits',
+ );
+ });
+
it('keeps the declared OAuth identity while stripping discovery headers', async () => {
const declared: t.ParsedServerConfig = {
type: 'streamable-http',
diff --git a/packages/api/src/mcp/apps.ts b/packages/api/src/mcp/apps.ts
index 78a4a03499..26f7f9c52a 100644
--- a/packages/api/src/mcp/apps.ts
+++ b/packages/api/src/mcp/apps.ts
@@ -7,6 +7,7 @@ import { logger } from '@librechat/data-schemas';
import { MCP_APP_MIME_TYPE } from 'librechat-data-provider';
import { McpError, ErrorCode } from '@modelcontextprotocol/sdk/types.js';
import type { PluginAuthMethods, TokenMethods, IUser } from '@librechat/data-schemas';
+import type { TMCPAppOperationLimits } from 'librechat-data-provider';
import type { UpstreamTokenProvider } from './oauth/obo';
import type { FlowStateManager } from '~/flow/manager';
import type { MCPOAuthTokens } from './oauth';
@@ -14,6 +15,7 @@ import type * as t from './types';
import { MCPAuthenticationRefreshError, MCPAuthenticationRejectedError } from './errors';
import { getServerCustomUserVars, getUserMCPAuthMap } from './auth';
import { OpenIDReauthRequiredError } from '~/utils/oidc';
+import { MCPAppBudgetError } from './apps/budget';
export interface ToolWithMeta {
_meta?: Record<string, unknown> | null;
@@ -48,6 +50,7 @@ export interface MCPAppAllowlists {
}
export interface MCPAppOperationContext extends MCPAppValidationContext {
+ operationLimits?: TMCPAppOperationLimits;
allowlists: MCPAppAllowlists;
flowManager: FlowStateManager<MCPOAuthTokens | null>;
tokenMethods?: TokenMethods;
@@ -260,6 +263,7 @@ export async function validateAppServerBinding(
/** A denied app request is an expected client error, not a host fault. */
export function isDeniedAppRequest(error: unknown): boolean {
return (
+ error instanceof MCPAppBudgetError ||
error instanceof OpenIDReauthRequiredError ||
error instanceof MCPAuthenticationRejectedError ||
error instanceof MCPAuthenticationRefreshError ||
@@ -273,6 +277,9 @@ export function buildAppProxyErrorResponse(
error: unknown,
fallbackMessage: string,
): { status: number; body: Record<string, unknown> } {
+ if (error instanceof MCPAppBudgetError) {
+ return { status: error.status, body: { error: error.code, message: error.message } };
+ }
if (
error != null &&
typeof error === 'object' &&
diff --git a/packages/api/src/mcp/apps/budget.spec.ts b/packages/api/src/mcp/apps/budget.spec.ts
new file mode 100644
index 0000000000..bdf040bc09
--- /dev/null
+++ b/packages/api/src/mcp/apps/budget.spec.ts
@@ -0,0 +1,124 @@
+import {
+ MCPAppBudgetError,
+ MCPAppOperationBudget,
+ assertMCPAppResultFits,
+ getMCPAppOperationLimits,
+ guardMCPAppSSEEvents,
+} from './budget';
+
+const limits = { maxBytes: 4 * 1024 * 1024, timeoutMs: 30_000, maxActive: 16 };
+
+function deferred<T>() {
+ let resolve!: (value: T) => void;
+ let reject!: (error: Error) => void;
+ const promise = new Promise<T>((yes, no) => {
+ resolve = yes;
+ reject = no;
+ });
+ return { promise, resolve, reject };
+}
+
+describe('MCP App operation limits', () => {
+ it('uses the injected validated limits or defaults without consulting process state', () => {
+ expect(getMCPAppOperationLimits()).toEqual(limits);
+ expect(getMCPAppOperationLimits({ ...limits, maxBytes: 64 })).toEqual({
+ ...limits,
+ maxBytes: 64,
+ });
+ });
+
+ it('rejects App JSON larger than the configured serialized UTF-8 limit', () => {
+ const small = { ...limits, maxBytes: 12 };
+ expect(() => assertMCPAppResultFits({ text: '😀' }, small)).toThrow(MCPAppBudgetError);
+ expect(() => assertMCPAppResultFits({ text: '😀' }, small)).toThrow('too large');
+ expect(() => assertMCPAppResultFits({ text: 'a' }, small)).not.toThrow();
+ });
+
+ it('admits at most one live operation with zero pending waiters, releasing on settlement', async () => {
+ const limited = { ...limits, maxActive: 1 };
+ const budget = new MCPAppOperationBudget();
+ const pending = deferred<string>();
+ const first = budget.run(new AbortController().signal, () => pending.promise, limited);
+ await expect(
+ budget.run(new AbortController().signal, async () => 'second', limited),
+ ).rejects.toMatchObject({ status: 503, code: 'mcp_app_capacity' });
+ pending.resolve('first');
+ await expect(first).resolves.toBe('first');
+ await expect(
+ budget.run(new AbortController().signal, async () => 'third', limited),
+ ).resolves.toBe('third');
+ });
+
+ it('holds an aborted work slot until the underlying operation actually settles', async () => {
+ const limited = { ...limits, maxActive: 1 };
+ const budget = new MCPAppOperationBudget();
+ const abort = new AbortController();
+ const pending = deferred<string>();
+ const first = budget.run(abort.signal, () => pending.promise, limited);
+ abort.abort(new Error('client left'));
+ await expect(first).rejects.toThrow('client left');
+ await expect(
+ budget.run(new AbortController().signal, async () => 'not yet', limited),
+ ).rejects.toMatchObject({ status: 503 });
+ pending.reject(new Error('late failure'));
+ await new Promise<void>((resolve) => setImmediate(resolve));
+ await expect(
+ budget.run(new AbortController().signal, async () => 'ready', limited),
+ ).resolves.toBe('ready');
+ });
+
+ it('aborts by absolute deadline even if the upstream promise ignores cancellation', async () => {
+ const limited = { ...limits, timeoutMs: 20 };
+ const budget = new MCPAppOperationBudget();
+ const pending = deferred<string>();
+ let signal: AbortSignal | undefined;
+ await expect(
+ budget.run(
+ new AbortController().signal,
+ (operationSignal) => {
+ signal = operationSignal;
+ return pending.promise;
+ },
+ limited,
+ ),
+ ).rejects.toMatchObject({ status: 504, code: 'mcp_app_timeout' });
+ expect(signal?.aborted).toBe(true);
+ pending.resolve('late');
+ });
+});
+
+describe('MCP App SSE event guard', () => {
+ const encoder = new TextEncoder();
+ const toResponse = (chunks: string[]) => {
+ let index = 0;
+ const body = new ReadableStream<Uint8Array>({
+ pull(controller) {
+ if (index < chunks.length) controller.enqueue(encoder.encode(chunks[index++]));
+ else controller.close();
+ },
+ });
+ return new Response(body, { status: 200, headers: { 'Content-Type': 'text/event-stream' } });
+ };
+
+ it('allows multiple events in a long-lived stream, resetting between CRLF-delimited events', async () => {
+ const data = 'data: hello\r\n\r\ndata: world\r\n\r\n';
+ const response = guardMCPAppSSEEvents(
+ toResponse(['data: hello\r', '\n\r', '\ndata: world\r\n\r\n']),
+ 14,
+ );
+ await expect(response.text()).resolves.toBe(data);
+ });
+
+ it('blocks a chunked event before the SDK can parse its data', async () => {
+ const response = guardMCPAppSSEEvents(toResponse(['data: ', 'x'.repeat(20), '\n\n']), 16);
+ await expect(response.text()).rejects.toThrow('MCP App event is too large');
+ });
+
+ it('does not modify non-event-stream HTTP responses', async () => {
+ const response = new Response('ok', {
+ status: 200,
+ headers: { 'Content-Type': 'application/json' },
+ });
+ expect(guardMCPAppSSEEvents(response, 1)).toBe(response);
+ });
+});
diff --git a/packages/api/src/mcp/apps/budget.ts b/packages/api/src/mcp/apps/budget.ts
new file mode 100644
index 0000000000..bceb779226
--- /dev/null
+++ b/packages/api/src/mcp/apps/budget.ts
@@ -0,0 +1,161 @@
+import { DEFAULT_MCP_APP_OPERATION_LIMITS } from 'librechat-data-provider';
+import type { TMCPAppOperationLimits } from 'librechat-data-provider';
+
+/** Runtime consumers take a validated deployment snapshot rather than reading process state. */
+export const getMCPAppOperationLimits = (limits?: TMCPAppOperationLimits): TMCPAppOperationLimits =>
+ limits ?? DEFAULT_MCP_APP_OPERATION_LIMITS;
+
+export class MCPAppBudgetError extends Error {
+ constructor(
+ public readonly status: 413 | 502 | 503 | 504,
+ public readonly code: string,
+ message: string,
+ ) {
+ super(message);
+ this.name = 'MCPAppBudgetError';
+ }
+}
+
+/** Count admitted operations, not just HTTP responses: aborted work holds its slot until it settles. */
+export class MCPAppOperationBudget {
+ private active = 0;
+
+ async run<T>(
+ callerSignal: AbortSignal,
+ operation: (signal: AbortSignal) => Promise<T>,
+ limits: TMCPAppOperationLimits = DEFAULT_MCP_APP_OPERATION_LIMITS,
+ ): Promise<T> {
+ const { timeoutMs, maxActive } = limits;
+ callerSignal.throwIfAborted();
+ if (this.active >= maxActive) {
+ throw new MCPAppBudgetError(
+ 503,
+ 'mcp_app_capacity',
+ 'MCP App capacity is temporarily exhausted',
+ );
+ }
+ this.active++;
+ const timeout = AbortSignal.timeout(timeoutMs);
+ const signal = AbortSignal.any([callerSignal, timeout]);
+ const work = Promise.resolve().then(() => operation(signal));
+ // Work may ignore abort. Keep its slot occupied, and observe a later rejection, even after the
+ // HTTP request has been answered. Never release a slot while an MCP operation can still run.
+ void work.then(
+ () => {
+ this.active--;
+ },
+ () => {
+ this.active--;
+ },
+ );
+ let onAbort: (() => void) | undefined;
+ const interrupted = new Promise<never>((_resolve, reject) => {
+ onAbort = () =>
+ reject(
+ timeout.aborted
+ ? new MCPAppBudgetError(504, 'mcp_app_timeout', 'MCP App operation timed out')
+ : (callerSignal.reason ?? new Error('MCP App request cancelled')),
+ );
+ signal.addEventListener('abort', onAbort, { once: true });
+ if (signal.aborted) onAbort();
+ });
+ try {
+ const result = await Promise.race([work, interrupted]);
+ if (signal.aborted) {
+ if (timeout.aborted) {
+ throw new MCPAppBudgetError(504, 'mcp_app_timeout', 'MCP App operation timed out');
+ }
+ callerSignal.throwIfAborted();
+ }
+ return result;
+ } finally {
+ signal.removeEventListener('abort', onAbort!);
+ }
+ }
+}
+
+/** JSON must be measured in the same UTF-8 form Express sends to the App bridge. */
+export function assertMCPAppResultFits(
+ result: unknown,
+ limits: TMCPAppOperationLimits = DEFAULT_MCP_APP_OPERATION_LIMITS,
+): void {
+ const { maxBytes } = limits;
+ let serialized: string | undefined;
+ try {
+ serialized = JSON.stringify(result);
+ } catch {
+ throw new MCPAppBudgetError(
+ 502,
+ 'mcp_app_invalid_response',
+ 'MCP App response cannot be serialized',
+ );
+ }
+ if (serialized === undefined || Buffer.byteLength(serialized, 'utf8') > maxBytes) {
+ throw new MCPAppBudgetError(502, 'mcp_app_response_too_large', 'MCP App response is too large');
+ }
+}
+
+/** Stateful across chunks and CRLF boundaries, resetting only at a blank SSE event separator. */
+export function createMCPAppSSEEventGuard(maxEventBytes: number): (chunk: Uint8Array) => boolean {
+ let eventBytes = 0;
+ let lineBytes = 0;
+ let previousCR = false;
+ return (chunk) => {
+ for (const byte of chunk) {
+ if (byte === 10 && previousCR) {
+ previousCR = false;
+ continue;
+ }
+ if (byte === 10 || byte === 13) {
+ previousCR = byte === 13;
+ if (lineBytes === 0) eventBytes = 0;
+ else eventBytes++;
+ lineBytes = 0;
+ } else {
+ previousCR = false;
+ eventBytes++;
+ lineBytes++;
+ }
+ if (eventBytes > maxEventBytes) return false;
+ }
+ return true;
+ };
+}
+
+/** GET SSE streams are long-lived: limit each complete event before EventSource calls JSON.parse. */
+export function guardMCPAppSSEEvents(
+ response: Response,
+ maxEventBytes: number,
+ onOversize?: (error: MCPAppBudgetError) => void,
+): Response {
+ if (
+ !response.body ||
+ !response.ok ||
+ response.headers.get('content-type')?.split(';', 1)[0].trim().toLowerCase() !==
+ 'text/event-stream'
+ ) {
+ return response;
+ }
+ const fitsEvent = createMCPAppSSEEventGuard(maxEventBytes);
+ const guarded = response.body.pipeThrough(
+ new TransformStream<Uint8Array, Uint8Array>({
+ transform(chunk, controller) {
+ if (!fitsEvent(chunk)) {
+ const error = new MCPAppBudgetError(
+ 502,
+ 'mcp_app_event_too_large',
+ 'MCP App event is too large',
+ );
+ onOversize?.(error);
+ throw error;
+ }
+ controller.enqueue(chunk);
+ },
+ }),
+ );
+ return new Response(guarded, {
+ status: response.status,
+ statusText: response.statusText,
+ headers: response.headers,
+ });
+}
diff --git a/packages/api/src/mcp/apps/controller.spec.ts b/packages/api/src/mcp/apps/controller.spec.ts
index bd2a4079b0..74a5610afc 100644
--- a/packages/api/src/mcp/apps/controller.spec.ts
+++ b/packages/api/src/mcp/apps/controller.spec.ts
@@ -5,6 +5,7 @@ import type { RequestHandler, Response } from 'express';
import type { MCPAppsControllerDependencies } from './controller';
import type { MCPAppsProxyManager } from '../apps';
import { createMCPAppsController } from './controller';
+import { MCPAppOperationBudget } from './budget';
import { getPluginAuthMap } from '~/agents/auth';
jest.mock('~/agents/auth', () => ({ getPluginAuthMap: jest.fn() }));
@@ -19,6 +20,9 @@ type MockRequest = EventEmitter & {
query: Record<string, unknown>;
user?: { id: string; role?: string };
config?: {
+ mcpAppSandbox?: {
+ operationLimits?: Partial<{ maxBytes: number; timeoutMs: number; maxActive: number }>;
+ };
mcpSettings?: {
apps?: boolean;
allowedDomains?: string[] | null;
@@ -73,6 +77,7 @@ const asHandlerRequest = (request: MockRequest): Parameters<RequestHandler>[0] =
const asHandlerResponse = (response: MockResponse): Response => response as unknown as Response;
function makeDependencies(manager: MCPAppsProxyManager) {
+ const operationBudget = new MCPAppOperationBudget();
const provider = jest.fn();
const onOAuthCredentialsChanging = jest.fn(async () => async () => undefined);
const dependencies: MCPAppsControllerDependencies = {
@@ -81,6 +86,7 @@ function makeDependencies(manager: MCPAppsProxyManager) {
sandboxFrameAncestors: 'https://host.example.com',
readSandboxFile: jest.fn(() => '<script>/*__CSP_APPLIED__*/ /*__VIEW_CSP__*/</script>'),
getManager: jest.fn(() => manager),
+ getOperationBudget: jest.fn(() => operationBudget),
getFlowManager: jest.fn(
() => ({}) as ReturnType<MCPAppsControllerDependencies['getFlowManager']>,
),
@@ -809,3 +815,148 @@ describe('createMCPAppsController', () => {
expect(next).not.toHaveBeenCalled();
});
});
+
+describe('App operation budgets at the authenticated HTTP boundary', () => {
+ const admitted = (
+ operationLimits: Partial<{ maxBytes: number; timeoutMs: number; maxActive: number }> = {},
+ ) =>
+ makeRequest({
+ config: {
+ mcpSettings: { apps: true },
+ mcpAppSandbox: { operationLimits },
+ mcpConfig: { srv: { type: 'stdio', command: 'test', args: [] } },
+ },
+ });
+
+ beforeEach(() => {
+ mockGetPluginAuthMap.mockResolvedValue({ mcp_srv: {} });
+ });
+
+ it('rejects oversized live resources and tool replies without serializing them into HTTP responses', async () => {
+ const manager = makeManager();
+ manager.readResource.mockResolvedValue({
+ contents: [{ uri: 'ui://view', text: 'x'.repeat(100) }],
+ });
+ manager.appToolCall.mockResolvedValue({ content: [{ type: 'text', text: 'x'.repeat(100) }] });
+ const { dependencies } = makeDependencies(manager);
+ const handlers = createMCPAppsController(dependencies);
+ const resourceResponse = makeResponse();
+ await handlers.readMCPResource(
+ asHandlerRequest(admitted({ maxBytes: 80 })),
+ asHandlerResponse(resourceResponse),
+ jest.fn(),
+ );
+ expect(resourceResponse.status).toHaveBeenCalledWith(502);
+ expect(resourceResponse.json).toHaveBeenCalledWith(
+ expect.objectContaining({ error: 'mcp_app_response_too_large' }),
+ );
+ expect(resourceResponse.json).not.toHaveBeenCalledWith(
+ expect.objectContaining({ contents: expect.any(Array) }),
+ );
+ const toolResponse = makeResponse();
+ const toolRequest = admitted({ maxBytes: 80 });
+ toolRequest.body.toolName = 'fixture-tool';
+ await handlers.appToolCall(
+ asHandlerRequest(toolRequest),
+ asHandlerResponse(toolResponse),
+ jest.fn(),
+ );
+ expect(toolResponse.status).toHaveBeenCalledWith(502);
+ expect(toolResponse.json).toHaveBeenCalledWith(
+ expect.objectContaining({ error: 'mcp_app_response_too_large' }),
+ );
+ });
+
+ it('shares the manager budget with an in-flight initial App resource read', async () => {
+ const manager = makeManager();
+ const operationBudget = new MCPAppOperationBudget();
+ const limits = { maxBytes: 4 * 1024 * 1024, timeoutMs: 30_000, maxActive: 1 };
+ let finish!: () => void;
+ const holding = operationBudget.run(
+ new AbortController().signal,
+ () =>
+ new Promise<void>((resolve) => {
+ finish = resolve;
+ }),
+ limits,
+ );
+ const { dependencies } = makeDependencies(manager);
+ dependencies.getOperationBudget = () => operationBudget;
+ const response = makeResponse();
+ await createMCPAppsController(dependencies).readMCPResource(
+ asHandlerRequest(admitted({ maxActive: 1 })),
+ asHandlerResponse(response),
+ jest.fn(),
+ );
+ expect(response.status).toHaveBeenCalledWith(503);
+ expect(manager.readResource).not.toHaveBeenCalled();
+ await Promise.resolve();
+ finish();
+ await holding;
+ });
+
+ it('rejects overload without allocating a second manager request, then admits after settlement', async () => {
+ const manager = makeManager();
+ let resolve!: (result: { contents: [] }) => void;
+ manager.readResource.mockImplementationOnce(
+ () =>
+ new Promise((done) => {
+ resolve = done;
+ }),
+ );
+ manager.readResource.mockResolvedValue({ contents: [] });
+ const { dependencies } = makeDependencies(manager);
+ const handler = createMCPAppsController(dependencies).readMCPResource;
+ const firstResponse = makeResponse();
+ const first = handler(
+ asHandlerRequest(admitted({ maxActive: 1 })),
+ asHandlerResponse(firstResponse),
+ jest.fn(),
+ );
+ for (let i = 0; i < 20 && !resolve; i++) await Promise.resolve();
+ expect(resolve).toBeDefined();
+ const blocked = makeResponse();
+ await handler(
+ asHandlerRequest(admitted({ maxActive: 1 })),
+ asHandlerResponse(blocked),
+ jest.fn(),
+ );
+ expect(blocked.status).toHaveBeenCalledWith(503);
+ expect(manager.readResource).toHaveBeenCalledTimes(1);
+ resolve({ contents: [] });
+ await first;
+ const allowed = makeResponse();
+ await handler(
+ asHandlerRequest(admitted({ maxActive: 1 })),
+ asHandlerResponse(allowed),
+ jest.fn(),
+ );
+ expect(manager.readResource).toHaveBeenCalledTimes(2);
+ expect(allowed.json).toHaveBeenCalledWith({ contents: [] });
+ });
+
+ it('returns 504 when upstream ignores the deadline and does not write its late reply', async () => {
+ const manager = makeManager();
+ let complete!: (value: { contents: [] }) => void;
+ manager.readResource.mockImplementation(
+ () =>
+ new Promise((resolve) => {
+ complete = resolve;
+ }),
+ );
+ const { dependencies } = makeDependencies(manager);
+ const response = makeResponse();
+ await createMCPAppsController(dependencies).readMCPResource(
+ asHandlerRequest(admitted({ timeoutMs: 20 })),
+ asHandlerResponse(response),
+ jest.fn(),
+ );
+ expect(response.status).toHaveBeenCalledWith(504);
+ complete({ contents: [] });
+ await new Promise<void>((resolve) => setImmediate(resolve));
+ expect(response.json).toHaveBeenCalledTimes(1);
+ expect(response.json).toHaveBeenCalledWith(
+ expect.objectContaining({ error: 'mcp_app_timeout' }),
+ );
+ });
+});
diff --git a/packages/api/src/mcp/apps/controller.ts b/packages/api/src/mcp/apps/controller.ts
index bebe5ef1e4..c6ced08f16 100644
--- a/packages/api/src/mcp/apps/controller.ts
+++ b/packages/api/src/mcp/apps/controller.ts
@@ -1,10 +1,11 @@
-import { resolveMCPAppsPolicy } from 'librechat-data-provider';
+import { resolveMCPAppsPolicy, resolveMCPAppOperationLimits } from 'librechat-data-provider';
+import type { MCPAppCspLimits, TMCPAppOperationLimits } from 'librechat-data-provider';
import type { PluginAuthMethods, TokenMethods } from '@librechat/data-schemas';
import type { Request, RequestHandler, Response } from 'express';
-import type { MCPAppCspLimits } from 'librechat-data-provider';
import type { MCPAppAllowlists, MCPAppsProxyManager, AuthenticatedMCPAppUser } from '../apps';
import type { UpstreamTokenProvider } from '../oauth/obo';
import type { FlowStateManager } from '~/flow/manager';
+import type { MCPAppOperationBudget } from './budget';
import type { MCPOAuthTokens } from '../oauth';
import type * as t from '../types';
import {
@@ -19,6 +20,7 @@ import {
resolveAppValidationContext,
resolveEffectiveAppServerConfig,
} from '../apps';
+import { assertMCPAppResultFits } from './budget';
import { buildSandboxResponse } from '../sandbox';
interface MCPAppsBody {
@@ -31,6 +33,7 @@ interface MCPAppsBody {
}
interface MCPAppsConfig {
+ mcpAppSandbox?: { operationLimits?: Partial<TMCPAppOperationLimits> };
mcpSettings?: {
apps?: boolean;
allowedDomains?: string[] | null;
@@ -54,6 +57,7 @@ export interface MCPAppsControllerDependencies {
sandboxFrameAncestors?: string;
readSandboxFile: (path: string, encoding: 'utf8') => string;
getManager: () => MCPAppsProxyManager;
+ getOperationBudget: () => MCPAppOperationBudget;
getFlowManager: () => FlowStateManager<MCPOAuthTokens | null>;
getAppConfig: (request: MCPAppsRequest) => Promise<MCPAppsConfig | undefined>;
getSandboxCspLimits: () => Promise<MCPAppCspLimits>;
@@ -208,32 +212,50 @@ export function createMCPAppsController(dependencies: MCPAppsControllerDependenc
const cancellation = attachCancellation(request, response);
try {
cancellation.signal.throwIfAborted();
- const body = request.body ?? {};
- const serverName = getServerName(body);
- const context = await resolveAppRequestContext({
- user,
- serverName,
- serverBinding: body.serverBinding,
- resolveServerConfig: () =>
- resolveEffectiveAppServerConfig({
- serverName,
+ const limits = resolveMCPAppOperationLimits(request.config?.mcpAppSandbox?.operationLimits);
+ const result = await dependencies.getOperationBudget().run(
+ cancellation.signal,
+ async (signal) => {
+ const body = request.body ?? {};
+ const serverName = getServerName(body);
+ const context = await resolveAppRequestContext({
user,
- mcpConfig: getAdmittedMCPConfig(request),
- ensureConfigServers: dependencies.ensureConfigServers,
- getAllServerConfigs: dependencies.getAllServerConfigs,
- recoverServerConfig: dependencies.recoverServerConfig,
- isAppServerConfig: dependencies.isAppServerConfig,
- }),
- findPluginAuthsByKeys: dependencies.findPluginAuthsByKeys,
- flowManager: dependencies.getFlowManager(),
- tokenMethods: dependencies.tokenMethods,
- onOAuthCredentialsChanging: dependencies.createOAuthCredentialsChanging(request),
- upstreamTokenProvider: dependencies.createUpstreamTokenProvider(request, response, user),
- allowlists: getAdmittedMCPAllowlists(request),
- signal: cancellation.signal,
- });
- cancellation.signal.throwIfAborted();
- const result = await options.proxy(dependencies.getManager(), context, body);
+ serverName,
+ serverBinding: body.serverBinding,
+ resolveServerConfig: () =>
+ resolveEffectiveAppServerConfig({
+ serverName,
+ user,
+ mcpConfig: getAdmittedMCPConfig(request),
+ ensureConfigServers: dependencies.ensureConfigServers,
+ getAllServerConfigs: dependencies.getAllServerConfigs,
+ recoverServerConfig: dependencies.recoverServerConfig,
+ isAppServerConfig: dependencies.isAppServerConfig,
+ }),
+ findPluginAuthsByKeys: dependencies.findPluginAuthsByKeys,
+ flowManager: dependencies.getFlowManager(),
+ tokenMethods: dependencies.tokenMethods,
+ onOAuthCredentialsChanging: dependencies.createOAuthCredentialsChanging(request),
+ upstreamTokenProvider: dependencies.createUpstreamTokenProvider(
+ request,
+ response,
+ user,
+ ),
+ allowlists: getAdmittedMCPAllowlists(request),
+ signal,
+ });
+ signal.throwIfAborted();
+ const result = await options.proxy(
+ dependencies.getManager(),
+ { ...context, operationLimits: limits },
+ body,
+ );
+ signal.throwIfAborted();
+ assertMCPAppResultFits(result, limits);
+ return result;
+ },
+ limits,
+ );
if (canWriteResponse(response)) {
response.json(result);
}
@@ -261,27 +283,37 @@ export function createMCPAppsController(dependencies: MCPAppsControllerDependenc
const cancellation = attachCancellation(request, response);
try {
cancellation.signal.throwIfAborted();
- const body = request.body ?? {};
- const serverName = getServerName(body);
- const allowlists = getAdmittedMCPAllowlists(request);
- const context = await resolveAppValidationContext({
- user,
- serverName,
- serverBinding: body.serverBinding,
- resolveServerConfig: () =>
- dependencies.resolveCachedAppServerConfig({
+ const limits = resolveMCPAppOperationLimits(request.config?.mcpAppSandbox?.operationLimits);
+ const result = await dependencies.getOperationBudget().run(
+ cancellation.signal,
+ async (signal) => {
+ const body = request.body ?? {};
+ const serverName = getServerName(body);
+ const allowlists = getAdmittedMCPAllowlists(request);
+ const context = await resolveAppValidationContext({
+ user,
serverName,
- userId: user.id,
- role: user.role,
- mcpConfig: getAdmittedMCPConfig(request),
- allowedDomains: allowlists.allowedDomains,
- allowedAddresses: allowlists.allowedAddresses,
- }),
- findPluginAuthsByKeys: dependencies.findPluginAuthsByKeys,
- signal: cancellation.signal,
- });
- cancellation.signal.throwIfAborted();
- const result = await validateAppServerBinding(dependencies.getManager(), context);
+ serverBinding: body.serverBinding,
+ resolveServerConfig: () =>
+ dependencies.resolveCachedAppServerConfig({
+ serverName,
+ userId: user.id,
+ role: user.role,
+ mcpConfig: getAdmittedMCPConfig(request),
+ allowedDomains: allowlists.allowedDomains,
+ allowedAddresses: allowlists.allowedAddresses,
+ }),
+ findPluginAuthsByKeys: dependencies.findPluginAuthsByKeys,
+ signal,
+ });
+ signal.throwIfAborted();
+ const result = await validateAppServerBinding(dependencies.getManager(), context);
+ signal.throwIfAborted();
+ assertMCPAppResultFits(result, limits);
+ return result;
+ },
+ limits,
+ );
if (canWriteResponse(response)) {
response.json(result);
}
diff --git a/packages/api/src/mcp/connection.ts b/packages/api/src/mcp/connection.ts
index c8ccfc776d..d44d917dde 100644
--- a/packages/api/src/mcp/connection.ts
+++ b/packages/api/src/mcp/connection.ts
@@ -23,6 +23,7 @@ import type {
} from 'undici';
import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js';
import type { ClientCapabilities } from '@modelcontextprotocol/sdk/types.js';
+import type { TMCPAppOperationLimits } from 'librechat-data-provider';
import type { MCPClientCapabilityProfile } from './capabilities';
import type { MCPOAuthTokens } from './oauth/types';
import type * as t from './types';
@@ -33,6 +34,11 @@ import {
MCPTransportAuthenticationError,
isStandaloneSseConflict,
} from './errors';
+import {
+ createMCPAppSSEEventGuard,
+ getMCPAppOperationLimits,
+ guardMCPAppSSEEvents,
+} from './apps/budget';
import { MCP_APPS_CAPABILITY_PROFILE, STANDARD_MCP_CAPABILITY_PROFILE } from './capabilities';
import { createSSRFSafeUndiciConnect, isSSRFTarget, resolveHostnameSSRF } from '~/auth';
import { projectMCPAppRuntimeTarget, type MCPAppRuntimeTarget } from './apps/binding';
@@ -296,10 +302,19 @@ function buildBlockedMCPResponseSSE(requestIds: JSONRPCRequestId[], message: str
return textEncoder.encode(events);
}
-function getMCPStreamableHTTPResponseLimits(): {
+function getMCPStreamableHTTPResponseLimits(
+ appProfile = false,
+ operationLimits?: TMCPAppOperationLimits,
+): {
maxResponseBytes: number;
maxLineBytes: number;
} {
+ if (appProfile) {
+ // Validated App-profile policy owns both bounds. Generic transport env defaults are for
+ // standard sessions and must not silently reject an operator-approved larger App event.
+ const maxBytes = getMCPAppOperationLimits(operationLimits).maxBytes;
+ return { maxResponseBytes: maxBytes, maxLineBytes: maxBytes };
+ }
return {
maxResponseBytes: getNonNegativeIntegerEnv(
'MCP_STREAMABLE_HTTP_MAX_RESPONSE_BYTES',
@@ -319,16 +334,39 @@ async function guardMCPStreamableHTTPResponse(
method: string;
url: string;
requestIds?: JSONRPCRequestId[];
+ appProfile?: boolean;
+ operationLimits?: TMCPAppOperationLimits;
+ onAppSSEOverflow?: () => void;
},
): Promise<UndiciResponse> {
- if (context.method === 'GET' || !response.body) {
+ const contentType = response.headers.get('content-type') ?? '';
+ const isEventStream = mediaTypeEssence(contentType) === 'text/event-stream';
+ if (context.method === 'GET') {
+ if (!context.appProfile) return response;
+ if (isEventStream && response.ok) {
+ return guardMCPAppSSEEvents(
+ response as unknown as Response,
+ getMCPAppOperationLimits(context.operationLimits).maxBytes,
+ context.onAppSSEOverflow,
+ ) as unknown as UndiciResponse;
+ }
+ // SDK GET error paths may call response.text(); bound those as ordinary HTTP bodies,
+ // including non-2xx responses mislabelled as an event stream.
+ }
+ if (!response.body) {
return response;
}
- const contentType = response.headers.get('content-type') ?? '';
- const isEventStream = mediaTypeEssence(contentType) === 'text/event-stream';
- const { maxResponseBytes, maxLineBytes } = getMCPStreamableHTTPResponseLimits();
+ const { maxResponseBytes, maxLineBytes } = getMCPStreamableHTTPResponseLimits(
+ context.appProfile,
+ context.operationLimits,
+ );
const canEmitFallbackSSEError = isEventStream && maxLineBytes > 0;
+ // SSE streams can emit many bounded events; cumulative bytes must not end a healthy App session.
+ const fitsAppEvent =
+ isEventStream && response.ok && context.appProfile
+ ? createMCPAppSSEEventGuard(maxResponseBytes)
+ : undefined;
if (!isEventStream && maxResponseBytes === 0) {
return response;
}
@@ -436,7 +474,10 @@ async function guardMCPStreamableHTTPResponse(
chunkCount += 1;
totalBytes += bytes.byteLength;
- if (maxResponseBytes > 0 && totalBytes > maxResponseBytes) {
+ if (
+ maxResponseBytes > 0 &&
+ (fitsAppEvent ? !fitsAppEvent(bytes) : totalBytes > maxResponseBytes)
+ ) {
blockResponse(controller, 'MCP response exceeded byte limit', {
chunkBytes: bytes.byteLength,
});
@@ -1004,6 +1045,7 @@ interface MCPConnectionParams {
/** The owner will replace this connection after a tools/list authentication rejection. */
directBearerRecoveryEnabled?: boolean;
capabilityProfile?: MCPClientCapabilityProfile;
+ operationLimits?: TMCPAppOperationLimits;
}
/** Result of an MCP `tools/list` request: one page of tools plus an optional pagination cursor. */
@@ -1030,6 +1072,7 @@ export class MCPConnection extends EventEmitter {
private readonly MAX_RECONNECT_ATTEMPTS = 3;
public readonly serverName: string;
public readonly capabilityProfile: MCPClientCapabilityProfile;
+ private readonly operationLimits?: TMCPAppOperationLimits;
private shouldStopReconnecting = false;
private isReconnecting = false;
private isInitializing = false;
@@ -1218,6 +1261,7 @@ export class MCPConnection extends EventEmitter {
this.options = params.serverConfig;
this.serverName = params.serverName;
this.capabilityProfile = params.capabilityProfile ?? STANDARD_MCP_CAPABILITY_PROFILE;
+ this.operationLimits = params.operationLimits;
this.userId = params.userId;
this.useSSRFProtection = params.useSSRFProtection === true;
this.allowedAddresses = params.allowedAddresses ?? null;
@@ -1276,6 +1320,8 @@ export class MCPConnection extends EventEmitter {
const agents = this.agents;
const logPrefix = this.getLogPrefix();
const rejectDirectBearerAuthentication = this.directBearerRecoveryEnabled;
+ const thisAppProfile = this.capabilityProfile === MCP_APPS_CAPABILITY_PROFILE;
+ const appOperationLimits = this.operationLimits;
const effectiveTimeout = timeout || DEFAULT_TIMEOUT;
const requestDispatchers = new Map<string, ManagedDispatcher>();
const ssrfConnects = new Map<string, ReturnType<typeof createSSRFSafeUndiciConnect>>();
@@ -1404,6 +1450,8 @@ export class MCPConnection extends EventEmitter {
method: (currentInit?.method ?? 'GET').toUpperCase(),
url: currentUrlString,
requestIds: getJSONRPCRequestIds(currentInit?.body),
+ appProfile: thisAppProfile,
+ operationLimits: appOperationLimits,
};
if (!isMethodPreservingRedirect || redirects >= MAX_REDIRECTS) {
@@ -1524,12 +1572,20 @@ export class MCPConnection extends EventEmitter {
// https://github.com/modelcontextprotocol/typescript-sdk/issues/216
env: { ...getDefaultEnvironment(), ...(options.env ?? {}) },
...(options.cwd !== undefined && { cwd: options.cwd }),
+ ...(this.capabilityProfile === MCP_APPS_CAPABILITY_PROFILE && {
+ maxBufferSize: getMCPAppOperationLimits(this.operationLimits).maxBytes,
+ }),
});
case 'websocket': {
if (!isWebSocketOptions(options)) {
throw new Error('Invalid options for websocket transport.');
}
+ if (this.capabilityProfile === MCP_APPS_CAPABILITY_PROFILE) {
+ // This SDK WebSocket transport JSON.parse's messages before exposing them and offers
+ // no maxPayload option. Fail closed for Apps rather than claim a post-parse cap is safe.
+ throw new Error('MCP Apps require a transport with a pre-parse response size limit');
+ }
this.url = options.url;
/**
* SSRF pre-check: always validate resolved IPs for WebSocket, regardless
@@ -1645,12 +1701,31 @@ export class MCPConnection extends EventEmitter {
}
}
}
- return undiciFetch(urlString, {
+ const response = await undiciFetch(urlString, {
...resolvedInit,
redirect: 'manual',
dispatcher: getSSEDispatcher(urlString),
headers: fetchHeaders,
});
+ return this.capabilityProfile === MCP_APPS_CAPABILITY_PROFILE
+ ? guardMCPStreamableHTTPResponse(response, {
+ logPrefix: this.getLogPrefix(),
+ method: 'GET',
+ url: urlString,
+ appProfile: true,
+ operationLimits: this.operationLimits,
+ onAppSSEOverflow: () => {
+ // Let the SDK observe its stream error and reject an in-flight start before
+ // closing EventSource. Closing synchronously leaves start() pending forever.
+ // A short bounded grace also prevents the SDK's automatic retry loop.
+ const stop = setTimeout(() => {
+ abortController.abort();
+ void transport.close().catch(() => undefined);
+ }, 30);
+ stop.unref?.();
+ },
+ })
+ : response;
},
},
fetch: this.createFetchFunction(
@@ -1659,6 +1734,7 @@ export class MCPConnection extends EventEmitter {
undefined,
sseConfiguredSecretHeaderKeys,
options.url,
+ this.capabilityProfile === MCP_APPS_CAPABILITY_PROFILE,
) as unknown as FetchLike,
});
diff --git a/packages/api/src/mcp/types/index.ts b/packages/api/src/mcp/types/index.ts
index fc03055bbb..a1c804241f 100644
--- a/packages/api/src/mcp/types/index.ts
+++ b/packages/api/src/mcp/types/index.ts
@@ -17,7 +17,12 @@ import type {
TextContent,
Tool,
} from '@modelcontextprotocol/sdk/types.js';
-import type { SearchResultData, UIResource, TPlugin } from 'librechat-data-provider';
+import type {
+ SearchResultData,
+ UIResource,
+ TPlugin,
+ TMCPAppOperationLimits,
+} from 'librechat-data-provider';
import type { TokenMethods, IUser } from '@librechat/data-schemas';
import type { LCTool } from '@librechat/agents';
import type {
@@ -250,6 +255,8 @@ export interface BasicConnectionOptions {
ephemeralConnection?: boolean;
/** Immutable client capabilities negotiated for this connection. */
capabilityProfile?: import('../capabilities').MCPClientCapabilityProfile;
+ /** Validated deployment limits captured for App-profile transport parsing. */
+ operationLimits?: TMCPAppOperationLimits;
}
/** User context for placeholder resolution in MCP connections (non-OAuth and OAuth alike) */
diff --git a/packages/data-provider/src/config.spec.ts b/packages/data-provider/src/config.spec.ts
index 9278b2fce9..223e45d1ce 100644
--- a/packages/data-provider/src/config.spec.ts
+++ b/packages/data-provider/src/config.spec.ts
@@ -15,6 +15,7 @@ import {
DEFAULT_MCP_APP_ACTION_PREVIEW_CHARS,
MAX_MCP_APP_ACTIVE_VIEWS,
MAX_MCP_APP_ACTION_PREVIEW_CHARS,
+ DEFAULT_MCP_APP_OPERATION_LIMITS,
MAX_MCP_APP_PERSISTED_BYTES,
resolveMCPAppRateLimits,
resolveMCPAppsPolicy,
@@ -2026,6 +2027,7 @@ describe('MCP Apps configuration', () => {
maxAdmissionRequestsPerMinute: DEFAULT_MCP_APP_ADMISSION_REQUESTS_PER_MINUTE,
maxActiveViews: DEFAULT_MCP_APP_MAX_ACTIVE_VIEWS,
maxActionPreviewChars: DEFAULT_MCP_APP_ACTION_PREVIEW_CHARS,
+ operationLimits: DEFAULT_MCP_APP_OPERATION_LIMITS,
});
expect(
configSchema.parse({
@@ -2038,6 +2040,7 @@ describe('MCP Apps configuration', () => {
maxAdmissionRequestsPerMinute: 480,
maxActiveViews: 7,
maxActionPreviewChars: 32768,
+ operationLimits: { maxBytes: 6 * 1024 * 1024, timeoutMs: 45_000, maxActive: 8 },
url: 'https://mcp-sandbox.example.com/api/mcp/sandbox',
},
}).mcpAppSandbox,
@@ -2049,6 +2052,7 @@ describe('MCP Apps configuration', () => {
maxAdmissionRequestsPerMinute: 480,
maxActiveViews: 7,
maxActionPreviewChars: 32768,
+ operationLimits: { maxBytes: 6 * 1024 * 1024, timeoutMs: 45_000, maxActive: 8 },
url: 'https://mcp-sandbox.example.com/api/mcp/sandbox',
});
for (const mcpAppSandbox of [
@@ -2072,6 +2076,11 @@ describe('MCP Apps configuration', () => {
{ maxActionPreviewChars: 0 },
{ maxActionPreviewChars: 1.5 },
{ maxActionPreviewChars: MAX_MCP_APP_ACTION_PREVIEW_CHARS + 1 },
+ { operationLimits: { maxBytes: 0 } },
+ { operationLimits: { maxBytes: 16 * 1024 * 1024 + 1 } },
+ { operationLimits: { timeoutMs: 10 * 60_000 + 1 } },
+ { operationLimits: { maxActive: 0 } },
+ { operationLimits: { maxActive: 257 } },
{ url: '/api/mcp/sandbox' },
{ url: 'ftp://mcp-sandbox.example.com/api/mcp/sandbox' },
]) {
@@ -2079,6 +2088,25 @@ describe('MCP Apps configuration', () => {
}
});
+ it('carries the validated App operation limits to server-side policy consumers', () => {
+ const sandbox = configSchema.parse({
+ version: '1.2.1',
+ mcpAppSandbox: { operationLimits: { maxBytes: 2 * 1024 * 1024 } },
+ }).mcpAppSandbox;
+ expect(
+ resolveMCPAppsPolicy(
+ true,
+ undefined,
+ undefined,
+ undefined,
+ undefined,
+ undefined,
+ undefined,
+ sandbox.operationLimits,
+ ).operationLimits,
+ ).toEqual({ ...DEFAULT_MCP_APP_OPERATION_LIMITS, maxBytes: 2 * 1024 * 1024 });
+ });
+
it('publishes optional runtime sandbox fields in the effective policy', () => {
expect(
resolveMCPAppsPolicy(
diff --git a/packages/data-provider/src/config.ts b/packages/data-provider/src/config.ts
index 55f55addf0..934eb8a2f0 100644
--- a/packages/data-provider/src/config.ts
+++ b/packages/data-provider/src/config.ts
@@ -2632,6 +2632,24 @@ export type StartupConfigContext = 'share';
*/
export type EndpointsDropParamsMap = Record<string, string[] | Record<string, string[]>>;
+export type TMCPAppOperationLimits = {
+ maxBytes: number;
+ timeoutMs: number;
+ maxActive: number;
+};
+
+export const DEFAULT_MCP_APP_OPERATION_LIMITS: TMCPAppOperationLimits = {
+ maxBytes: 4 * 1024 * 1024,
+ timeoutMs: 30_000,
+ maxActive: 16,
+};
+
+export function resolveMCPAppOperationLimits(
+ limits?: Partial<TMCPAppOperationLimits>,
+): TMCPAppOperationLimits {
+ return { ...DEFAULT_MCP_APP_OPERATION_LIMITS, ...limits };
+}
+
export type TMCPAppsPolicy = {
enabled: boolean;
legacyHtmlEnabled: boolean;
@@ -2644,6 +2662,8 @@ export type TMCPAppsPolicy = {
maxActiveViews?: number;
/** Maximum UTF-16 characters displayed for an App-requested action before denying it. */
maxActionPreviewChars?: number;
+ /** Server-side bounds for App operations; never derived from App-provided content. */
+ operationLimits?: TMCPAppOperationLimits;
/** Deployment-owned dedicated Sandbox Proxy URL published to authenticated clients. */
sandboxUrl?: string;
};
@@ -2676,6 +2696,7 @@ export function resolveMCPAppsPolicy(
sandboxUrl?: string,
maxActiveViews = DEFAULT_MCP_APP_MAX_ACTIVE_VIEWS,
maxActionPreviewChars = DEFAULT_MCP_APP_ACTION_PREVIEW_CHARS,
+ operationLimits?: Partial<TMCPAppOperationLimits>,
): TMCPAppsPolicy {
return {
enabled: value === true,
@@ -2685,6 +2706,9 @@ export function resolveMCPAppsPolicy(
maxAdmissionRequestsPerMinute,
maxActiveViews,
maxActionPreviewChars,
+ ...(operationLimits != null
+ ? { operationLimits: resolveMCPAppOperationLimits(operationLimits) }
+ : {}),
...(sandboxUrl !== undefined ? { sandboxUrl } : {}),
};
}
@@ -3378,6 +3402,28 @@ export const configSchema = z.object({
.positive()
.max(MAX_MCP_APP_ACTION_PREVIEW_CHARS)
.default(DEFAULT_MCP_APP_ACTION_PREVIEW_CHARS),
+ operationLimits: z
+ .object({
+ maxBytes: z
+ .number()
+ .int()
+ .positive()
+ .max(16 * 1024 * 1024)
+ .default(DEFAULT_MCP_APP_OPERATION_LIMITS.maxBytes),
+ timeoutMs: z
+ .number()
+ .int()
+ .positive()
+ .max(10 * 60_000)
+ .default(DEFAULT_MCP_APP_OPERATION_LIMITS.timeoutMs),
+ maxActive: z
+ .number()
+ .int()
+ .positive()
+ .max(256)
+ .default(DEFAULT_MCP_APP_OPERATION_LIMITS.maxActive),
+ })
+ .default({}),
})
.default({}),
mcpSettings: z