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

mongodb-js / mongodb-mcp-server / 14645766670

24 Apr 2025 03:40PM UTC coverage: 85.585% (+4.0%) from 81.603%
14645766670

Pull #115

github

blva
update
Pull Request #115: chore: add server events

131 of 202 branches covered (64.85%)

Branch coverage included in aggregate %.

13 of 17 new or added lines in 1 file covered. (76.47%)

2 existing lines in 1 file now uncovered.

718 of 790 relevant lines covered (90.89%)

44.14 hits per line

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

83.33
/src/server.ts
1
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
import { Session } from "./session.js";
3
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
4
import { AtlasTools } from "./tools/atlas/tools.js";
30✔
5
import { MongoDbTools } from "./tools/mongodb/tools.js";
30✔
6
import logger, { initializeLogger } from "./logger.js";
30✔
7
import { mongoLogId } from "mongodb-log-writer";
30✔
8
import { ObjectId } from "mongodb";
30✔
9
import { Telemetry } from "./telemetry/telemetry.js";
30✔
10
import { UserConfig } from "./config.js";
11
import { type ServerEvent } from "./telemetry/types.js";
12
import { type ServerCommand } from "./telemetry/types.js";
13

14
export interface ServerOptions {
15
    session: Session;
16
    userConfig: UserConfig;
17
    mcpServer: McpServer;
18
}
19

20
export class Server {
30✔
21
    public readonly session: Session;
22
    private readonly mcpServer: McpServer;
23
    private readonly telemetry: Telemetry;
24
    private readonly userConfig: UserConfig;
25
    private readonly startTime: number;
26

27
    constructor({ session, mcpServer, userConfig }: ServerOptions) {
28
        this.startTime = Date.now();
26✔
29
        this.session = session;
26✔
30
        this.telemetry = new Telemetry(session);
26✔
31
        this.mcpServer = mcpServer;
26✔
32
        this.userConfig = userConfig;
26✔
33
    }
34

35
    async connect(transport: Transport) {
36
        this.mcpServer.server.registerCapabilities({ logging: {} });
26✔
37
        this.registerTools();
26✔
38
        this.registerResources();
26✔
39

40
        await initializeLogger(this.mcpServer, this.userConfig.logPath);
26✔
41

42
        await this.mcpServer.connect(transport);
26✔
43

44
        this.mcpServer.server.oninitialized = () => {
26✔
45
            this.session.setAgentRunner(this.mcpServer.server.getClientVersion());
26✔
46
            this.session.sessionId = new ObjectId().toString();
26✔
47

48
            logger.info(
26✔
49
                mongoLogId(1_000_004),
50
                "server",
51
                `Server started with transport ${transport.constructor.name} and agent runner ${this.session.agentRunner?.name}`
52
            );
53

54
            this.emitServerEvent("start", Date.now() - this.startTime);
26✔
55
        };
56

57
        this.mcpServer.server.onclose = () => {
26✔
58
            const closeTime = Date.now();
26✔
59
            this.emitServerEvent("stop", Date.now() - closeTime);
26✔
60
        };
61

62
        this.mcpServer.server.onerror = (error: Error) => {
26✔
NEW
UNCOV
63
            const closeTime = Date.now();
×
NEW
UNCOV
64
            this.emitServerEvent("stop", Date.now() - closeTime, error);
×
65
        };
66
    }
67

68
    async close(): Promise<void> {
69
        await this.session.close();
26✔
70
        await this.mcpServer.close();
26✔
71
    }
72

73
    /**
74
     * Emits a server event
75
     * @param command - The server command (e.g., "start", "stop", "register", "deregister")
76
     * @param additionalProperties - Additional properties specific to the event
77
     */
78
    emitServerEvent(command: ServerCommand, commandDuration: number, error?: Error) {
79
        const event: ServerEvent = {
52✔
80
            timestamp: new Date().toISOString(),
81
            source: "mdbmcp",
82
            properties: {
83
                ...this.telemetry.getCommonProperties(),
84
                result: "success",
85
                duration_ms: commandDuration,
86
                component: "server",
87
                category: "other",
88
                command: command,
89
            },
90
        };
91

92
        if (command === "start") {
52✔
93
            event.properties.startup_time_ms = commandDuration;
26✔
94
        }
95
        if (command === "stop") {
52✔
96
            event.properties.runtime_duration_ms = Date.now() - this.startTime;
26✔
97
            if (error) {
26!
NEW
98
                event.properties.result = "failure";
×
NEW
99
                event.properties.reason = error.message;
×
100
            }
101
        }
102

103
        this.telemetry.emitEvents([event]).catch(() => {});
52✔
104
    }
105

106
    private registerTools() {
107
        for (const tool of [...AtlasTools, ...MongoDbTools]) {
26✔
108
            new tool(this.session, this.userConfig, this.telemetry).register(this.mcpServer);
754✔
109
        }
110
    }
111

112
    private registerResources() {
113
        if (this.userConfig.connectionString) {
26!
114
            this.mcpServer.resource(
×
115
                "connection-string",
116
                "config://connection-string",
117
                {
118
                    description: "Preconfigured connection string that will be used as a default in the `connect` tool",
119
                },
120
                (uri) => {
121
                    return {
×
122
                        contents: [
123
                            {
124
                                text: `Preconfigured connection string: ${this.userConfig.connectionString}`,
125
                                uri: uri.href,
126
                            },
127
                        ],
128
                    };
129
                }
130
            );
131
        }
132
    }
133
}
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

© 2026 Coveralls, Inc