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

mongodb-js / mongodb-mcp-server / 16876454078

11 Aug 2025 09:43AM UTC coverage: 82.089% (+0.7%) from 81.362%
16876454078

Pull #424

github

web-flow
Merge 34aff9a17 into 7572ec5d6
Pull Request #424: feat: adds an export tool and exported-data resource MCP-16

780 of 999 branches covered (78.08%)

Branch coverage included in aggregate %.

592 of 659 new or added lines in 13 files covered. (89.83%)

12 existing lines in 2 files now uncovered.

4078 of 4919 relevant lines covered (82.9%)

66.28 hits per line

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

77.27
/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 server?: Server;
24
    protected session: Session;
25
    protected config: UserConfig;
26
    protected telemetry: Telemetry;
27

28
    protected current: Value;
29
    protected readonly name: string;
30
    protected readonly uri: string;
31
    protected readonly resourceConfig: ResourceMetadata;
32
    protected readonly events: RelevantEvents;
33

34
    constructor({
2✔
35
        resourceConfiguration,
158✔
36
        options,
158✔
37
        session,
158✔
38
        config,
158✔
39
        telemetry,
158✔
40
        current,
158✔
41
    }: {
158✔
42
        resourceConfiguration: ResourceConfiguration;
43
        options: ReactiveResourceOptions<Value, RelevantEvents>;
44
        session: Session;
45
        config: UserConfig;
46
        telemetry: Telemetry;
47
        current?: Value;
48
    }) {
158✔
49
        this.session = session;
158✔
50
        this.config = config;
158✔
51
        this.telemetry = telemetry;
158✔
52

53
        this.name = resourceConfiguration.name;
158✔
54
        this.uri = resourceConfiguration.uri;
158✔
55
        this.resourceConfig = resourceConfiguration.config;
158✔
56
        this.events = options.events;
158✔
57
        this.current = current ?? options.initial;
158✔
58

59
        this.setupEventListeners();
158✔
60
    }
158✔
61

62
    private setupEventListeners(): void {
2✔
63
        for (const event of this.events) {
158✔
64
            this.session.on(event, (...args: SessionEvents[typeof event]) => {
336✔
65
                this.reduceApply(event, (args as unknown[])[0] as PayloadOf<typeof event>);
885✔
66
                void this.triggerUpdate();
885✔
67
            });
336✔
68
        }
336✔
69
    }
158✔
70

71
    public register(server: Server): void {
2✔
72
        this.server = server;
148✔
73
        this.server.mcpServer.registerResource(this.name, this.uri, this.resourceConfig, this.resourceCallback);
148✔
74
    }
148✔
75

76
    private resourceCallback: ReadResourceCallback = (uri) => ({
2✔
77
        contents: [
×
78
            {
×
79
                text: this.toOutput(),
×
80
                mimeType: "application/json",
×
81
                uri: uri.href,
×
82
            },
×
83
        ],
×
84
    });
×
85

86
    private triggerUpdate(): void {
2✔
87
        try {
885✔
88
            this.server?.sendResourceListChanged();
885✔
89
            this.server?.sendResourceUpdated(this.uri);
885✔
90
        } catch (error: unknown) {
885!
UNCOV
91
            this.session.logger.warning({
×
UNCOV
92
                id: LogId.resourceUpdateFailure,
×
UNCOV
93
                context: "resource",
×
UNCOV
94
                message: `Could not send the latest resources to the client: ${error as string}`,
×
UNCOV
95
            });
×
UNCOV
96
        }
×
97
    }
885✔
98

99
    public reduceApply(eventName: RelevantEvents[number], ...event: PayloadOf<RelevantEvents[number]>[]): void {
2✔
100
        this.current = this.reduce(eventName, ...event);
893✔
101
    }
893✔
102

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