{"id":"8cc25b4e02817159","repo":"mongodb/node-mongodb-native","slug":"no-authprovider-for-credentials-mechanism-defin","errorCode":null,"errorMessage":"No AuthProvider for ${credentials.mechanism} defined.","messagePattern":"No AuthProvider for (.+?) defined\\.","errorType":"exception","errorClass":"MongoInvalidArgumentError","httpStatus":null,"severity":"error","filePath":"src/cmap/connect.ts","lineNumber":277,"sourceCode":"\n      const provider = authContext.options.authProviders.getOrCreateProvider(\n        AuthMechanism.MONGODB_SCRAM_SHA256,\n        credentials.mechanismProperties\n      );\n      if (!provider) {\n        // This auth mechanism is always present.\n        throw new MongoInvalidArgumentError(\n          `No AuthProvider for ${AuthMechanism.MONGODB_SCRAM_SHA256} defined.`\n        );\n      }\n      return await provider.prepare(handshakeDoc, authContext);\n    }\n    const provider = authContext.options.authProviders.getOrCreateProvider(\n      credentials.mechanism,\n      credentials.mechanismProperties\n    );\n    if (!provider) {\n      throw new MongoInvalidArgumentError(`No AuthProvider for ${credentials.mechanism} defined.`);\n    }\n    return await provider.prepare(handshakeDoc, authContext);\n  }\n  return handshakeDoc;\n}\n\n/**\n * @internal\n * Default TCP keepAlive initial delay in milliseconds.\n * Set to half the Azure load balancer idle timeout (240s) to ensure\n * probes fire well before cloud LBs (Azure, AWS PrivateLink/NLB)\n * drop idle connections.\n */\nexport const DEFAULT_KEEP_ALIVE_INITIAL_DELAY_MS = 120_000;\n\n/** @public */\nexport const LEGAL_TLS_SOCKET_OPTIONS = [\n  'allowPartialTrustChain',","sourceCodeStart":259,"sourceCodeEnd":295,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/cmap/connect.ts#L259-L295","documentation":"Thrown in prepareHandshakeDocument for a non-default credentials.mechanism when getOrCreateProvider(mechanism) returns undefined. Unlike error 104 this is the general case: the user named a specific mechanism but the registry has no provider for it. This is the prepare-stage twin of error 101; 101 checks before the handshake and 105 checks while building the handshake document.","triggerScenarios":"Constructing MongoClient with credentials whose mechanism is not 'MONGODB-CR' and is not registered. Fires synchronously inside prepareHandshakeDocument (src/cmap/connect.ts:272-278) during the first command on a new connection.","commonSituations":"Specifying an authMechanism the driver doesn't support (e.g. 'MONGODB-CR' literal in newer driver builds where it was removed, or a future mechanism on an old driver); a custom authProviders registry missing the named mechanism; optional dependency for the mechanism not installed (kerberos for GSSAPI).","solutions":["Confirm the mechanism string matches a registered provider name exactly (case-sensitive).","Install the optional dependency required by the mechanism (kerberos for GSSAPI, aws-sdk for MONGODB-AWS).","Drop the explicit authMechanism and let the driver negotiate.","If you pass a custom authProviders, ensure it registers a provider for the mechanism you named in credentials."],"exampleFix":"// before\nnew MongoClient(uri, { auth: { username: 'u', password: 'p', mechanism: 'MONGODB-CR' } });\n\n// after\nnew MongoClient(uri, { auth: { username: 'u', password: 'p' } }); // negotiate SCRAM","handlingStrategy":"validation","validationCode":"import { AuthMechanism } from 'mongodb';\nconst KNOWN = new Set(Object.values(AuthMechanism));\nfunction validateNamedMechanism(m: string) {\n  if (!KNOWN.has(m as AuthMechanism)) {\n    throw new Error(`No provider for mechanism ${m}; pick from ${[...KNOWN].join(', ')}`);\n  }\n}","typeGuard":"import { AuthMechanism } from 'mongodb';\nfunction isKnownMechanism(m: string): m is AuthMechanism {\n  return Object.values(AuthMechanism).includes(m as AuthMechanism);\n}","tryCatchPattern":"import { MongoInvalidArgumentError } from 'mongodb';\ntry {\n  await client.connect();\n} catch (e) {\n  if (e instanceof MongoInvalidArgumentError && /No AuthProvider for/.test(e.message)) {\n    // remove the explicit mechanism or install the optional dep it needs\n  }\n  throw e;\n}","preventionTips":["Type auth.mechanism as AuthMechanism so the compiler rejects typos.","Install optional dependencies before referencing their mechanisms.","Default to SCRAM negotiation by omitting the mechanism."],"tags":["auth","auth-provider","configuration","handshake"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}