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

mongodb-js / mongodb-mcp-server / 23235404787

18 Mar 2026 08:18AM UTC coverage: 84.992% (-0.2%) from 85.169%
23235404787

Pull #978

github

web-flow
Merge d0d9b882e into fa70fb6a2
Pull Request #978: chore: remove the mcp autoembeddings feature

2107 of 2721 branches covered (77.43%)

Branch coverage included in aggregate %.

43 of 43 new or added lines in 9 files covered. (100.0%)

80 existing lines in 6 files now uncovered.

9933 of 11445 relevant lines covered (86.79%)

110.46 hits per line

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

76.74
/src/common/sessionStore.ts
1
import type { LoggerBase } from "./logging/index.js";
2✔
2
import { LogId } from "./logging/index.js";
1✔
3
import type { ManagedTimeout } from "./managedTimeout.js";
4
import { setManagedTimeout } from "./managedTimeout.js";
1✔
5
import type { Metrics } from "./metrics/metricsTypes.js";
6
import type { DefaultMetrics } from "./metrics/metricDefinitions.js";
7

8
/**
9
 * Minimal interface for a transport that can be stored in a SessionStore.
10
 * The transport must have a close method for cleanup.
11
 */
12
export type CloseableTransport = {
13
    close(): Promise<void>;
14
};
15

16
export type SessionCloseReason = "idle_timeout" | "transport_closed" | "server_stop" | "unknown";
17

18
export class SessionStore<T extends CloseableTransport = CloseableTransport> {
1✔
19
    private sessions: {
1✔
20
        [sessionId: string]: {
21
            logger: LoggerBase;
22
            transport: T;
23
            abortTimeout: ManagedTimeout;
24
            notificationTimeout: ManagedTimeout;
25
        };
26
    } = {};
1✔
27

28
    constructor(
1✔
29
        private readonly idleTimeoutMS: number,
94✔
30
        private readonly notificationTimeoutMS: number,
94✔
31
        private readonly logger: LoggerBase,
94✔
32
        private readonly metrics: Metrics<DefaultMetrics>
94✔
33
    ) {
94✔
34
        if (idleTimeoutMS <= 0) {
94!
UNCOV
35
            throw new Error("idleTimeoutMS must be greater than 0");
×
36
        }
×
37
        if (notificationTimeoutMS <= 0) {
94!
UNCOV
38
            throw new Error("notificationTimeoutMS must be greater than 0");
×
UNCOV
39
        }
×
40
        if (idleTimeoutMS <= notificationTimeoutMS) {
94!
UNCOV
41
            throw new Error("idleTimeoutMS must be greater than notificationTimeoutMS");
×
UNCOV
42
        }
×
43
    }
94✔
44

45
    getSession(sessionId: string): T | undefined {
1✔
46
        this.resetTimeout(sessionId);
156✔
47
        return this.sessions[sessionId]?.transport;
156✔
48
    }
156✔
49

50
    private resetTimeout(sessionId: string): void {
1✔
51
        const session = this.sessions[sessionId];
156✔
52
        if (!session) {
156✔
53
            return;
15✔
54
        }
15✔
55

56
        session.abortTimeout.restart();
141✔
57

58
        session.notificationTimeout.restart();
141✔
59
    }
156✔
60

61
    private sendNotification(sessionId: string): void {
1✔
62
        const session = this.sessions[sessionId];
3✔
63
        if (!session) {
3!
64
            this.logger.warning({
×
65
                id: LogId.streamableHttpTransportSessionCloseNotificationFailure,
×
UNCOV
66
                context: "sessionStore",
×
UNCOV
67
                message: `session ${sessionId} not found, no notification delivered`,
×
UNCOV
68
            });
×
UNCOV
69
            return;
×
UNCOV
70
        }
×
71
        session.logger.info({
3✔
72
            id: LogId.streamableHttpTransportSessionCloseNotification,
3✔
73
            context: "sessionStore",
3✔
74
            message: "Session is about to be closed due to inactivity",
3✔
75
        });
3✔
76
    }
3✔
77

78
    setSession(sessionId: string, transport: T, logger: LoggerBase): void {
1✔
79
        const session = this.sessions[sessionId];
67✔
80
        if (session) {
67!
UNCOV
81
            throw new Error(`Session ${sessionId} already exists`);
×
UNCOV
82
        }
×
83
        const abortTimeout = setManagedTimeout(async () => {
67✔
84
            if (this.sessions[sessionId]) {
3✔
85
                this.sessions[sessionId].logger.info({
3✔
86
                    id: LogId.streamableHttpTransportSessionCloseNotification,
3✔
87
                    context: "sessionStore",
3✔
88
                    message: "Session closed due to inactivity",
3✔
89
                });
3✔
90

91
                await this.closeSession({ sessionId, reason: "idle_timeout" });
3✔
92
            }
3✔
93
        }, this.idleTimeoutMS);
67✔
94
        const notificationTimeout = setManagedTimeout(
67✔
95
            () => this.sendNotification(sessionId),
67✔
96
            this.notificationTimeoutMS
67✔
97
        );
67✔
98
        this.sessions[sessionId] = {
67✔
99
            transport,
67✔
100
            abortTimeout,
67✔
101
            notificationTimeout,
67✔
102
            logger,
67✔
103
        };
67✔
104
        this.metrics.get("sessionCreated").inc();
67✔
105
    }
67✔
106

107
    async closeSession({
1✔
108
        sessionId,
64✔
109
        reason = "unknown",
64✔
110
    }: {
64✔
111
        sessionId: string;
112
        reason?: SessionCloseReason;
113
    }): Promise<void> {
64✔
114
        const session = this.sessions[sessionId];
64✔
115
        if (!session) {
64!
116
            throw new Error(`Session ${sessionId} not found`);
×
117
        }
×
118

119
        // Remove from map before closing transport so that a re-entrant
120
        // onsessionclosed callback (fired by transport.close()) sees the
121
        // session as already gone and doesn't double-count metrics.
122
        delete this.sessions[sessionId];
64✔
123

124
        session.abortTimeout.cancel();
64✔
125
        session.notificationTimeout.cancel();
64✔
126

127
        if (reason !== "transport_closed") {
64✔
128
            // Only close the transport when the server initiates the close.
129
            try {
60✔
130
                await session.transport.close();
60✔
131
            } catch (error) {
60!
UNCOV
132
                this.logger.error({
×
UNCOV
133
                    id: LogId.streamableHttpTransportSessionCloseFailure,
×
UNCOV
134
                    context: "streamableHttpTransport",
×
UNCOV
135
                    message: `Error closing transport ${sessionId}: ${error instanceof Error ? error.message : String(error)}`,
×
UNCOV
136
                });
×
UNCOV
137
            }
×
138
        }
60✔
139

140
        this.metrics.get("sessionClosed").inc({ reason: reason });
64✔
141
    }
64✔
142

143
    async closeAllSessions(): Promise<void> {
1✔
144
        await Promise.all(
87✔
145
            Object.keys(this.sessions).map((sessionId) => this.closeSession({ sessionId, reason: "server_stop" }))
87✔
146
        );
87✔
147
    }
87✔
148
}
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

© 2026 Coveralls, Inc