Skip to content

chore: avoid logging files on disk and move them to console for containers #340

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 5 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/common/container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import fs from "fs/promises";

let containerEnv: boolean | undefined;

export async function detectContainerEnv(): Promise<boolean> {
if (containerEnv !== undefined) {
return containerEnv;
}

const detect = async function (): Promise<boolean> {
if (process.platform !== "linux") {
return false; // we only support linux containers for now
}

if (process.env.container) {
return true;
}

const exists = await Promise.all(
["/.dockerenv", "/run/.containerenv", "/var/run/.containerenv"].map(async (file) => {
try {
await fs.access(file);
return true;
} catch {
return false;
}
})
);

return exists.includes(true);
};

containerEnv = await detect();
return containerEnv;
}
9 changes: 7 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,16 @@ class CompositeLogger extends LoggerBase {
const logger = new CompositeLogger();
export default logger;

export async function initializeLogger(server: McpServer, logPath: string): Promise<LoggerBase> {
export async function setStdioPreset(server: McpServer, logPath: string): Promise<void> {
const diskLogger = await DiskLogger.fromPath(logPath);
const mcpLogger = new McpLogger(server);

logger.setLoggers(mcpLogger, diskLogger);
}

export function setContainerPreset(server: McpServer): void {
const mcpLogger = new McpLogger(server);
const consoleLogger = new ConsoleLogger();

return logger;
logger.setLoggers(mcpLogger, consoleLogger);
}
11 changes: 9 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import { Session } from "./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 logger, { initializeLogger, LogId } from "./logger.js";
import logger, { setStdioPreset, setContainerPreset, LogId } from "./logger.js";
import { ObjectId } from "mongodb";
import { Telemetry } from "./telemetry/telemetry.js";
import { UserConfig } from "./config.js";
import { type ServerEvent } from "./telemetry/types.js";
import { type ServerCommand } from "./telemetry/types.js";
import { CallToolRequestSchema, CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import assert from "assert";
import { detectContainerEnv } from "./common/container.js";

export interface ServerOptions {
session: Session;
Expand Down Expand Up @@ -63,7 +64,13 @@ export class Server {
return existingHandler(request, extra);
});

await initializeLogger(this.mcpServer, this.userConfig.logPath);
const containerEnv = await detectContainerEnv();

if (containerEnv) {
setContainerPreset(this.mcpServer);
} else {
await setStdioPreset(this.mcpServer, this.userConfig.logPath);
}

await this.mcpServer.connect(transport);

Expand Down
27 changes: 2 additions & 25 deletions src/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { MACHINE_METADATA } from "./constants.js";
import { EventCache } from "./eventCache.js";
import nodeMachineId from "node-machine-id";
import { getDeviceId } from "@mongodb-js/device-id";
import fs from "fs/promises";
import { detectContainerEnv } from "../common/container.js";

type EventResult = {
success: boolean;
Expand Down Expand Up @@ -53,29 +53,6 @@ export class Telemetry {
return instance;
}

private async isContainerEnv(): Promise<boolean> {
if (process.platform !== "linux") {
return false; // we only support linux containers for now
}

if (process.env.container) {
return true;
}

const exists = await Promise.all(
["/.dockerenv", "/run/.containerenv", "/var/run/.containerenv"].map(async (file) => {
try {
await fs.access(file);
return true;
} catch {
return false;
}
})
);

return exists.includes(true);
}

private async setup(): Promise<void> {
if (!this.isTelemetryEnabled()) {
return;
Expand All @@ -98,7 +75,7 @@ export class Telemetry {
},
abortSignal: this.deviceIdAbortController.signal,
}),
this.isContainerEnv(),
detectContainerEnv(),
]);

const [deviceId, containerEnv] = await this.setupPromise;
Expand Down
17 changes: 14 additions & 3 deletions tests/integration/tools/atlas/clusters.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Session } from "../../../../src/session.js";
import { expectDefined } from "../../helpers.js";
import { describeWithAtlas, withProject, randomId } from "./atlasHelpers.js";
import { ClusterDescription20240805 } from "../../../../src/common/atlas/openapi.js";
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";

function sleep(ms: number) {
Expand Down Expand Up @@ -33,7 +34,12 @@ async function deleteAndWaitCluster(session: Session, projectId: string, cluster
}
}

async function waitClusterState(session: Session, projectId: string, clusterName: string, state: string) {
async function waitCluster(
session: Session,
projectId: string,
clusterName: string,
check: (cluster: ClusterDescription20240805) => boolean | Promise<boolean>
) {
while (true) {
const cluster = await session.apiClient.getCluster({
params: {
Expand All @@ -43,7 +49,7 @@ async function waitClusterState(session: Session, projectId: string, clusterName
},
},
});
if (cluster?.stateName === state) {
if (await check(cluster)) {
return;
}
await sleep(1000);
Expand Down Expand Up @@ -142,7 +148,12 @@ describeWithAtlas("clusters", (integration) => {
describe("atlas-connect-cluster", () => {
beforeAll(async () => {
const projectId = getProjectId();
await waitClusterState(integration.mcpServer().session, projectId, clusterName, "IDLE");
await waitCluster(integration.mcpServer().session, projectId, clusterName, (cluster) => {
return (
cluster.stateName === "IDLE" &&
(cluster.connectionStrings?.standardSrv || cluster.connectionStrings?.standard) !== undefined
);
});
await integration.mcpServer().session.apiClient.createProjectIpAccessList({
params: {
path: {
Expand Down
Loading