@modelcontextprotocol/sdk
    Preparing search index...

    Class Protocol<SendRequestT, SendNotificationT, SendResultT>Abstract

    Implements MCP protocol framing on top of a pluggable transport, including features like request/response linking, notifications, and progress.

    Type Parameters

    Hierarchy (View Summary)

    Index

    Constructors

    • Type Parameters

      • SendRequestT extends {
            method: string;
            params?: {
                _meta?: { progressToken?: string | number; [key: string]: unknown };
                [key: string]: unknown;
            };
        }
      • SendNotificationT extends {
            method: string;
            params?: { _meta?: { [key: string]: unknown }; [key: string]: unknown };
        }
      • SendResultT extends { _meta?: { [key: string]: unknown }; [key: string]: unknown }

      Parameters

      Returns Protocol<SendRequestT, SendNotificationT, SendResultT>

    Properties

    fallbackNotificationHandler?: (
        notification: {
            method: string;
            params?: { _meta?: { [key: string]: unknown }; [key: string]: unknown };
        },
    ) => Promise<void>

    A handler to invoke for any notification types that do not have their own handler installed.

    fallbackRequestHandler?: (
        request: {
            method: string;
            params?: {
                _meta?: { progressToken?: string | number; [key: string]: unknown };
                [key: string]: unknown;
            };
        },
    ) => Promise<SendResultT>

    A handler to invoke for any request types that do not have their own handler installed.

    onclose?: () => void

    Callback for when the connection is closed for any reason.

    This is invoked when close() is called as well.

    onerror?: (error: Error) => void

    Callback for when an error occurs.

    Note that errors are not necessarily fatal; they are used for reporting any kind of exceptional condition out of band.

    Accessors

    Methods

    • Asserts that a request handler has not already been set for the given method, in preparation for a new one being automatically installed.

      Parameters

      • method: string

      Returns void

    • A method to check if a capability is supported by the remote side, for the given method to be called.

      This should be implemented by subclasses.

      Parameters

      Returns void

    • A method to check if a notification is supported by the local side, for the given method to be sent.

      This should be implemented by subclasses.

      Parameters

      Returns void

    • A method to check if a request handler is supported by the local side, for the given method to be handled.

      This should be implemented by subclasses.

      Parameters

      • method: string

      Returns void

    • Attaches to the given transport, starts it, and starts listening for messages.

      The Protocol object assumes ownership of the Transport, replacing any callbacks that have already been set, and expects that it is the only user of the Transport instance going forward.

      Parameters

      Returns Promise<void>

    • Removes the notification handler for the given method.

      Parameters

      • method: string

      Returns void

    • Removes the request handler for the given method.

      Parameters

      • method: string

      Returns void

    • Sends a request and wait for a response.

      Do not use this method to emit notifications! Use notification() instead.

      Type Parameters

      • T extends ZodType<object, ZodTypeDef, object>

      Parameters

      Returns Promise<TypeOf<T>>

    • Registers a handler to invoke when this protocol object receives a notification with the given method.

      Note that this will replace any previous notification handler for the same method.

      Type Parameters

      • T extends ZodObject<
            { method: ZodLiteral<string> },
            UnknownKeysParam,
            ZodTypeAny,
            { method: string },
            { method: string },
        >

      Parameters

      • notificationSchema: T
      • handler: (notification: TypeOf<T>) => void | Promise<void>

      Returns void

    • Registers a handler to invoke when this protocol object receives a request with the given method.

      Note that this will replace any previous request handler for the same method.

      Type Parameters

      • T extends ZodObject<
            { method: ZodLiteral<string> },
            UnknownKeysParam,
            ZodTypeAny,
            { method: string },
            { method: string },
        >

      Parameters

      Returns void