Skip to content

chore: Reactive resource support for debugging connectivity MCP-80 #413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 31, 2025
Merged
47 changes: 31 additions & 16 deletions src/common/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
apiClientSecret?: string;
}

export class Session extends EventEmitter<{
export type SessionEvents = {
connected: [];
close: [];
disconnect: [];
}> {
"connection-error": [string];
};

export class Session extends EventEmitter<SessionEvents> {
sessionId?: string;
serviceProvider?: NodeDriverServiceProvider;
apiClient: ApiClient;
Expand Down Expand Up @@ -102,19 +106,30 @@
connectionString,
defaultAppName: `${packageInfo.mcpServerName} ${packageInfo.version}`,
});
this.serviceProvider = await NodeDriverServiceProvider.connect(connectionString, {
productDocsLink: "https://github.com/mongodb-js/mongodb-mcp-server/",
productName: "MongoDB MCP",
readConcern: {
level: connectOptions.readConcern,
},
readPreference: connectOptions.readPreference,
writeConcern: {
w: connectOptions.writeConcern,
},
timeoutMS: connectOptions.timeoutMS,
proxy: { useEnvironmentVariableProxies: true },
applyProxyToOIDC: true,
});

try {
this.serviceProvider = await NodeDriverServiceProvider.connect(connectionString, {
productDocsLink: "https://github.com/mongodb-js/mongodb-mcp-server/",
productName: "MongoDB MCP",
readConcern: {
level: connectOptions.readConcern,
},
readPreference: connectOptions.readPreference,
writeConcern: {
w: connectOptions.writeConcern,
},
timeoutMS: connectOptions.timeoutMS,
proxy: { useEnvironmentVariableProxies: true },
applyProxyToOIDC: true,
});

await this.serviceProvider?.runCommand?.("admin", { hello: 1 });
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NodeDriverServiceProvider.connect, despite it's name, does not in fact connect. This behaviour is likely inherited from the driver.

By forcing a ping (by using hello) we force a connection and validate that it's in fact correct.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have a ping on atlas cluster connect tool where we added a ping, should we drop it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that it's used for answering the client with the connection status. I think we can keep it for now and then when we remove this logic to the new ConnectionManager (Tech Design) we can refactor and use the same code for both connections.

How do you feel about this strategy?

} catch (error: unknown) {
const message = error instanceof Error ? error.message : `${error}`;

Check failure on line 128 in src/common/session.ts

View workflow job for this annotation

GitHub Actions / check-style

Invalid type "unknown" of template literal expression
this.emit("connection-error", message);
throw error;
}

this.emit("connected");
}
}
42 changes: 42 additions & 0 deletions src/resources/common/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ReactiveResource } from "../resource.js";
import { config } from "../../common/config.js";
import type { UserConfig } from "../../common/config.js";

export class ConfigResource extends ReactiveResource(
{
name: "config",
uri: "config://config",
config: {
description:
"Server configuration, supplied by the user either as environment variables or as startup arguments",
},
},
{
initial: { ...config },
events: [],
}
) {
reduce(eventName: undefined, event: undefined): UserConfig {
void eventName;
void event;
Comment on lines +19 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could possibly be avoided by having reduce without any params?


return this.current;
}

toOutput(): string {
const result = {
telemetry: this.current.telemetry,
logPath: this.current.logPath,
connectionString: this.current.connectionString
? "set; access to MongoDB tools are currently available to use"
: "not set; before using any MongoDB tool, you need to configure a connection string, alternatively you can setup MongoDB Atlas access, more info at 'https://github.com/mongodb-js/mongodb-mcp-server'.",
connectOptions: this.current.connectOptions,
atlas:
this.current.apiClientId && this.current.apiClientSecret
? "set; MongoDB Atlas tools are currently available to use"
: "not set; MongoDB Atlas tools are currently unavailable, to have access to MongoDB Atlas tools like creating clusters or connecting to clusters make sure to setup credentials, more info at 'https://github.com/mongodb-js/mongodb-mcp-server'.",
};

return JSON.stringify(result);
}
}
59 changes: 59 additions & 0 deletions src/resources/common/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ReactiveResource } from "../resource.js";

type ConnectionStateDebuggingInformation = {
readonly tag: "connected" | "connecting" | "disconnected" | "errored";
readonly connectionStringAuthType?: "scram" | "ldap" | "kerberos" | "oidc-auth-flow" | "oidc-device-flow" | "x.509";
readonly oidcLoginUrl?: string;
readonly oidcUserCode?: string;
readonly errorReason?: string;
};

export class DebugResource extends ReactiveResource(
{
name: "debug",
uri: "config://debug",
config: {
description: "Debugging information for connectivity issues.",
},
},
{
initial: { tag: "disconnected" } as ConnectionStateDebuggingInformation,
events: ["connected", "disconnect", "close", "connection-error"],
}
) {
reduce(
eventName: "connected" | "disconnect" | "close" | "connection-error",
event: string | undefined
): ConnectionStateDebuggingInformation {
void event;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary anymore, now that you're using event in connection-error case?


switch (eventName) {
case "connected":
return { tag: "connected" };
case "connection-error":
return { tag: "errored", errorReason: event };
case "disconnect":
case "close":
return { tag: "disconnected" };
}
}

toOutput(): string {
let result = "";

switch (this.current.tag) {

Check failure on line 44 in src/resources/common/debug.ts

View workflow job for this annotation

GitHub Actions / check-style

Switch is not exhaustive. Cases not matched: "connecting"
case "connected":
result += "The user is connected to the MongoDB cluster.";
break;
case "errored":
result += `The user is not connected to a MongoDB cluster because of an error.\n`;
result += `<error>${this.current.errorReason}</error>`;
break;
case "disconnected":
result += "The user is not connected to a MongoDB cluster.";
break;
}

return result;
}
}
80 changes: 80 additions & 0 deletions src/resources/resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Server } from "../server.js";
import { Session } from "../common/session.js";
import { UserConfig } from "../common/config.js";
import { Telemetry } from "../telemetry/telemetry.js";
import type { SessionEvents } from "../common/session.js";
import { ReadResourceCallback, RegisteredResource, ResourceMetadata } from "@modelcontextprotocol/sdk/server/mcp.js";

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

type ResourceConfiguration = { name: string; uri: string; config: ResourceMetadata };

export function ReactiveResource<V, KE extends readonly (keyof SessionEvents)[]>(
{ name, uri, config: resourceConfig }: ResourceConfiguration,
{
initial,
events,
}: {
initial: V;
events: KE;
}
) {
type E = KE[number];

abstract class NewReactiveResource {
private registeredResource?: RegisteredResource;
protected readonly session: Session;
protected readonly config: UserConfig;
protected current: V;

constructor(
protected readonly server: Server,
protected readonly telemetry: Telemetry,
current?: V
) {
this.current = current ?? initial;
this.session = server.session;
this.config = server.userConfig;

for (const event of events) {
this.session.on(event, (...args: SessionEvents[typeof event]) => {
this.reduceApply(event, (args as unknown[])[0] as PayloadOf<typeof event>);
this.triggerUpdate();
});
}
}

public register(): void {
this.registeredResource = this.server.mcpServer.registerResource(
name,
uri,
resourceConfig,
this.resourceCallback
);
}

private resourceCallback: ReadResourceCallback = (uri) => ({
contents: [
{
text: this.toOutput(),
mimeType: "application/json",
uri: uri.href,
},
],
});

private triggerUpdate() {
this.registeredResource?.update({});
this.server.mcpServer.sendResourceListChanged();
}

reduceApply(eventName: E, ...event: PayloadOf<E>[]): void {
this.current = this.reduce(eventName, ...event);
}

protected abstract reduce(eventName: E, ...event: PayloadOf<E>[]): V;
abstract toOutput(): string;
}

return NewReactiveResource;
}
4 changes: 4 additions & 0 deletions src/resources/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { ConfigResource } from "./common/config.js";
import { DebugResource } from "./common/debug.js";

export const Resources = [ConfigResource, DebugResource] as const;
36 changes: 5 additions & 31 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Session } from "./common/session.js";
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
import { AtlasTools } from "./tools/atlas/tools.js";
import { MongoDbTools } from "./tools/mongodb/tools.js";
import { Resources } from "./resources/resources.js";
import logger, { LogId, LoggerBase, McpLogger, DiskLogger, ConsoleLogger } from "./common/logger.js";
import { ObjectId } from "mongodb";
import { Telemetry } from "./telemetry/telemetry.js";
Expand Down Expand Up @@ -155,37 +156,10 @@ export class Server {
}

private registerResources() {
this.mcpServer.resource(
"config",
"config://config",
{
description:
"Server configuration, supplied by the user either as environment variables or as startup arguments",
},
(uri) => {
const result = {
telemetry: this.userConfig.telemetry,
logPath: this.userConfig.logPath,
connectionString: this.userConfig.connectionString
? "set; access to MongoDB tools are currently available to use"
: "not set; before using any MongoDB tool, you need to configure a connection string, alternatively you can setup MongoDB Atlas access, more info at 'https://github.com/mongodb-js/mongodb-mcp-server'.",
connectOptions: this.userConfig.connectOptions,
atlas:
this.userConfig.apiClientId && this.userConfig.apiClientSecret
? "set; MongoDB Atlas tools are currently available to use"
: "not set; MongoDB Atlas tools are currently unavailable, to have access to MongoDB Atlas tools like creating clusters or connecting to clusters make sure to setup credentials, more info at 'https://github.com/mongodb-js/mongodb-mcp-server'.",
};
return {
contents: [
{
text: JSON.stringify(result),
mimeType: "application/json",
uri: uri.href,
},
],
};
}
);
for (const resourceConstructor of Resources) {
const resource = new resourceConstructor(this, this.telemetry);
resource.register();
}
}

private async validateConfig(): Promise<void> {
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/resources/common/debug.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

Check failure on line 1 in tests/unit/resources/common/debug.test.ts

View workflow job for this annotation

GitHub Actions / check-style

'vi' is defined but never used

Check failure on line 1 in tests/unit/resources/common/debug.test.ts

View workflow job for this annotation

GitHub Actions / check-style

'beforeEach' is defined but never used

Check failure on line 1 in tests/unit/resources/common/debug.test.ts

View workflow job for this annotation

GitHub Actions / check-style

'afterEach' is defined but never used
import { DebugResource } from "../../../../src/resources/common/debug.js";
import { Session } from "../../../../src/common/session.js";
import { Server } from "../../../../src/server.js";
import { Telemetry } from "../../../../src/telemetry/telemetry.js";
import { config } from "../../../../src/common/config.js";

describe("debug resource", () => {
let session = new Session({} as any);

Check failure on line 9 in tests/unit/resources/common/debug.test.ts

View workflow job for this annotation

GitHub Actions / check-style

Unexpected any. Specify a different type

Check failure on line 9 in tests/unit/resources/common/debug.test.ts

View workflow job for this annotation

GitHub Actions / check-style

Unsafe argument of type `any` assigned to a parameter of type `SessionOptions`

Check failure on line 9 in tests/unit/resources/common/debug.test.ts

View workflow job for this annotation

GitHub Actions / check-style

'session' is never reassigned. Use 'const' instead
let server = new Server({ session } as any);

Check failure on line 10 in tests/unit/resources/common/debug.test.ts

View workflow job for this annotation

GitHub Actions / check-style

Unsafe argument of type `any` assigned to a parameter of type `ServerOptions`

Check failure on line 10 in tests/unit/resources/common/debug.test.ts

View workflow job for this annotation

GitHub Actions / check-style

'server' is never reassigned. Use 'const' instead
let telemetry = Telemetry.create(session, { ...config, telemetry: "disabled" });

let debugResource: DebugResource = new DebugResource(server, telemetry, { tag: "disconnected" });

it("should be connected when a connected event happens", () => {
debugResource.reduceApply("connected", undefined);
const output = debugResource.toOutput();

expect(output).toContain(`The user is connected to the MongoDB cluster.`);
});

it("should be disconnected when a disconnect event happens", () => {
debugResource.reduceApply("disconnect", undefined);
const output = debugResource.toOutput();

expect(output).toContain(`The user is not connected to a MongoDB cluster.`);
});

it("should be disconnected when a close event happens", () => {
debugResource.reduceApply("close", undefined);
const output = debugResource.toOutput();

expect(output).toContain(`The user is not connected to a MongoDB cluster.`);
});

it("should be disconnected and contain an error when an error event occurred", () => {
debugResource.reduceApply("connection-error", "Error message from the server");
const output = debugResource.toOutput();

expect(output).toContain(`The user is not connected to a MongoDB cluster because of an error.`);
expect(output).toContain(`<error>Error message from the server</error>`);
});
});
Loading