Server

A Model Context Protocol (MCP) server implementation that handles MCP requests from a connected client via a specified Transport.

Example usage:

val server = Server.Builder()
.withPageSize(25)
.withPrompt(::myPromptFunction)
.withResourceProvider(myLocalFileProvider)
.withServerInfo("MyServer", "1.0.0")
.withTool(::myToolFunction)
.withTransport(myTransport)
.withTransportLogger(
logIncoming = { msg -> println("SERVER INCOMING: $msg") },
logOutgoing = { msg -> println("SERVER OUTGOING: $msg") },
)
.build()

server.start()

Tools and prompts can be added at build time via Builder.withTool, Builder.withTools, Builder.withPrompt, and Builder.withPrompts. They can also be added or removed dynamically at runtime if needed.

Types

Link copied to clipboard
class Builder

Builder for constructing a Server instance.

Functions

Link copied to clipboard
fun addPrompt(prompt: KFunction<*>): Boolean

Dynamically adds a new prompt to the server at runtime.

Link copied to clipboard
fun addTool(tool: KFunction<*>): Boolean

Dynamically adds a new tool to the server at runtime and sends a ToolListChangedNotification

Link copied to clipboard
inline fun <T> getContextAs(): T
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun removePrompt(prompt: KFunction<*>): Boolean

Dynamically removes a previously added prompt by its @McpPrompt-annotated function reference.

Link copied to clipboard

Dynamically removes a previously added tool by its @McpTool-annotated function reference and sends a ToolListChangedNotification

Link copied to clipboard
suspend fun sendNotification(notification: JsonRpcNotification)

Sends a notification (fire-and-forget).

Link copied to clipboard
suspend fun sendRequest(builder: (id: String) -> JsonRpcRequest): JsonRpcResponse

Sends a request and suspends until a corresponding response is received or this coroutine is cancelled.

Link copied to clipboard
suspend fun start()

Starts reading/writing from the given transport.