Skip to main content
Glama
mongodb-js

MongoDB MCP Server

Official
by mongodb-js

export

Read-only

Export MongoDB query or aggregation results to EJSON format for data transfer or analysis, handling large datasets beyond standard response limits.

Instructions

Export a query or aggregation results in the specified EJSON format.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseYesDatabase name
collectionYesCollection name
exportTitleYesA short description to uniquely identify the export.
exportTargetYesThe export target along with its arguments.
jsonExportFormatNoThe format to be used when exporting collection data as EJSON with default being relaxed. relaxed: A string format that emphasizes readability and interoperability at the expense of type preservation. That is, conversion from relaxed format to BSON can lose type information. canonical: A string format that emphasizes type preservation at the expense of readability and interoperability. That is, conversion from canonical to BSON will generally preserve type information except in certain specific cases.relaxed

Implementation Reference

  • The ExportTool class implements the "export" tool. It defines the tool name, description, input schema (argsShape), and the execute method that creates a MongoDB cursor (find or aggregate) and uses exportsManager to export results as JSON, returning resource links.
    export class ExportTool extends MongoDBToolBase {
        public name = "export";
        protected description = "Export a query or aggregation results in the specified EJSON format.";
        protected argsShape = {
            ...DbOperationArgs,
            exportTitle: z.string().describe("A short description to uniquely identify the export."),
            exportTarget: z
                .array(
                    z.discriminatedUnion("name", [
                        z.object({
                            name: z
                                .literal("find")
                                .describe("The literal name 'find' to represent a find cursor as target."),
                            arguments: z
                                .object({
                                    ...FindArgs,
                                    limit: FindArgs.limit.removeDefault(),
                                })
                                .describe("The arguments for 'find' operation."),
                        }),
                        z.object({
                            name: z
                                .literal("aggregate")
                                .describe("The literal name 'aggregate' to represent an aggregation cursor as target."),
                            arguments: z
                                .object(getAggregateArgs(this.isFeatureEnabled("search")))
                                .describe("The arguments for 'aggregate' operation."),
                        }),
                    ])
                )
                .describe("The export target along with its arguments."),
            jsonExportFormat: jsonExportFormat
                .default("relaxed")
                .describe(
                    [
                        "The format to be used when exporting collection data as EJSON with default being relaxed.",
                        "relaxed: A string format that emphasizes readability and interoperability at the expense of type preservation. That is, conversion from relaxed format to BSON can lose type information.",
                        "canonical: A string format that emphasizes type preservation at the expense of readability and interoperability. That is, conversion from canonical to BSON will generally preserve type information except in certain specific cases.",
                    ].join("\n")
                ),
        };
        static operationType: OperationType = "read";
    
        protected async execute({
            database,
            collection,
            jsonExportFormat,
            exportTitle,
            exportTarget: target,
        }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
            const provider = await this.ensureConnected();
            const exportTarget = target[0];
            if (!exportTarget) {
                throw new Error("Export target not provided. Expected one of the following: `aggregate`, `find`");
            }
    
            let cursor: FindCursor | AggregationCursor;
            if (exportTarget.name === "find") {
                const { filter, projection, sort, limit } = exportTarget.arguments;
                cursor = provider.find(database, collection, filter ?? {}, {
                    projection,
                    sort,
                    limit,
                    promoteValues: false,
                    bsonRegExp: true,
                });
            } else {
                const { pipeline } = exportTarget.arguments;
                cursor = provider.aggregate(database, collection, pipeline, {
                    promoteValues: false,
                    bsonRegExp: true,
                    allowDiskUse: true,
                });
            }
    
            const exportName = `${new ObjectId().toString()}.json`;
    
            const { exportURI, exportPath } = await this.session.exportsManager.createJSONExport({
                input: cursor,
                exportName,
                exportTitle:
                    exportTitle ||
                    `Export for namespace ${database}.${collection} requested on ${new Date().toLocaleString()}`,
                jsonExportFormat,
            });
            const toolCallContent: CallToolResult["content"] = [
                // Not all the clients as of this commit understands how to
                // parse a resource_link so we provide a text result for them to
                // understand what to do with the result.
                {
                    type: "text",
                    text: `Data for namespace ${database}.${collection} is being exported and will be made available under resource URI - "${exportURI}".`,
                },
                {
                    type: "resource_link",
                    name: exportName,
                    uri: exportURI,
                    description: "Resource URI for fetching exported data once it is ready.",
                    mimeType: "application/json",
                },
            ];
    
            // This special case is to make it easier to work with exported data for
            // clients that still cannot reference resources (Cursor).
            // More information here: https://jira.mongodb.org/browse/MCP-104
            if (this.isServerRunningLocally()) {
                toolCallContent.push({
                    type: "text",
                    text: `Optionally, when the export is finished, the exported data can also be accessed under path - "${exportPath}"`,
                });
            }
    
            return {
                content: toolCallContent,
            };
        }
    
        private isServerRunningLocally(): boolean {
            return this.config.transport === "stdio" || ["127.0.0.1", "localhost"].includes(this.config.httpHost);
        }
    }
  • Input schema definition (argsShape) for the export tool, including database/collection, exportTitle, exportTarget (find or aggregate args), and jsonExportFormat.
    protected argsShape = {
        ...DbOperationArgs,
        exportTitle: z.string().describe("A short description to uniquely identify the export."),
        exportTarget: z
            .array(
                z.discriminatedUnion("name", [
                    z.object({
                        name: z
                            .literal("find")
                            .describe("The literal name 'find' to represent a find cursor as target."),
                        arguments: z
                            .object({
                                ...FindArgs,
                                limit: FindArgs.limit.removeDefault(),
                            })
                            .describe("The arguments for 'find' operation."),
                    }),
                    z.object({
                        name: z
                            .literal("aggregate")
                            .describe("The literal name 'aggregate' to represent an aggregation cursor as target."),
                        arguments: z
                            .object(getAggregateArgs(this.isFeatureEnabled("search")))
                            .describe("The arguments for 'aggregate' operation."),
                    }),
                ])
            )
            .describe("The export target along with its arguments."),
        jsonExportFormat: jsonExportFormat
            .default("relaxed")
            .describe(
                [
                    "The format to be used when exporting collection data as EJSON with default being relaxed.",
                    "relaxed: A string format that emphasizes readability and interoperability at the expense of type preservation. That is, conversion from relaxed format to BSON can lose type information.",
                    "canonical: A string format that emphasizes type preservation at the expense of readability and interoperability. That is, conversion from canonical to BSON will generally preserve type information except in certain specific cases.",
                ].join("\n")
            ),
    };
  • Re-export of the ExportTool class for inclusion in the MongoDB tools module.
    export { ExportTool } from "./read/export.js";
  • Import of MongoDB tools (including export) and inclusion in AllTools array, which is used by the server to register all tools dynamically via ToolBase.register().
    import * as MongoDbTools from "./mongodb/tools.js";
    import type { ToolClass } from "./tool.js";
    
    // Export the collection of tools for easier reference
    export const AllTools: ToolClass[] = Object.values({
        ...MongoDbTools,
        ...AtlasTools,
        ...AtlasLocalTools,
    });
  • ExportsManager.createJSONExport method called by the export tool handler to asynchronously generate the JSON export file from the MongoDB cursor and provide URI/path for access.
    public async createJSONExport({
        input,
        exportName,
        exportTitle,
        jsonExportFormat,
    }: {
        input: FindCursor | AggregationCursor;
        exportName: string;
        exportTitle: string;
        jsonExportFormat: JSONExportFormat;
    }): Promise<AvailableExport> {
        try {
            this.assertIsNotShuttingDown();
            const exportNameWithExtension = decodeAndNormalize(ensureExtension(exportName, "json"));
            if (this.storedExports[exportNameWithExtension]) {
                return Promise.reject(
                    new Error("Export with same name is either already available or being generated.")
                );
            }
            const exportURI = `exported-data://${encodeURIComponent(exportNameWithExtension)}`;
            const exportFilePath = path.join(this.exportsDirectoryPath, exportNameWithExtension);
            const inProgressExport: InProgressExport = (this.storedExports[exportNameWithExtension] = {
                exportName: exportNameWithExtension,
                exportTitle,
                exportPath: exportFilePath,
                exportURI: exportURI,
                exportStatus: "in-progress",
            });
    
            void this.startExport({ input, jsonExportFormat, inProgressExport });
            return Promise.resolve(inProgressExport);
        } catch (error) {
            this.logger.error({
                id: LogId.exportCreationError,
                context: "Error when registering JSON export request",
                message: error instanceof Error ? error.message : String(error),
            });
            throw error;
        }
    }
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations already declare readOnlyHint=true and destructiveHint=false, so the agent knows this is a safe read operation. The description adds that it exports results in EJSON format, which provides useful context about output behavior. However, it doesn't mention potential limitations like export size constraints, performance implications, or authentication requirements beyond what annotations cover.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that clearly states the tool's purpose without unnecessary words. It's front-loaded with the core functionality and includes essential details about format. Every element serves a purpose with zero waste.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a read-only export tool with comprehensive schema documentation (100% coverage) and clear annotations, the description provides adequate context. It specifies the output format (EJSON) which is crucial for understanding the tool's behavior. However, without an output schema, some additional detail about the export result structure or potential limitations would be helpful for full completeness.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already thoroughly documents all 5 parameters. The description mentions 'query or aggregation results' which aligns with the exportTarget parameter's structure, but adds no additional semantic context beyond what's in the schema. With complete schema documentation, the baseline score of 3 is appropriate.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Export') and the target ('a query or aggregation results') with the output format specified ('EJSON format'). It distinguishes from sibling tools like 'find' and 'aggregate' by focusing on export functionality rather than direct query execution. However, it doesn't explicitly contrast with all potential export-related alternatives that might exist.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage for exporting query/aggregation results in EJSON format, but doesn't explicitly state when to use this tool versus alternatives like direct 'find' or 'aggregate' tools. The schema's 'responseBytesLimit' notes suggest using 'export' for entire results, but this guidance isn't in the description itself. No explicit when-not-to-use or prerequisite information is provided.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mongodb-js/mongodb-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server