• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mongodb-js / mongodb-mcp-server / 16876636630

11 Aug 2025 09:51AM UTC coverage: 81.999% (+0.6%) from 81.362%
16876636630

Pull #424

github

web-flow
Merge 602f1c199 into 7572ec5d6
Pull Request #424: feat: adds an export tool and exported-data resource MCP-16

784 of 1003 branches covered (78.17%)

Branch coverage included in aggregate %.

592 of 659 new or added lines in 13 files covered. (89.83%)

18 existing lines in 3 files now uncovered.

4072 of 4919 relevant lines covered (82.78%)

66.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.43
/src/tools/mongodb/read/export.ts
1
import z from "zod";
2✔
2
import { ObjectId } from "bson";
2✔
3
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
4
import { OperationType, ToolArgs } from "../../tool.js";
5
import { DbOperationArgs, MongoDBToolBase } from "../mongodbTool.js";
2✔
6
import { FindArgs } from "./find.js";
2✔
7
import { jsonExportFormat } from "../../../common/exportsManager.js";
2✔
8

9
export class ExportTool extends MongoDBToolBase {
2✔
10
    public name = "export";
74✔
11
    protected description = "Export a collection data or query results in the specified EJSON format.";
74✔
12
    protected argsShape = {
74✔
13
        exportTitle: z.string().describe("A short description to uniquely identify the export."),
74✔
14
        ...DbOperationArgs,
74✔
15
        ...FindArgs,
74✔
16
        limit: z.number().optional().describe("The maximum number of documents to return"),
74✔
17
        jsonExportFormat: jsonExportFormat
74✔
18
            .default("relaxed")
74✔
19
            .describe(
74✔
20
                [
74✔
21
                    "The format to be used when exporting collection data as EJSON with default being relaxed.",
74✔
22
                    "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.",
74✔
23
                    "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.",
74✔
24
                ].join("\n")
74✔
25
            ),
74✔
26
    };
74✔
27
    public operationType: OperationType = "read";
74✔
28

29
    protected async execute({
2✔
30
        database,
38✔
31
        collection,
38✔
32
        jsonExportFormat,
38✔
33
        filter,
38✔
34
        projection,
38✔
35
        sort,
38✔
36
        limit,
38✔
37
        exportTitle,
38✔
38
    }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
38✔
39
        const provider = await this.ensureConnected();
38✔
40
        const findCursor = provider.find(database, collection, filter ?? {}, {
38✔
41
            projection,
38✔
42
            sort,
38✔
43
            limit,
38✔
44
            promoteValues: false,
38✔
45
            bsonRegExp: true,
38✔
46
        });
38✔
47
        const exportName = `${database}.${collection}.${new ObjectId().toString()}.json`;
38✔
48

49
        const { exportURI, exportPath } = await this.session.exportsManager.createJSONExport({
38✔
50
            input: findCursor,
38✔
51
            exportName,
38✔
52
            exportTitle:
38✔
53
                exportTitle ||
38!
NEW
54
                `Export for namespace ${database}.${collection} requested on ${new Date().toLocaleString()}`,
×
55
            jsonExportFormat,
38✔
56
        });
38✔
57
        const toolCallContent: CallToolResult["content"] = [
38✔
58
            // Not all the clients as of this commit understands how to
59
            // parse a resource_link so we provide a text result for them to
60
            // understand what to do with the result.
61
            {
38✔
62
                type: "text",
38✔
63
                text: `Data for namespace ${database}.${collection} is being exported and will be made available under resource URI - "${exportURI}".`,
38✔
64
            },
38✔
65
            {
38✔
66
                type: "resource_link",
38✔
67
                name: exportName,
38✔
68
                uri: exportURI,
38✔
69
                description: "Resource URI for fetching exported data once it is ready.",
38✔
70
                mimeType: "application/json",
38✔
71
            },
38✔
72
        ];
38✔
73

74
        // This special case is to make it easier to work with exported data for
75
        // clients that still cannot reference resources (Cursor).
76
        // More information here: https://jira.mongodb.org/browse/MCP-104
77
        if (this.isServerRunningLocally()) {
38✔
78
            toolCallContent.push({
38✔
79
                type: "text",
38✔
80
                text: `Optionally, when the export is finished, the exported data can also be accessed under path - "${exportPath}"`,
38✔
81
            });
38✔
82
        }
38✔
83

84
        return {
38✔
85
            content: toolCallContent,
38✔
86
        };
38✔
87
    }
38✔
88

89
    private isServerRunningLocally(): boolean {
2✔
90
        return this.config.transport === "stdio" || ["127.0.0.1", "localhost"].includes(this.config.httpHost);
38!
91
    }
38✔
92
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc