LibreChat/api/server/services/Tools/mcp.spec.js
Danny Avila 037b0331f8
📇 fix: Stop an MCP Credential Refresh From Fencing Its Own Catalog Recovery (#15865)
* 📇 fix: Stop an MCP Credential Refresh From Fencing Its Own Catalog Recovery

Passive catalog recovery captures the tool-cache generation before discovering a
cold server and discards the catalog when the stored generation has moved by the
time discovery returns. A silent OAuth refresh inside that same discovery
publishes a rotation of its own and then clears this replica's recovery state,
so the tracker drops the flight as superseded and the generation check rejects
it: the server is listed with no tools, and a concurrent catalog request starts
a second discovery instead of joining the first.

The authorization fence now reaches discovery through recovery's own
dependencies, which report each generation a discovery's refresh publishes. The
tracker adopts that generation while the flight is still running, so the flight
stays current and its outcome carries the generation it produced; a rotation or
clear from any other writer afterwards still supersedes it, and a refresh that
completes after its discovery settled no longer touches that settled state.

* 🪢 fix: Keep a Recovery Flight Joinable Through Its Own Credential Publication

A refresh's authorization fence writes the new shared generation, clears local
recovery state, and only then reports what it wrote. A catalog request that read
the new generation during that span found the flight still keyed to the old
generation, or its slot vacated by the clear, and replaced it with a second
discovery; the original flight was then discarded.

Discovery now runs its publication through a tracker bracket. While a flight's
own publication is in progress the tracker holds any clear for that server and
lets requests for the same config join the flight; when it ends, the flight
adopts the generation it wrote, or applies the held clear if the publication
failed or the flight settled first. The flight is registered before its
discovery starts, so a publication can never precede its own registration.

* ⏳ fix: Bound and Count What a Recovery Flight Holds During Its Own Publication

A recovery flight held every clear for its key and admitted any request for its
server for as long as its own credential publication stayed open. A clear from
another writer collapsed into the fence's own clear and was dropped on adoption,
leaving a backoff or reauthorization decision in place after a teardown; a
publication stalled in an unbounded storage write kept every later request
joined to the stalled flight; and a joiner that had already read a foreign
generation could keep tools under an older adopted generation when the final
generation read failed.

An open publication now holds clears and admits joins only while its flight is
in flight and for at most one discovery budget, after which requests take over
as before. The fence clears local recovery once when it completes, so any other
held clear still applies after the flight adopts its generation, and every held
clear applies when the publication fails or the flight settles first. An outcome
under a generation the request never observed stands only when the final shared
generation read confirms it.

* 🧬 fix: Correlate Catalog Recovery Clears With the Generation Their Fence Wrote

Local catalog-recovery clears carried only a user and server, so the tracker
could not tell a flight's own credential publication from any other writer. It
counted clears and bounded its hold with a time window instead, which left a
request that joined before the window expired waiting on a stalled publication,
and let a late fence clear delete the flight that took over after the window.

The authorization fence now hands local recovery the generation it wrote, and a
clear spares state already recorded under that generation; a clear without one
still clears. A flight holds the clears it receives while its own publication is
open, adopts the generation that publication wrote, and then applies each held
clear against it, so its own clear is spared while a teardown or another
publication's clear still applies. Retry-intent cleanup inside the fence is
bounded by the same per-attempt timeout as the generation write, since a leftover
intent only advances the generation again, so the publication itself can no
longer hold its joiners open and no time window is needed.

* 🔏 fix: Confirm an Adopted Catalog Generation for Requests That Read None

A request whose pre-discovery generation reads both failed observed no
generation, so the confirmation rule exempted it. When that request's own
discovery refreshed credentials, adopted the generation its publication wrote,
and another replica rotated past it before a final read that also failed, the
catalog was served without ever being confirmed against shared state.

A flight's own result now records that its generation came from its own
publication, and that mark is never copied into retained state. An outcome
under a generation the request did not read needs final confirmation whenever
the request read a different generation or the generation was adopted; an outcome
whose generation the tracker merely carried over for a request that read nothing
keeps the existing fail-open behavior, so a cache outage still preserves retained
backoff and reauthorization state.

* 🧭 fix: Judge In-Flight Catalog Recovery by the Shared Generation, Not a Tagged Clear

A publication's local clear carries the generation it wrote, and the tracker
deleted any state recorded under a different one. Generations carry no order, so
a refresh that outlived its own flight could clear with an older generation and
delete a flight that had started under a newer rotation from another replica,
discarding a catalog that was still current.

A tagged clear now only signals that the shared generation advanced. A flight
still in flight under a known generation, or publishing its own credential
change, is left to the requests awaiting it, which already confirm their outcome
against the shared generation exactly as they do for a rotation from another
replica. Retained state and flights with no known generation are cleared unless
recorded under the clear's generation, and a clear without a generation still
clears unconditionally. The tracker no longer holds clears during a publication:
the refresh's own clear spares its flight directly, and the flight adopts the
generation it wrote when the publication completes.

The shared-discovery tests compared catalog maps through expect.objectContaining,
which does not compare Map contents; they now assert each request's catalog with
toEqual.

* 🧾 fix: Confirm a Catalog Flight That a Clear for Another Generation Spared

A tagged clear spares a flight still in flight, because an unordered generation
cannot say whether that flight predates the rotation. The spare was not recorded,
so when the request's final shared-generation read timed out, a flight that had
observed its own generation kept a catalog the clear may have superseded: an older
recovery whose refresh published after a replacement flight started could leave
that replacement serving tools no read ever confirmed.

A spared flight now records the generation each such clear carried and settles
against its final generation. A clear for its own generation, including its own
refresh's, changes nothing; any other marks the flight's result unconfirmed, keeps
it out of retained state, and requires every request awaiting it to confirm the
generation against the shared read before using its tools. A spared flight that
finishes with no known generation is discarded, as the clear would have done. A
tagged clear no longer spares retained state recorded under its generation.
2026-09-12 19:50:28 -04:00

726 lines
24 KiB
JavaScript

const { Constants } = require('librechat-data-provider');
const { logger } = require('@librechat/data-schemas');
const mockGetConnection = jest.fn();
const mockDiscoverServerTools = jest.fn();
const mockGetGraphApiToken = jest.fn();
const mockUpdateMCPServerTools = jest.fn();
const mockGetMCPToolsCacheGeneration = jest.fn().mockResolvedValue('generation-current');
const mockGetToolPublicationGeneration = jest.fn().mockReturnValue('generation-current');
const mockLoadCatalogs = jest.fn();
const mockGetUserMCPAuthMap = jest.fn();
const mockFormatMCPServerTools = jest.fn();
const mockGetMCPServerTools = jest.fn();
const mockCacheMCPServerTools = jest.fn();
const mockGetServerToolFunctionsSnapshot = jest.fn();
const mockClearCatalogRecoveryState = jest.fn();
const mockInvalidateCachedTools = jest.fn();
jest.mock('@librechat/api', () => ({
...jest.requireActual('@librechat/api'),
loadMCPServerCatalogs: (...args) => mockLoadCatalogs(...args),
getUserMCPAuthMap: (...args) => mockGetUserMCPAuthMap(...args),
formatMCPServerTools: (...args) => mockFormatMCPServerTools(...args),
}));
jest.mock('~/config', () => ({
getMCPManager: jest.fn(() => ({
getConnection: mockGetConnection,
discoverServerTools: mockDiscoverServerTools,
getServerToolFunctionsSnapshot: mockGetServerToolFunctionsSnapshot,
getToolPublicationGeneration: mockGetToolPublicationGeneration,
clearCatalogRecoveryState: mockClearCatalogRecoveryState,
})),
getMCPServersRegistry: jest.fn(() => ({ getServerConfig: jest.fn() })),
getFlowStateManager: jest.fn(() => ({})),
}));
jest.mock('~/models', () => ({
findToken: jest.fn(),
createToken: jest.fn(),
updateToken: jest.fn(),
deleteTokens: jest.fn(),
findPluginAuthsByKeys: jest.fn(),
}));
jest.mock('~/server/services/Config', () => ({
updateMCPServerTools: mockUpdateMCPServerTools,
getMCPToolsCacheGeneration: mockGetMCPToolsCacheGeneration,
getMCPServerTools: mockGetMCPServerTools,
cacheMCPServerTools: mockCacheMCPServerTools,
invalidateCachedTools: mockInvalidateCachedTools,
}));
jest.mock('~/server/services/MCPAuthorizationFenceRetry', () => ({
persistMCPAuthorizationFenceRetry: jest.fn().mockResolvedValue('retry-v1'),
clearMCPAuthorizationFenceRetry: jest.fn().mockResolvedValue(undefined),
}));
jest.mock('~/server/services/GraphTokenService', () => ({
getGraphApiToken: mockGetGraphApiToken,
}));
jest.mock('~/cache', () => ({
getLogStores: jest.fn(() => ({})),
}));
const { reinitMCPServer, loadMCPServerCatalogs } = require('./mcp');
describe('loadMCPServerCatalogs', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('wires batched auth and passive discovery without opening a managed connection', async () => {
const user = { id: 'user-123' };
const servers = [
{
serverName: 'config-only',
serverConfig: { type: 'sse', url: 'https://config.example.com/sse' },
},
{
serverName: 'user-server',
serverConfig: { type: 'sse', url: 'https://user.example.com/sse' },
},
];
mockGetUserMCPAuthMap.mockResolvedValue({});
mockDiscoverServerTools.mockResolvedValue({ tools: [] });
mockFormatMCPServerTools.mockReturnValue({});
const observedCredentialFence = jest.fn();
let recoveryDeps;
mockLoadCatalogs.mockImplementation(async (params, deps) => {
recoveryDeps = deps;
await deps.loadUserMCPAuthMap(
user.id,
servers.map(({ serverName }) => serverName),
);
await deps.discoverServerTools({
user,
serverName: 'config-only',
configServers: { 'config-only': servers[0].serverConfig },
onOAuthCredentialsChanging: observedCredentialFence,
});
deps.formatServerTools('config-only', []);
await deps.getCachedServerTools(user.id, 'config-only', servers[0].serverConfig);
await deps.getServerToolFunctionsSnapshot(user.id, 'config-only', servers[0].serverConfig, {
deadlineMs: 123,
});
await deps.getRecoveryGeneration({ userId: user.id, serverName: 'config-only' });
await deps.cacheServerTools({ serverName: 'config-only' });
return { serverTools: new Map([['config-only', {}]]), serversWithoutTools: [] };
});
const upstreamTokenProvider = jest.fn();
const oboIdentityContext = { appUserId: 'user-123' };
const result = await loadMCPServerCatalogs({
user,
servers,
upstreamTokenProvider,
oboIdentityContext,
});
expect(mockGetUserMCPAuthMap).toHaveBeenCalledTimes(1);
expect(mockGetMCPToolsCacheGeneration).toHaveBeenCalledWith({
userId: user.id,
serverName: 'config-only',
});
expect(mockGetUserMCPAuthMap).toHaveBeenCalledWith({
userId: user.id,
servers: ['config-only', 'user-server'],
findPluginAuthsByKeys: require('~/models').findPluginAuthsByKeys,
});
expect(recoveryDeps.onOAuthCredentialsChanging).toEqual(expect.any(Function));
expect(mockDiscoverServerTools).toHaveBeenCalledWith(
expect.objectContaining({
user,
serverName: 'config-only',
configServers: { 'config-only': servers[0].serverConfig },
flowManager: expect.any(Object),
tokenMethods: expect.any(Object),
upstreamTokenProvider,
oboIdentityContext,
onOAuthCredentialsChanging: observedCredentialFence,
}),
);
expect(mockGetConnection).not.toHaveBeenCalled();
expect(mockGetMCPServerTools).toHaveBeenCalledWith(
user.id,
'config-only',
servers[0].serverConfig,
);
expect(mockGetServerToolFunctionsSnapshot).toHaveBeenCalledWith(
user.id,
'config-only',
servers[0].serverConfig,
{ deadlineMs: 123 },
);
expect(mockCacheMCPServerTools).toHaveBeenCalledWith({ serverName: 'config-only' });
expect(result).toEqual({
serverTools: new Map([['config-only', {}]]),
serversWithoutTools: [],
});
});
it('clears catalog recovery with the generation its credential fence published', async () => {
let recoveryDeps;
mockInvalidateCachedTools.mockResolvedValue('generation-2');
mockLoadCatalogs.mockImplementation(async (params, deps) => {
recoveryDeps = deps;
return { serverTools: new Map(), serversWithoutTools: [] };
});
await loadMCPServerCatalogs({ user: { id: 'user-123' }, servers: [] });
const publish = await recoveryDeps.onOAuthCredentialsChanging({
userId: 'user-123',
serverName: 'oauth-server',
});
await expect(publish()).resolves.toBe('generation-2');
expect(mockClearCatalogRecoveryState).toHaveBeenCalledWith(
'user-123',
'oauth-server',
'generation-2',
);
});
});
describe('reinitMCPServer — customUserVars gating (issue #10969)', () => {
const user = { id: 'user-123' };
const serverName = 'Thingy';
const serverConfig = {
type: 'streamable-http',
url: 'https://thingy.example.com/mcp',
customUserVars: {
THINGY_TOKEN: { title: 'Thingy Access Token', description: 'Create this in Thingy' },
},
};
beforeEach(() => {
jest.clearAllMocks();
mockUpdateMCPServerTools.mockResolvedValue({});
});
it('does not connect and exposes no tools when a required customUserVar is unset', async () => {
const result = await reinitMCPServer({
user,
serverName,
serverConfig,
userMCPAuthMap: undefined,
});
expect(mockGetConnection).not.toHaveBeenCalled();
expect(result).toMatchObject({
availableTools: null,
success: false,
tools: null,
failureReason: 'missing_custom_user_vars',
missingUserVars: ['THINGY_TOKEN'],
oauthRequired: false,
serverName,
});
expect(result.message).toContain('THINGY_TOKEN');
});
it('does not connect when the stored value for a required customUserVar is empty', async () => {
const result = await reinitMCPServer({
user,
serverName,
serverConfig,
userMCPAuthMap: { [`${Constants.mcp_prefix}${serverName}`]: { THINGY_TOKEN: '' } },
});
expect(mockGetConnection).not.toHaveBeenCalled();
expect(result.success).toBe(false);
expect(result.availableTools).toBeNull();
});
it('proceeds to connect once every required customUserVar is provided', async () => {
mockGetConnection.mockResolvedValue({ fetchTools: jest.fn().mockResolvedValue([]) });
await reinitMCPServer({
user,
serverName,
serverConfig,
userMCPAuthMap: {
[`${Constants.mcp_prefix}${serverName}`]: { THINGY_TOKEN: 'secret-token' },
},
});
expect(mockGetConnection).toHaveBeenCalledTimes(1);
expect(mockGetConnection).toHaveBeenCalledWith(
expect.objectContaining({
serverName,
customUserVars: { THINGY_TOKEN: 'secret-token' },
}),
);
});
it('updates the cache with an empty catalog after a successful connection', async () => {
mockGetConnection.mockResolvedValue({ fetchTools: jest.fn().mockResolvedValue([]) });
await reinitMCPServer({
user,
serverName,
serverConfig: { type: 'streamable-http', url: 'https://thingy.example.com/mcp' },
userMCPAuthMap: undefined,
});
expect(mockUpdateMCPServerTools).toHaveBeenCalledWith({
userId: user.id,
serverName,
tools: [],
serverConfig: { type: 'streamable-http', url: 'https://thingy.example.com/mcp' },
publicationGeneration: 'generation-current',
});
});
/** An app-level catalog write is dropped unless it carries the ordering reserved before its
* own tools/list. When this path forwarded no revision, every publication was discarded and
* agents were told the server had no tools at all (#14857). */
it('publishes under the ordering its snapshot was fetched with', async () => {
mockGetConnection.mockResolvedValue({
fetchOrderedToolsSnapshot: jest.fn().mockResolvedValue({
tools: [{ name: 'search', inputSchema: { type: 'object' } }],
complete: true,
publicationRevision: '7',
}),
});
await reinitMCPServer({
user,
serverName,
serverConfig: { type: 'streamable-http', url: 'https://thingy.example.com/mcp' },
});
expect(mockUpdateMCPServerTools).toHaveBeenCalledWith(
expect.objectContaining({ serverName, publicationRevision: '7' }),
);
});
it('asks the connection to republish a catalog it could not order', async () => {
const refreshToolList = jest.fn().mockResolvedValue(undefined);
mockGetConnection.mockResolvedValue({
refreshToolList,
fetchOrderedToolsSnapshot: jest.fn().mockResolvedValue({
tools: [{ name: 'search', inputSchema: { type: 'object' } }],
complete: true,
orderingUnavailable: true,
}),
});
const result = await reinitMCPServer({
user,
serverName,
serverConfig: { type: 'streamable-http', url: 'https://thingy.example.com/mcp' },
});
expect(refreshToolList).toHaveBeenCalledTimes(1);
expect(result.tools).toHaveLength(1);
});
it('preserves cached tools when live recovery returns an incomplete snapshot', async () => {
const fetchOrderedToolsSnapshot = jest.fn().mockResolvedValue({
tools: [{ name: 'partial', inputSchema: { type: 'object' } }],
complete: false,
});
mockGetConnection.mockResolvedValue({
fetchOrderedToolsSnapshot,
});
const result = await reinitMCPServer({
user,
serverName,
serverConfig: { type: 'streamable-http', url: 'https://thingy.example.com/mcp' },
});
expect(result.tools).toBeNull();
expect(fetchOrderedToolsSnapshot).toHaveBeenCalledTimes(1);
expect(mockUpdateMCPServerTools).not.toHaveBeenCalled();
});
it('discards a snapshot when another replica rotates its generation during discovery', async () => {
mockGetMCPToolsCacheGeneration
.mockResolvedValueOnce('generation-current')
.mockResolvedValueOnce('generation-replaced');
mockGetConnection.mockResolvedValue({
fetchOrderedToolsSnapshot: jest.fn().mockResolvedValue({
tools: [{ name: 'stale', inputSchema: { type: 'object' } }],
complete: true,
}),
});
const result = await reinitMCPServer({
user,
serverName,
serverConfig: { type: 'streamable-http', url: 'https://thingy.example.com/mcp' },
});
expect(result.tools).toBeNull();
expect(result.availableTools).toBeNull();
expect(mockUpdateMCPServerTools).not.toHaveBeenCalled();
});
it('does not return tools when the guarded publication loses its generation race', async () => {
mockGetConnection.mockResolvedValue({
fetchOrderedToolsSnapshot: jest.fn().mockResolvedValue({
tools: [{ name: 'stale', inputSchema: { type: 'object' } }],
complete: true,
}),
});
mockUpdateMCPServerTools.mockResolvedValue(null);
const result = await reinitMCPServer({
user,
serverName,
serverConfig: { type: 'streamable-http', url: 'https://thingy.example.com/mcp' },
});
expect(result.tools).toBeNull();
expect(result.availableTools).toBeNull();
});
it('passes request body and Graph resolver into connection creation', async () => {
mockGetConnection.mockResolvedValue({ fetchTools: jest.fn().mockResolvedValue([]) });
const requestBody = { conversationId: 'conv-123', messageId: 'msg-123' };
await reinitMCPServer({
user,
serverName,
serverConfig: { type: 'streamable-http', url: 'https://thingy.example.com/mcp' },
requestBody,
userMCPAuthMap: undefined,
});
expect(mockGetConnection).toHaveBeenCalledWith(
expect.objectContaining({
requestBody,
graphTokenResolver: mockGetGraphApiToken,
}),
);
});
it('forwards the pre-built upstreamTokenProvider closure into connection creation', async () => {
mockGetConnection.mockResolvedValue({ fetchTools: jest.fn().mockResolvedValue([]) });
const upstreamTokenProvider = jest.fn().mockResolvedValue(null);
await reinitMCPServer({
user,
serverName,
serverConfig: { type: 'streamable-http', url: 'https://thingy.example.com/mcp' },
upstreamTokenProvider,
});
expect(mockGetConnection).toHaveBeenCalledWith(
expect.objectContaining({ upstreamTokenProvider }),
);
});
it('passes request body and Graph resolver into OAuth discovery fallback', async () => {
mockGetConnection.mockRejectedValue(new Error('OAuth authentication required'));
mockDiscoverServerTools.mockResolvedValue({ tools: [], oauthRequired: true, oauthUrl: null });
const requestBody = { conversationId: 'conv-456', messageId: 'msg-456' };
const result = await reinitMCPServer({
user,
serverName,
serverConfig: { type: 'streamable-http', url: 'https://thingy.example.com/mcp' },
requestBody,
userMCPAuthMap: undefined,
});
expect(result).toMatchObject({
success: false,
failureReason: 'oauth_required',
oauthRequired: true,
oauthUrl: null,
});
expect(mockDiscoverServerTools).toHaveBeenCalledWith(
expect.objectContaining({
requestBody,
graphTokenResolver: mockGetGraphApiToken,
}),
);
});
it('disposes ephemeral BODY-scoped connections after loading tools', async () => {
const dispose = jest.fn().mockResolvedValue(undefined);
const tools = [{ name: 'search', inputSchema: { type: 'object', properties: {} } }];
const serverConfig = {
type: 'streamable-http',
url: 'https://thingy.example.com/messages/{{LIBRECHAT_BODY_MESSAGEID}}/mcp',
source: 'yaml',
};
mockGetConnection.mockResolvedValue({
dispose,
fetchTools: jest.fn().mockResolvedValue(tools),
});
await reinitMCPServer({
user,
serverName,
serverConfig,
requestBody: { messageId: 'msg-789' },
userMCPAuthMap: undefined,
});
expect(dispose).toHaveBeenCalledTimes(1);
expect(mockUpdateMCPServerTools).toHaveBeenCalledWith(
expect.objectContaining({
tools,
serverConfig,
}),
);
});
it('proceeds to connect when the server declares no customUserVars', async () => {
mockGetConnection.mockResolvedValue({ fetchTools: jest.fn().mockResolvedValue([]) });
await reinitMCPServer({
user,
serverName,
serverConfig: { type: 'streamable-http', url: 'https://thingy.example.com/mcp' },
userMCPAuthMap: undefined,
});
expect(mockGetConnection).toHaveBeenCalledTimes(1);
});
});
describe('reinitMCPServer — recovery of a server that failed inspection', () => {
const user = { id: 'user-123' };
const serverName = 'Recovering';
const stub = {
type: 'streamable-http',
url: 'https://recovering.example.com/mcp',
source: 'yaml',
inspectionFailed: true,
};
const { getMCPServersRegistry } = require('~/config');
beforeEach(() => {
mockUpdateMCPServerTools.mockResolvedValue({});
});
it('connects with the recovered config instead of the stub it read', async () => {
const recovered = {
type: 'streamable-http',
url: 'https://recovering.example.com/mcp',
source: 'yaml',
requiresOAuth: false,
};
const recoverServerConfig = jest.fn().mockResolvedValue(recovered);
getMCPServersRegistry.mockReturnValueOnce({ recoverServerConfig });
mockGetConnection.mockResolvedValue({ fetchTools: jest.fn().mockResolvedValue([]) });
const result = await reinitMCPServer({ user, serverName, serverConfig: stub });
expect(recoverServerConfig).toHaveBeenCalledWith(serverName, stub, user.id);
expect(mockGetConnection).toHaveBeenCalledWith(
expect.objectContaining({ serverName, serverConfig: recovered }),
);
expect(result).toMatchObject({ success: true, serverName });
});
it('reports the server unreachable without connecting while it cannot be recovered', async () => {
const recoverServerConfig = jest.fn().mockResolvedValue(undefined);
getMCPServersRegistry.mockReturnValueOnce({ recoverServerConfig });
const result = await reinitMCPServer({ user, serverName, serverConfig: stub });
expect(mockGetConnection).not.toHaveBeenCalled();
expect(result).toMatchObject({
availableTools: null,
success: false,
message: `MCP server '${serverName}' is still unreachable`,
failureReason: 'unreachable',
tools: null,
});
});
});
describe('reinitMCPServer — direct bearer authentication outcomes', () => {
it('preserves a typed rejection instead of reducing it to a generic result', async () => {
const { MCPAuthenticationRejectedError } = require('@librechat/api');
const rejection = new MCPAuthenticationRejectedError('private-mcp', false);
mockGetConnection.mockRejectedValue(rejection);
await expect(
reinitMCPServer({
user: { id: 'user-123' },
serverName: 'private-mcp',
serverConfig: {
type: 'streamable-http',
url: 'https://mcp.example.com',
source: 'yaml',
headers: { Authorization: 'Bearer {{LIBRECHAT_OPENID_ACCESS_TOKEN}}' },
},
}),
).rejects.toBe(rejection);
});
});
describe('reinitMCPServer — runtime BODY placeholder pre-check (issue #14074)', () => {
const user = { id: 'user-123' };
const serverName = 'Thingy';
const serverConfig = {
type: 'streamable-http',
url: 'https://thingy.example.com/mcp',
source: 'yaml',
headers: { 'X-Conversation-Id': '{{LIBRECHAT_BODY_CONVERSATIONID}}' },
};
beforeEach(() => {
jest.clearAllMocks();
mockUpdateMCPServerTools.mockResolvedValue({});
});
it('defers connection without failing when body placeholders cannot resolve outside a chat turn', async () => {
const result = await reinitMCPServer({
user,
serverName,
serverConfig,
userMCPAuthMap: undefined,
});
expect(mockGetConnection).not.toHaveBeenCalled();
expect(mockDiscoverServerTools).not.toHaveBeenCalled();
expect(result).toMatchObject({
availableTools: null,
success: true,
connectionDeferred: true,
tools: null,
oauthRequired: false,
serverName,
});
expect(result.message).toContain('first use in a chat turn');
});
it('treats an empty-string body field as missing', async () => {
const result = await reinitMCPServer({
user,
serverName,
serverConfig,
requestBody: { conversationId: ' ' },
userMCPAuthMap: undefined,
});
expect(mockGetConnection).not.toHaveBeenCalled();
expect(result.success).toBe(true);
});
it('connects normally when the request body provides the placeholder fields', async () => {
mockGetConnection.mockResolvedValue({
dispose: jest.fn().mockResolvedValue(undefined),
fetchTools: jest.fn().mockResolvedValue([]),
});
const result = await reinitMCPServer({
user,
serverName,
serverConfig,
requestBody: { conversationId: 'convo-1' },
userMCPAuthMap: undefined,
});
expect(mockGetConnection).toHaveBeenCalledTimes(1);
expect(result.connectionDeferred).toBeUndefined();
});
it('reports missing customUserVars before deferring on body placeholders', async () => {
const result = await reinitMCPServer({
user,
serverName,
serverConfig: {
...serverConfig,
customUserVars: { THINGY_TOKEN: { title: 'Thingy Access Token' } },
},
userMCPAuthMap: undefined,
});
expect(result.success).toBe(false);
expect(result.message).toContain('THINGY_TOKEN');
});
it('still treats unrelated connection errors as real failures', async () => {
mockGetConnection.mockRejectedValue(new Error('ECONNREFUSED'));
const result = await reinitMCPServer({
user,
serverName,
serverConfig: { type: 'streamable-http', url: 'https://thingy.example.com/mcp' },
userMCPAuthMap: undefined,
});
expect(mockDiscoverServerTools).not.toHaveBeenCalled();
expect(result.success).toBe(false);
expect(result.failureReason).toBe('initialization_failed');
expect(result.message).toBe(`Failed to reinitialize MCP server '${serverName}'`);
});
});
describe('reinitMCPServer — OAuth attempt lifetime', () => {
const user = { id: 'user-123' };
const serverName = 'Thingy';
const serverConfig = {
type: 'streamable-http',
url: 'https://thingy.example.com/mcp',
};
beforeEach(() => {
jest.clearAllMocks();
mockUpdateMCPServerTools.mockResolvedValue({});
});
it('returns the expiry supplied when a pending OAuth URL is replayed', async () => {
const expiresAt = Date.now() + 45_000;
mockGetConnection.mockImplementation(async ({ oauthStart }) => {
await oauthStart('https://oauth.example.com/authorize', { expiresAt });
await oauthStart('https://oauth.example.com/authorize');
throw new Error('OAuth flow initiated - return early');
});
mockDiscoverServerTools.mockResolvedValue({ tools: [], oauthRequired: true, oauthUrl: null });
const result = await reinitMCPServer({
user,
serverName,
serverConfig,
});
expect(result).toMatchObject({
success: true,
oauthRequired: true,
oauthUrl: 'https://oauth.example.com/authorize',
oauthExpiresAt: expiresAt,
});
});
});
describe('reinitMCPServer — log hygiene', () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('keeps user-created server and connection details out of discovery logs', async () => {
const serverName = 'PRIVATE-MCP-SERVER-NAME';
const privateUrl = 'https://private.example.test/PRIVATE-CONFIG-PATH';
const privateError = `PRIVATE-CONNECTION-ERROR for ${privateUrl}`;
const logSpies = ['debug', 'info', 'warn', 'error'].map((level) =>
jest.spyOn(logger, level).mockImplementation(() => {}),
);
mockGetConnection.mockRejectedValue(new Error(privateError));
const result = await reinitMCPServer({
user: { id: 'user-123' },
serverName,
serverConfig: { type: 'streamable-http', url: privateUrl },
userMCPAuthMap: undefined,
});
const loggedText = logSpies
.flatMap((spy) => spy.mock.calls)
.flat()
.map((value) => String(value))
.join('\n');
expect(result.message).toContain(serverName);
expect(loggedText).not.toContain(serverName);
expect(loggedText).not.toContain(privateUrl);
expect(loggedText).not.toContain(privateError);
expect(logger.error).toHaveBeenCalledWith('[MCP Reinitialize] Error initializing MCP server');
});
});