Skip to content

Commit 184ce5a

Browse files
committed
* In src/server/index.ts
- In Server class - in SetLevelRequestSchema request handler - parse level against LoggingLevelSchema rather than checking obviated array - replace _levelNames array with LOG_LEVEL_SEVERITY map, mapped from LoggingLevelSchema.options - In isMessageIgnored method, - get the current level set for the given session id - if no level set, return false - otherwise, compare LOG_LEVEL_SEVERITY map entries for level and currentLevel, returning true if level is less than current level.
1 parent 657f6d7 commit 184ce5a

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/server/index.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ import {
3232
ServerRequest,
3333
ServerResult,
3434
SUPPORTED_PROTOCOL_VERSIONS,
35-
LoggingLevel, SetLevelRequestSchema,
35+
LoggingLevel,
36+
SetLevelRequestSchema,
37+
LoggingLevelSchema
3638
} from "../types.js";
3739
import Ajv from "ajv";
3840

@@ -114,33 +116,32 @@ export class Server<
114116
this.setRequestHandler(SetLevelRequestSchema, async (request, extra) => {
115117
const transportSessionId: string | undefined = extra.sessionId || extra.requestInfo?.headers['mcp-session-id'] as string || undefined;
116118
const { level } = request.params;
117-
if (transportSessionId && this._levelNames.some(l => l === level)) {
118-
this._loggingLevels.set(transportSessionId, level);
119+
const parseResult = LoggingLevelSchema.safeParse(level);
120+
if (transportSessionId && parseResult.success) {
121+
this._loggingLevels.set(transportSessionId, parseResult.data);
119122
}
120123
return {};
121124
})
122125
}
123126
}
124127

128+
// Map log levels by session id
125129
private _loggingLevels = new Map<string, LoggingLevel>();
126-
private _levelNames = [
127-
"debug",
128-
"info",
129-
"notice",
130-
"warning",
131-
"error",
132-
"critical",
133-
"alert",
134-
"emergency",
135-
];
136130

131+
// Map LogLevelSchema to severity index
132+
private readonly LOG_LEVEL_SEVERITY = new Map(
133+
LoggingLevelSchema.options.map((level, index) => [level, index])
134+
);
135+
136+
// Is a message with the given level ignored in the log level set for the given session id?
137137
private isMessageIgnored = (level: LoggingLevel, sessionId: string): boolean => {
138-
const currentLevel = this._levelNames.findIndex((l) => this._loggingLevels.get(sessionId) === l);
139-
const messageLevel = this._levelNames.findIndex((l) => level === l);
140-
return messageLevel < currentLevel;
141-
};
138+
const currentLevel = this._loggingLevels.get(sessionId);
139+
return (currentLevel)
140+
? this.LOG_LEVEL_SEVERITY.get(level)! < this.LOG_LEVEL_SEVERITY.get(currentLevel)!
141+
: false;
142+
};
142143

143-
/**
144+
/**
144145
* Registers new capabilities. This can only be called before connecting to a transport.
145146
*
146147
* The new capabilities will be merged with any existing capabilities previously given (e.g., at initialization).

0 commit comments

Comments
 (0)