@@ -2,6 +2,44 @@ import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
22import { DbOperationArgs , MongoDBToolBase } from "../mongodbTool.js" ;
33import { ToolArgs , OperationType } from "../../tool.js" ;
44
5+ export function collectionIndexesResponse ( {
6+ database,
7+ collection,
8+ indexes = [ ] ,
9+ namespaceNotFound,
10+ } : {
11+ database : string ;
12+ collection : string ;
13+ indexes ?: { name : string ; key : string } [ ] ;
14+ namespaceNotFound ?: boolean ;
15+ } ) : CallToolResult {
16+ if ( namespaceNotFound ) {
17+ return {
18+ content : [
19+ {
20+ text : `The indexes for "${ database } .${ collection } " cannot be determined because the collection does not exist.` ,
21+ type : "text" ,
22+ } ,
23+ ] ,
24+ } ;
25+ }
26+
27+ return {
28+ content : [
29+ {
30+ text : `Found ${ indexes . length } indexes in the collection "${ collection } ":` ,
31+ type : "text" ,
32+ } ,
33+ ...( indexes . map ( ( indexDefinition ) => {
34+ return {
35+ text : `Name "${ indexDefinition . name } ", definition: ${ JSON . stringify ( indexDefinition . key ) } ` ,
36+ type : "text" ,
37+ } ;
38+ } ) as { text : string ; type : "text" } [ ] ) ,
39+ ] ,
40+ } ;
41+ }
42+
543export class CollectionIndexesTool extends MongoDBToolBase {
644 protected name = "collection-indexes" ;
745 protected description = "Describe the indexes for a collection" ;
@@ -11,36 +49,26 @@ export class CollectionIndexesTool extends MongoDBToolBase {
1149 protected async execute ( { database, collection } : ToolArgs < typeof DbOperationArgs > ) : Promise < CallToolResult > {
1250 const provider = await this . ensureConnected ( ) ;
1351 const indexes = await provider . getIndexes ( database , collection ) ;
14-
15- return {
16- content : [
17- {
18- text : `Found ${ indexes . length } indexes in the collection "${ collection } ":` ,
19- type : "text" ,
20- } ,
21- ...( indexes . map ( ( indexDefinition ) => {
22- return {
23- text : `Name "${ indexDefinition . name } ", definition: ${ JSON . stringify ( indexDefinition . key ) } ` ,
24- type : "text" ,
25- } ;
26- } ) as { text : string ; type : "text" } [ ] ) ,
27- ] ,
28- } ;
52+ return collectionIndexesResponse ( {
53+ database,
54+ collection,
55+ indexes : indexes . map ( ( index ) => ( {
56+ name : `${ index . name } ` ,
57+ key : JSON . stringify ( index . key ) ,
58+ } ) ) ,
59+ } ) ;
2960 }
3061
3162 protected handleError (
3263 error : unknown ,
3364 args : ToolArgs < typeof this . argsShape >
3465 ) : Promise < CallToolResult > | CallToolResult {
3566 if ( error instanceof Error && "codeName" in error && error . codeName === "NamespaceNotFound" ) {
36- return {
37- content : [
38- {
39- text : `The indexes for "${ args . database } .${ args . collection } " cannot be determined because the collection does not exist.` ,
40- type : "text" ,
41- } ,
42- ] ,
43- } ;
67+ return collectionIndexesResponse ( {
68+ database : args . database ,
69+ collection : args . collection ,
70+ namespaceNotFound : true ,
71+ } ) ;
4472 }
4573
4674 return super . handleError ( error , args ) ;
0 commit comments