@modelcontextprotocol/sdk
    Preparing search index...

    Class Server<RequestT, NotificationT, ResultT>

    An MCP server on top of a pluggable transport.

    This server will automatically respond to the initialization flow as initiated from the client.

    To use with custom types, extend the base Request/Notification/Result types and pass them as type parameters:

    // Custom schemas
    const CustomRequestSchema = RequestSchema.extend({...})
    const CustomNotificationSchema = NotificationSchema.extend({...})
    const CustomResultSchema = ResultSchema.extend({...})

    // Type aliases
    type CustomRequest = z.infer<typeof CustomRequestSchema>
    type CustomNotification = z.infer<typeof CustomNotificationSchema>
    type CustomResult = z.infer<typeof CustomResultSchema>

    // Create typed server
    const server = new Server<CustomRequest, CustomNotification, CustomResult>({
    name: "CustomServer",
    version: "1.0.0"
    })

    Type Parameters

    Hierarchy (View Summary)

    Index

    Constructors

    • Initializes this server with the given name and version information.

      Type Parameters

      • RequestT extends {
            method: string;
            params?: {
                _meta?: { progressToken?: string | number; [key: string]: unknown };
                [key: string]: unknown;
            };
        } = {
            method: string;
            params?: {
                _meta?: { progressToken?: string | number; [key: string]: unknown };
                [key: string]: unknown;
            };
        }
      • NotificationT extends {
            method: string;
            params?: { _meta?: { [key: string]: unknown }; [key: string]: unknown };
        } = {
            method: string;
            params?: { _meta?: { [key: string]: unknown }; [key: string]: unknown };
        }
      • ResultT extends { _meta?: { [key: string]: unknown }; [key: string]: unknown } = { _meta?: { [key: string]: unknown }; [key: string]: unknown }

      Parameters

      • _serverInfo: { name: string; title?: string; version: string; [key: string]: unknown }
        • [key: string]: unknown
        • name: string

          Intended for programmatic or logical use, but used as a display name in past specs or fallback

        • Optionaltitle?: string

          Intended for UI and end-user contexts — optimized to be human-readable and easily understood, even by those unfamiliar with domain-specific terminology.

          If not provided, the name should be used for display (except for Tool, where annotations.title should be given precedence over using name, if present).

        • version: string
      • Optionaloptions: ServerOptions

      Returns Server<RequestT, NotificationT, ResultT>

    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<ServerResult | ResultT>

    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.

    oninitialized?: () => void

    Callback for when initialization has fully completed (i.e., the client has sent an initialized notification).

    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 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>

    • Parameters

      • params: {
            _meta?: { progressToken?: string | number; [key: string]: unknown };
            includeContext?: "none" | "thisServer" | "allServers";
            maxTokens: number;
            messages: {
                content:
                    | {
                        _meta?: { [key: string]: unknown };
                        text: string;
                        type: "text";
                        [key: string]: unknown;
                    }
                    | {
                        _meta?: { [key: string]: unknown };
                        data: string;
                        mimeType: string;
                        type: "image";
                        [key: string]: unknown;
                    }
                    | {
                        _meta?: { [key: string]: unknown };
                        data: string;
                        mimeType: string;
                        type: "audio";
                        [key: string]: unknown;
                    };
                role: "user"
                | "assistant";
                [key: string]: unknown;
            }[];
            metadata?: { [key: string]: unknown };
            modelPreferences?: {
                costPriority?: number;
                hints?: { name?: string; [key: string]: unknown }[];
                intelligencePriority?: number;
                speedPriority?: number;
                [key: string]: unknown;
            };
            stopSequences?: string[];
            systemPrompt?: string;
            temperature?: number;
            [key: string]: unknown;
        }
        • [key: string]: unknown
        • Optional_meta?: { progressToken?: string | number; [key: string]: unknown }
          • OptionalprogressToken?: string | number

            If specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.

        • OptionalincludeContext?: "none" | "thisServer" | "allServers"

          A request to include context from one or more MCP servers (including the caller), to be attached to the prompt. The client MAY ignore this request.

        • maxTokens: number

          The maximum number of tokens to sample, as requested by the server. The client MAY choose to sample fewer tokens than requested.

        • messages: {
              content:
                  | {
                      _meta?: { [key: string]: unknown };
                      text: string;
                      type: "text";
                      [key: string]: unknown;
                  }
                  | {
                      _meta?: { [key: string]: unknown };
                      data: string;
                      mimeType: string;
                      type: "image";
                      [key: string]: unknown;
                  }
                  | {
                      _meta?: { [key: string]: unknown };
                      data: string;
                      mimeType: string;
                      type: "audio";
                      [key: string]: unknown;
                  };
              role: "user"
              | "assistant";
              [key: string]: unknown;
          }[]
        • Optionalmetadata?: { [key: string]: unknown }

          Optional metadata to pass through to the LLM provider. The format of this metadata is provider-specific.

        • OptionalmodelPreferences?: {
              costPriority?: number;
              hints?: { name?: string; [key: string]: unknown }[];
              intelligencePriority?: number;
              speedPriority?: number;
              [key: string]: unknown;
          }

          The server's preferences for which model to select.

          • OptionalcostPriority?: number

            How much to prioritize cost when selecting a model.

          • Optionalhints?: { name?: string; [key: string]: unknown }[]

            Optional hints to use for model selection.

          • OptionalintelligencePriority?: number

            How much to prioritize intelligence and capabilities when selecting a model.

          • OptionalspeedPriority?: number

            How much to prioritize sampling speed (latency) when selecting a model.

        • OptionalstopSequences?: string[]
        • OptionalsystemPrompt?: string

          An optional system prompt the server wants to use for sampling. The client MAY modify or omit this prompt.

        • Optionaltemperature?: number
      • Optionaloptions: RequestOptions

      Returns Promise<
          objectOutputType<
              extendShape<
                  {
                      _meta: ZodOptional<
                          ZodObject<
                              {},
                              "passthrough",
                              ZodTypeAny,
                              objectOutputType<{}, ZodTypeAny, "passthrough">,
                              objectInputType<{}, ZodTypeAny, "passthrough">,
                          >,
                      >;
                  },
                  {
                      content: ZodDiscriminatedUnion<
                          "type",
                          [
                              ZodObject<
                                  {
                                      _meta: ZodOptional<
                                          ZodObject<
                                              {},
                                              "passthrough",
                                              ZodTypeAny,
                                              objectOutputType<(...), (...), (...)>,
                                              objectInputType<(...), (...), (...)>,
                                          >,
                                      >;
                                      text: ZodString;
                                      type: ZodLiteral<"text">;
                                  },
                                  "passthrough",
                                  ZodTypeAny,
                                  objectOutputType<
                                      {
                                          _meta: ZodOptional<ZodObject<(...), (...), (...), (...), (...)>>;
                                          text: ZodString;
                                          type: ZodLiteral<"text">;
                                      },
                                      ZodTypeAny,
                                      "passthrough",
                                  >,
                                  objectInputType<
                                      {
                                          _meta: ZodOptional<ZodObject<(...), (...), (...), (...), (...)>>;
                                          text: ZodString;
                                          type: ZodLiteral<"text">;
                                      },
                                      ZodTypeAny,
                                      "passthrough",
                                  >,
                              >,
                              ZodObject<
                                  {
                                      _meta: ZodOptional<
                                          ZodObject<
                                              {},
                                              "passthrough",
                                              ZodTypeAny,
                                              objectOutputType<(...), (...), (...)>,
                                              objectInputType<(...), (...), (...)>,
                                          >,
                                      >;
                                      data: ZodString;
                                      mimeType: ZodString;
                                      type: ZodLiteral<"image">;
                                  },
                                  "passthrough",
                                  ZodTypeAny,
                                  objectOutputType<
                                      {
                                          _meta: ZodOptional<ZodObject<(...), (...), (...), (...), (...)>>;
                                          data: ZodString;
                                          mimeType: ZodString;
                                          type: ZodLiteral<"image">;
                                      },
                                      ZodTypeAny,
                                      "passthrough",
                                  >,
                                  objectInputType<
                                      {
                                          _meta: ZodOptional<ZodObject<(...), (...), (...), (...), (...)>>;
                                          data: ZodString;
                                          mimeType: ZodString;
                                          type: ZodLiteral<"image">;
                                      },
                                      ZodTypeAny,
                                      "passthrough",
                                  >,
                              >,
                              ZodObject<
                                  {
                                      _meta: ZodOptional<
                                          ZodObject<
                                              {},
                                              "passthrough",
                                              ZodTypeAny,
                                              objectOutputType<(...), (...), (...)>,
                                              objectInputType<(...), (...), (...)>,
                                          >,
                                      >;
                                      data: ZodString;
                                      mimeType: ZodString;
                                      type: ZodLiteral<"audio">;
                                  },
                                  "passthrough",
                                  ZodTypeAny,
                                  objectOutputType<
                                      {
                                          _meta: ZodOptional<ZodObject<(...), (...), (...), (...), (...)>>;
                                          data: ZodString;
                                          mimeType: ZodString;
                                          type: ZodLiteral<"audio">;
                                      },
                                      ZodTypeAny,
                                      "passthrough",
                                  >,
                                  objectInputType<
                                      {
                                          _meta: ZodOptional<ZodObject<(...), (...), (...), (...), (...)>>;
                                          data: ZodString;
                                          mimeType: ZodString;
                                          type: ZodLiteral<"audio">;
                                      },
                                      ZodTypeAny,
                                      "passthrough",
                                  >,
                              >,
                          ],
                      >;
                      model: ZodString;
                      role: ZodEnum<["user", "assistant"]>;
                      stopReason: ZodOptional<
                          ZodUnion<
                              [ZodEnum<["endTurn", "stopSequence", "maxTokens"]>, ZodString],
                          >,
                      >;
                  },
              >,
              ZodTypeAny,
              "passthrough",
          >,
      >

    • Parameters

      • params: {
            _meta?: { progressToken?: string | number; [key: string]: unknown };
            message: string;
            requestedSchema: {
                properties: {
                    [key: string]:
                        | {
                            default?: boolean;
                            description?: string;
                            title?: string;
                            type: "boolean";
                            [key: string]: unknown;
                        }
                        | {
                            description?: string;
                            format?: "date"
                            | "uri"
                            | "email"
                            | "date-time";
                            maxLength?: number;
                            minLength?: number;
                            title?: string;
                            type: "string";
                            [key: string]: unknown;
                        }
                        | {
                            description?: string;
                            maximum?: number;
                            minimum?: number;
                            title?: string;
                            type: "number"
                            | "integer";
                            [key: string]: unknown;
                        }
                        | {
                            description?: string;
                            enum: string[];
                            enumNames?: string[];
                            title?: string;
                            type: "string";
                            [key: string]: unknown;
                        };
                };
                required?: string[];
                type: "object";
                [key: string]: unknown;
            };
            [key: string]: unknown;
        }
        • [key: string]: unknown
        • Optional_meta?: { progressToken?: string | number; [key: string]: unknown }
          • OptionalprogressToken?: string | number

            If specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.

        • message: string

          The message to present to the user.

        • requestedSchema: {
              properties: {
                  [key: string]:
                      | {
                          default?: boolean;
                          description?: string;
                          title?: string;
                          type: "boolean";
                          [key: string]: unknown;
                      }
                      | {
                          description?: string;
                          format?: "date"
                          | "uri"
                          | "email"
                          | "date-time";
                          maxLength?: number;
                          minLength?: number;
                          title?: string;
                          type: "string";
                          [key: string]: unknown;
                      }
                      | {
                          description?: string;
                          maximum?: number;
                          minimum?: number;
                          title?: string;
                          type: "number"
                          | "integer";
                          [key: string]: unknown;
                      }
                      | {
                          description?: string;
                          enum: string[];
                          enumNames?: string[];
                          title?: string;
                          type: "string";
                          [key: string]: unknown;
                      };
              };
              required?: string[];
              type: "object";
              [key: string]: unknown;
          }

          The schema for the requested user input.

      • Optionaloptions: RequestOptions

      Returns Promise<
          {
              _meta?: { [key: string]: unknown };
              action: "accept" | "decline" | "cancel";
              content?: { [key: string]: unknown };
              [key: string]: unknown;
          },
      >

    • After initialization has completed, this will be populated with the client's reported capabilities.

      Returns
          | {
              elicitation?: { [key: string]: unknown };
              experimental?: { [key: string]: unknown };
              roots?: { listChanged?: boolean; [key: string]: unknown };
              sampling?: { [key: string]: unknown };
              [key: string]: unknown;
          }
          | undefined

      • {
            elicitation?: { [key: string]: unknown };
            experimental?: { [key: string]: unknown };
            roots?: { listChanged?: boolean; [key: string]: unknown };
            sampling?: { [key: string]: unknown };
            [key: string]: unknown;
        }
        • [key: string]: unknown
        • Optionalelicitation?: { [key: string]: unknown }

          Present if the client supports eliciting user input.

        • Optionalexperimental?: { [key: string]: unknown }

          Experimental, non-standard capabilities that the client supports.

        • Optionalroots?: { listChanged?: boolean; [key: string]: unknown }

          Present if the client supports listing roots.

          • OptionallistChanged?: boolean

            Whether the client supports issuing notifications for changes to the roots list.

        • Optionalsampling?: { [key: string]: unknown }

          Present if the client supports sampling from an LLM.

      • undefined
    • After initialization has completed, this will be populated with information about the client's name and version.

      Returns
          | {
              name: string;
              title?: string;
              version: string;
              [key: string]: unknown;
          }
          | undefined

      • { name: string; title?: string; version: string; [key: string]: unknown }
        • [key: string]: unknown
        • name: string

          Intended for programmatic or logical use, but used as a display name in past specs or fallback

        • Optionaltitle?: string

          Intended for UI and end-user contexts — optimized to be human-readable and easily understood, even by those unfamiliar with domain-specific terminology.

          If not provided, the name should be used for display (except for Tool, where annotations.title should be given precedence over using name, if present).

        • version: string
      • undefined
    • Parameters

      • Optionalparams: {
            _meta?: { progressToken?: string | number; [key: string]: unknown };
            [key: string]: unknown;
        }
      • Optionaloptions: RequestOptions

      Returns Promise<
          objectOutputType<
              extendShape<
                  {
                      _meta: ZodOptional<
                          ZodObject<
                              {},
                              "passthrough",
                              ZodTypeAny,
                              objectOutputType<{}, ZodTypeAny, "passthrough">,
                              objectInputType<{}, ZodTypeAny, "passthrough">,
                          >,
                      >;
                  },
                  {
                      roots: ZodArray<
                          ZodObject<
                              {
                                  _meta: ZodOptional<
                                      ZodObject<
                                          {},
                                          "passthrough",
                                          ZodTypeAny,
                                          objectOutputType<{}, ZodTypeAny, "passthrough">,
                                          objectInputType<{}, ZodTypeAny, "passthrough">,
                                      >,
                                  >;
                                  name: ZodOptional<ZodString>;
                                  uri: ZodString;
                              },
                              "passthrough",
                              ZodTypeAny,
                              objectOutputType<
                                  {
                                      _meta: ZodOptional<
                                          ZodObject<
                                              {},
                                              "passthrough",
                                              ZodTypeAny,
                                              objectOutputType<(...), (...), (...)>,
                                              objectInputType<(...), (...), (...)>,
                                          >,
                                      >;
                                      name: ZodOptional<ZodString>;
                                      uri: ZodString;
                                  },
                                  ZodTypeAny,
                                  "passthrough",
                              >,
                              objectInputType<
                                  {
                                      _meta: ZodOptional<
                                          ZodObject<
                                              {},
                                              "passthrough",
                                              ZodTypeAny,
                                              objectOutputType<(...), (...), (...)>,
                                              objectInputType<(...), (...), (...)>,
                                          >,
                                      >;
                                      name: ZodOptional<ZodString>;
                                      uri: ZodString;
                                  },
                                  ZodTypeAny,
                                  "passthrough",
                              >,
                          >,
                          "many",
                      >;
                  },
              >,
              ZodTypeAny,
              "passthrough",
          >,
      >

    • Returns Promise<{ _meta?: objectOutputType<{}, ZodTypeAny, "passthrough"> }>

    • Registers new capabilities. This can only be called before connecting to a transport.

      The new capabilities will be merged with any existing capabilities previously given (e.g., at initialization).

      Parameters

      • capabilities: {
            completions?: { [key: string]: unknown };
            experimental?: { [key: string]: unknown };
            logging?: { [key: string]: unknown };
            prompts?: { listChanged?: boolean; [key: string]: unknown };
            resources?: {
                listChanged?: boolean;
                subscribe?: boolean;
                [key: string]: unknown;
            };
            tools?: { listChanged?: boolean; [key: string]: unknown };
            [key: string]: unknown;
        }
        • [key: string]: unknown
        • Optionalcompletions?: { [key: string]: unknown }

          Present if the server supports sending completions to the client.

        • Optionalexperimental?: { [key: string]: unknown }

          Experimental, non-standard capabilities that the server supports.

        • Optionallogging?: { [key: string]: unknown }

          Present if the server supports sending log messages to the client.

        • Optionalprompts?: { listChanged?: boolean; [key: string]: unknown }

          Present if the server offers any prompt templates.

          • OptionallistChanged?: boolean

            Whether this server supports issuing notifications for changes to the prompt list.

        • Optionalresources?: { listChanged?: boolean; subscribe?: boolean; [key: string]: unknown }

          Present if the server offers any resources to read.

          • OptionallistChanged?: boolean

            Whether this server supports issuing notifications for changes to the resource list.

          • Optionalsubscribe?: boolean

            Whether this server supports clients subscribing to resource updates.

        • Optionaltools?: { listChanged?: boolean; [key: string]: unknown }

          Present if the server offers any tools to call.

          • OptionallistChanged?: boolean

            Whether this server supports issuing notifications for changes to the tool list.

      Returns void

    • Parameters

      • params: {
            _meta?: { [key: string]: unknown };
            data?: unknown;
            level:
                | "error"
                | "debug"
                | "info"
                | "notice"
                | "warning"
                | "critical"
                | "alert"
                | "emergency";
            logger?: string;
            [key: string]: unknown;
        }
        • [key: string]: unknown
        • Optional_meta?: { [key: string]: unknown }

          See MCP specification for notes on _meta usage.

        • Optionaldata?: unknown

          The data to be logged, such as a string message or an object. Any JSON serializable type is allowed here.

        • level:
              | "error"
              | "debug"
              | "info"
              | "notice"
              | "warning"
              | "critical"
              | "alert"
              | "emergency"

          The severity of this log message.

        • Optionallogger?: string

          An optional name of the logger issuing this message.

      Returns Promise<void>

    • Parameters

      • params: { _meta?: { [key: string]: unknown }; uri: string; [key: string]: unknown }
        • [key: string]: unknown
        • Optional_meta?: { [key: string]: unknown }

          See MCP specification for notes on _meta usage.

        • uri: string

          The URI of the resource that has been updated. This might be a sub-resource of the one that the client actually subscribed to.

      Returns Promise<void>

    • 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