add server capabilities derivation

This commit is contained in:
Ashwin Bhat 2024-10-15 17:08:08 -07:00
commit bcb4b219ee
No known key found for this signature in database
2 changed files with 46 additions and 10 deletions

View file

@ -1,6 +1,7 @@
import { Protocol } from "../shared/protocol.js";
import {
ClientCapabilities,
ClientRequest,
Implementation,
InitializedNotificationSchema,
InitializeRequest,
@ -10,6 +11,11 @@ import {
ServerNotification,
ServerRequest,
ServerResult,
ServerCapabilities,
ListResourcesRequestSchema,
ListToolsRequestSchema,
ListPromptsRequestSchema,
SetLevelRequestSchema,
} from "../types.js";
/**
@ -58,7 +64,7 @@ export class Server extends Protocol<
return {
protocolVersion: PROTOCOL_VERSION,
capabilities: {},
capabilities: this.getCapabilities(),
serverInfo: this._serverInfo,
};
}
@ -76,4 +82,21 @@ export class Server extends Protocol<
getClientVersion(): Implementation | undefined {
return this._clientVersion;
}
private getCapability(
reqType: ClientRequest["method"],
): ServerCapabilities[keyof ServerCapabilities] {
return this._requestHandlers.has(reqType as string) ? {} : undefined;
}
private getCapabilities(): ServerCapabilities {
return {
prompts: this.getCapability(ListPromptsRequestSchema.shape.method.value),
resources: this.getCapability(
ListResourcesRequestSchema.shape.method.value,
),
tools: this.getCapability(ListToolsRequestSchema.shape.method.value),
logging: this.getCapability(SetLevelRequestSchema.shape.method.value),
};
}
}

View file

@ -32,7 +32,7 @@ export class Protocol<
> {
private _transport?: Transport;
private _requestMessageId = 0;
private _requestHandlers: Map<
protected _requestHandlers: Map<
string,
(request: JSONRPCRequest) => Promise<SendResultT>
> = new Map();