fix: registerCapabilities registers logging/setLevel handler (#1605)
Co-authored-by: Konstantin Konstantinov <KKonstantinov@users.noreply.github.com>
This commit is contained in:
parent
4663a26f2a
commit
26c7242d67
1 changed files with 52 additions and 10 deletions
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in a new issue