Skip to content

Commit 26c7242

Browse files
fix: registerCapabilities registers logging/setLevel handler (modelcontextprotocol#1605)
Co-authored-by: Konstantin Konstantinov <KKonstantinov@users.noreply.github.com>
1 parent 4663a26 commit 26c7242

2 files changed

Lines changed: 52 additions & 10 deletions

File tree

packages/server/src/server/server.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,23 @@ export class Server extends Protocol<ServerContext> {
115115
this.setNotificationHandler('notifications/initialized', () => this.oninitialized?.());
116116

117117
if (this._capabilities.logging) {
118-
this.setRequestHandler('logging/setLevel', async (request, ctx) => {
119-
const transportSessionId: string | undefined =
120-
ctx.sessionId || (ctx.http?.req?.headers.get('mcp-session-id') as string) || undefined;
121-
const { level } = request.params;
122-
const parseResult = parseSchema(LoggingLevelSchema, level);
123-
if (parseResult.success) {
124-
this._loggingLevels.set(transportSessionId, parseResult.data);
125-
}
126-
return {};
127-
});
118+
this._registerLoggingHandler();
128119
}
129120
}
130121

122+
private _registerLoggingHandler(): void {
123+
this.setRequestHandler('logging/setLevel', async (request, ctx) => {
124+
const transportSessionId: string | undefined =
125+
ctx.sessionId || (ctx.http?.req?.headers.get('mcp-session-id') as string) || undefined;
126+
const { level } = request.params;
127+
const parseResult = parseSchema(LoggingLevelSchema, level);
128+
if (parseResult.success) {
129+
this._loggingLevels.set(transportSessionId, parseResult.data);
130+
}
131+
return {};
132+
});
133+
}
134+
131135
protected override buildContext(ctx: BaseContext, transportInfo?: MessageExtraInfo): ServerContext {
132136
// Only create http when there's actual HTTP transport info or auth info
133137
const hasHttpInfo =
@@ -188,7 +192,11 @@ export class Server extends Protocol<ServerContext> {
188192
if (this.transport) {
189193
throw new SdkError(SdkErrorCode.AlreadyConnected, 'Cannot register capabilities after connecting to transport');
190194
}
195+
const hadLogging = !!this._capabilities.logging;
191196
this._capabilities = mergeCapabilities(this._capabilities, capabilities);
197+
if (!hadLogging && this._capabilities.logging) {
198+
this._registerLoggingHandler();
199+
}
192200
}
193201

194202
/**

test/integration/test/server.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3705,3 +3705,37 @@ describe('elicitInputStream', () => {
37053705
});
37063706
});
37073707
});
3708+
3709+
describe('Server registerCapabilities with logging', () => {
3710+
test('registerCapabilities should register logging/setLevel handler', async () => {
3711+
const server = new Server({ name: 'test-server', version: '1.0.0' });
3712+
server.registerCapabilities({ logging: {} });
3713+
3714+
const client = new Client({ name: 'test-client', version: '1.0.0' });
3715+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
3716+
3717+
await server.connect(serverTransport);
3718+
await client.connect(clientTransport);
3719+
3720+
// logging/setLevel should succeed, not throw "Method not found"
3721+
await expect(client.setLoggingLevel('error')).resolves.not.toThrow();
3722+
3723+
await client.close();
3724+
await server.close();
3725+
});
3726+
3727+
test('logging in constructor capabilities should register logging/setLevel handler', async () => {
3728+
const server = new Server({ name: 'test-server', version: '1.0.0' }, { capabilities: { logging: {} } });
3729+
3730+
const client = new Client({ name: 'test-client', version: '1.0.0' });
3731+
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
3732+
3733+
await server.connect(serverTransport);
3734+
await client.connect(clientTransport);
3735+
3736+
await expect(client.setLoggingLevel('error')).resolves.not.toThrow();
3737+
3738+
await client.close();
3739+
await server.close();
3740+
});
3741+
});

0 commit comments

Comments
 (0)