Skip to content
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8ac71ba
chore: Add new session-level service for getting embeddings of a spec…
kmruiz Oct 8, 2025
cb52116
chore: add unit tests to embedding validation
kmruiz Oct 8, 2025
082fce9
chore: add the ability to disable embedding validation
kmruiz Oct 9, 2025
ed7a16e
chore: Make sure that cache works
kmruiz Oct 9, 2025
d68deee
chore: Do not query for the embedding information if the validation i…
kmruiz Oct 9, 2025
32fe96d
chore: it can't be undefined anymore, so this check is useless
kmruiz Oct 9, 2025
2e013f8
chore: Embedding validation on insert and minor refactor of formatUnt…
kmruiz Oct 9, 2025
998cf1b
Merge remote-tracking branch 'origin/main' into chore/mcp-246
kmruiz Oct 9, 2025
81f9ddd
Update src/tools/mongodb/create/insertMany.ts
kmruiz Oct 9, 2025
0a1c789
chore: Add integration test for insert many
kmruiz Oct 13, 2025
c68e4ad
chore: Make eslint happy
kmruiz Oct 13, 2025
539c4a5
chore: test slightly older image of atlas-local in case it's broken i…
kmruiz Oct 13, 2025
44a3ce8
chore: increase timeout time for CI
kmruiz Oct 13, 2025
a5842ef
chore: minor fixes from the PR comments
kmruiz Oct 15, 2025
13c1c35
Merge remote-tracking branch 'origin/main' into chore/mcp-246
kmruiz Oct 15, 2025
a04c2f3
chore: Merge reliably search permission detection
kmruiz Oct 15, 2025
94fdcda
Merge branch 'main' into chore/mcp-246
kmruiz Oct 15, 2025
3264796
chore: cleanup embeddings cache when the connection is closed
kmruiz Oct 15, 2025
3b104b5
chore: clean up embeddings cache after creating an index
kmruiz Oct 15, 2025
19a333c
chore: simplify, assume search indexes are available just by listing …
kmruiz Oct 16, 2025
7eed735
chore: add the Manager suffix
kmruiz Oct 16, 2025
519a0c4
Merge branch 'main' into chore/mcp-246
kmruiz Oct 16, 2025
3d69362
Update src/common/search/vectorSearchEmbeddingsManager.ts
kmruiz Oct 16, 2025
debc6f9
chore: Remove unused error code and messages
kmruiz Oct 16, 2025
c0d9dee
chore: use ts private fields for now
kmruiz Oct 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const OPTIONS = {
boolean: [
"apiDeprecationErrors",
"apiStrict",
"disableEmbeddingsValidation",
"help",
"indexCheck",
"ipv6",
Expand Down Expand Up @@ -183,6 +184,7 @@ export interface UserConfig extends CliOptions {
maxBytesPerQuery: number;
atlasTemporaryDatabaseUserLifetimeMs: number;
voyageApiKey: string;
disableEmbeddingsValidation: boolean;
vectorSearchDimensions: number;
vectorSearchSimilarityFunction: "cosine" | "euclidean" | "dotProduct";
}
Expand Down Expand Up @@ -216,6 +218,7 @@ export const defaultUserConfig: UserConfig = {
maxBytesPerQuery: 16 * 1024 * 1024, // By default, we only return ~16 mb of data per query / aggregation
atlasTemporaryDatabaseUserLifetimeMs: 4 * 60 * 60 * 1000, // 4 hours
voyageApiKey: "",
disableEmbeddingsValidation: false,
vectorSearchDimensions: 1024,
vectorSearchSimilarityFunction: "euclidean",
};
Expand Down
87 changes: 77 additions & 10 deletions src/common/connectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,106 @@ export interface ConnectionSettings {
type ConnectionTag = "connected" | "connecting" | "disconnected" | "errored";
type OIDCConnectionAuthType = "oidc-auth-flow" | "oidc-device-flow";
export type ConnectionStringAuthType = "scram" | "ldap" | "kerberos" | OIDCConnectionAuthType | "x.509";
export type SearchAvailability = false | "not-available-yet" | "available";

export interface ConnectionState {
tag: ConnectionTag;
connectionStringAuthType?: ConnectionStringAuthType;
connectedAtlasCluster?: AtlasClusterConnectionInfo;
}

const MCP_TEST_DATABASE = "#mongodb-mcp";
const SEARCH_AVAILABILITY_CHECK_TIMEOUT_MS = 500;
export class ConnectionStateConnected implements ConnectionState {
public tag = "connected" as const;

constructor(
public serviceProvider: NodeDriverServiceProvider,
public connectionStringAuthType?: ConnectionStringAuthType,
public connectedAtlasCluster?: AtlasClusterConnectionInfo
) {}
) {
this.#isSearchAvailable = false;
}

#isSearchSupported?: boolean;
#isSearchAvailable: boolean;

private _isSearchSupported?: boolean;
public async getSearchAvailability(): Promise<SearchAvailability> {
if ((await this.isSearchSupported()) === true) {
if ((await this.isSearchAvailable()) === true) {
return "available";
}

return "not-available-yet";
}

public async isSearchSupported(): Promise<boolean> {
if (this._isSearchSupported === undefined) {
return false;
}

private async isSearchSupported(): Promise<boolean> {
if (this.#isSearchSupported === undefined) {
try {
const dummyDatabase = "test";
const dummyCollection = "test";
// If a cluster supports search indexes, the call below will succeed
// with a cursor otherwise will throw an Error
await this.serviceProvider.getSearchIndexes(dummyDatabase, dummyCollection);
this._isSearchSupported = true;
await this.serviceProvider.getSearchIndexes(MCP_TEST_DATABASE, "test");
this.#isSearchSupported = true;
} catch {
this._isSearchSupported = false;
this.#isSearchSupported = false;
}
}

return this.#isSearchSupported;
}

private async isSearchAvailable(): Promise<boolean> {
if (this.#isSearchAvailable === true) {
return true;
}

const timeoutPromise = new Promise<boolean>((_resolve, reject) =>
setTimeout(
() =>
reject(
new MongoDBError(
ErrorCodes.AtlasSearchNotAvailable,
"Atlas Search is supported in your environment but is not available yet. Retry again later."
)
),
SEARCH_AVAILABILITY_CHECK_TIMEOUT_MS
)
);

const checkPromise = new Promise<boolean>((resolve) => {
void this.doCheckSearchIndexIsAvailable(resolve);
});

return await Promise.race([checkPromise, timeoutPromise]);
}

private async doCheckSearchIndexIsAvailable(resolve: (result: boolean) => void): Promise<void> {
for (let i = 0; i < 100; i++) {
try {
try {
await this.serviceProvider.insertOne(MCP_TEST_DATABASE, "test", { search: "search is available" });
} catch (err) {
// if inserting one document fails, it means we are in readOnly mode. We can't verify reliably if
// Search is available, so assume it is.
void err;
resolve(true);
return;
}
await this.serviceProvider.createSearchIndexes(MCP_TEST_DATABASE, "test", [
{ definition: { mappings: { dynamic: true } } },
]);
await this.serviceProvider.dropDatabase(MCP_TEST_DATABASE);
resolve(true);
return;
} catch (err) {
void err;
}
}

return this._isSearchSupported;
resolve(false);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/common/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export enum ErrorCodes {
MisconfiguredConnectionString = 1_000_001,
ForbiddenCollscan = 1_000_002,
ForbiddenWriteOperation = 1_000_003,
AtlasSearchNotSupported = 1_000_004,
AtlasSearchNotAvailable = 1_000_005,
}

export class MongoDBError<ErrorCode extends ErrorCodes = ErrorCodes> extends Error {
Expand Down
176 changes: 176 additions & 0 deletions src/common/search/vectorSearchEmbeddings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import type { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver";
import { BSON, type Document } from "bson";
import type { UserConfig } from "../config.js";
import type { ConnectionManager } from "../connectionManager.js";

export type VectorFieldIndexDefinition = {
type: "vector";
path: string;
numDimensions: number;
quantization: "none" | "scalar" | "binary";
similarity: "euclidean" | "cosine" | "dotProduct";
};

export type EmbeddingNamespace = `${string}.${string}`;
export class VectorSearchEmbeddings {
constructor(
private readonly config: UserConfig,
private readonly connectionManager: ConnectionManager,
private readonly embeddings: Map<EmbeddingNamespace, VectorFieldIndexDefinition[]> = new Map()
) {
connectionManager.events.on("connection-close", () => {
this.embeddings.clear();
});
}

cleanupEmbeddingsForNamespace({ database, collection }: { database: string; collection: string }): void {
const embeddingDefKey: EmbeddingNamespace = `${database}.${collection}`;
this.embeddings.delete(embeddingDefKey);
}

async embeddingsForNamespace({
database,
collection,
}: {
database: string;
collection: string;
}): Promise<VectorFieldIndexDefinition[]> {
const provider = await this.assertAtlasSearchIsAvailable();
if (!provider) {
return [];
}

// We only need the embeddings for validation now, so don't query them if
// validation is disabled.
if (this.config.disableEmbeddingsValidation) {
return [];
}

const embeddingDefKey: EmbeddingNamespace = `${database}.${collection}`;
const definition = this.embeddings.get(embeddingDefKey);

if (!definition) {
const allSearchIndexes = await provider.getSearchIndexes(database, collection);
const vectorSearchIndexes = allSearchIndexes.filter((index) => index.type === "vectorSearch");
const vectorFields = vectorSearchIndexes
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
.flatMap<Document>((index) => (index.latestDefinition?.fields as Document) ?? [])
.filter((field) => this.isVectorFieldIndexDefinition(field));

this.embeddings.set(embeddingDefKey, vectorFields);
return vectorFields;
}

return definition;
}

async findFieldsWithWrongEmbeddings(
{
database,
collection,
}: {
database: string;
collection: string;
},
document: Document
): Promise<VectorFieldIndexDefinition[]> {
const provider = await this.assertAtlasSearchIsAvailable();
if (!provider) {
return [];
}

// While we can do our best effort to ensure that the embedding validation is correct
// based on https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-quantization/
// it's a complex process so we will also give the user the ability to disable this validation
if (this.config.disableEmbeddingsValidation) {
return [];
}

const embeddings = await this.embeddingsForNamespace({ database, collection });
return embeddings.filter((emb) => !this.documentPassesEmbeddingValidation(emb, document));
}

private async assertAtlasSearchIsAvailable(): Promise<NodeDriverServiceProvider | null> {
const connectionState = this.connectionManager.currentConnectionState;
if (connectionState.tag === "connected") {
if ((await connectionState.getSearchAvailability()) === "available") {
return connectionState.serviceProvider;
}
}

return null;
}

private isVectorFieldIndexDefinition(doc: Document): doc is VectorFieldIndexDefinition {
return doc["type"] === "vector";
}

private documentPassesEmbeddingValidation(definition: VectorFieldIndexDefinition, document: Document): boolean {
const fieldPath = definition.path.split(".");
let fieldRef: unknown = document;

for (const field of fieldPath) {
if (fieldRef && typeof fieldRef === "object" && field in fieldRef) {
fieldRef = (fieldRef as Record<string, unknown>)[field];
} else {
return true;
}
}

switch (definition.quantization) {
// Because quantization is not defined by the use
// we have to trust them in the format they use.
case "none":
return true;
case "scalar":
case "binary":
if (fieldRef instanceof BSON.Binary) {
try {
const elements = fieldRef.toFloat32Array();
return elements.length === definition.numDimensions;
} catch {
// bits are also supported
try {
const bits = fieldRef.toBits();
return bits.length === definition.numDimensions;
} catch {
return false;
}
}
} else {
if (!Array.isArray(fieldRef)) {
return false;
}

if (fieldRef.length !== definition.numDimensions) {
return false;
}

if (!fieldRef.every((e) => this.isANumber(e))) {
return false;
}
}

break;
}

return true;
}

private isANumber(value: unknown): boolean {
if (typeof value === "number") {
return true;
}

if (
value instanceof BSON.Int32 ||
value instanceof BSON.Decimal128 ||
value instanceof BSON.Double ||
value instanceof BSON.Long
) {
return true;
}

return false;
}
}
Loading
Loading