feat: support extending McpServer with subclass

This commit is contained in:
wangshijun 2025-04-01 12:21:08 +08:00
commit 3571fc353f
2 changed files with 27 additions and 19 deletions

View file

@ -93,6 +93,11 @@ export type RequestHandlerExtra = {
* The session ID from the transport, if available.
*/
sessionId?: string;
/**
* The authenticated user, if available.
*/
user?: unknown;
};
/**
@ -316,6 +321,7 @@ export abstract class Protocol<
const extra: RequestHandlerExtra = {
signal: abortController.signal,
sessionId: this._transport?.sessionId,
user: this._transport?.user,
};
// Starting with Promise.resolve() puts any synchronous errors into the monad as well.
@ -361,7 +367,7 @@ export abstract class Protocol<
private _onprogress(notification: ProgressNotification): void {
const { progressToken, ...params } = notification.params;
const messageId = Number(progressToken);
const handler = this._progressHandlers.get(messageId);
if (!handler) {
this._onerror(new Error(`Received a progress notification for an unknown token: ${JSON.stringify(notification)}`));

View file

@ -46,4 +46,9 @@ export interface Transport {
* The session ID generated for this connection.
*/
sessionId?: string;
/**
* The authenticated user for this transport session.
*/
user?: unknown;
}