@@ -2,6 +2,44 @@ import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
2
2
import { DbOperationArgs , MongoDBToolBase } from "../mongodbTool.js" ;
3
3
import { ToolArgs , OperationType } from "../../tool.js" ;
4
4
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
+
5
43
export class CollectionIndexesTool extends MongoDBToolBase {
6
44
protected name = "collection-indexes" ;
7
45
protected description = "Describe the indexes for a collection" ;
@@ -11,36 +49,26 @@ export class CollectionIndexesTool extends MongoDBToolBase {
11
49
protected async execute ( { database, collection } : ToolArgs < typeof DbOperationArgs > ) : Promise < CallToolResult > {
12
50
const provider = await this . ensureConnected ( ) ;
13
51
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
+ } ) ;
29
60
}
30
61
31
62
protected handleError (
32
63
error : unknown ,
33
64
args : ToolArgs < typeof this . argsShape >
34
65
) : Promise < CallToolResult > | CallToolResult {
35
66
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
+ } ) ;
44
72
}
45
73
46
74
return super . handleError ( error , args ) ;
0 commit comments