Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-register-capabilities-logging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modelcontextprotocol/server': patch
---

Fix `registerCapabilities` to properly register the `logging/setLevel` request handler when logging capability is added after construction
36 changes: 26 additions & 10 deletions packages/server/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,7 @@ 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();
}
}

Expand Down Expand Up @@ -179,6 +170,27 @@ export class Server extends Protocol<ServerContext> {
return currentLevel ? this.LOG_LEVEL_SEVERITY.get(level)! < this.LOG_LEVEL_SEVERITY.get(currentLevel)! : false;
};

private _loggingHandlerRegistered = false;

private registerLoggingHandler(): void {
if (this._loggingHandlerRegistered) {
return;
}

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._loggingHandlerRegistered = true;
}

/**
* Registers new capabilities. This can only be called before connecting to a transport.
*
Expand All @@ -189,6 +201,10 @@ export class Server extends Protocol<ServerContext> {
throw new SdkError(SdkErrorCode.AlreadyConnected, 'Cannot register capabilities after connecting to transport');
}
this._capabilities = mergeCapabilities(this._capabilities, capabilities);

if (this._capabilities.logging) {
this.registerLoggingHandler();
}
}

/**
Expand Down
29 changes: 29 additions & 0 deletions test/integration/test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,35 @@ test('should only allow setRequestHandler for declared capabilities', () => {
}).toThrow(/^Server does not support logging/);
});

test('should register logging/setLevel handler when logging capability is added via registerCapabilities', async () => {
const server = new Server({
name: 'test server',
version: '1.0'
});

// Register logging capability after construction
server.registerCapabilities({ logging: {} });

const client = new Client({
name: 'test client',
version: '1.0'
});

const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
await Promise.all([client.connect(clientTransport), server.connect(serverTransport)]);

// Client sets logging level - should work without error
await expect(client.setLoggingLevel('warning')).resolves.not.toThrow();

// Sending a log message should also work
await expect(
server.sendLoggingMessage({
level: 'warning',
data: 'Test log message'
})
).resolves.not.toThrow();
});

test('should handle server cancelling a request', async () => {
const server = new Server(
{
Expand Down
Loading