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

🏎️ perf: Parallelize Independent Agent and Schedule Lookups (#17465)

Marco Beretta7e7f559ea61c
diff --git a/api/server/services/Endpoints/agents/addedConvo.js b/api/server/services/Endpoints/agents/addedConvo.js
index 93caeec43a..d8876ac614 100644
--- a/api/server/services/Endpoints/agents/addedConvo.js
+++ b/api/server/services/Endpoints/agents/addedConvo.js
@@ -162,6 +162,7 @@ const processAddedConvo = async ({
names: selectedModelSpec.skills,
accessibleSkillIds,
getSkillByName: skillDbMethods.getSkillByName,
+ appConfig: req.config,
});
addedAgent.skills_enabled = true;
addedAgent.skills = resolvedSkillIds.map((id) => id.toString());
diff --git a/api/server/services/Endpoints/agents/addedConvo.spec.js b/api/server/services/Endpoints/agents/addedConvo.spec.js
index 8224eb4f5d..a1f6a85726 100644
--- a/api/server/services/Endpoints/agents/addedConvo.spec.js
+++ b/api/server/services/Endpoints/agents/addedConvo.spec.js
@@ -309,11 +309,13 @@ describe('processAddedConvo', () => {
}),
);
- expect(mockResolveModelSpecSkillIds).toHaveBeenCalledWith({
- names: ['finance-analyst'],
- accessibleSkillIds: [accessibleSkillId],
- getSkillByName: mockRegistryGetSkillByName,
- });
+ expect(mockResolveModelSpecSkillIds).toHaveBeenCalledWith(
+ expect.objectContaining({
+ names: ['finance-analyst'],
+ accessibleSkillIds: [accessibleSkillId],
+ getSkillByName: mockRegistryGetSkillByName,
+ }),
+ );
expect(mockResolveAgentScopedSkillIds).toHaveBeenNthCalledWith(1, {
agent: expect.objectContaining({
id: Constants.EPHEMERAL_AGENT_ID,
diff --git a/api/server/services/Endpoints/agents/initialize.js b/api/server/services/Endpoints/agents/initialize.js
index 2f102f8f8d..a76b677ac5 100644
--- a/api/server/services/Endpoints/agents/initialize.js
+++ b/api/server/services/Endpoints/agents/initialize.js
@@ -766,6 +766,7 @@ const initializeClientWithProvider = async ({
names: selectedModelSpec.skills,
accessibleSkillIds,
getSkillByName: skillDbMethods.getSkillByName,
+ appConfig,
});
primaryAgent.skills_enabled = true;
primaryAgent.skills = resolvedSkillIds.map((id) => id.toString());
diff --git a/e2e/specs/mock/agent-handoffs.spec.ts b/e2e/specs/mock/agent-handoffs.spec.ts
index a450c25e3b..ad62c96ec5 100644
--- a/e2e/specs/mock/agent-handoffs.spec.ts
+++ b/e2e/specs/mock/agent-handoffs.spec.ts
@@ -167,6 +167,11 @@ async function selectAgentForChat(page: Page, agentName: string): Promise<void>
await page.getByRole('option', { name: agentName }).click();
await expect(form.getByLabel('Agent name')).toHaveValue(agentName);
await form.getByRole('button', { name: 'Select Agent' }).click();
+ /** On narrow viewports the builder drawer stays open over the composer. */
+ const closeSidebar = page.getByRole('button', { name: 'Close sidebar' });
+ if (await closeSidebar.isVisible()) {
+ await closeSidebar.click();
+ }
await expect(page.getByRole('textbox', { name: 'Message input' })).toBeVisible();
}
@@ -991,7 +996,9 @@ test.describe('agent handoffs', () => {
}
});
- test('executes a transitive router-to-specialist-to-reviewer handoff', async ({ page }) => {
+ test('executes a transitive router-to-specialist-to-reviewer handoff @scenario:connected-agents-transitive', async ({
+ page,
+ }) => {
test.setTimeout(180000);
await page.goto('/c/new', { timeout: 10000 });
@@ -1073,7 +1080,9 @@ test.describe('agent handoffs', () => {
}
});
- test('executes simultaneous handoffs and renders both transfer branches', async ({ page }) => {
+ test('executes simultaneous handoffs and renders both transfer branches @scenario:connected-agents-simultaneous', async ({
+ page,
+ }) => {
test.setTimeout(180000);
await page.goto('/c/new', { timeout: 10000 });
diff --git a/e2e/specs/mock/model-spec-skills.spec.ts b/e2e/specs/mock/model-spec-skills.spec.ts
index e1fc905acb..184868e428 100644
--- a/e2e/specs/mock/model-spec-skills.spec.ts
+++ b/e2e/specs/mock/model-spec-skills.spec.ts
@@ -181,7 +181,7 @@ async function deleteInaccessibleSkill(skillId: string) {
}
test.describe('model spec skills', () => {
- test('loads accessible configured skills and skips missing or inaccessible names', async ({
+ test('loads accessible configured skills and skips missing or inaccessible names @scenario:model-spec-skills-load', async ({
page,
}) => {
test.setTimeout(120000);
diff --git a/e2e/specs/mock/schedules-execution.spec.ts b/e2e/specs/mock/schedules-execution.spec.ts
index c5a9bd161b..30dfb13e5f 100644
--- a/e2e/specs/mock/schedules-execution.spec.ts
+++ b/e2e/specs/mock/schedules-execution.spec.ts
@@ -82,7 +82,9 @@ test.describe('scheduled chat execution', () => {
* where a broken loopback URL, a rejected fire token, or a lost schedule identity
* surfaces. The smoke spec only proves the card renders.
*/
- test('Run Now generates a conversation and records it on the schedule', async ({ page }) => {
+ test('Run Now generates a conversation and records it on the schedule @scenario:schedule-run-now', async ({
+ page,
+ }) => {
test.setTimeout(120000);
await page.goto('/c/new', { timeout: 15000 });
const token = await getAccessToken(page);
diff --git a/e2e/specs/mock/unified-upload.spec.ts b/e2e/specs/mock/unified-upload.spec.ts
index e36eea60d8..bb89a05870 100644
--- a/e2e/specs/mock/unified-upload.spec.ts
+++ b/e2e/specs/mock/unified-upload.spec.ts
@@ -69,7 +69,7 @@ test.describe('unified file upload', () => {
expect(persisted?.llmDeliveryPath).toBe('none');
});
- test('single attach button still delivers a provider-routed upload and shows it in chat', async ({
+ test('single attach button still delivers a provider-routed upload and shows it in chat @scenario:provider-upload-delivered', async ({
page,
}) => {
test.setTimeout(120000);
diff --git a/librechat.example.yaml b/librechat.example.yaml
index 874483cd0d..498b41582f 100644
--- a/librechat.example.yaml
+++ b/librechat.example.yaml
@@ -575,6 +575,14 @@ registration:
# messages:
# maxPageSize: 1000 # default: 1000
+# Upper bounds on independent lookups a request runs at once (1 to 32). Each omitted key keeps the default shown.
+# concurrency:
+# connectedAgentLoads: 4 # connected agents initialized together per level of a handoff graph
+# mcpCatalogLookups: 4 # MCP tool catalog reads when assembling an ephemeral or added agent
+# skillNameLookups: 8 # model spec skill name resolutions
+# skillFileInspections: 4 # bundled skill file inspections before a skill is primed
+# resumeAgentLoads: 4 # agent reads while building the resume snapshot of a paused run
+
# rateLimits:
# # Per-user MCP App browser-route limits. Each limit uses a fixed one-minute window.
# mcpApps:
diff --git a/packages/api/src/agents/__tests__/skills.test.ts b/packages/api/src/agents/__tests__/skills.test.ts
index 7fda03560f..1f372eb0f8 100644
--- a/packages/api/src/agents/__tests__/skills.test.ts
+++ b/packages/api/src/agents/__tests__/skills.test.ts
@@ -607,7 +607,7 @@ describe('resolveModelSpecSkillIds', () => {
});
});
- it('resolves configured names sequentially to avoid query bursts', async () => {
+ it('resolves configured names concurrently and keeps configured order', async () => {
const firstId = new Types.ObjectId();
const secondId = new Types.ObjectId();
const order: string[] = [];
@@ -643,12 +643,32 @@ describe('resolveModelSpecSkillIds', () => {
});
await Promise.resolve();
- expect(order).toEqual(['start:first']);
+ expect(order).toEqual(['start:first', 'start:second', 'end:second']);
releaseFirst();
const result = await promise;
expect(result.map((id) => id.toString())).toEqual([firstId.toString(), secondId.toString()]);
+ expect(order).toEqual(['start:first', 'start:second', 'end:second', 'end:first']);
+ });
+
+ it('resolves names one at a time when concurrency.skillNameLookups is 1', async () => {
+ const ids = [new Types.ObjectId(), new Types.ObjectId()];
+ const order: string[] = [];
+ const getSkillByName = jest.fn(async (name: string) => {
+ order.push(`start:${name}`);
+ await Promise.resolve();
+ order.push(`end:${name}`);
+ return { _id: ids[name === 'first' ? 0 : 1], name, body: 'body', author: userObjectId };
+ });
+
+ await resolveModelSpecSkillIds({
+ names: ['first', 'second'],
+ accessibleSkillIds: ids,
+ getSkillByName,
+ appConfig: { concurrency: { skillNameLookups: 1 } },
+ });
+
expect(order).toEqual(['start:first', 'end:first', 'start:second', 'end:second']);
});
diff --git a/packages/api/src/agents/added.ts b/packages/api/src/agents/added.ts
index 970072af92..0b0e0f8a7f 100644
--- a/packages/api/src/agents/added.ts
+++ b/packages/api/src/agents/added.ts
@@ -23,7 +23,9 @@ import { resolveMCPClientCapabilityProfile } from '~/mcp/capabilities';
import { synthesizeBackgroundToolOptions } from '~/agents/background';
import { mergeSynthesizedToolOptions } from '~/agents/selection';
import { synthesizeIntentToolOptions } from '~/agents/intent';
+import { getLookupConcurrency } from '~/utils/concurrency';
import { getCustomEndpointConfig } from '~/app/config';
+import { mapWithConcurrency } from '~/utils/promise';
const { mcp_all, mcp_delimiter } = Constants;
@@ -244,26 +246,25 @@ export async function loadAddedAgent(
tools.push(ASK_USER_QUESTION_TOOL_NAME);
}
- const addedServers = new Set<string>();
- for (const mcpServer of mcpServers) {
- if (addedServers.has(mcpServer)) {
- continue;
- }
- /** Address durable catalogs by the effective request overlay; request-scoped
- * overlays still expand fresh through `mcp_all`. */
- const rawOverlayConfig = appConfig?.mcpConfig?.[mcpServer];
- const overlayConfig = rawOverlayConfig ? validateMCPServerConfig(rawOverlayConfig) : undefined;
- const serverTools =
- overlayConfig && requiresEphemeralUserConnection(overlayConfig)
- ? null
- : await deps.getMCPServerTools(userId, mcpServer, overlayConfig, capabilityProfile);
- if (!serverTools) {
- tools.push(`${mcp_all}${mcp_delimiter}${mcpServer}`);
- addedServers.add(mcpServer);
- continue;
- }
- tools.push(...Object.keys(serverTools));
- addedServers.add(mcpServer);
+ const serverToolKeys = await mapWithConcurrency(
+ [...mcpServers],
+ getLookupConcurrency(appConfig, 'mcpCatalogLookups'),
+ async (mcpServer): Promise<string[]> => {
+ /** Address durable catalogs by the effective request overlay; request-scoped
+ * overlays still expand fresh through `mcp_all`. */
+ const rawOverlayConfig = appConfig?.mcpConfig?.[mcpServer];
+ const overlayConfig = rawOverlayConfig
+ ? validateMCPServerConfig(rawOverlayConfig)
+ : undefined;
+ const serverTools =
+ overlayConfig && requiresEphemeralUserConnection(overlayConfig)
+ ? null
+ : await deps.getMCPServerTools(userId, mcpServer, overlayConfig, capabilityProfile);
+ return serverTools ? Object.keys(serverTools) : [`${mcp_all}${mcp_delimiter}${mcpServer}`];
+ },
+ );
+ for (const keys of serverToolKeys) {
+ tools.push(...keys);
}
const model_parameters: Record<string, unknown> = {};
diff --git a/packages/api/src/agents/discovery.spec.ts b/packages/api/src/agents/discovery.spec.ts
index a7fbb6e36c..5b731ca275 100644
--- a/packages/api/src/agents/discovery.spec.ts
+++ b/packages/api/src/agents/discovery.spec.ts
@@ -1069,6 +1069,77 @@ describe('discoverConnectedAgents', () => {
expect(result.edges).toHaveLength(2);
});
+ it('initializes a sibling referenced by another member of the same level once', async () => {
+ const primaryConfig = makeConfig('A', [
+ { from: 'A', to: 'B', edgeType: 'handoff' },
+ { from: 'A', to: 'C', edgeType: 'handoff' },
+ ]);
+ const agents: Record<string, Agent> = {
+ B: makeAgent('B', [{ from: 'B', to: 'C', edgeType: 'handoff' }]),
+ C: makeAgent('C'),
+ };
+ const getAgent = jest.fn(async ({ id }: { id: string }) => agents[id] ?? null);
+
+ const result = await discoverConnectedAgents(
+ {
+ req: makeReq(),
+ res: makeRes(),
+ primaryConfig,
+ allowedProviders: new Set(),
+ modelsConfig: { openai: ['gpt-4o'] },
+ loadTools: jest.fn(),
+ },
+ {
+ getAgent,
+ checkPermission: jest.fn().mockResolvedValue(true),
+ logViolation: jest.fn(),
+ db: {} as never,
+ },
+ );
+
+ expect(getAgent.mock.calls.map(([arg]) => arg.id).sort()).toEqual(['B', 'C']);
+ expect([...result.agentConfigs.keys()]).toEqual(['B', 'C']);
+ });
+
+ it('initializes one connected agent at a time when concurrency.connectedAgentLoads is 1', async () => {
+ let active = 0;
+ let peak = 0;
+ mockInitializeAgent.mockImplementation(async ({ agent }: { agent: Agent }) => {
+ active++;
+ peak = Math.max(peak, active);
+ await new Promise((resolve) => setTimeout(resolve, 5));
+ active--;
+ return makeConfig(agent.id);
+ });
+ const req = {
+ user: { id: 'u1', role: 'USER' },
+ config: { concurrency: { connectedAgentLoads: 1 } },
+ } as unknown as ServerRequest;
+ const primaryConfig = makeConfig('A', [
+ { from: 'A', to: 'B', edgeType: 'handoff' },
+ { from: 'A', to: 'C', edgeType: 'handoff' },
+ ]);
+
+ await discoverConnectedAgents(
+ {
+ req,
+ res: makeRes(),
+ primaryConfig,
+ allowedProviders: new Set(),
+ modelsConfig: { openai: ['gpt-4o'] },
+ loadTools: jest.fn(),
+ },
+ {
+ getAgent: jest.fn(async ({ id }: { id: string }) => makeAgent(id)),
+ checkPermission: jest.fn().mockResolvedValue(true),
+ logViolation: jest.fn(),
+ db: {} as never,
+ },
+ );
+
+ expect(peak).toBe(1);
+ });
+
it('processes legacy agent_ids chain and adds sequential chain edges', async () => {
const primaryConfig = makeConfig('A', []);
@@ -1205,6 +1276,78 @@ describe('discoverConnectedAgents', () => {
).rejects.toBe(toolError);
});
+ it('propagates a fatal error without waiting for a blocked later sibling', async () => {
+ const toolError = Object.assign(new Error('tools unavailable'), {
+ code: 'AGENT_EXPECTED_MCP_TOOLS_UNAVAILABLE',
+ statusCode: 503,
+ });
+ mockInitializeAgent.mockImplementation(async ({ agent }: { agent: Agent }) => {
+ if (agent.id === 'B') {
+ throw toolError;
+ }
+ return new Promise<InitializedAgent>(() => undefined);
+ });
+ const primaryConfig = makeConfig('A', [
+ { from: 'A', to: 'B', edgeType: 'handoff' },
+ { from: 'A', to: 'C', edgeType: 'handoff' },
+ ]);
+
+ await expect(
+ discoverConnectedAgents(
+ {
+ req: makeReq(),
+ res: makeRes(),
+ primaryConfig,
+ allowedProviders: new Set(),
+ modelsConfig: { openai: ['gpt-4o'] },
+ loadTools: jest.fn(),
+ },
+ {
+ getAgent: jest.fn(async ({ id }: { id: string }) => makeAgent(id)),
+ checkPermission: jest.fn().mockResolvedValue(true),
+ logViolation: jest.fn(),
+ db: {} as never,
+ },
+ ),
+ ).rejects.toBe(toolError);
+ });
+
+ it('propagates a fatal error even when an earlier sibling is still pending', async () => {
+ const toolError = Object.assign(new Error('tools unavailable'), {
+ code: 'AGENT_EXPECTED_MCP_TOOLS_UNAVAILABLE',
+ statusCode: 503,
+ });
+ mockInitializeAgent.mockImplementation(async ({ agent }: { agent: Agent }) => {
+ if (agent.id === 'C') {
+ throw toolError;
+ }
+ return new Promise<InitializedAgent>(() => undefined);
+ });
+ const primaryConfig = makeConfig('A', [
+ { from: 'A', to: 'B', edgeType: 'handoff' },
+ { from: 'A', to: 'C', edgeType: 'handoff' },
+ ]);
+
+ await expect(
+ discoverConnectedAgents(
+ {
+ req: makeReq(),
+ res: makeRes(),
+ primaryConfig,
+ allowedProviders: new Set(),
+ modelsConfig: { openai: ['gpt-4o'] },
+ loadTools: jest.fn(),
+ },
+ {
+ getAgent: jest.fn(async ({ id }: { id: string }) => makeAgent(id)),
+ checkPermission: jest.fn().mockResolvedValue(true),
+ logViolation: jest.fn(),
+ db: {} as never,
+ },
+ ),
+ ).rejects.toBe(toolError);
+ });
+
it.each([
['expected MCP tools are unavailable', 'AGENT_EXPECTED_MCP_TOOLS_UNAVAILABLE', 503],
['CodeAPI resource recovery is required', ErrorTypes.RESOURCE_RECOVERY_REQUIRED, 409],
diff --git a/packages/api/src/agents/discovery.ts b/packages/api/src/agents/discovery.ts
index 18b9cb4f77..65069601a1 100644
--- a/packages/api/src/agents/discovery.ts
+++ b/packages/api/src/agents/discovery.ts
@@ -25,6 +25,7 @@ import { initializeAgent as defaultInitializeAgent } from './initialize';
import { createEdgeCollector, resolveReachableGraph } from './edges';
import { isFatalAgentInitializationError } from './errors';
import { createConcurrencyLimiter } from '~/utils/promise';
+import { getLookupConcurrency } from '~/utils/concurrency';
import { createSequentialChainEdges } from './chain';
import { detachOnAbort } from '~/utils/promises';
@@ -443,11 +444,17 @@ export async function discoverConnectedAgents(
onAgentSkipped?.(agentId);
};
- const processAgent = async (agentId: string): Promise<Agent | null> => {
- const loaded = await initializeReferencedAgent(agentId, params, {
+ const loadAgent = (agentId: string): ReturnType<typeof initializeReferencedAgent> =>
+ initializeReferencedAgent(agentId, params, {
...deps,
onAgentSkipped: markSkipped,
});
+
+ /** Side effects are applied in discovery order so merges stay deterministic. */
+ const applyLoaded = (
+ agentId: string,
+ loaded: Awaited<ReturnType<typeof initializeReferencedAgent>>,
+ ): Agent | null => {
if (!loaded) {
return null;
}
@@ -466,6 +473,9 @@ export async function discoverConnectedAgents(
return agent;
};
+ const processAgent = async (agentId: string): Promise<Agent | null> =>
+ applyLoaded(agentId, await loadAgent(agentId));
+
const checkAgentInit = (agentId: string): boolean =>
agentId === primaryConfig.id || agentConfigs.has(agentId);
@@ -476,20 +486,53 @@ export async function discoverConnectedAgents(
collectEdges(primaryConfig.edges);
+ const loadDiscovered = createConcurrencyLimiter(
+ getLookupConcurrency(params.req.config, 'connectedAgentLoads'),
+ );
while (agentsToProcess.size > 0) {
- const agentId = agentsToProcess.values().next().value as string;
- agentsToProcess.delete(agentId);
- try {
- const agent = await processAgent(agentId);
+ /** One BFS level: agents already queued are independent, so load them together
+ * and fold results back in queue order. Edges they expose form the next level. */
+ const level = [...agentsToProcess];
+ agentsToProcess.clear();
+ /** Outcomes never reject; a fatal error also rejects `fatal`, so it surfaces as soon
+ * as it happens instead of after earlier, slower siblings finish. */
+ let rejectFatal: (error: unknown) => void = () => undefined;
+ const fatal = new Promise<never>((_, reject) => {
+ rejectFatal = reject;
+ });
+ fatal.catch(() => undefined);
+ const outcomes = level.map((agentId) =>
+ loadDiscovered(async () => {
+ try {
+ return { loaded: await loadAgent(agentId) };
+ } catch (error) {
+ if (isFatalAgentInitializationError(error, { signal: params.signal })) {
+ rejectFatal(error);
+ }
+ return { error };
+ }
+ }),
+ );
+ /** Stage every loaded config before collecting edges, so a sibling referenced by
+ * another member of the level is not queued a second time. */
+ const loadedAgents: Array<Agent | null> = [];
+ for (let index = 0; index < level.length; index++) {
+ const agentId = level[index];
+ const outcome = await Promise.race([outcomes[index], fatal]);
+ if ('error' in outcome) {
+ if (isFatalAgentInitializationError(outcome.error, { signal: params.signal })) {
+ throw outcome.error;
+ }
+ logger.error(`[discoverConnectedAgents] Error processing agent ${agentId}:`, outcome.error);
+ markSkipped(agentId);
+ continue;
+ }
+ loadedAgents.push(applyLoaded(agentId, outcome.loaded));
+ }
+ for (const agent of loadedAgents) {
if (agent?.edges?.length) {
collectEdges(agent.edges);
}
- } catch (err) {
- if (isFatalAgentInitializationError(err, { signal: params.signal })) {
- throw err;
- }
- logger.error(`[discoverConnectedAgents] Error processing agent ${agentId}:`, err);
- markSkipped(agentId);
}
}
diff --git a/packages/api/src/agents/hitl/inspection.spec.ts b/packages/api/src/agents/hitl/inspection.spec.ts
index 214cc2f924..aeacbbd0b4 100644
--- a/packages/api/src/agents/hitl/inspection.spec.ts
+++ b/packages/api/src/agents/hitl/inspection.spec.ts
@@ -1,6 +1,6 @@
-import { Constants } from 'librechat-data-provider';
+import { Constants, MAX_SUBAGENT_GRAPH_NODES } from 'librechat-data-provider';
import type { AppConfig } from '@librechat/data-schemas';
-import { getResumeContentInspection } from './inspection';
+import { getResumeAgentSnapshot, getResumeContentInspection } from './inspection';
const strictExtractedTextConfig = {
filters: {
@@ -162,3 +162,46 @@ describe('resume file inspection trust boundary', () => {
expect(getMessages).not.toHaveBeenCalled();
});
});
+
+describe('getResumeAgentSnapshot', () => {
+ it('stops fetching references once the node budget is exceeded', async () => {
+ const total = MAX_SUBAGENT_GRAPH_NODES * 20;
+ const edges = Array.from({ length: total }, (_, index) => ({
+ from: 'primary',
+ to: `child-${index}`,
+ edgeType: 'handoff' as const,
+ }));
+ const getAgent = jest.fn(async ({ id }: { id: string }) => ({ id }) as never);
+
+ await expect(
+ getResumeAgentSnapshot({
+ primaryAgent: { id: 'primary', edges } as never,
+ subagentsEnabled: false,
+ getAgent,
+ }),
+ ).rejects.toThrow();
+ expect(getAgent.mock.calls.length).toBeLessThan(MAX_SUBAGENT_GRAPH_NODES + 10);
+ });
+
+ it('rejects on a failed lookup without waiting for a stalled sibling', async () => {
+ const edges = ['slow', 'fails'].map((to) => ({
+ from: 'primary',
+ to,
+ edgeType: 'handoff' as const,
+ }));
+ const getAgent = jest.fn(async ({ id }: { id: string }) => {
+ if (id === 'slow') {
+ return new Promise<never>(() => undefined);
+ }
+ throw new Error('lookup failed');
+ });
+
+ await expect(
+ getResumeAgentSnapshot({
+ primaryAgent: { id: 'primary', edges } as never,
+ subagentsEnabled: false,
+ getAgent,
+ }),
+ ).rejects.toThrow();
+ });
+});
diff --git a/packages/api/src/agents/hitl/inspection.ts b/packages/api/src/agents/hitl/inspection.ts
index 536a91b568..e42269d982 100644
--- a/packages/api/src/agents/hitl/inspection.ts
+++ b/packages/api/src/agents/hitl/inspection.ts
@@ -18,6 +18,7 @@ import {
type CanonicalFileInspectionFile,
} from '~/protection/files';
import { ContentTraversalLimitError } from '~/protection/adapters/nested';
+import { getLookupConcurrency } from '~/utils/concurrency';
import { collectReachableAgents } from '../traversal';
import { getEdgeParticipants } from '../edges';
@@ -62,6 +63,7 @@ interface ResumeAgentSnapshotInput {
subagentsEnabled: boolean;
getAgent: (filter: { id: string }) => Promise<Agent | null>;
canAccessAgent?: (agent: Agent) => Promise<boolean>;
+ appConfig?: Pick<AppConfig, 'concurrency'>;
}
type GetResumeMessages = (filter: {
@@ -152,6 +154,7 @@ export async function getResumeAgentSnapshot({
subagentsEnabled,
getAgent,
canAccessAgent,
+ appConfig,
}: ResumeAgentSnapshotInput): Promise<ResumeSnapshotAgent[]> {
const primary: ResumeSnapshotAgent = {
...primaryAgent,
@@ -161,6 +164,7 @@ export async function getResumeAgentSnapshot({
},
subagentAgentConfigs: [],
};
+ const resumeLoadConcurrency = getLookupConcurrency(appConfig, 'resumeAgentLoads');
const roots: ResumeSnapshotAgent[] = [primary];
const byId = new Map<string, ResumeSnapshotAgent>([[primary.id, primary]]);
@@ -180,29 +184,44 @@ export async function getResumeAgentSnapshot({
for (let index = 0; index < pending.length; index++) {
const { agent, includeLegacyChain } = pending[index];
const children: ResumeSnapshotAgent[] = [];
- for (const id of getReferencedAgentIds(agent, subagentsEnabled, includeLegacyChain)) {
- let child = byId.get(id);
- if (!child) {
- let loaded: Agent | null;
- try {
- loaded = await getAgent({ id });
- } catch {
- throw new ContentTraversalLimitError();
- }
- if (!loaded) {
- continue;
- }
- if (canAccessAgent && !(await canAccessAgent(loaded))) {
- continue;
- }
- if (byId.size >= MAX_SUBAGENT_GRAPH_NODES) {
- throw new ContentTraversalLimitError();
+ const referencedIds = getReferencedAgentIds(agent, subagentsEnabled, includeLegacyChain);
+ /** Fetch one small chunk of unseen references together, then fold it in reference
+ * order, so the node limit and de-duplication match a serial walk, a failed lookup
+ * rejects at once, and an over-budget graph stops fetching after at most one chunk. */
+ for (let start = 0; start < referencedIds.length; start += resumeLoadConcurrency) {
+ const chunk = referencedIds.slice(start, start + resumeLoadConcurrency);
+ const unseenIds = [...new Set(chunk)].filter((id) => !byId.has(id));
+ const fetched = new Map<string, Agent | null>();
+ await Promise.all(
+ unseenIds.map(async (id) => {
+ let loaded: Agent | null;
+ try {
+ loaded = await getAgent({ id });
+ } catch {
+ throw new ContentTraversalLimitError();
+ }
+ if (loaded && canAccessAgent && !(await canAccessAgent(loaded))) {
+ loaded = null;
+ }
+ fetched.set(id, loaded);
+ }),
+ );
+ for (const id of chunk) {
+ let child = byId.get(id);
+ if (!child) {
+ const loaded = fetched.get(id);
+ if (!loaded) {
+ continue;
+ }
+ if (byId.size >= MAX_SUBAGENT_GRAPH_NODES) {
+ throw new ContentTraversalLimitError();
+ }
+ child = { ...loaded, subagentAgentConfigs: [] };
+ byId.set(id, child);
+ pending.push({ agent: child, includeLegacyChain: false });
}
- child = { ...loaded, subagentAgentConfigs: [] };
- byId.set(id, child);
- pending.push({ agent: child, includeLegacyChain: false });
+ children.push(child);
}
- children.push(child);
}
agent.subagentAgentConfigs = children;
}
diff --git a/packages/api/src/agents/hitl/protection.ts b/packages/api/src/agents/hitl/protection.ts
index 88d2720034..601e243a66 100644
--- a/packages/api/src/agents/hitl/protection.ts
+++ b/packages/api/src/agents/hitl/protection.ts
@@ -791,6 +791,7 @@ async function assertResumeAgentContentAllowed({
subagentsEnabled,
getAgent: async (filter) => (await dependencies.getAgent(filter)) ?? null,
canAccessAgent: (agent) => dependencies.canAccessAgent(agent, user),
+ appConfig,
});
const memoryAgentDefinition = hasResumeAgentDefinitionProtection(appConfig)
? await getResumeMemoryAgentDefinition(appConfig, primaryAgent, dependencies)
diff --git a/packages/api/src/agents/load.ts b/packages/api/src/agents/load.ts
index 889a1cf5cf..8ded99d4a9 100644
--- a/packages/api/src/agents/load.ts
+++ b/packages/api/src/agents/load.ts
@@ -28,7 +28,9 @@ import { resolveMCPClientCapabilityProfile } from '~/mcp/capabilities';
import { synthesizeBackgroundToolOptions } from '~/agents/background';
import { mergeSynthesizedToolOptions } from '~/agents/selection';
import { synthesizeIntentToolOptions } from '~/agents/intent';
+import { getLookupConcurrency } from '~/utils/concurrency';
import { getCustomEndpointConfig } from '~/app/config';
+import { mapWithConcurrency } from '~/utils/promise';
const { mcp_all, mcp_delimiter } = Constants;
type ModelParametersWithPromptPrefix = AgentModelParameters & { promptPrefix?: string | null };
@@ -128,29 +130,26 @@ export async function loadEphemeralAgent(
tools.push(ASK_USER_QUESTION_TOOL_NAME);
}
- const addedServers = new Set<string>();
if (mcpServers.size > 0) {
- for (const mcpServer of mcpServers) {
- if (addedServers.has(mcpServer)) {
- continue;
- }
- /** Address durable catalogs by the effective request overlay; request-scoped
- * overlays still expand fresh through `mcp_all`. */
- const rawOverlayConfig = req.config?.mcpConfig?.[mcpServer];
- const overlayConfig = rawOverlayConfig
- ? validateMCPServerConfig(rawOverlayConfig)
- : undefined;
- const serverTools =
- overlayConfig && requiresEphemeralUserConnection(overlayConfig)
- ? null
- : await deps.getMCPServerTools(userId, mcpServer, overlayConfig, capabilityProfile);
- if (!serverTools) {
- tools.push(`${mcp_all}${mcp_delimiter}${mcpServer}`);
- addedServers.add(mcpServer);
- continue;
- }
- tools.push(...Object.keys(serverTools));
- addedServers.add(mcpServer);
+ const serverToolKeys = await mapWithConcurrency(
+ [...mcpServers],
+ getLookupConcurrency(req.config, 'mcpCatalogLookups'),
+ async (mcpServer): Promise<string[]> => {
+ /** Address durable catalogs by the effective request overlay; request-scoped
+ * overlays still expand fresh through `mcp_all`. */
+ const rawOverlayConfig = req.config?.mcpConfig?.[mcpServer];
+ const overlayConfig = rawOverlayConfig
+ ? validateMCPServerConfig(rawOverlayConfig)
+ : undefined;
+ const serverTools =
+ overlayConfig && requiresEphemeralUserConnection(overlayConfig)
+ ? null
+ : await deps.getMCPServerTools(userId, mcpServer, overlayConfig, capabilityProfile);
+ return serverTools ? Object.keys(serverTools) : [`${mcp_all}${mcp_delimiter}${mcpServer}`];
+ },
+ );
+ for (const keys of serverToolKeys) {
+ tools.push(...keys);
}
}
diff --git a/packages/api/src/agents/skillFiles.ts b/packages/api/src/agents/skillFiles.ts
index 26ac74e51e..8c5d0a420b 100644
--- a/packages/api/src/agents/skillFiles.ts
+++ b/packages/api/src/agents/skillFiles.ts
@@ -31,11 +31,14 @@ import { ContentFilterError, isContentFilterError } from '~/middleware/contentFi
import { getCodeExecutionRouteKey, type CodeExecutionContext } from './execution';
import { assertSkillFileContentAllowed } from '~/skills/protection';
import { createSkillContentDigest } from './compatibility';
+import { getLookupConcurrency } from '~/utils/concurrency';
import { extractInvokedSkillsFromPayload } from './run';
import { resolveDownloadPath } from '~/storage/path';
+import { mapWithConcurrency } from '~/utils/promise';
import { SKILL_FILE_PREFIX } from './skills';
const MAX_INSPECTABLE_SKILL_FILE_BYTES = 10 * 1024 * 1024;
+/** Bounds buffered bytes in flight: at most this many files of up to the cap above. */
const SKILL_FILE_CONTENT_FIELDS = ['file_text'] as const;
const FILE_CONTENT_FIELDS = ['content', 'extracted_text'] as const;
@@ -441,40 +444,44 @@ async function executePrimeSkillFiles(
}
if (inspectBundledFileContent) {
- for (const file of skillFiles) {
- signal?.throwIfAborted();
- if (file.bytes > MAX_INSPECTABLE_SKILL_FILE_BYTES) {
- throwIfStoredSkillFileMustBeInspectable(req);
- continue;
- }
- try {
- const strategy = getStrategyFunctions(file.source);
- if (!strategy.getDownloadStream) {
+ await mapWithConcurrency(
+ skillFiles,
+ getLookupConcurrency(req.config, 'skillFileInspections'),
+ async (file) => {
+ signal?.throwIfAborted();
+ if (file.bytes > MAX_INSPECTABLE_SKILL_FILE_BYTES) {
throwIfStoredSkillFileMustBeInspectable(req);
- logger.warn('[primeSkillFiles] No download stream for stored skill file');
- continue;
+ return;
}
- const sourceStream = await strategy.getDownloadStream(req, resolveDownloadPath(file), {
- signal,
- });
- const buffer = await bufferSkillFileStream(sourceStream, signal);
- if (buffer == null) {
+ try {
+ const strategy = getStrategyFunctions(file.source);
+ if (!strategy.getDownloadStream) {
+ throwIfStoredSkillFileMustBeInspectable(req);
+ logger.warn('[primeSkillFiles] No download stream for stored skill file');
+ return;
+ }
+ const sourceStream = await strategy.getDownloadStream(req, resolveDownloadPath(file), {
+ signal,
+ });
+ const buffer = await bufferSkillFileStream(sourceStream, signal);
+ if (buffer == null) {
+ throwIfStoredSkillFileMustBeInspectable(req);
+ return;
+ }
+ assertStoredSkillFileAllowed(file, buffer, req);
+ inspectedBuffers.set(file, buffer);
+ } catch (error) {
+ if (isAbortError(error)) {
+ throw error;
+ }
+ if (isContentFilterError(error)) {
+ throw error;
+ }
throwIfStoredSkillFileMustBeInspectable(req);
- continue;
- }
- assertStoredSkillFileAllowed(file, buffer, req);
- inspectedBuffers.set(file, buffer);
- } catch (error) {
- if (isAbortError(error)) {
- throw error;
+ logger.error('[primeSkillFiles] Failed to inspect bundled file before use');
}
- if (isContentFilterError(error)) {
- throw error;
- }
- throwIfStoredSkillFileMustBeInspectable(req);
- logger.error('[primeSkillFiles] Failed to inspect bundled file before use');
- }
- }
+ },
+ );
}
/* Cache-hit path: every skillFile carries a `codeEnvRef` from the
diff --git a/packages/api/src/agents/skills.ts b/packages/api/src/agents/skills.ts
index a31b17c537..7de9d67117 100644
--- a/packages/api/src/agents/skills.ts
+++ b/packages/api/src/agents/skills.ts
@@ -9,9 +9,12 @@ import type {
} from 'librechat-data-provider';
import type { LCToolRegistry, LCTool, InjectedMessage } from '@librechat/agents';
import type { BaseMessage } from '@librechat/agents/langchain/messages';
+import type { AppConfig } from '@librechat/data-schemas';
import type { Types } from 'mongoose';
import { getSkillToolDefinition, isSkillToolAvailable, registerCodeExecutionTools } from './tools';
import { createSkillContentDigest } from './compatibility';
+import { getLookupConcurrency } from '~/utils/concurrency';
+import { mapWithConcurrency } from '~/utils/promise';
import { logAxiosError } from '~/utils';
/**
@@ -250,6 +253,8 @@ export interface ResolveModelSpecSkillIdsParams {
accessibleSkillIds: Types.ObjectId[];
/** DB lookup: name → skill doc constrained to the user's accessible IDs. */
getSkillByName?: TGetSkillByName;
+ /** Source of the `concurrency` bounds; defaults apply when absent. */
+ appConfig?: Pick<AppConfig, 'concurrency'>;
}
/**
@@ -262,6 +267,7 @@ export async function resolveModelSpecSkillIds({
names,
accessibleSkillIds,
getSkillByName,
+ appConfig,
}: ResolveModelSpecSkillIdsParams): Promise<Types.ObjectId[]> {
if (!names.length || accessibleSkillIds.length === 0 || !getSkillByName) {
return [];
@@ -289,23 +295,25 @@ export async function resolveModelSpecSkillIds({
boundedNames = uniqueNames.slice(0, MAX_MODEL_SPEC_SKILLS);
}
- const resolved: Array<Types.ObjectId | null> = [];
- for (const name of boundedNames) {
- try {
- const skill = await getSkillByName(name, accessibleSkillIds, {
- preferModelInvocable: true,
- });
- if (!skill) {
- logger.warn('[resolveModelSpecSkillIds] Requested skill not found or not accessible');
- resolved.push(null);
- continue;
+ const resolved = await mapWithConcurrency(
+ boundedNames,
+ getLookupConcurrency(appConfig, 'skillNameLookups'),
+ async (name): Promise<Types.ObjectId | null> => {
+ try {
+ const skill = await getSkillByName(name, accessibleSkillIds, {
+ preferModelInvocable: true,
+ });
+ if (!skill) {
+ logger.warn('[resolveModelSpecSkillIds] Requested skill not found or not accessible');
+ return null;
+ }
+ return skill._id;
+ } catch {
+ logger.warn('[resolveModelSpecSkillIds] Failed to resolve a requested skill');
+ return null;
}
- resolved.push(skill._id);
- } catch {
- logger.warn('[resolveModelSpecSkillIds] Failed to resolve a requested skill');
- resolved.push(null);
- }
- }
+ },
+ );
const seenIds = new Set<string>();
return resolved.filter((id): id is Types.ObjectId => {
diff --git a/packages/api/src/files/encode/image.spec.ts b/packages/api/src/files/encode/image.spec.ts
index 4249b49c86..260b99d30e 100644
--- a/packages/api/src/files/encode/image.spec.ts
+++ b/packages/api/src/files/encode/image.spec.ts
@@ -66,6 +66,63 @@ describe('encodeAndFormatImages', () => {
expect(getDownloadStream).toHaveBeenCalledWith(expect.anything(), file.filepath);
});
+ it('overlaps storage reads while keeping attachment order and the first error in order', async () => {
+ let active = 0;
+ let peak = 0;
+ getDownloadStream.mockImplementation(async (_req, key: string) => {
+ active++;
+ peak = Math.max(peak, active);
+ await new Promise((resolve) => setTimeout(resolve, 5));
+ active--;
+ if (key === 'b') {
+ throw new Error('second failed');
+ }
+ return Readable.from(imageBytes);
+ });
+ const files = ['a', 'c', 'd'].map((key) => ({ ...file, file_id: key, storageKey: key }));
+
+ const result = await encodeAndFormatImages(makeReq(), files, {}, deps);
+ expect(result.files.map((f) => f.file_id)).toEqual(['a', 'c', 'd']);
+ expect(peak).toBeGreaterThan(1);
+
+ await expect(
+ encodeAndFormatImages(
+ makeReq(),
+ [files[0], { ...file, file_id: 'b', storageKey: 'b' }, files[1]],
+ {},
+ deps,
+ ),
+ ).rejects.toBeInstanceOf(AttachmentStorageError);
+ });
+
+ it('surfaces an early storage failure without waiting for a stalled later read', async () => {
+ getDownloadStream.mockImplementation(async (_req, key: string) => {
+ if (key === 'stalled') {
+ return new Promise(() => undefined);
+ }
+ throw new Error('first failed');
+ });
+ const files = ['early', 'stalled'].map((key) => ({ ...file, file_id: key, storageKey: key }));
+
+ await expect(encodeAndFormatImages(makeReq(), files, {}, deps)).rejects.toBeInstanceOf(
+ AttachmentStorageError,
+ );
+ });
+
+ it('surfaces a later failure without waiting for an earlier pending read', async () => {
+ getDownloadStream.mockImplementation(async (_req, key: string) => {
+ if (key === 'slow') {
+ return new Promise(() => undefined);
+ }
+ throw new Error('later failed');
+ });
+ const files = ['slow', 'fails'].map((key) => ({ ...file, file_id: key, storageKey: key }));
+
+ await expect(encodeAndFormatImages(makeReq(), files, {}, deps)).rejects.toBeInstanceOf(
+ AttachmentStorageError,
+ );
+ });
+
it('returns an empty result without acquiring a strategy for absent files', async () => {
await expect(encodeAndFormatImages(makeReq(), undefined, {}, deps)).resolves.toEqual({
files: [],
diff --git a/packages/api/src/files/encode/image.ts b/packages/api/src/files/encode/image.ts
index 57b439315a..c01e639f8f 100644
--- a/packages/api/src/files/encode/image.ts
+++ b/packages/api/src/files/encode/image.ts
@@ -103,6 +103,12 @@ export async function encodeAndFormatImages(
return result;
}
+ /** A later synchronous throw abandons earlier in-flight reads; keep their rejections handled. */
+ const track = (entry: Promise<ImageEntry>): Promise<ImageEntry> => {
+ entry.catch(() => undefined);
+ return entry;
+ };
+
for (const file of files) {
const source = file.source ?? FileSources.local;
if (!file.height) {
@@ -111,20 +117,24 @@ export async function encodeAndFormatImages(
}
if (blobStorageSources.has(source)) {
- try {
- const processedFile = await runGuardedEncode(file.bytes ?? 0, () =>
- getFileStream(req, file, encodingMethods, getStrategyFunctions),
- );
- promises.push([file, processedFile?.content ?? null]);
- } catch (error) {
- if (isAttachmentObjectNotFoundError(error)) {
- throw error;
- }
- if (typeof error === 'object' && error != null && 'bufferedData' in error) {
- delete (error as { bufferedData?: unknown }).bufferedData;
- }
- throw new AttachmentStorageError();
- }
+ promises.push(
+ track(
+ runGuardedEncode(file.bytes ?? 0, () =>
+ getFileStream(req, file, encodingMethods, getStrategyFunctions),
+ ).then(
+ (processedFile): ImageEntry => [file, processedFile?.content ?? null],
+ (error: unknown): never => {
+ if (isAttachmentObjectNotFoundError(error)) {
+ throw error;
+ }
+ if (typeof error === 'object' && error != null && 'bufferedData' in error) {
+ delete (error as { bufferedData?: unknown }).bufferedData;
+ }
+ throw new AttachmentStorageError();
+ },
+ ),
+ ),
+ );
continue;
}
@@ -137,7 +147,7 @@ export async function encodeAndFormatImages(
}
if (source !== FileSources.local && effectiveEndpoint && base64Only.has(effectiveEndpoint)) {
- const entry = await runGuardedEncode<ImageEntry>(file.bytes ?? 0, async () => {
+ const entry = runGuardedEncode<ImageEntry>(file.bytes ?? 0, async () => {
const [preparedFile, imageURL] = await preparePayload(req, file);
const fetchTimeoutMs = getOutboundTimeoutMs(req.config, 'imageFetchMs');
try {
@@ -151,10 +161,10 @@ export async function encodeAndFormatImages(
throw new Error(logAxiosError({ message, error }));
}
});
- promises.push(entry);
+ promises.push(track(entry));
continue;
}
- promises.push(preparePayload(req, file));
+ promises.push(track(preparePayload(req, file)));
}
const detail = params.imageDetail ?? req.body.imageDetail ?? ImageDetail.auto;
diff --git a/packages/api/src/prompts/migration.ts b/packages/api/src/prompts/migration.ts
index e0680da708..c1592458d7 100644
--- a/packages/api/src/prompts/migration.ts
+++ b/packages/api/src/prompts/migration.ts
@@ -51,9 +51,11 @@ export async function checkPromptPermissionsMigration({
await ensureRequiredCollectionsExist(db);
}
- const ownerRole = await methods.findRoleByIdentifier(AccessRoleIds.PROMPTGROUP_OWNER);
- const viewerRole = await methods.findRoleByIdentifier(AccessRoleIds.PROMPTGROUP_VIEWER);
- const editorRole = await methods.findRoleByIdentifier(AccessRoleIds.PROMPTGROUP_EDITOR);
+ const [ownerRole, viewerRole, editorRole] = await Promise.all([
+ methods.findRoleByIdentifier(AccessRoleIds.PROMPTGROUP_OWNER),
+ methods.findRoleByIdentifier(AccessRoleIds.PROMPTGROUP_VIEWER),
+ methods.findRoleByIdentifier(AccessRoleIds.PROMPTGROUP_EDITOR),
+ ]);
if (!ownerRole || !viewerRole || !editorRole) {
logger.warn(
@@ -66,21 +68,21 @@ export async function checkPromptPermissionsMigration({
};
}
- let globalPromptGroupIds = new Set<string>();
- if (db) {
- const project = await db
- .collection('projects')
- .findOne({ name: GLOBAL_PROJECT_NAME }, { projection: { promptGroupIds: 1 } });
- globalPromptGroupIds = new Set(
- (project?.promptGroupIds || []).map((id: { toString(): string }) => id.toString()),
- );
- }
-
const AclEntry = mongoose.model('AclEntry');
- const migratedGroupIds = await AclEntry.distinct('resourceId', {
- resourceType: ResourceType.PROMPTGROUP,
- principalType: PrincipalType.USER,
- });
+ const [project, migratedGroupIds] = await Promise.all([
+ db
+ ? db
+ .collection('projects')
+ .findOne({ name: GLOBAL_PROJECT_NAME }, { projection: { promptGroupIds: 1 } })
+ : Promise.resolve(null),
+ AclEntry.distinct('resourceId', {
+ resourceType: ResourceType.PROMPTGROUP,
+ principalType: PrincipalType.USER,
+ }),
+ ]);
+ const globalPromptGroupIds = new Set<string>(
+ (project?.promptGroupIds || []).map((id: { toString(): string }) => id.toString()),
+ );
const promptGroupsToMigrate = (await PromptGroupModel.find({
_id: { $nin: migratedGroupIds },
diff --git a/packages/api/src/schedules/authorization/enrollment.ts b/packages/api/src/schedules/authorization/enrollment.ts
index 9fd73e7a15..7d4df442f8 100644
--- a/packages/api/src/schedules/authorization/enrollment.ts
+++ b/packages/api/src/schedules/authorization/enrollment.ts
@@ -55,24 +55,23 @@ export function createScheduleMCPEnrollmentResolver(
const schedules = appConfig?.interfaceConfig?.schedules;
const bindings = typeof schedules === 'object' ? schedules?.mcpConsent?.resources : undefined;
if (!bindings || !Object.keys(bindings).length) return [];
- const servers = await deps.getServers(
- user,
- (appConfig?.mcpConfig ?? {}) as Record<string, ParsedServerConfig>,
- );
+ const [servers, { tools, candidates: candidateTools }] = await Promise.all([
+ deps.getServers(user, (appConfig?.mcpConfig ?? {}) as Record<string, ParsedServerConfig>),
+ resolveScheduledMCPRequirements(
+ identity.agentId,
+ user,
+ {
+ getAgentGraphNodes: deps.getNodes,
+ resolveAgentGraphAccess: () => deps.resolveGraphAccess(user),
+ getModelsConfig: deps.getModelsConfig,
+ },
+ async () => appConfig,
+ signal,
+ ),
+ ]);
const names = Object.keys(servers);
const aliases = buildServerNameAliases(names);
const candidates = [...names, ...aliases.keys()];
- const { tools, candidates: candidateTools } = await resolveScheduledMCPRequirements(
- identity.agentId,
- user,
- {
- getAgentGraphNodes: deps.getNodes,
- resolveAgentGraphAccess: () => deps.resolveGraphAccess(user),
- getModelsConfig: deps.getModelsConfig,
- },
- async () => appConfig,
- signal,
- );
if (candidateTools.some(({ name }) => !isScheduledMCPCandidate(name)))
throw new ScheduleMCPConsentError('consent_unavailable');
const selected = new Map<string, Map<string, Set<string>>>();
diff --git a/packages/api/src/schedules/bearer.ts b/packages/api/src/schedules/bearer.ts
index 1bffae63fc..e814826cdc 100644
--- a/packages/api/src/schedules/bearer.ts
+++ b/packages/api/src/schedules/bearer.ts
@@ -245,8 +245,11 @@ export function createScheduledMCPBearerHost(deps: {
let pending = flights.get(key);
if (!pending) {
pending = (async () => {
- for (const item of selections)
- await authorize(target, item, mintStage, ownerSignal, onFailure);
+ await Promise.all(
+ selections.map((item) =>
+ authorize(target, item, mintStage, ownerSignal, onFailure),
+ ),
+ );
const result = await awaitOboOperation(
deps
.resolveBearer(
diff --git a/packages/api/src/schedules/mcp.ts b/packages/api/src/schedules/mcp.ts
index 6b3fabe504..efa6700810 100644
--- a/packages/api/src/schedules/mcp.ts
+++ b/packages/api/src/schedules/mcp.ts
@@ -465,6 +465,7 @@ export function createScheduleMCPPreflight(deps: ScheduleMCPDeps): ScheduleMCPPr
const snapshot = await connection.fetchToolsSnapshot(options.deadlineMs, signal);
if (snapshot.authenticationError) throw snapshot.authenticationError;
if (execution) {
+ const authorizations: Array<Promise<unknown>> = [];
for (const [toolKey, owners] of toolAgentIds.get(server) ?? []) {
const [selectionName] = splitMCPToolKey(toolKey, authoritativeCandidates);
const definitions = snapshot.tools.filter(
@@ -474,16 +475,19 @@ export function createScheduleMCPPreflight(deps: ScheduleMCPDeps): ScheduleMCPPr
selectionName,
);
for (const ownerId of owners) {
- await execution.bind(ownerId, selectionName).authorize({
- user,
- serverName: server,
- serverConfig,
- toolName: definitions.length === 1 ? definitions[0].name : '',
- loadTools: async () => snapshot,
- signal,
- });
+ authorizations.push(
+ execution.bind(ownerId, selectionName).authorize({
+ user,
+ serverName: server,
+ serverConfig,
+ toolName: definitions.length === 1 ? definitions[0].name : '',
+ loadTools: async () => snapshot,
+ signal,
+ }),
+ );
}
}
+ await Promise.all(authorizations);
}
const available = new Set(
Object.keys(formatMCPServerTools(server, snapshot.tools)),
diff --git a/packages/api/src/utils/concurrency.spec.ts b/packages/api/src/utils/concurrency.spec.ts
new file mode 100644
index 0000000000..8004cc9a27
--- /dev/null
+++ b/packages/api/src/utils/concurrency.spec.ts
@@ -0,0 +1,18 @@
+import { DEFAULT_LOOKUP_CONCURRENCY, getLookupConcurrency } from './concurrency';
+
+describe('getLookupConcurrency', () => {
+ it('falls back to the default for each key', () => {
+ expect(getLookupConcurrency(undefined, 'mcpCatalogLookups')).toBe(
+ DEFAULT_LOOKUP_CONCURRENCY.mcpCatalogLookups,
+ );
+ expect(getLookupConcurrency({ concurrency: { skillNameLookups: 2 } }, 'resumeAgentLoads')).toBe(
+ DEFAULT_LOOKUP_CONCURRENCY.resumeAgentLoads,
+ );
+ });
+
+ it('uses the configured value, including 1 for serial lookups', () => {
+ expect(getLookupConcurrency({ concurrency: { skillNameLookups: 1 } }, 'skillNameLookups')).toBe(
+ 1,
+ );
+ });
+});
diff --git a/packages/api/src/utils/concurrency.ts b/packages/api/src/utils/concurrency.ts
new file mode 100644
index 0000000000..997821f61d
--- /dev/null
+++ b/packages/api/src/utils/concurrency.ts
@@ -0,0 +1,20 @@
+import type { AppConfig } from '@librechat/data-schemas';
+
+/** Bounds on parallel per-request lookups, used when `concurrency` omits a key. */
+export const DEFAULT_LOOKUP_CONCURRENCY = {
+ connectedAgentLoads: 4,
+ mcpCatalogLookups: 4,
+ skillNameLookups: 8,
+ skillFileInspections: 4,
+ resumeAgentLoads: 4,
+} as const;
+
+export type LookupConcurrency = keyof typeof DEFAULT_LOOKUP_CONCURRENCY;
+
+/** The `librechat.yaml` `concurrency` value for `key`, else its default. */
+export function getLookupConcurrency(
+ appConfig: Pick<AppConfig, 'concurrency'> | null | undefined,
+ key: LookupConcurrency,
+): number {
+ return appConfig?.concurrency?.[key] ?? DEFAULT_LOOKUP_CONCURRENCY[key];
+}
diff --git a/packages/api/src/utils/promise.spec.ts b/packages/api/src/utils/promise.spec.ts
index 93c5fd8893..6ca7ef8902 100644
--- a/packages/api/src/utils/promise.spec.ts
+++ b/packages/api/src/utils/promise.spec.ts
@@ -1,4 +1,4 @@
-import { createConcurrencyLimiter, withTimeout } from './promise';
+import { createConcurrencyLimiter, mapWithConcurrency, withTimeout } from './promise';
const tick = (ms = 0): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms));
@@ -312,3 +312,30 @@ describe('createConcurrencyLimiter', () => {
expect(queuedTaskCalled).toBe(true);
});
});
+
+describe('mapWithConcurrency', () => {
+ it('keeps input order and never exceeds the concurrency cap', async () => {
+ let active = 0;
+ let peak = 0;
+ const result = await mapWithConcurrency([30, 5, 20, 1, 10], 2, async (delay, index) => {
+ active++;
+ peak = Math.max(peak, active);
+ await tick(delay);
+ active--;
+ return `${index}:${delay}`;
+ });
+ expect(result).toEqual(['0:30', '1:5', '2:20', '3:1', '4:10']);
+ expect(peak).toBe(2);
+ });
+
+ it('rejects with the first failure', async () => {
+ await expect(
+ mapWithConcurrency([1, 2, 3], 2, async (value) => {
+ if (value === 2) {
+ throw new Error('boom');
+ }
+ return value;
+ }),
+ ).rejects.toThrow('boom');
+ });
+});
diff --git a/packages/api/src/utils/promise.ts b/packages/api/src/utils/promise.ts
index 06a3e440d4..7c4b0974fb 100644
--- a/packages/api/src/utils/promise.ts
+++ b/packages/api/src/utils/promise.ts
@@ -106,3 +106,17 @@ export function createConcurrencyLimiter(
}
});
}
+
+/**
+ * Maps `items` through `fn` with at most `concurrency` calls in flight.
+ * Results keep input order. The first rejection rejects the whole call, like
+ * `Promise.all`; calls already started are not cancelled.
+ */
+export function mapWithConcurrency<T, R>(
+ items: readonly T[],
+ concurrency: number,
+ fn: (item: T, index: number) => Promise<R>,
+): Promise<R[]> {
+ const limit = createConcurrencyLimiter(concurrency);
+ return Promise.all(items.map((item, index) => limit(() => fn(item, index))));
+}
diff --git a/packages/data-provider/src/config.ts b/packages/data-provider/src/config.ts
index dc1df759ae..049fbb2e19 100644
--- a/packages/data-provider/src/config.ts
+++ b/packages/data-provider/src/config.ts
@@ -2725,6 +2725,25 @@ export const violationLogSchema = z.object({
export type TOutboundTimeouts = z.infer<typeof outboundTimeoutsSchema>;
+const lookupConcurrency = z.number().int().min(1).max(32).optional();
+
+/** Upper bounds on independent lookups a request runs at once. Omitted keys use the server
+ * defaults (`DEFAULT_LOOKUP_CONCURRENCY` in `@librechat/api`); `1` restores serial lookups. */
+export const lookupConcurrencySchema = z.object({
+ /** Connected agents initialized together per level while discovering a handoff graph. */
+ connectedAgentLoads: lookupConcurrency,
+ /** MCP tool catalog reads when assembling an ephemeral or added agent. */
+ mcpCatalogLookups: lookupConcurrency,
+ /** Model spec skill name resolutions. */
+ skillNameLookups: lookupConcurrency,
+ /** Bundled skill file inspections before a skill is primed. */
+ skillFileInspections: lookupConcurrency,
+ /** Agent reads while building the resume snapshot of a paused run. */
+ resumeAgentLoads: lookupConcurrency,
+});
+
+export type TLookupConcurrency = z.infer<typeof lookupConcurrencySchema>;
+
export const rateLimitSchema = z.object({
twoFactorManagement: z
.object({
@@ -4668,6 +4687,7 @@ export const configSchema = z.object({
maxPageSize: z.number().int().min(1).max(10000).optional(),
})
.optional(),
+ concurrency: lookupConcurrencySchema.optional(),
fileConfig: fileConfigSchema.optional(),
modelSpecs: specsConfigSchema.optional(),
filters: filtersConfigSchema.optional(),
diff --git a/packages/data-schemas/src/app/service.ts b/packages/data-schemas/src/app/service.ts
index b9cbae7f9b..00001d68a4 100644
--- a/packages/data-schemas/src/app/service.ts
+++ b/packages/data-schemas/src/app/service.ts
@@ -224,6 +224,7 @@ export const AppService = async (params?: {
timeouts: config.timeouts,
violationLog: config.violationLog,
messages: config.messages,
+ concurrency: config.concurrency,
mcpSettings,
mcpAppSandbox,
fileStrategy,
diff --git a/packages/data-schemas/src/types/app.ts b/packages/data-schemas/src/types/app.ts
index 7208395767..8a6b6fdb9a 100644
--- a/packages/data-schemas/src/types/app.ts
+++ b/packages/data-schemas/src/types/app.ts
@@ -74,6 +74,8 @@ export interface AppConfig {
/** Violation log retention; unset keeps the server default */
violationLog?: TCustomConfig['violationLog'];
messages?: TCustomConfig['messages'];
+ /** Bounds on parallel per-request lookups; unset keys fall back to the server defaults */
+ concurrency?: TCustomConfig['concurrency'];
skillDeletion?: {
finalizationEnabled?: boolean;
sweepIntervalMs?: number;