Server

open class Server(serverInfo: Implementation, options: ServerOptions, var onCloseCallback: () -> Unit? = null) : Protocol<ServerRequest, ServerNotification, ServerResult> (source)

An MCP server on top of a pluggable transport.

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.

Parameters

serverInfo

Information about this server implementation (name, version).

options

Configuration options for the server.

onCloseCallback

A callback invoked when the server connection closes.

Constructors

Link copied to clipboard
constructor(serverInfo: Implementation, options: ServerOptions, onCloseCallback: () -> Unit? = null)

Properties

Link copied to clipboard

The client's reported capabilities after initialization.

Link copied to clipboard

The client's version information after initialization.

Link copied to clipboard
var fallbackNotificationHandler: suspend (notification: JSONRPCNotification) -> Unit?

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

Link copied to clipboard

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

Link copied to clipboard
Link copied to clipboard
var onCloseCallback: () -> Unit?
Link copied to clipboard
var onInitialized: () -> Unit?

A callback invoked when the server has completed the initialization sequence. After initialization, the server is ready to handle requests.

Link copied to clipboard

Functions

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

Registers a single prompt. The prompt can then be retrieved by the client.

fun addPrompt(name: String, description: String? = null, arguments: List<PromptArgument>? = null, promptProvider: suspend (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 (ReadResourceRequest) -> ReadResourceResult)

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

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

Registers multiple resources at once.

Link copied to clipboard
fun addTool(name: String, description: String, inputSchema: Tool.Input = Tool.Input(), handler: suspend (CallToolRequest) -> CallToolResult)

Registers a single tool. This tool can then be called by the client.

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

Registers multiple tools at once.

Link copied to clipboard
open override fun assertCapabilityForMethod(method: Method)

Asserts that the client supports the capability required for the given method.

Link copied to clipboard
open override fun assertRequestHandlerCapability(method: Method)

Asserts that the server can handle the specified request method.

Link copied to clipboard
suspend fun close()

Closes the connection.

Link copied to clipboard
open suspend fun connect(transport: Transport)

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

Link copied to clipboard

Creates a message using the server's sampling capability.

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

Lists the available "roots" from the client's perspective (if supported).

Link copied to clipboard
suspend fun notification(notification: ServerNotification)

Emits a notification, which is a one-way message that does not expect a response.

Link copied to clipboard
open override fun onclose()

Called when the server connection is closing. Invokes onCloseCallback if set.

Link copied to clipboard
open fun onerror(error: Throwable)

Callback for when an error occurs.

Link copied to clipboard
suspend fun ping(): EmptyRequestResult

Sends a ping request to the client to check connectivity.

Link copied to clipboard

Removes the notification handler for the given method.

Link copied to clipboard

Removes the request handler for the given method.

Link copied to clipboard
suspend fun <T : RequestResult> request(request: ServerRequest, options: RequestOptions? = null): T

Sends a request and wait for a response.

Link copied to clipboard

Sends a logging message notification to the client.

Link copied to clipboard
suspend fun sendPromptListChanged()

Sends a notification to the client indicating that the list of prompts has changed.

Link copied to clipboard

Sends a notification to the client indicating that the list of resources has changed.

Link copied to clipboard

Sends a resource-updated notification to the client, indicating that a specific resource has changed.

Link copied to clipboard
suspend fun sendToolListChanged()

Sends a notification to the client indicating that the list of tools has changed.

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

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

Link copied to clipboard
inline fun <T : Request> setRequestHandler(method: Method, noinline block: suspend (T, RequestHandlerExtra) -> ServerResult?)

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