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: Project Only Accepted Agents API Tool Calls (#16384)

lia-by-librechat[bot]24f950a484e1
diff --git a/packages/api/src/agents/openai/handlers.spec.ts b/packages/api/src/agents/openai/handlers.spec.ts
index 00bc3d321b..1a3edca37a 100644
--- a/packages/api/src/agents/openai/handlers.spec.ts
+++ b/packages/api/src/agents/openai/handlers.spec.ts
@@ -7,6 +7,7 @@ import {
buildCompletionUsage,
OpenAIModelEndHandler,
createOpenAIStreamTracker,
+ createOpenAIContentAggregator,
createOpenAIHandlers,
createChunk,
writeSSE,
@@ -39,12 +40,14 @@ describe('OpenAI-compatible agent stream handlers', () => {
handlers.on_reasoning_delta.handle('on_reasoning_delta', {
delta: { content: [{ type: 'think', think: 'reasoning' }] },
});
- handlers.on_run_step.handle('on_run_step', {
- id: 'step-1',
- stepDetails: {
- type: 'tool_calls',
- tool_calls: [{ id: 'call-1', name: 'lookup', args: '{"city":"Madrid"}' }],
- },
+ handlers.on_model_response.handle('on_model_response', {
+ type: 'model_response',
+ id: 'accepted-1',
+ agentId: 'agent',
+ messageId: 'message-1',
+ toolCalls: [{ id: 'call-1', name: 'lookup', args: { city: 'Madrid' } }],
+ toolCallDispositions: ['client'],
+ invalidToolCalls: [],
});
expect(frames).toHaveLength(3);
sendFinalChunk(config, 'stop', {
@@ -80,6 +83,52 @@ describe('OpenAI-compatible agent stream handlers', () => {
expect(current[current.length - 1]).toBe('data: [DONE]\n\n');
});
+ it.each(['sdk', 'provider'] as const)(
+ 'does not project %s-owned calls from accepted results',
+ (disposition) => {
+ const frames: string[] = [];
+ const tracker = createOpenAIStreamTracker();
+ const config: OpenAIStreamWriterConfig = {
+ writer: { write: (frame): void => void frames.push(frame) },
+ context,
+ tracker,
+ };
+ const handlers = createOpenAIHandlers(config);
+ handlers.on_model_response.handle('on_model_response', {
+ type: 'model_response',
+ id: 'accepted-internal',
+ agentId: 'agent',
+ toolCalls: [{ id: 'internal', name: 'lookup', args: { city: 'Madrid' } }],
+ toolCallDispositions: [disposition],
+ invalidToolCalls: [],
+ });
+ sendFinalChunk(config);
+ expect(tracker.toolCalls.size).toBe(0);
+ expect(frames.some((frame) => frame.includes('tool_calls'))).toBe(false);
+ expect(handlers.on_run_step).toBeUndefined();
+ expect(handlers.on_run_step_delta).toBeUndefined();
+ },
+ );
+
+ it('projects explicit client delegation into non-streaming output', () => {
+ const aggregator = createOpenAIContentAggregator();
+ const handlers = createOpenAIHandlers({ aggregator });
+ handlers.on_model_response.handle('on_model_response', {
+ type: 'model_response',
+ id: 'accepted-client',
+ agentId: 'agent',
+ toolCalls: [{ id: 'client', name: 'lookup', args: { city: 'Madrid' } }],
+ toolCallDispositions: ['client'],
+ invalidToolCalls: [],
+ });
+ aggregator.finishToolCalls?.();
+ expect(aggregator.toolCalls.get(0)).toEqual({
+ id: 'client',
+ type: 'function',
+ function: { name: 'lookup', arguments: '{"city":"Madrid"}' },
+ });
+ });
+
it('propagates a synchronous transport failure instead of claiming completion', () => {
const writer = {
write: (): never => {
diff --git a/packages/api/src/agents/openai/handlers.ts b/packages/api/src/agents/openai/handlers.ts
index 8d63de6119..69643326dd 100644
--- a/packages/api/src/agents/openai/handlers.ts
+++ b/packages/api/src/agents/openai/handlers.ts
@@ -4,6 +4,7 @@
* These handlers convert LibreChat's internal graph events into OpenAI-compatible
* streaming format (SSE with chat.completion.chunk objects).
*/
+import { createOpenAIToolCallStream as createAcceptedToolCallStream } from '@librechat/agents/openai';
import type { Agents } from 'librechat-data-provider';
import type { Graph } from '@librechat/agents';
import type {
@@ -189,6 +190,8 @@ export const GraphEvents = {
ON_MESSAGE_DELTA: 'on_message_delta',
ON_REASONING_DELTA: 'on_reasoning_delta',
ON_TOOL_EXECUTE: 'on_tool_execute',
+ ON_MODEL_RESPONSE: 'on_model_response',
+ ON_MODEL_TOOLS_CLAIMED: 'on_model_tools_claimed',
} as const;
/**
@@ -770,23 +773,20 @@ export function createOpenAIHandlers(
config: OpenAIContentHandlerConfig,
toolExecuteOptions?: ToolExecuteOptions,
): Record<string, EventHandler> {
- /** One projection across both events, so a call keeps a single outward index. */
- const toolCallStream = createOpenAIToolCallStream({
+ const target = 'aggregator' in config ? config.aggregator : config.tracker;
+ const toolCallProjection = createAcceptedToolCallStream({
signal: config.signal,
- toolCalls: 'aggregator' in config ? config.aggregator.toolCalls : config.tracker.toolCalls,
+ toolCalls: target.toolCalls,
emit:
'aggregator' in config
? undefined
: (delta) => writeSSE(getStreamWriter(config), createChunk(config.context, delta)),
});
- const target = 'aggregator' in config ? config.aggregator : config.tracker;
- target.finishToolCalls = toolCallStream.finish;
- target.abortToolCalls = toolCallStream.abort;
+ target.finishToolCalls = toolCallProjection.finish;
+ target.abortToolCalls = toolCallProjection.abort;
const handlers: Record<string, EventHandler> = {
+ ...toolCallProjection.handlers,
[GraphEvents.ON_MESSAGE_DELTA]: new OpenAIMessageDeltaHandler(config),
- [GraphEvents.ON_RUN_STEP_DELTA]: new OpenAIRunStepDeltaHandler(toolCallStream),
- [GraphEvents.ON_RUN_STEP]: new OpenAIRunStepHandler(toolCallStream),
- [GraphEvents.ON_RUN_STEP_COMPLETED]: new OpenAIRunStepHandler(toolCallStream),
[GraphEvents.CHAT_MODEL_END]: new OpenAIModelEndHandler(config),
[GraphEvents.CHAT_MODEL_STREAM]: new OpenAIChatModelStreamHandler(),
[GraphEvents.TOOL_END]: new OpenAIToolEndHandler(),
diff --git a/packages/api/src/agents/openai/service.spec.ts b/packages/api/src/agents/openai/service.spec.ts
index 8e1381f8e9..d37b058233 100644
--- a/packages/api/src/agents/openai/service.spec.ts
+++ b/packages/api/src/agents/openai/service.spec.ts
@@ -105,76 +105,44 @@ describe('createAgentChatCompletion - MCP permission user propagation', () => {
};
});
- it.each(
- [true, false].flatMap((stream) =>
- ['wire-string', 'wire-object', 'native-string', 'idless'].map(
- (shape) => [stream, shape] as const,
- ),
- ),
- )(
- 'retains complete and mixed snapshots through the service (stream=%s, shape=%s)',
- async (stream, shape) => {
+ it.each([true, false])(
+ 'keeps graph and provider-owned calls internal (stream=%s)',
+ async (stream) => {
const req = createMockReq(
{ id: 'user' },
{ model: 'agent_test', messages: [{ role: 'user', content: 'hi' }], stream },
);
const res = createMockRes();
processStream.mockImplementationOnce(async () => {
- const { customHandlers: h } = createRun.mock.calls[0][0] as Parameters<
+ const { customHandlers: handlers } = createRun.mock.calls[0][0] as Parameters<
NonNullable<ChatCompletionDependencies['createRun']>
>[0];
- const meta = { langgraph_node: 'agent=test', langgraph_step: 1 };
- await h.on_run_step.handle(
- 'on_run_step',
- {
- id: 'step',
- stepDetails: {
- type: 'tool_calls',
- tool_calls: [
- (() => {
- if (shape === 'idless')
- return { name: 'get_time', args: { city: 'Madrid' }, index: 0 };
- if (shape === 'native-string')
- return { id: 'a', name: 'get_time', args: '{"city":"Madrid"}', index: 0 };
- return {
- id: 'a',
- function: {
- name: 'get_time',
- arguments: shape === 'wire-object' ? { city: 'Madrid' } : '{"city":"Madrid"}',
- },
- index: 0,
- };
- })(),
- { id: 'b', name: 'get_time', args: {}, index: 1 },
- ],
- },
- },
- meta,
- );
- await h.on_run_step_delta.handle(
- 'on_run_step_delta',
- {
- id: 'step',
- delta: { type: 'tool_calls', tool_calls: [{ index: 1, args: '{"city":"Paris"}' }] },
- },
- meta,
- );
- await h.on_message_delta.handle('on_message_delta', {
+ await handlers.on_model_response.handle('on_model_response', {
+ type: 'model_response',
+ id: 'accepted',
+ agentId: 'agent_test',
+ messageId: 'message',
+ toolCalls: [
+ { id: 'internal', name: 'get_time', args: { city: 'Madrid' } },
+ { id: 'server', name: 'web_search', args: { query: 'weather' } },
+ ],
+ toolCallDispositions: ['sdk', 'provider'],
+ invalidToolCalls: [],
+ });
+ await handlers.on_message_delta.handle('on_message_delta', {
delta: { content: [{ type: 'text', text: 'Finished.' }] },
});
});
+
await createAgentChatCompletion(req, res, deps);
+
if (stream) {
- const frames = (res.write as jest.Mock).mock.calls
+ const chunks: ChatCompletionChunk[] = (res.write as jest.Mock).mock.calls
.map(([frame]: [string]) => frame)
- .filter((frame) => frame !== 'data: [DONE]\n\n');
- const chunks: ChatCompletionChunk[] = frames.map((frame) => JSON.parse(frame.slice(6)));
- const args = new Map<number, string>();
- for (const chunk of chunks)
- for (const call of chunk.choices[0].delta.tool_calls ?? [])
- args.set(call.index, (args.get(call.index) ?? '') + (call.function?.arguments ?? ''));
- expect(args.get(0)).toBe('{"city":"Madrid"}');
- expect(args.get(1)).toBe('{"city":"Paris"}');
+ .filter((frame) => frame !== 'data: [DONE]\n\n')
+ .map((frame) => JSON.parse(frame.slice(6)));
+ expect(chunks.flatMap((chunk) => chunk.choices[0].delta.tool_calls ?? [])).toEqual([]);
+ expect(chunks.some((chunk) => chunk.choices[0].delta.content === 'Finished.')).toBe(true);
expect(chunks[chunks.length - 1].choices[0].finish_reason).toBe('stop');
} else {
expect(getResponseMock(res, 'json')).toHaveBeenCalledWith(
@@ -182,25 +150,13 @@ describe('createAgentChatCompletion - MCP permission user propagation', () => {
choices: [
expect.objectContaining({
finish_reason: 'stop',
- message: expect.objectContaining({
- content: 'Finished.',
- tool_calls: [
- {
- id: shape === 'idless' ? 'call_0' : 'a',
- type: 'function',
- function: { name: 'get_time', arguments: '{"city":"Madrid"}' },
- },
- {
- id: 'b',
- type: 'function',
- function: { name: 'get_time', arguments: '{"city":"Paris"}' },
- },
- ],
- }),
+ message: expect.objectContaining({ content: 'Finished.' }),
}),
],
}),
);
+ const response = getResponseMock(res, 'json').mock.calls[0][0] as ChatCompletionChunk;
+ expect(JSON.stringify(response)).not.toContain('tool_calls');
}
},
);
@@ -217,14 +173,13 @@ describe('createAgentChatCompletion - MCP permission user propagation', () => {
const { customHandlers: h } = createRun.mock.calls[0][0] as Parameters<
NonNullable<ChatCompletionDependencies['createRun']>
>[0];
- await h.on_run_step.handle('on_run_step', {
- id: 'step',
- stepDetails: {
- type: 'tool_calls',
- tool_calls: [
- { id: 'a', function: { name: 'get_time', arguments: '{"city":"DO_NOT_FLUSH"}' } },
- ],
- },
+ await h.on_model_response.handle('on_model_response', {
+ type: 'model_response',
+ id: 'accepted-before-failure',
+ agentId: 'agent_test',
+ toolCalls: [{ id: 'a', name: 'get_time', args: { city: 'DO_NOT_FLUSH' } }],
+ toolCallDispositions: ['client'],
+ invalidToolCalls: [],
});
throw new Error('provider failed');
});
@@ -234,9 +189,13 @@ describe('createAgentChatCompletion - MCP permission user propagation', () => {
const { customHandlers: h } = createRun.mock.calls[0][0] as Parameters<
NonNullable<ChatCompletionDependencies['createRun']>
>[0];
- await h.on_run_step_delta.handle('on_run_step_delta', {
- id: 'step',
- delta: { type: 'tool_calls', tool_calls: [{ id: 'a', args: '{"late":true}' }] },
+ await h.on_model_response.handle('on_model_response', {
+ type: 'model_response',
+ id: 'late',
+ agentId: 'agent_test',
+ toolCalls: [{ id: 'late', name: 'get_time', args: { late: true } }],
+ toolCallDispositions: ['client'],
+ invalidToolCalls: [],
});
expect(JSON.stringify((res.write as jest.Mock).mock.calls)).not.toContain('late');
},
diff --git a/packages/api/src/agents/openai/toolCalls.graph.spec.ts b/packages/api/src/agents/openai/toolCalls.graph.spec.ts
index 302cc65dff..96c7775efc 100644
--- a/packages/api/src/agents/openai/toolCalls.graph.spec.ts
+++ b/packages/api/src/agents/openai/toolCalls.graph.spec.ts
@@ -1,11 +1,15 @@
+import { z } from 'zod';
import { response } from 'express';
-import { AIMessageChunk } from '@librechat/agents/langchain/messages';
+import { tool } from '@langchain/core/tools';
+import { AIMessageChunk, HumanMessage } from '@librechat/agents/langchain/messages';
import {
ChatModelStreamHandler,
HandlerRegistry,
Providers,
StandardGraph,
+ Run,
} from '@librechat/agents';
+import type { BaseMessage } from '@langchain/core/messages';
import type { Response } from 'express';
import type { ChatCompletionChunk, ToolCall } from './types';
import {
@@ -14,6 +18,7 @@ import {
createOpenAIContentAggregator,
createOpenAIToolCallStream,
OpenAIRunStepHandler,
+ createChunk,
OpenAIRunStepDeltaHandler,
sendFinalChunk,
} from './handlers';
@@ -33,13 +38,16 @@ describe('tool-call projection with real SDK graph dispatch', () => {
return true;
});
const config = { tracker, context, res };
- const stream = createOpenAIToolCallStream({ toolCalls: tracker.toolCalls });
- const handlers = streaming
- ? createOpenAIHandlers(config)
- : {
- on_run_step: new OpenAIRunStepHandler(stream),
- on_run_step_delta: new OpenAIRunStepDeltaHandler(stream),
- };
+ const stream = createOpenAIToolCallStream({
+ toolCalls: tracker.toolCalls,
+ emit: streaming
+ ? (delta) => frames.push(`data: ${JSON.stringify(createChunk(context, delta))}\n\n`)
+ : undefined,
+ });
+ const handlers = {
+ on_run_step: new OpenAIRunStepHandler(stream),
+ on_run_step_delta: new OpenAIRunStepDeltaHandler(stream),
+ };
const graph = new StandardGraph({
runId: 'test',
agents: [{ agentId: 'agent', provider: Providers.OPENAI, tools: [] }],
@@ -47,7 +55,10 @@ describe('tool-call projection with real SDK graph dispatch', () => {
graph.config = { configurable: { run_id: 'test', thread_id: 'thread' } };
graph.handlerRegistry = new HandlerRegistry();
for (const [event, handler] of Object.entries(handlers)) {
- graph.handlerRegistry.register(event, handler);
+ graph.handlerRegistry.register(
+ event,
+ handler as unknown as Parameters<HandlerRegistry['register']>[1],
+ );
}
const producer = new ChatModelStreamHandler();
for (const chunk of [
@@ -70,8 +81,7 @@ describe('tool-call projection with real SDK graph dispatch', () => {
);
}
expect(frames).toEqual([]);
- if (streaming) tracker.finishToolCalls?.();
- else stream.finish();
+ stream.finish();
expect([...tracker.toolCalls.values()].map((call) => call.function.arguments)).toEqual([
'{"city":"Madrid"}',
'{"city":"Paris"}',
@@ -99,14 +109,7 @@ describe('tool-call projection with real SDK graph dispatch', () => {
}
}
expect([...received.values()]).toEqual([...tracker.toolCalls.values()]);
- await producer.handle(
- 'on_chat_model_stream',
- {
- chunk: new AIMessageChunk({ content: 'Both tools completed. Here is the answer.' }),
- },
- { langgraph_node: 'agent=agent', langgraph_step: 3 },
- graph,
- );
+ tracker.addText();
expect(tracker.hasText).toBe(true);
sendFinalChunk(config, 'stop');
const final: ChatCompletionChunk = JSON.parse(frames[frames.length - 2].slice(6));
@@ -156,7 +159,7 @@ describe('tool-call projection with real SDK graph dispatch', () => {
expect(new Set([...tracker.toolCalls.values()].map((call) => call.id)).size).toBe(4);
});
it.each([true, false])(
- 'projects complete-only SDK messages at response completion (stream=%s)',
+ 'omits graph-owned SDK calls from accepted output (stream=%s)',
async (streaming) => {
const frames: string[] = [];
const res: Response = Object.create(response);
@@ -191,21 +194,24 @@ describe('tool-call projection with real SDK graph dispatch', () => {
{ langgraph_node: 'agent=agent', langgraph_step: 1 },
graph,
);
+ await handlers.on_model_response.handle('on_model_response', {
+ type: 'model_response',
+ id: 'accepted-internal',
+ agentId: 'agent',
+ toolCalls: [{ id: 'a', name: 'get_time', args: { city: 'Madrid' } }],
+ toolCallDispositions: ['sdk'],
+ invalidToolCalls: [],
+ });
const target = streaming ? tracker : aggregator;
expect(target.toolCalls.size).toBe(0);
if (streaming) sendFinalChunk(config);
else target.finishToolCalls?.();
- expect(target.toolCalls.get(0)?.function.arguments).toBe('{"city":"Madrid"}');
+ expect(target.toolCalls.size).toBe(0);
if (streaming) {
const chunks: ChatCompletionChunk[] = frames
.filter((frame) => frame !== 'data: [DONE]\n\n')
.map((frame) => JSON.parse(frame.slice(6)));
- expect(
- chunks
- .flatMap((chunk) => chunk.choices[0].delta.tool_calls ?? [])
- .map((call) => call.function?.arguments)
- .join(''),
- ).toBe('{"city":"Madrid"}');
+ expect(chunks.flatMap((chunk) => chunk.choices[0].delta.tool_calls ?? [])).toEqual([]);
}
},
);
@@ -258,6 +264,77 @@ describe('tool-call projection with real SDK graph dispatch', () => {
'{"i":1}',
]);
});
+ it('keeps tools executed by the SDK out of the final OpenAI response', async () => {
+ const frames: string[] = [];
+ const tracker = createOpenAIStreamTracker();
+ const context = { requestId: 'accepted', created: 1, model: 'fixture' };
+ const res: Response = Object.create(response);
+ jest.spyOn(res, 'write').mockImplementation((frame) => {
+ frames.push(String(frame));
+ return true;
+ });
+ const config = { tracker, context, res };
+ const executed: string[] = [];
+ const handlers = createOpenAIHandlers(config);
+ const run = await Run.create({
+ runId: 'accepted-host-integration',
+ skipCleanup: true,
+ tokenCounter: () => 1,
+ graphConfig: {
+ type: 'standard',
+ maxContextTokens: 100_000,
+ llmConfig: {
+ provider: Providers.OPENAI,
+ streaming: false,
+ streamUsage: false,
+ },
+ tools: [
+ tool(
+ async ({ city }) => {
+ executed.push(city);
+ return 'sunny';
+ },
+ {
+ name: 'lookup',
+ description: 'Weather',
+ schema: z.object({ city: z.string() }),
+ },
+ ),
+ ],
+ },
+ customHandlers: handlers,
+ });
+ if (run.Graph == null) throw new Error('Missing graph');
+ let calls = 0;
+ run.Graph.overrideModel = {
+ invoke: async (messages: BaseMessage[]): Promise<AIMessageChunk> => {
+ calls++;
+ return messages.some((message) => message.getType() === 'tool')
+ ? new AIMessageChunk('done')
+ : new AIMessageChunk({
+ content: '',
+ tool_calls: [{ id: 'internal', name: 'lookup', args: { city: 'Paris' } }],
+ });
+ },
+ };
+
+ await run.processStream(
+ { messages: [new HumanMessage('weather')] },
+ { configurable: { thread_id: 'accepted-host-integration' }, version: 'v2' },
+ );
+ sendFinalChunk(config);
+
+ expect(calls).toBe(2);
+ expect(executed).toEqual(['Paris']);
+ expect(tracker.toolCalls.size).toBe(0);
+ const chunks: ChatCompletionChunk[] = frames
+ .filter((frame) => frame !== 'data: [DONE]\n\n')
+ .map((frame) => JSON.parse(frame.slice(6)));
+ expect(chunks.flatMap((chunk) => chunk.choices[0].delta.tool_calls ?? [])).toEqual([]);
+ expect(chunks.some((chunk) => chunk.choices[0].delta.content === 'done')).toBe(true);
+ expect(chunks[chunks.length - 1].choices[0].finish_reason).toBe('stop');
+ });
+
it('assembles the name before emitting when real SDK chunks split it', async () => {
const tracker = createOpenAIStreamTracker();
const emitted: ChatCompletionChunk['choices'][number]['delta'][] = [];