-
Notifications
You must be signed in to change notification settings - Fork 138
chore: merge list-search-indexes into collection-indexes #654
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; | ||
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js"; | ||
import type { ToolArgs, OperationType } from "../../tool.js"; | ||
import { formatUntrustedData } from "../../tool.js"; | ||
import { FeatureFlags, formatUntrustedData } from "../../tool.js"; | ||
|
||
type SearchIndexStatus = { | ||
name: string; | ||
type: string; | ||
status: string; | ||
queryable: boolean; | ||
latestDefinition: Document; | ||
}; | ||
|
||
type IndexStatus = { | ||
name: string; | ||
key: Document; | ||
}; | ||
|
||
export class CollectionIndexesTool extends MongoDBToolBase { | ||
public name = "collection-indexes"; | ||
|
@@ -12,16 +25,30 @@ export class CollectionIndexesTool extends MongoDBToolBase { | |
protected async execute({ database, collection }: ToolArgs<typeof DbOperationArgs>): Promise<CallToolResult> { | ||
const provider = await this.ensureConnected(); | ||
const indexes = await provider.getIndexes(database, collection); | ||
const indexDefinitions: IndexStatus[] = indexes.map((index) => ({ | ||
name: index.name as string, | ||
key: index.key as Document, | ||
})); | ||
|
||
const searchIndexDefinitions: SearchIndexStatus[] = []; | ||
if (this.isFeatureFlagEnabled(FeatureFlags.VectorSearch) && (await this.session.isSearchSupported())) { | ||
const searchIndexes = await provider.getSearchIndexes(database, collection); | ||
searchIndexDefinitions.push(...this.extractSearchIndexDetails(searchIndexes)); | ||
} | ||
|
||
return { | ||
content: formatUntrustedData( | ||
`Found ${indexes.length} indexes in the collection "${collection}":`, | ||
indexes.length > 0 | ||
? indexes | ||
.map((index) => `Name: "${index.name}", definition: ${JSON.stringify(index.key)}`) | ||
.join("\n") | ||
: undefined | ||
), | ||
content: [ | ||
...formatUntrustedData( | ||
`Found ${indexDefinitions.length} indexes in the collection "${collection}":`, | ||
indexDefinitions.length > 0 ? JSON.stringify(indexDefinitions, null, 2) : undefined | ||
), | ||
...(searchIndexDefinitions.length > 0 | ||
? formatUntrustedData( | ||
`Found ${searchIndexDefinitions.length} search and vector search indexes in the collection "${collection}":`, | ||
JSON.stringify(searchIndexDefinitions, null, 2) | ||
) | ||
: []), | ||
Comment on lines
+41
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous list-search-indexes tool serialized index data with EJSON to preserve extended BSON types; switching to JSON.stringify may lose fidelity (e.g., ObjectId, Date) in future index definitions. Recommend importing EJSON from bson and using Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
], | ||
}; | ||
} | ||
|
||
|
@@ -43,4 +70,20 @@ export class CollectionIndexesTool extends MongoDBToolBase { | |
|
||
return super.handleError(error, args); | ||
} | ||
|
||
/** | ||
* Atlas Search index status contains a lot of information that is not relevant for the agent at this stage. | ||
* Like for example, the status on each of the dedicated nodes. We only care about the main status, if it's | ||
* queryable and the index name. We are also picking the index definition as it can be used by the agent to | ||
* understand which fields are available for searching. | ||
**/ | ||
protected extractSearchIndexDetails(indexes: Record<string, unknown>[]): SearchIndexStatus[] { | ||
return indexes.map((index) => ({ | ||
name: (index["name"] ?? "default") as string, | ||
type: (index["type"] ?? "UNKNOWN") as string, | ||
status: (index["status"] ?? "UNKNOWN") as string, | ||
queryable: (index["queryable"] ?? false) as boolean, | ||
latestDefinition: index["latestDefinition"] as Document, | ||
})); | ||
} | ||
} |
This file was deleted.
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.