Server

open class Server(serverInfo: Implementation, options: ServerOptions, instructionsProvider: () -> String? = null)(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.

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.

Constructors

Link copied to clipboard
constructor(serverInfo: Implementation, options: ServerOptions, instructionsProvider: () -> String? = null)
constructor(serverInfo: Implementation, options: ServerOptions, instructions: String)

Alternative constructor that provides the instructions directly as a string.

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
fun addPrompt(prompt: Prompt, promptProvider: suspend (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 (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 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
fun addTool(tool: Tool, handler: suspend (CallToolRequest) -> CallToolResult)
fun addTool(name: String, description: String, inputSchema: Tool.Input = Tool.Input(), title: String? = null, outputSchema: Tool.Output? = null, toolAnnotations: ToolAnnotations? = null, handler: suspend (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
suspend fun close()
Link copied to clipboard
suspend fun connect(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
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
fun onInitialized(block: () -> Unit)

Registers a callback to be invoked when the server has completed initialization.

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 single tool by name.

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

Removes multiple tools at once.