import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
import { ToolArgs, OperationType } from "../../tool.js";
import { EJSON } from "bson";
export class CollectionIndexesTool extends MongoDBToolBase {
protected name = "collection-indexes";
protected description = "Describe the indexes for a collection";
protected argsShape = DbOperationArgs;
protected operationType: OperationType = "read";
protected async execute({ database, collection }: ToolArgs<typeof DbOperationArgs>): Promise<CallToolResult> {
const provider = await this.ensureConnected();
const resolvedDatabase = this.resolveDatabase(database);
const indexes = await provider.getIndexes(resolvedDatabase, collection);
return {
content: [
{
text: EJSON.stringify(indexes),
type: "text",
},
],
};
}
protected handleError(
error: unknown,
args: ToolArgs<typeof this.argsShape>
): Promise<CallToolResult> | CallToolResult {
if (error instanceof Error && "codeName" in error && error.codeName === "NamespaceNotFound") {
return {
content: [
{
text: JSON.stringify({ error: "Collection not found" }, undefined, 2),
type: "text",
},
],
};
}
return super.handleError(error, args);
}
}