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
2 changed files with 52 additions and 10 deletions

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();
}
}
/**

View file

@ -3705,3 +3705,37 @@ describe('elicitInputStream', () => {
});
});
});
describe('Server registerCapabilities with logging', () => {
test('registerCapabilities should register logging/setLevel handler', async () => {
const server = new Server({ name: 'test-server', version: '1.0.0' });
server.registerCapabilities({ logging: {} });
const client = new Client({ name: 'test-client', version: '1.0.0' });
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
await server.connect(serverTransport);
await client.connect(clientTransport);
// logging/setLevel should succeed, not throw "Method not found"
await expect(client.setLoggingLevel('error')).resolves.not.toThrow();
await client.close();
await server.close();
});
test('logging in constructor capabilities should register logging/setLevel handler', async () => {
const server = new Server({ name: 'test-server', version: '1.0.0' }, { capabilities: { logging: {} } });
const client = new Client({ name: 'test-client', version: '1.0.0' });
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
await server.connect(serverTransport);
await client.connect(clientTransport);
await expect(client.setLoggingLevel('error')).resolves.not.toThrow();
await client.close();
await server.close();
});
});