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

mongodb-js / mongodb-mcp-server / 16790782045

06 Aug 2025 11:09PM UTC coverage: 81.233% (-0.07%) from 81.298%
16790782045

Pull #425

github

web-flow
Merge 7712da066 into 53ac631ea
Pull Request #425: fix: remove global logger MCP-103

681 of 881 branches covered (77.3%)

Branch coverage included in aggregate %.

158 of 207 new or added lines in 17 files covered. (76.33%)

6 existing lines in 2 files now uncovered.

3522 of 4293 relevant lines covered (82.04%)

58.12 hits per line

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

64.41
/src/transports/stdio.ts
1
import { LogId } from "../common/logger.js";
2✔
2
import { Server } from "../server.js";
3
import { TransportRunnerBase } from "./base.js";
2✔
4
import { JSONRPCMessage, JSONRPCMessageSchema } from "@modelcontextprotocol/sdk/types.js";
2✔
5
import { EJSON } from "bson";
2✔
6
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
2✔
7
import { UserConfig } from "../common/config.js";
8

9
// This is almost a copy of ReadBuffer from @modelcontextprotocol/sdk
10
// but it uses EJSON.parse instead of JSON.parse to handle BSON types
11
export class EJsonReadBuffer {
2✔
12
    private _buffer?: Buffer;
13

14
    append(chunk: Buffer): void {
2✔
15
        this._buffer = this._buffer ? Buffer.concat([this._buffer, chunk]) : chunk;
2!
16
    }
2✔
17

18
    readMessage(): JSONRPCMessage | null {
2✔
19
        if (!this._buffer) {
4!
20
            return null;
×
21
        }
×
22

23
        const index = this._buffer.indexOf("\n");
4✔
24
        if (index === -1) {
4✔
25
            return null;
2✔
26
        }
2✔
27

28
        const line = this._buffer.toString("utf8", 0, index).replace(/\r$/, "");
2✔
29
        this._buffer = this._buffer.subarray(index + 1);
2✔
30

31
        // This is using EJSON.parse instead of JSON.parse to handle BSON types
32
        return JSONRPCMessageSchema.parse(EJSON.parse(line));
2✔
33
    }
4✔
34

35
    clear(): void {
2✔
36
        this._buffer = undefined;
6✔
37
    }
6✔
38
}
2✔
39

40
// This is a hacky workaround for https://github.com/mongodb-js/mongodb-mcp-server/issues/211
41
// The underlying issue is that StdioServerTransport uses JSON.parse to deserialize
42
// messages, but that doesn't handle bson types, such as ObjectId when serialized as EJSON.
43
//
44
// This function creates a StdioServerTransport and replaces the internal readBuffer with EJsonReadBuffer
45
// that uses EJson.parse instead.
46
export function createStdioTransport(): StdioServerTransport {
2✔
47
    const server = new StdioServerTransport();
6✔
48
    server["_readBuffer"] = new EJsonReadBuffer();
6✔
49

50
    return server;
6✔
51
}
6✔
52

53
export class StdioRunner extends TransportRunnerBase {
2✔
54
    private server: Server | undefined;
55

56
    constructor(userConfig: UserConfig) {
2✔
NEW
57
        super(userConfig);
×
UNCOV
58
    }
×
59

60
    async start(): Promise<void> {
2✔
61
        try {
×
62
            this.server = this.setupServer(this.userConfig);
×
63

64
            const transport = createStdioTransport();
×
65

66
            await this.server.connect(transport);
×
67
        } catch (error: unknown) {
×
NEW
68
            this.logger.emergency({
×
69
                id: LogId.serverStartFailure,
×
70
                context: "server",
×
71
                message: `Fatal error running server: ${error as string}`,
×
72
            });
×
73
            process.exit(1);
×
74
        }
×
75
    }
×
76

77
    async close(): Promise<void> {
2✔
78
        await this.server?.close();
×
79
    }
×
80
}
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