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

mongodb-js / mongodb-mcp-server / 16804714278

07 Aug 2025 12:39PM UTC coverage: 81.376% (+0.1%) from 81.276%
16804714278

Pull #371

github

web-flow
Merge 0e28212a1 into 42837a410
Pull Request #371: chore(build): build a universal ESM and CommonJS package

661 of 854 branches covered (77.4%)

Branch coverage included in aggregate %.

79 of 90 new or added lines in 5 files covered. (87.78%)

1 existing line in 1 file now uncovered.

3538 of 4306 relevant lines covered (82.16%)

58.36 hits per line

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

87.3
/src/resources/resource.ts
1
import { Server } from "../server.js";
2
import { Session } from "../common/session.js";
3
import { UserConfig } from "../common/config.js";
4
import { Telemetry } from "../telemetry/telemetry.js";
5
import type { SessionEvents } from "../common/session.js";
6
import { ReadResourceCallback, ResourceMetadata } from "@modelcontextprotocol/sdk/server/mcp.js";
7
import { LogId } from "../common/logger.js";
2✔
8

9
type PayloadOf<K extends keyof SessionEvents> = SessionEvents[K][0];
10

11
export type ResourceConfiguration = {
12
    name: string;
13
    uri: string;
14
    config: ResourceMetadata;
15
};
16

17
export type ReactiveResourceOptions<Value, RelevantEvents extends readonly (keyof SessionEvents)[]> = {
18
    initial: Value;
19
    events: RelevantEvents;
20
};
21

22
export abstract class ReactiveResource<Value, RelevantEvents extends readonly (keyof SessionEvents)[]> {
2✔
23
    protected readonly session: Session;
24
    protected readonly config: UserConfig;
25
    protected current: Value;
26
    protected readonly name: string;
27
    protected readonly uri: string;
28
    protected readonly resourceConfig: ResourceMetadata;
29
    protected readonly events: RelevantEvents;
30

31
    constructor(
2✔
32
        resourceConfiguration: ResourceConfiguration,
146✔
33
        options: ReactiveResourceOptions<Value, RelevantEvents>,
146✔
34
        protected readonly server: Server,
146✔
35
        protected readonly telemetry: Telemetry,
146✔
36
        current?: Value
146✔
37
    ) {
146✔
38
        this.name = resourceConfiguration.name;
146✔
39
        this.uri = resourceConfiguration.uri;
146✔
40
        this.resourceConfig = resourceConfiguration.config;
146✔
41
        this.events = options.events;
146✔
42
        this.current = current ?? options.initial;
146✔
43
        this.session = server.session;
146✔
44
        this.config = server.userConfig;
146✔
45

46
        this.setupEventListeners();
146✔
47
    }
146✔
48

49
    private setupEventListeners(): void {
2✔
50
        for (const event of this.events) {
146✔
51
            this.session.on(event, (...args: SessionEvents[typeof event]) => {
312✔
52
                this.reduceApply(event, (args as unknown[])[0] as PayloadOf<typeof event>);
723✔
53
                void this.triggerUpdate();
723✔
54
            });
312✔
55
        }
312✔
56
    }
146✔
57

58
    public register(): void {
2✔
59
        this.server.mcpServer.registerResource(this.name, this.uri, this.resourceConfig, this.resourceCallback);
136✔
60
    }
136✔
61

62
    private resourceCallback: ReadResourceCallback = (uri) => ({
2✔
NEW
63
        contents: [
×
NEW
64
            {
×
NEW
65
                text: this.toOutput(),
×
NEW
66
                mimeType: "application/json",
×
NEW
67
                uri: uri.href,
×
NEW
68
            },
×
NEW
69
        ],
×
NEW
70
    });
×
71

72
    private async triggerUpdate(): Promise<void> {
2✔
73
        try {
723✔
74
            await this.server.mcpServer.server.sendResourceUpdated({ uri: this.uri });
723✔
75
            this.server.mcpServer.sendResourceListChanged();
720✔
76
        } catch (error: unknown) {
723✔
77
            this.session.logger.warning({
3✔
78
                id: LogId.resourceUpdateFailure,
3✔
79
                context: "resource",
3✔
80
                message: `Could not send the latest resources to the client: ${error as string}`,
3✔
81
            });
3✔
82
        }
3✔
83
    }
723✔
84

85
    public reduceApply(eventName: RelevantEvents[number], ...event: PayloadOf<RelevantEvents[number]>[]): void {
2✔
86
        this.current = this.reduce(eventName, ...event);
731✔
87
    }
731✔
88

89
    protected abstract reduce(eventName: RelevantEvents[number], ...event: PayloadOf<RelevantEvents[number]>[]): Value;
90
    public abstract toOutput(): string;
91
}
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