🍂 fix: Emit Persisted Agent IDs in Attribution Headers (#18151)

Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
lia-by-librechat[bot] 2026-11-14 12:15:33 -05:00 • committed by GitHub
parent 3c175717b4
commit 8ed7a7d3d7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 82 additions and 2 deletions

View file

@ -89,6 +89,41 @@ describe('MCP executing agent context', () => {
expect(requiresEphemeralUserConnection(config)).toBe(true);
});
it('emits the persisted tool owner while keeping parallel connection identities distinct', async () => {
const runtimeIds = ['agent_root____1', 'agent_root____12'];
const keys = new Set<string>();
for (const agentId of runtimeIds) {
const callTool = jest.fn().mockImplementation(({ requestBody }) => {
expect(requestBody.agentId).toBe(agentId);
keys.add(getMCPAgentConnectionKey('connection', config, requestBody));
return Promise.resolve(processMCPEnv({ options: config, body: requestBody }));
});
const tool = createAgentMCPTool(
{ agentId, requestBody: { agentId: 'agent_spoofed' } },
({ requestBody, invokeTool }) => {
expect(requestBody.agentId).toBe(agentId);
expect(processMCPEnv({ options: config, body: requestBody })).toMatchObject({
headers: { 'X-Agent-Id': 'agent_root' },
});
return () =>
invokeTool(
{ callTool },
{
serverName: 'analytics',
toolName: 'select',
provider: 'openai',
flowManager: {} as Parameters<typeof invokeTool>[1]['flowManager'],
requestBody: { agentId: 'agent_override' },
},
);
},
);
await expect(tool()).resolves.toMatchObject({ headers: { 'X-Agent-Id': 'agent_root' } });
expect(callTool).toHaveBeenCalledTimes(1);
}
expect(keys.size).toBe(runtimeIds.length);
});
it('does not let a custom user variable replace the tool owner', () => {
expect(
processMCPEnv({

View file

@ -2425,6 +2425,30 @@ describe('resolveHeaders agent placeholders', () => {
expect(result['X-Agent-Id']).toBe('agent_HTbJ5rDW0kOOK1a5MPBSt');
});
it.each(['1', '12'])(
'emits the persisted id for parallel runtime node %s without mutating its identity',
(index) => {
const agent = { id: `${persistedAgent.id}____${index}` };
expect(
resolveHeaders({
headers: { 'X-Meta': 'agent={{LIBRECHAT_AGENT_ID}};ref={{LIBRECHAT_AGENT_ID}}' },
agent,
stripUnresolved: true,
}),
).toEqual({ 'X-Meta': `agent=${persistedAgent.id};ref=${persistedAgent.id}` });
expect(agent.id).toBe(`${persistedAgent.id}____${index}`);
},
);
it.each(['agent_with_underscores', 'agent_with____text', 'agent_with___1'])(
'preserves non-runtime suffixes in persisted id %s',
(id) => {
expect(resolveHeaders({ headers: agentHeaders, agent: { id } })).toEqual({
'X-Agent-Id': id,
});
},
);
it('resolves alongside user and body placeholders without interfering', () => {
const result = resolveHeaders({
headers: {

View file

@ -1,5 +1,9 @@
import { logger } from '@librechat/data-schemas';
import { extractEnvVariable, isEphemeralAgentId } from 'librechat-data-provider';
import {
extractEnvVariable,
isEphemeralAgentId,
stripAgentIdSuffix,
} from 'librechat-data-provider';
import type { Agent, MCPOptions } from 'librechat-data-provider';
import type { IUser } from '@librechat/data-schemas';
import type { RequestBody } from '~/types';
@ -342,7 +346,7 @@ export function processAgentPlaceholders(value: string, agent: Partial<Agent>):
}
const fieldValue = agent[field];
const replacementValue = fieldValue == null ? '' : String(fieldValue);
const replacementValue = fieldValue == null ? '' : stripAgentIdSuffix(fieldValue);
value = value.replace(new RegExp(placeholder, 'g'), replacementValue);
}

View file

@ -257,6 +257,23 @@ describe('resolveConfigHeaders', () => {
});
});
it('normalizes parallel identity across provider carriers without rewriting managed Google auth', () => {
const template = { 'X-Agent-Id': '{{LIBRECHAT_AGENT_ID}}' };
const credential = 'Bearer {{LIBRECHAT_AGENT_ID}}';
const llmConfig = {
configuration: { defaultHeaders: { ...template } },
clientOptions: { defaultHeaders: { ...template } },
customHeaders: mergeGoogleHeaders(template, { Authorization: credential }),
};
resolveConfigHeaders({ llmConfig, agent: { id: 'agent_root____12' } });
expect(llmConfig.configuration.defaultHeaders['X-Agent-Id']).toBe('agent_root');
expect(llmConfig.clientOptions.defaultHeaders['X-Agent-Id']).toBe('agent_root');
expect(llmConfig.customHeaders?.['X-Agent-Id']).toBe('agent_root');
expect(llmConfig.customHeaders?.Authorization).toBe(credential);
});
it('leaves configs without header maps untouched', () => {
const llmConfig = { model: 'gpt-4o', configuration: {} } as unknown as RunLLMConfig;
expect(() => resolveConfigHeaders({ llmConfig, user, body })).not.toThrow();