Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auto-convert ObjectId strings in find tool filters (#6) #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
40 changes: 39 additions & 1 deletion src/tools/documents/find.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { db } from "../../mongodb/client.js";
import { BaseTool, ToolParams } from "../base/tool.js";
import { ObjectId } from "mongodb";

export interface FindParams extends ToolParams {
collection: string;
Expand Down Expand Up @@ -39,12 +40,49 @@ export class FindTool extends BaseTool<FindParams> {
required: ["collection"],
};

/**
* Recursively converts filter fields ending with "Id" or "_id" that match the ObjectId format
* (24-character hexadecimal strings) into MongoDB ObjectId instances.
*
* @param filter - The original MongoDB query filter object.
* @returns A new filter object with applicable string fields converted to ObjectId instances.
*/
private convertFilterFields(filter: Record<string, unknown>): Record<string, unknown> {
const convertedFilter = { ...filter };

function convertIfObjectId(obj: Record<string, unknown>) {
for (const [key, value] of Object.entries(obj)) {
if (
(key.endsWith("Id") || key.endsWith("_id")) &&
typeof value === "string" &&
/^[0-9a-fA-F]{24}$/.test(value)
) {
try {
obj[key] = new ObjectId(value);
} catch (err) {
console.error(`Invalid ObjectId for field "${key}": ${value}`);
}
} else if (value && typeof value === "object") {
convertIfObjectId(value as Record<string, unknown>);
}
}
}

convertIfObjectId(convertedFilter);
return convertedFilter;
}

async execute(params: FindParams) {
try {
const collection = this.validateCollection(params.collection);

// Convert applicable fields in the filter to ObjectId
let filter = params.filter ? { ...params.filter } : {};
filter = this.convertFilterFields(filter);

const results = await db
.collection(collection)
.find(params.filter || {})
.find(filter)
.project(params.projection || {})
.limit(Math.min(params.limit || 10, 1000))
.toArray();
Expand Down