fix: registerCapabilities registers logging/setLevel handler (#1605)

Co-authored-by: Konstantin Konstantinov <KKonstantinov@users.noreply.github.com>
This commit is contained in:
Giulio Leone 2026-03-05 10:28:32 +01:00 • committed by GitHub
commit 26c7242d67
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -115,19 +115,23 @@ export class Server extends Protocol<ServerContext> {
this.setNotificationHandler('notifications/initialized', () => this.oninitialized?.());
if (this._capabilities.logging) {
this.setRequestHandler('logging/setLevel', async (request, ctx) => {
const transportSessionId: string | undefined =
ctx.sessionId || (ctx.http?.req?.headers.get('mcp-session-id') as string) || undefined;
const { level } = request.params;
const parseResult = parseSchema(LoggingLevelSchema, level);
if (parseResult.success) {
this._loggingLevels.set(transportSessionId, parseResult.data);
}
return {};
});
this._registerLoggingHandler();
}
}
private _registerLoggingHandler(): void {
this.setRequestHandler('logging/setLevel', async (request, ctx) => {
const transportSessionId: string | undefined =
ctx.sessionId || (ctx.http?.req?.headers.get('mcp-session-id') as string) || undefined;
const { level } = request.params;
const parseResult = parseSchema(LoggingLevelSchema, level);
if (parseResult.success) {
this._loggingLevels.set(transportSessionId, parseResult.data);
}
return {};
});
}
protected override buildContext(ctx: BaseContext, transportInfo?: MessageExtraInfo): ServerContext {
// Only create http when there's actual HTTP transport info or auth info
const hasHttpInfo =
@ -188,7 +192,11 @@ export class Server extends Protocol<ServerContext> {
if (this.transport) {
throw new SdkError(SdkErrorCode.AlreadyConnected, 'Cannot register capabilities after connecting to transport');
}
const hadLogging = !!this._capabilities.logging;
this._capabilities = mergeCapabilities(this._capabilities, capabilities);
if (!hadLogging && this._capabilities.logging) {
this._registerLoggingHandler();
}
}
/**