LibreChat/api/server/services/MCP/oauthCleanup.js
Danny Avila 601ed07870
🫧 fix: Retire Cancelled MCP OAuth State (#15702)
* fix: Clean Up MCP OAuth Teardown State

* fix: Harden OAuth Teardown Boundaries

* fix: harden MCP OAuth teardown races

* fix: avoid flow cache dependency cycle

* fix: bind OAuth teardown to credential generations

* test: cover active OAuth server fixtures

* style: sort OAuth cleanup imports

* fix: discard mismatched OAuth token snapshots

* fix: fence OAuth teardown across callback races

* test: align OAuth callback fixtures with server fencing

* test: complete OAuth callback server fixtures

* fix: version OAuth flow settlement

* fix: fence OAuth teardown snapshots

* test: align OAuth teardown fixtures

* test: complete OAuth snapshot fixtures

* fix: snapshot OAuth deletion lifecycle

* test: bind OAuth revocation snapshots

* test: assert per-record OAuth fence

* fix: harden MCP OAuth teardown consistency

* test: expect snapshotted OAuth readers
2026-09-07 15:43:02 -04:00

27 lines
1 KiB
JavaScript

const { CacheKeys } = require('librechat-data-provider');
const { MCPOAuthHandler, MCPTokenStorage, cleanupMCPServerOAuth } = require('@librechat/api');
const { getFlowStateManager, getMCPServersRegistry } = require('~/config');
const { getLogStores } = require('~/cache');
const db = require('~/models');
const maybeUninstallOAuthMCP = async (userId, pluginKey, appConfig, serverConfigOverride) => {
const registry = getMCPServersRegistry();
return cleanupMCPServerOAuth({
userId,
pluginKey,
appConfig,
serverConfigOverride,
dependencies: {
flowManager: getFlowStateManager(getLogStores(CacheKeys.FLOWS)),
oauthHandler: MCPOAuthHandler,
tokenStorage: MCPTokenStorage,
findToken: db.findToken,
deleteTokens: db.deleteTokens,
getServerConfig: (serverName, ownerId) => registry.getServerConfig(serverName, ownerId),
isRegisteredOAuthServer: async (serverName, ownerId) =>
(await registry.getOAuthServers(ownerId)).has(serverName),
},
});
};
module.exports = { maybeUninstallOAuthMCP };