11import z from "zod" ;
22import { ObjectId } from "bson" ;
3+ import { AggregationCursor , FindCursor } from "mongodb" ;
34import { CallToolResult } from "@modelcontextprotocol/sdk/types.js" ;
45import { OperationType , ToolArgs } from "../../tool.js" ;
56import { DbOperationArgs , MongoDBToolBase } from "../mongodbTool.js" ;
67import { FindArgs } from "./find.js" ;
78import { jsonExportFormat } from "../../../common/exportsManager.js" ;
9+ import { AggregateArgs } from "./aggregate.js" ;
810
911export class ExportTool extends MongoDBToolBase {
1012 public name = "export" ;
1113 protected description = "Export a collection data or query results in the specified EJSON format." ;
1214 protected argsShape = {
13- exportTitle : z . string ( ) . describe ( "A short description to uniquely identify the export." ) ,
1415 ...DbOperationArgs ,
15- ...FindArgs ,
16- limit : z . number ( ) . optional ( ) . describe ( "The maximum number of documents to return" ) ,
16+ exportTitle : z . string ( ) . describe ( "A short description to uniquely identify the export." ) ,
17+ exportTarget : z
18+ . array (
19+ z . discriminatedUnion ( "name" , [
20+ z . object ( {
21+ name : z
22+ . literal ( "find" )
23+ . describe ( "The literal name 'find' to represent a find cursor as target." ) ,
24+ arguments : z
25+ . object ( {
26+ ...FindArgs ,
27+ limit : FindArgs . limit . removeDefault ( ) ,
28+ } )
29+ . describe ( "The arguments for 'find' operation." ) ,
30+ } ) ,
31+ z . object ( {
32+ name : z
33+ . literal ( "aggregate" )
34+ . describe ( "The literal name 'aggregate' to represent an aggregation cursor as target." ) ,
35+ arguments : z . object ( AggregateArgs ) . describe ( "The arguments for 'aggregate' operation." ) ,
36+ } ) ,
37+ ] )
38+ )
39+ . describe ( "The export target along with its arguments." ) ,
1740 jsonExportFormat : jsonExportFormat
1841 . default ( "relaxed" )
1942 . describe (
@@ -30,24 +53,38 @@ export class ExportTool extends MongoDBToolBase {
3053 database,
3154 collection,
3255 jsonExportFormat,
33- filter,
34- projection,
35- sort,
36- limit,
3756 exportTitle,
57+ exportTarget : target ,
3858 } : ToolArgs < typeof this . argsShape > ) : Promise < CallToolResult > {
3959 const provider = await this . ensureConnected ( ) ;
40- const findCursor = provider . find ( database , collection , filter ?? { } , {
41- projection,
42- sort,
43- limit,
44- promoteValues : false ,
45- bsonRegExp : true ,
46- } ) ;
60+ const exportTarget = target [ 0 ] ;
61+ if ( ! exportTarget ) {
62+ throw new Error ( "Export target not provided. Expected one of the following: `aggregate`, `find`" ) ;
63+ }
64+
65+ let cursor : FindCursor | AggregationCursor ;
66+ if ( exportTarget . name === "find" ) {
67+ const { filter, projection, sort, limit } = exportTarget . arguments ;
68+ cursor = provider . find ( database , collection , filter ?? { } , {
69+ projection,
70+ sort,
71+ limit,
72+ promoteValues : false ,
73+ bsonRegExp : true ,
74+ } ) ;
75+ } else {
76+ const { pipeline } = exportTarget . arguments ;
77+ cursor = provider . aggregate ( database , collection , pipeline , {
78+ promoteValues : false ,
79+ bsonRegExp : true ,
80+ allowDiskUse : true ,
81+ } ) ;
82+ }
83+
4784 const exportName = `${ database } .${ collection } .${ new ObjectId ( ) . toString ( ) } .json` ;
4885
4986 const { exportURI, exportPath } = await this . session . exportsManager . createJSONExport ( {
50- input : findCursor ,
87+ input : cursor ,
5188 exportName,
5289 exportTitle :
5390 exportTitle ||
0 commit comments