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

mongodb-js / mongodb-mcp-server / 14660881741

25 Apr 2025 08:59AM UTC coverage: 81.98% (+6.5%) from 75.474%
14660881741

Pull #111

github

fmenezes
fix: address comments
Pull Request #111: refactor: split loggers

125 of 202 branches covered (61.88%)

Branch coverage included in aggregate %.

23 of 25 new or added lines in 1 file covered. (92.0%)

2 existing lines in 1 file now uncovered.

703 of 808 relevant lines covered (87.0%)

48.61 hits per line

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

91.89
/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";
31✔
5
import { MongoDbTools } from "./tools/mongodb/tools.js";
31✔
6
import logger, { initializeLogger } from "./logger.js";
31✔
7
import { mongoLogId } from "mongodb-log-writer";
31✔
8
import { ObjectId } from "mongodb";
31✔
9
import { Telemetry } from "./telemetry/telemetry.js";
31✔
10
import { UserConfig } from "./config.js";
11

31✔
12
export interface ServerOptions {
31✔
13
    session: Session;
14
    userConfig: UserConfig;
15
    mcpServer: McpServer;
16
}
17

18
export class Server {
19
    public readonly session: Session;
20
    private readonly mcpServer: McpServer;
31✔
21
    private readonly telemetry: Telemetry;
22
    private readonly userConfig: UserConfig;
23

24
    constructor({ session, mcpServer, userConfig }: ServerOptions) {
25
        this.session = session;
26
        this.telemetry = new Telemetry(session);
27
        this.mcpServer = mcpServer;
27✔
28
        this.userConfig = userConfig;
27✔
29
    }
27✔
30

27✔
31
    async connect(transport: Transport) {
32
        this.mcpServer.server.registerCapabilities({ logging: {} });
33
        this.registerTools();
34
        this.registerResources();
27✔
35

27✔
36
        await initializeLogger(this.mcpServer, this.userConfig.logPath);
27✔
37

38
        await this.mcpServer.connect(transport);
39

40
        this.mcpServer.server.oninitialized = () => {
41
            this.session.setAgentRunner(this.mcpServer.server.getClientVersion());
42
            this.session.sessionId = new ObjectId().toString();
43

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

27✔
52
    async close(): Promise<void> {
53
        await this.session.close();
27✔
54
        await this.mcpServer.close();
345✔
55
    }
2✔
56

57
    private registerTools() {
58
        for (const tool of [...AtlasTools, ...MongoDbTools]) {
345✔
59
            new tool(this.session, this.userConfig, this.telemetry).register(this.mcpServer);
60
        }
61
    }
27✔
62

63
    private registerResources() {
27✔
64
        if (this.userConfig.connectionString) {
65
            this.mcpServer.resource(
27✔
66
                "connection-string",
27✔
67
                "config://connection-string",
27✔
68
                {
69
                    description: "Preconfigured connection string that will be used as a default in the `connect` tool",
27✔
70
                },
71
                (uri) => {
72
                    return {
73
                        contents: [
74
                            {
75
                                text: `Preconfigured connection string: ${this.userConfig.connectionString}`,
76
                                uri: uri.href,
77
                            },
78
                        ],
27✔
79
                    };
27✔
80
                }
81
            );
82
        }
83
    }
27✔
84
}
810✔
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