1
1
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js" ;
2
2
import { DbOperationArgs , MongoDBToolBase } from "../mongodbTool.js" ;
3
3
import { ToolArgs , OperationType } from "../../tool.js" ;
4
- import { getSimplifiedSchema } from "mongodb-schema" ;
4
+ import { getSimplifiedSchema , SimplifiedSchema } from "mongodb-schema" ;
5
+
6
+ export function collectionSchemaResponse (
7
+ database : string ,
8
+ collection : string ,
9
+ schema : SimplifiedSchema
10
+ ) : CallToolResult {
11
+ const fieldsCount = Object . entries ( schema ) . length ;
12
+ if ( fieldsCount === 0 ) {
13
+ return {
14
+ content : [
15
+ {
16
+ text : `Could not deduce the schema for "${ database } .${ collection } ". This may be because it doesn't exist or is empty.` ,
17
+ type : "text" ,
18
+ } ,
19
+ ] ,
20
+ } ;
21
+ }
22
+
23
+ return {
24
+ content : [
25
+ {
26
+ text : `Found ${ fieldsCount } fields in the schema for "${ database } .${ collection } "` ,
27
+ type : "text" ,
28
+ } ,
29
+ {
30
+ text : JSON . stringify ( schema ) ,
31
+ type : "text" ,
32
+ } ,
33
+ ] ,
34
+ } ;
35
+ }
5
36
6
37
export class CollectionSchemaTool extends MongoDBToolBase {
7
38
protected name = "collection-schema" ;
@@ -14,30 +45,6 @@ export class CollectionSchemaTool extends MongoDBToolBase {
14
45
const provider = await this . ensureConnected ( ) ;
15
46
const documents = await provider . find ( database , collection , { } , { limit : 5 } ) . toArray ( ) ;
16
47
const schema = await getSimplifiedSchema ( documents ) ;
17
-
18
- const fieldsCount = Object . entries ( schema ) . length ;
19
- if ( fieldsCount === 0 ) {
20
- return {
21
- content : [
22
- {
23
- text : `Could not deduce the schema for "${ database } .${ collection } ". This may be because it doesn't exist or is empty.` ,
24
- type : "text" ,
25
- } ,
26
- ] ,
27
- } ;
28
- }
29
-
30
- return {
31
- content : [
32
- {
33
- text : `Found ${ fieldsCount } fields in the schema for "${ database } .${ collection } "` ,
34
- type : "text" ,
35
- } ,
36
- {
37
- text : JSON . stringify ( schema ) ,
38
- type : "text" ,
39
- } ,
40
- ] ,
41
- } ;
48
+ return collectionSchemaResponse ( database , collection , schema ) ;
42
49
}
43
50
}
0 commit comments