-
Notifications
You must be signed in to change notification settings - Fork 132
fix: handle setLogLevel requests #486
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,4 +55,4 @@ jobs: | |
rm -rf node_modules | ||
npm pkg set scripts.prepare="exit 0" | ||
npm install --omit=dev | ||
- run: npx -y @modelcontextprotocol/[email protected].2 --cli --method tools/list -- node dist/esm/index.js | ||
- run: npx -y @modelcontextprotocol/[email protected].5 --cli --method tools/list -- node dist/esm/index.js |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,9 @@ import fs from "fs/promises"; | |
import type { MongoLogId, MongoLogWriter } from "mongodb-log-writer"; | ||
import { mongoLogId, MongoLogManager } from "mongodb-log-writer"; | ||
import redact from "mongodb-redact"; | ||
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
import type { LoggingMessageNotification } from "@modelcontextprotocol/sdk/types.js"; | ||
import { EventEmitter } from "events"; | ||
import type { Server } from "../lib.js"; | ||
|
||
export type LogLevel = LoggingMessageNotification["params"]["level"]; | ||
|
||
|
@@ -250,19 +250,37 @@ export class DiskLogger extends LoggerBase<{ initialized: [] }> { | |
} | ||
|
||
export class McpLogger extends LoggerBase { | ||
public constructor(private readonly server: McpServer) { | ||
private static readonly LOG_LEVELS: LogLevel[] = [ | ||
"debug", | ||
"info", | ||
"notice", | ||
"warning", | ||
"error", | ||
"critical", | ||
"alert", | ||
"emergency", | ||
]; | ||
|
||
public constructor(private readonly server: Server) { | ||
super(); | ||
} | ||
|
||
protected readonly type: LoggerType = "mcp"; | ||
|
||
protected logCore(level: LogLevel, payload: LogPayload): void { | ||
// Only log if the server is connected | ||
if (!this.server?.isConnected()) { | ||
if (!this.server.mcpServer.isConnected()) { | ||
return; | ||
} | ||
|
||
void this.server.server.sendLoggingMessage({ | ||
const minimumLevel = McpLogger.LOG_LEVELS.indexOf(this.server.mcpLogLevel); | ||
const currentLevel = McpLogger.LOG_LEVELS.indexOf(level); | ||
if (minimumLevel > currentLevel) { | ||
Comment on lines
+276
to
+278
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comparison logic is inverted. If minimumLevel > currentLevel, it means the current level is actually lower priority than the minimum. The condition should be Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks wrong! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrong as the copilot suggestion is wrong or the code itself? |
||
// Don't log if the requested level is lower than the minimum level | ||
return; | ||
} | ||
|
||
void this.server.mcpServer.server.sendLoggingMessage({ | ||
level, | ||
data: `[${payload.context}]: ${payload.message}`, | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No validation for invalid log levels. If
this.server.mcpLogLevel
orlevel
contains an invalid value not in LOG_LEVELS, indexOf() will return -1, leading to incorrect filtering behavior. Add validation to handle invalid log levels gracefully.Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a valid one.