@@ -2,44 +2,6 @@ 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-
435export class CollectionIndexesTool extends MongoDBToolBase {
446 protected name = "collection-indexes" ;
457 protected description = "Describe the indexes for a collection" ;
@@ -49,26 +11,36 @@ export class CollectionIndexesTool extends MongoDBToolBase {
4911 protected async execute ( { database, collection } : ToolArgs < typeof DbOperationArgs > ) : Promise < CallToolResult > {
5012 const provider = await this . ensureConnected ( ) ;
5113 const indexes = await provider . getIndexes ( database , collection ) ;
52- return collectionIndexesResponse ( {
53- database,
54- collection,
55- indexes : indexes . map ( ( index ) => ( {
56- name : `${ index . name } ` ,
57- key : JSON . stringify ( index . key ) ,
58- } ) ) ,
59- } ) ;
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+ } ;
6029 }
6130
6231 protected handleError (
6332 error : unknown ,
6433 args : ToolArgs < typeof this . argsShape >
6534 ) : Promise < CallToolResult > | CallToolResult {
6635 if ( error instanceof Error && "codeName" in error && error . codeName === "NamespaceNotFound" ) {
67- return collectionIndexesResponse ( {
68- database : args . database ,
69- collection : args . collection ,
70- namespaceNotFound : true ,
71- } ) ;
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+ } ;
7244 }
7345
7446 return super . handleError ( error , args ) ;
0 commit comments