Server

open class Server(serverInfo: Implementation, options: ServerOptions, instructionsProvider: () -> String? = null, block: Server.() -> Unit = {})(source)

An MCP server is responsible for storing features and handling new connections.

This server automatically responds to the initialization flow as initiated by the client. You can register tools, prompts, and resources using addTool, addPrompt, and addResource. The server will then automatically handle listing and retrieval requests from the client.

In case the server supports feature list notification or resource substitution, the server will automatically send notifications for all connected clients. Currently, after subscription to a resource, the server will NOT send the subscription confirmation as this response schema is not defined in the protocol.

Parameters

serverInfo

Information about this server implementation (name, version).

options

Configuration options for the server.

instructionsProvider

Optional provider for instructions from the server to the client about how to use this server. The provider is called each time a new session is started to support dynamic instructions.

block

A block to configure the mcp server.

Constructors

Link copied to clipboard
constructor(serverInfo: Implementation, options: ServerOptions, instructionsProvider: () -> String? = null, block: Server.() -> Unit = {})
constructor(serverInfo: Implementation, options: ServerOptions, instructions: String, block: Server.() -> Unit = {})

Alternative constructor that provides the instructions directly as a string.

Properties

Link copied to clipboard

Provides a snapshot of all prompts currently registered in the server

Link copied to clipboard

Provides a snapshot of all resources currently registered in the server

Link copied to clipboard

Provides a snapshot of all resource templates currently registered in the server.

Link copied to clipboard

Provides a snapshot of all sessions currently registered in the server

Link copied to clipboard

Provides a snapshot of all tools currently registered in the server

Functions

Link copied to clipboard
fun addPrompt(prompt: Prompt, promptProvider: suspend ClientConnection.(GetPromptRequest) -> GetPromptResult)

Registers a single prompt. The client can then retrieve the prompt.

fun addPrompt(name: String, description: String? = null, arguments: List<PromptArgument>? = null, promptProvider: suspend ClientConnection.(GetPromptRequest) -> GetPromptResult)

Registers a single prompt by constructing a Prompt from given parameters.

Link copied to clipboard
fun addPrompts(promptsToAdd: List<RegisteredPrompt>)

Registers multiple prompts at once.

Link copied to clipboard
fun addResource(uri: String, name: String, description: String, mimeType: String = "text/html", readHandler: suspend ClientConnection.(ReadResourceRequest) -> ReadResourceResult)

Registers a single resource. The client can then read the resource content.

Link copied to clipboard
fun addResources(resourcesToAdd: List<RegisteredResource>)

Registers multiple resources at once.

Link copied to clipboard

Registers a resource template. Clients can discover it via resources/templates/list and read matching URIs via resources/read.

fun addResourceTemplate(uriTemplate: String, name: String, description: String? = null, mimeType: String? = null, readHandler: suspend ClientConnection.(ReadResourceRequest, Map<String, String>) -> ReadResourceResult)

Registers a resource template by constructing a ResourceTemplate from given parameters.

Link copied to clipboard
fun addTool(tool: Tool, handler: suspend ClientConnection.(CallToolRequest) -> CallToolResult)
fun addTool(name: String, description: String, inputSchema: ToolSchema = ToolSchema(), title: String? = null, outputSchema: ToolSchema? = null, toolAnnotations: ToolAnnotations? = null, execution: ToolExecution? = null, meta: JsonObject? = null, handler: suspend ClientConnection.(CallToolRequest) -> CallToolResult)

Registers a single tool. The client can then call this tool.

Link copied to clipboard
fun addTools(toolsToAdd: List<RegisteredTool>)

Registers multiple tools at once.

Link copied to clipboard

Returns the ClientConnection for the session identified by sessionId.

Link copied to clipboard
suspend fun close()

Closes this server, shutting down the notification service and all active sessions.

Link copied to clipboard
suspend fun createElicitation(sessionId: String, message: String, requestedSchema: ElicitRequestParams.RequestedSchema, options: RequestOptions? = null): ElicitResult

Triggers ClientConnection.createElicitation request for session by provided sessionId.

suspend fun createElicitation(sessionId: String, message: String, elicitationId: String, url: String, options: RequestOptions? = null): ElicitResult

Triggers URL mode ClientConnection.createElicitation request for session by provided sessionId.

Link copied to clipboard
suspend fun createMessage(sessionId: String, params: CreateMessageRequest, options: RequestOptions? = null): CreateMessageResult

Triggers ClientConnection.createMessage request for session by provided sessionId.

Link copied to clipboard
suspend fun createSession(transport: Transport): ServerSession

Starts a new server session with the given transport and initializes internal request handlers based on the server's capabilities.

Link copied to clipboard
suspend fun listRoots(sessionId: String, params: JsonObject = EmptyJsonObject, options: RequestOptions? = null): ListRootsResult

Triggers ClientConnection.listRoots request for session by provided sessionId.

Link copied to clipboard
fun onClose(block: () -> Unit)

Registers a callback to be invoked when the server connection is closing.

Link copied to clipboard
fun onConnect(block: () -> Unit)

Registers a callback to be invoked when the new server session connected.

Link copied to clipboard
suspend fun ping(sessionId: String): EmptyResult

Triggers ClientConnection.ping request for session by provided sessionId.

Link copied to clipboard

Removes the notification handler for the given method from all active sessions.

fun removeNotificationHandler(sessionId: String, method: Method)

Removes the notification handler for the given method from a specific session.

Link copied to clipboard

Removes a single prompt by name.

Link copied to clipboard
fun removePrompts(promptNames: List<String>): Int

Removes multiple prompts at once.

Link copied to clipboard

Removes a single resource by URI.

Link copied to clipboard

Removes multiple resources at once.

Link copied to clipboard

Removes a resource template by its URI template string.

Link copied to clipboard

Removes a single tool by name.

Link copied to clipboard
fun removeTools(toolNames: List<String>): Int

Removes multiple tools at once.

Link copied to clipboard
suspend fun sendElicitationComplete(sessionId: String, notification: ElicitationCompleteNotification)

Triggers ClientConnection.sendElicitationComplete for session by provided sessionId.

Link copied to clipboard
suspend fun sendLoggingMessage(sessionId: String, notification: LoggingMessageNotification)

Triggers ClientConnection.sendLoggingMessage for session by provided sessionId.

Link copied to clipboard
suspend fun sendPromptListChanged(sessionId: String)

Triggers ClientConnection.sendPromptListChanged for session by provided sessionId.

Link copied to clipboard
suspend fun sendResourceListChanged(sessionId: String)

Triggers ClientConnection.sendResourceListChanged for session by provided sessionId.

Link copied to clipboard
suspend fun sendResourceUpdated(sessionId: String, notification: ResourceUpdatedNotification)

Triggers ClientConnection.sendResourceUpdated for session by provided sessionId.

Link copied to clipboard
suspend fun sendToolListChanged(sessionId: String)

Triggers ClientConnection.sendToolListChanged for session by provided sessionId.

Link copied to clipboard
fun <T : Notification> setNotificationHandler(method: Method, handler: (notification: T) -> Deferred<Unit>)

Registers a notification handler for the given method on all active sessions.

fun <T : Notification> setNotificationHandler(sessionId: String, method: Method, handler: (notification: T) -> Deferred<Unit>)

Registers a notification handler for the given method on a specific session.