Client

A client implementation that communicates using the MCP protocol.

The Client is responsible for connecting to an MCP server, performing initialization, and then participating in the MCP lifecycle (sending requests, handling responses and notifications).

Typical usage:

val client = Client.Builder()
.withClientInfo("MyCustomClient", "1.0.0")
.withRoot(Root(uri = "file:///home/user/project", name = "My Project"))
.withSamplingProvider { createMessageParams ->
// ...
}
.withTransport(StdioTransport()))
.withTransportLogger(
logIncoming = { msg -> println("CLIENT INCOMING: $msg") },
logOutgoing = { msg -> println("CLIENT OUTGOING: $msg") },
)
.build()

client.start()
client.initialize()
// Now the client can send requests and handle responses.

Types

Link copied to clipboard
class Builder

Builder for creating a Client instance.

Functions

Link copied to clipboard
fun addRoot(root: Root): Boolean

Adds a Root to the client. If a root with the same name or uri already exists, it will not be added.

Link copied to clipboard
suspend fun callTool(name: String, arguments: Map<String, JsonElement>? = null, _meta: Map<String, JsonElement>? = null): JsonRpcResponse
Link copied to clipboard

Repeatedly calls a paginated endpoint, emitting one set of ITEMS per page until PaginatedResult.nextCursor is null or an error is thrown.

Link copied to clipboard
suspend fun getAllTools(): List<Tool>

Fetches all available tools from the server using pagination.

Link copied to clipboard
open suspend override fun handleListRootsRequest(): ListRootsResult
Link copied to clipboard
Link copied to clipboard

Initiates the MCP lifecycle by sending an initialize request with the specified client info and capabilities, and then sending an initialized notification once the server responds. Returns an InitializeResult containing information about the server.

Link copied to clipboard

Checks if the client has completed the initialization phase.

Link copied to clipboard
fun removeRoot(root: Root): Boolean

Removes a Root from the client.

Link copied to clipboard

Removes a Root from the client.

Link copied to clipboard

Removes a Root from the client.

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.