mongodb-logs
Retrieve recent MongoDB log events, including global logs or startup warnings, with customizable entry limits for efficient database diagnostics and monitoring.
Instructions
Returns the most recent logged mongod events
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | The maximum number of log entries to return. | |
| type | No | The type of logs to return. Global returns all recent log entries, while startupWarnings returns only warnings and errors from when the process started. | global |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"limit": {
"default": 50,
"description": "The maximum number of log entries to return.",
"maximum": 1024,
"minimum": 1,
"type": "integer"
},
"type": {
"default": "global",
"description": "The type of logs to return. Global returns all recent log entries, while startupWarnings returns only warnings and errors from when the process started.",
"enum": [
"global",
"startupWarnings"
],
"type": "string"
}
},
"type": "object"
}
Implementation Reference
- src/tools/mongodb/metadata/logs.ts:29-47 (handler)The main execution handler for the 'mongodb-logs' tool. It ensures connection to MongoDB, executes the 'getLog' command on the admin database with the specified type, limits the results, trims logs, and formats the output using formatUntrustedData.protected async execute({ type, limit }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { const provider = await this.ensureConnected(); const result = await provider.runCommandWithCheck("admin", { getLog: type, }); // Trim ending newlines so that when we join the logs we don't insert empty lines // between messages. const logs = (result.log as string[]).slice(0, limit).map((l) => l.trimEnd()); let message = `Found: ${result.totalLinesWritten} messages`; if (result.totalLinesWritten > limit) { message += ` (showing only the first ${limit})`; } return { content: formatUntrustedData(message, logs.join("\n")), }; }
- Zod-based input schema definition for the tool arguments: 'type' (enum: global/startupWarnings, default 'global') and 'limit' (number 1-1024, default 50). Includes descriptions for LLM guidance.protected argsShape = { type: z .enum(["global", "startupWarnings"]) .optional() .default("global") .describe( "The type of logs to return. Global returns all recent log entries, while startupWarnings returns only warnings and errors from when the process started." ), limit: z .number() .int() .max(1024) .min(1) .optional() .default(50) .describe("The maximum number of log entries to return."), };
- src/tools/mongodb/metadata/logs.ts:6-48 (registration)The LogsTool class definition, extending MongoDBToolBase, sets the tool name to 'mongodb-logs', description, schema, operationType 'metadata', and the execute handler. This class is exported and included in the tools registry via src/tools/mongodb/tools.ts and src/tools/index.ts.export class LogsTool extends MongoDBToolBase { public name = "mongodb-logs"; protected description = "Returns the most recent logged mongod events"; protected argsShape = { type: z .enum(["global", "startupWarnings"]) .optional() .default("global") .describe( "The type of logs to return. Global returns all recent log entries, while startupWarnings returns only warnings and errors from when the process started." ), limit: z .number() .int() .max(1024) .min(1) .optional() .default(50) .describe("The maximum number of log entries to return."), }; static operationType: OperationType = "metadata"; protected async execute({ type, limit }: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> { const provider = await this.ensureConnected(); const result = await provider.runCommandWithCheck("admin", { getLog: type, }); // Trim ending newlines so that when we join the logs we don't insert empty lines // between messages. const logs = (result.log as string[]).slice(0, limit).map((l) => l.trimEnd()); let message = `Found: ${result.totalLinesWritten} messages`; if (result.totalLinesWritten > limit) { message += ` (showing only the first ${limit})`; } return { content: formatUntrustedData(message, logs.join("\n")), }; } }
- src/tools/mongodb/tools.ts:20-20 (registration)Re-export of the LogsTool from its implementation file, allowing it to be imported as part of MongoDB tools and included in the central AllTools registry.export { LogsTool } from "./metadata/logs.js";
- src/tools/index.ts:7-11 (registration)Central registry where all tools, including MongoDB tools (containing LogsTool / mongodb-logs), are collected into the AllTools array for use in the MCP tools implementation.export const AllTools: ToolClass[] = Object.values({ ...MongoDbTools, ...AtlasTools, ...AtlasLocalTools, });