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

mongodb-js / mongodb-mcp-server / 17429932259

03 Sep 2025 09:59AM UTC coverage: 81.125% (+0.2%) from 80.965%
17429932259

Pull #502

github

web-flow
Merge ed6adc6d6 into 47c0a09e0
Pull Request #502: chore: extend library interfaces to allow injecting a custom connection error handler MCP-132

901 of 1199 branches covered (75.15%)

Branch coverage included in aggregate %.

92 of 106 new or added lines in 9 files covered. (86.79%)

2 existing lines in 2 files now uncovered.

4579 of 5556 relevant lines covered (82.42%)

44.39 hits per line

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

63.33
/src/transports/stdio.ts
1
import { EJSON } from "bson";
1!
2
import type { JSONRPCMessage } from "@modelcontextprotocol/sdk/types.js";
3
import { JSONRPCMessageSchema } from "@modelcontextprotocol/sdk/types.js";
1✔
4
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
1✔
5
import { LogId } from "../common/logger.js";
1✔
6
import type { Server } from "../server.js";
7
import { TransportRunnerBase, type TransportRunnerConfig } from "./base.js";
1✔
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 {
1✔
12
    private _buffer?: Buffer;
13

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

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

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

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

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

35
    clear(): void {
1✔
36
        this._buffer = undefined;
3✔
37
    }
3✔
38
}
1✔
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 {
1✔
47
    const server = new StdioServerTransport();
3✔
48
    server["_readBuffer"] = new EJsonReadBuffer();
3✔
49

50
    return server;
3✔
51
}
3✔
52

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

56
    constructor(config: TransportRunnerConfig) {
1✔
NEW
57
        super(config);
×
UNCOV
58
    }
×
59

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

64
            const transport = createStdioTransport();
×
65

66
            await this.server.connect(transport);
×
67
        } catch (error: unknown) {
×
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 closeTransport(): Promise<void> {
1✔
78
        await this.server?.close();
×
79
    }
×
80
}
1✔
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