11import { CallToolResult } from "@modelcontextprotocol/sdk/types.js" ;
22import { DbOperationArgs , MongoDBToolBase } from "../mongodbTool.js" ;
33import { 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+ }
536
637export class CollectionSchemaTool extends MongoDBToolBase {
738 protected name = "collection-schema" ;
@@ -14,30 +45,6 @@ export class CollectionSchemaTool extends MongoDBToolBase {
1445 const provider = await this . ensureConnected ( ) ;
1546 const documents = await provider . find ( database , collection , { } , { limit : 5 } ) . toArray ( ) ;
1647 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 ) ;
4249 }
4350}
0 commit comments