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: Propagate Host Cancellation Into API Tools (#15812)

Danny Avilaa785a6196ae2
diff --git a/api/server/controllers/agents/__tests__/openai.spec.js b/api/server/controllers/agents/__tests__/openai.spec.js
index 5d51069f26..d6935069fc 100644
--- a/api/server/controllers/agents/__tests__/openai.spec.js
+++ b/api/server/controllers/agents/__tests__/openai.spec.js
@@ -1310,13 +1310,34 @@ describe('OpenAIChatCompletionController', () => {
const toolExecuteOptions = createToolExecuteHandler.mock.calls.at(-1)[0];
expect(toolExecuteOptions.ordinaryToolCancellation).toBe(true);
- await toolExecuteOptions.loadTools(['file_search'], 'agent-123');
+ expect(toolExecuteOptions.runSignal).toBe(mockExecution.signal);
+ expect(toolExecuteOptions.foregroundRunId).toBe(initializeParams.requestBody.messageId);
+ const effectiveSignal = new AbortController().signal;
+ await toolExecuteOptions.loadTools(
+ ['file_search'],
+ 'agent-123',
+ undefined,
+ undefined,
+ effectiveSignal,
+ );
expect(loadToolsForExecution).toHaveBeenLastCalledWith(
expect.objectContaining({
agentResourceType: ResourceType.REMOTE_AGENT,
requestBody: initializeParams.requestBody,
+ signal: effectiveSignal,
}),
);
+ mockExecution.abort();
+ await toolExecuteOptions.loadTools(
+ ['file_search'],
+ 'agent-123',
+ undefined,
+ undefined,
+ undefined,
+ );
+ expect(loadToolsForExecution).toHaveBeenLastCalledWith(
+ expect.objectContaining({ signal: undefined }),
+ );
});
it('returns 503 when an agent expects MCP tools but resolves none', async () => {
diff --git a/api/server/controllers/agents/__tests__/responses.unit.spec.js b/api/server/controllers/agents/__tests__/responses.unit.spec.js
index 7ab3cef884..635e6099df 100644
--- a/api/server/controllers/agents/__tests__/responses.unit.spec.js
+++ b/api/server/controllers/agents/__tests__/responses.unit.spec.js
@@ -1867,48 +1867,73 @@ describe('createResponse controller', () => {
});
describe('remote-agent file authorization', () => {
- it('threads the remote-agent permission boundary through initialization and tool loading', async () => {
- const { initializeAgent, createToolExecuteHandler } = require('@librechat/api');
- const { loadAgentTools, loadToolsForExecution } = require('~/server/services/ToolService');
- const { filterFilesByAgentAccess } = require('~/server/services/Files/permissions');
-
- req.config.endpoints.agents.backgroundTasks = { ordinaryToolCancellation: true };
- await createResponse(req, res);
-
- const [initializeParams, dbMethods] = initializeAgent.mock.calls.at(-1);
- const filterParams = {
- files: [{ file_id: 'owner-file', user: 'agent-owner' }],
- userId: 'user-123',
- role: 'USER',
- agentId: 'agent-123',
- };
- await dbMethods.filterFilesByAgentAccess(filterParams);
- expect(filterFilesByAgentAccess).toHaveBeenLastCalledWith({
- ...filterParams,
- resourceType: ResourceType.REMOTE_AGENT,
- });
-
- await initializeParams.loadTools({
- agentId: 'agent-123',
- tools: ['file_search'],
- provider: 'anthropic',
- model: 'claude-3',
- tool_resources: { file_search: { file_ids: ['owner-file'] } },
- });
- expect(loadAgentTools).toHaveBeenLastCalledWith(
- expect.objectContaining({ agentResourceType: ResourceType.REMOTE_AGENT }),
- );
+ it.each([false, true])(
+ 'threads the remote-agent permission boundary through initialization and tool loading (stream=%s)',
+ async (stream) => {
+ const { initializeAgent, createToolExecuteHandler } = require('@librechat/api');
+ const { loadAgentTools, loadToolsForExecution } = require('~/server/services/ToolService');
+ const { filterFilesByAgentAccess } = require('~/server/services/Files/permissions');
+
+ req.config.endpoints.agents.backgroundTasks = { ordinaryToolCancellation: true };
+ req.body.stream = stream;
+ await createResponse(req, res);
+
+ const [initializeParams, dbMethods] = initializeAgent.mock.calls.at(-1);
+ const filterParams = {
+ files: [{ file_id: 'owner-file', user: 'agent-owner' }],
+ userId: 'user-123',
+ role: 'USER',
+ agentId: 'agent-123',
+ };
+ await dbMethods.filterFilesByAgentAccess(filterParams);
+ expect(filterFilesByAgentAccess).toHaveBeenLastCalledWith({
+ ...filterParams,
+ resourceType: ResourceType.REMOTE_AGENT,
+ });
- const toolExecuteOptions = createToolExecuteHandler.mock.calls.at(-1)[0];
- expect(toolExecuteOptions.ordinaryToolCancellation).toBe(true);
- await toolExecuteOptions.loadTools(['file_search'], 'agent-123');
- expect(loadToolsForExecution).toHaveBeenLastCalledWith(
- expect.objectContaining({
- agentResourceType: ResourceType.REMOTE_AGENT,
- requestBody: initializeParams.requestBody,
- }),
- );
- });
+ await initializeParams.loadTools({
+ agentId: 'agent-123',
+ tools: ['file_search'],
+ provider: 'anthropic',
+ model: 'claude-3',
+ tool_resources: { file_search: { file_ids: ['owner-file'] } },
+ });
+ expect(loadAgentTools).toHaveBeenLastCalledWith(
+ expect.objectContaining({ agentResourceType: ResourceType.REMOTE_AGENT }),
+ );
+
+ const toolExecuteOptions = createToolExecuteHandler.mock.calls.at(-1)[0];
+ expect(toolExecuteOptions.ordinaryToolCancellation).toBe(true);
+ expect(toolExecuteOptions.runSignal).toBe(mockExecution.signal);
+ expect(toolExecuteOptions.foregroundRunId).toBe(initializeParams.requestBody.messageId);
+ const effectiveSignal = new AbortController().signal;
+ await toolExecuteOptions.loadTools(
+ ['file_search'],
+ 'agent-123',
+ undefined,
+ undefined,
+ effectiveSignal,
+ );
+ expect(loadToolsForExecution).toHaveBeenLastCalledWith(
+ expect.objectContaining({
+ agentResourceType: ResourceType.REMOTE_AGENT,
+ requestBody: initializeParams.requestBody,
+ signal: effectiveSignal,
+ }),
+ );
+ mockExecution.abort();
+ await toolExecuteOptions.loadTools(
+ ['file_search'],
+ 'agent-123',
+ undefined,
+ undefined,
+ undefined,
+ );
+ expect(loadToolsForExecution).toHaveBeenLastCalledWith(
+ expect.objectContaining({ signal: undefined }),
+ );
+ },
+ );
});
describe('token usage recording - non-streaming', () => {
diff --git a/api/server/controllers/agents/openai.js b/api/server/controllers/agents/openai.js
index 629fa8a539..78f930753f 100644
--- a/api/server/controllers/agents/openai.js
+++ b/api/server/controllers/agents/openai.js
@@ -803,13 +803,21 @@ const executeOpenAIChatCompletion = async (envelope, { req, res }) => {
agent never gains sandbox access even if the admin enabled the
capability globally. */
const toolExecuteOptions = {
+ runSignal: execution.signal,
+ foregroundRunId: responseId,
ordinaryToolCancellation: ordinaryToolCancellationEnabled,
provisionFiles: createProvisionFilesCallback({
req,
agentToolContexts,
resolvePrimaryAgentId: () => primaryConfig.id,
}),
- loadTools: async (toolNames, agentId, _configurable, callerCapabilityProjection) => {
+ loadTools: async (
+ toolNames,
+ agentId,
+ _configurable,
+ callerCapabilityProjection,
+ runSignal,
+ ) => {
const ctx =
agentToolContexts.get(agentId) ?? agentToolContexts.get(primaryConfig.id) ?? {};
const result = await loadToolsForExecution({
@@ -820,7 +828,7 @@ const executeOpenAIChatCompletion = async (envelope, { req, res }) => {
requestBody: mcpRequestBody,
toolNames,
agent: ctx.agent ?? agent,
- signal: execution.signal,
+ signal: runSignal,
toolRegistry: ctx.toolRegistry,
callerCapabilityProjection,
backgroundToolNames: ctx.backgroundToolNames,
diff --git a/api/server/controllers/agents/responses.js b/api/server/controllers/agents/responses.js
index bb16156cd6..aa851fbb51 100644
--- a/api/server/controllers/agents/responses.js
+++ b/api/server/controllers/agents/responses.js
@@ -1146,13 +1146,21 @@ const executeResponse = async (envelope, { req, res }) => {
// Create tool execute options for event-driven tool execution
const toolExecuteOptions = {
+ runSignal: execution.signal,
+ foregroundRunId: responseId,
ordinaryToolCancellation: ordinaryToolCancellationEnabled,
provisionFiles: createProvisionFilesCallback({
req,
agentToolContexts,
resolvePrimaryAgentId: () => primaryConfig.id,
}),
- loadTools: async (toolNames, agentId, _configurable, callerCapabilityProjection) => {
+ loadTools: async (
+ toolNames,
+ agentId,
+ _configurable,
+ callerCapabilityProjection,
+ runSignal,
+ ) => {
const ctx =
agentToolContexts.get(agentId) ?? agentToolContexts.get(primaryConfig.id) ?? {};
const result = await loadToolsForExecution({
@@ -1163,7 +1171,7 @@ const executeResponse = async (envelope, { req, res }) => {
requestBody: mcpRequestBody,
toolNames,
agent: ctx.agent ?? agent,
- signal: execution.signal,
+ signal: runSignal,
toolRegistry: ctx.toolRegistry,
callerCapabilityProjection,
backgroundToolNames: ctx.backgroundToolNames,
@@ -1375,13 +1383,21 @@ const executeResponse = async (envelope, { req, res }) => {
});
const toolExecuteOptions = {
+ runSignal: execution.signal,
+ foregroundRunId: responseId,
ordinaryToolCancellation: ordinaryToolCancellationEnabled,
provisionFiles: createProvisionFilesCallback({
req,
agentToolContexts,
resolvePrimaryAgentId: () => primaryConfig.id,
}),
- loadTools: async (toolNames, agentId, _configurable, callerCapabilityProjection) => {
+ loadTools: async (
+ toolNames,
+ agentId,
+ _configurable,
+ callerCapabilityProjection,
+ runSignal,
+ ) => {
const ctx =
agentToolContexts.get(agentId) ?? agentToolContexts.get(primaryConfig.id) ?? {};
const result = await loadToolsForExecution({
@@ -1392,7 +1408,7 @@ const executeResponse = async (envelope, { req, res }) => {
requestBody: mcpRequestBody,
toolNames,
agent: ctx.agent ?? agent,
- signal: execution.signal,
+ signal: runSignal,
toolRegistry: ctx.toolRegistry,
callerCapabilityProjection,
backgroundToolNames: ctx.backgroundToolNames,
diff --git a/packages/api/src/agents/openai/service.spec.ts b/packages/api/src/agents/openai/service.spec.ts
index b06b32ba67..b9aa611fa5 100644
--- a/packages/api/src/agents/openai/service.spec.ts
+++ b/packages/api/src/agents/openai/service.spec.ts
@@ -255,8 +255,18 @@ describe('createAgentChatCompletion - MCP permission user propagation', () => {
'agent_test',
expect.objectContaining({ requestBody: runArgs.requestBody }),
undefined,
- undefined,
+ processStream.mock.calls[0][1].signal,
);
+ expect(loadTools.mock.calls[0][4]).toBeInstanceOf(AbortSignal);
+ await runArgs.customHandlers[GraphEvents.ON_TOOL_EXECUTE].handle(GraphEvents.ON_TOOL_EXECUTE, {
+ toolCalls: [{ id: 'child-call', name: 'deferred_mcp_tool', args: {} }],
+ agentId: 'agent_test',
+ configurable: streamConfig.configurable,
+ metadata: { run_id: 'detached-child' },
+ resolve,
+ reject,
+ });
+ expect(loadTools.mock.calls[1][4]).toBeUndefined();
});
it('uses the root parent sentinel when chat completions omit a parent id', async () => {
diff --git a/packages/api/src/agents/openai/service.ts b/packages/api/src/agents/openai/service.ts
index 508967e5b6..782494635c 100644
--- a/packages/api/src/agents/openai/service.ts
+++ b/packages/api/src/agents/openai/service.ts
@@ -818,7 +818,16 @@ export async function createAgentChatCompletion(
// Create event handlers
const eventHandlers =
isStreaming && handlerConfig
- ? createOpenAIHandlers(handlerConfig, deps.toolExecuteOptions)
+ ? createOpenAIHandlers(
+ handlerConfig,
+ deps.toolExecuteOptions == null
+ ? undefined
+ : {
+ ...deps.toolExecuteOptions,
+ runSignal: abortController.signal,
+ foregroundRunId: requestId,
+ },
+ )
: {};
// Convert messages to internal format