Client

open class Client(clientInfo: Implementation, options: ClientOptions = ClientOptions()) : Protocol(source)

An MCP client on top of a pluggable transport.

The client automatically performs the initialization handshake with the server when connect is called. After initialization, serverCapabilities and serverVersion provide details about the connected server.

You can extend this class with custom request/notification/result types if needed.

Parameters

clientInfo

Information about the client implementation (name, version).

options

Configuration options for this client.

Constructors

Link copied to clipboard
constructor(clientInfo: Implementation, options: ClientOptions = ClientOptions())

Properties

Link copied to clipboard
var fallbackNotificationHandler: suspend (notification: JSONRPCNotification) -> Unit?
Link copied to clipboard
Link copied to clipboard
val notificationHandlers: Map<String, suspend (notification: JSONRPCNotification) -> Unit>
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Retrieves the server's reported capabilities after the initialization process completes.

Link copied to clipboard

Retrieves the server's reported version information after initialization.

Link copied to clipboard

Functions

Link copied to clipboard
fun addRoot(uri: String, name: String)

Registers a single root.

Link copied to clipboard
fun addRoots(rootsToAdd: List<Root>)

Registers multiple roots at once.

Link copied to clipboard
open override fun assertRequestHandlerCapability(method: Method)
Link copied to clipboard
suspend fun callTool(request: CallToolRequest, compatibility: Boolean = false, options: RequestOptions? = null): CallToolResultBase?

Calls a tool on the server using a CallToolRequest object.

suspend fun callTool(name: String, arguments: Map<String, Any?>, compatibility: Boolean = false, options: RequestOptions? = null): CallToolResultBase?

Calls a tool on the server by name, passing the specified arguments.

Link copied to clipboard
suspend fun close()
Link copied to clipboard
suspend fun complete(params: CompleteRequest, options: RequestOptions? = null): CompleteResult

Sends a completion request to the server, typically to generate or complete some content.

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

Connects the client to the given transport, performing the initialization handshake with the server.

Link copied to clipboard
suspend fun getPrompt(request: GetPromptRequest, options: RequestOptions? = null): GetPromptResult

Retrieves a prompt by name from the server.

Link copied to clipboard
suspend fun listPrompts(request: ListPromptsRequest = ListPromptsRequest(), options: RequestOptions? = null): ListPromptsResult

Lists all available prompts from the server.

Link copied to clipboard
suspend fun listResources(request: ListResourcesRequest = ListResourcesRequest(), options: RequestOptions? = null): ListResourcesResult

Lists all available resources from the server.

Link copied to clipboard

Lists resource templates available on the server.

Link copied to clipboard
suspend fun listTools(request: ListToolsRequest = ListToolsRequest(), options: RequestOptions? = null): ListToolsResult

Lists all available tools on the server.

Link copied to clipboard
suspend fun notification(notification: Notification)
Link copied to clipboard
open fun onClose()
Link copied to clipboard
open fun onError(error: Throwable)
Link copied to clipboard
suspend fun ping(options: RequestOptions? = null): EmptyRequestResult

Sends a ping request to the server to check connectivity.

Link copied to clipboard
suspend fun readResource(request: ReadResourceRequest, options: RequestOptions? = null): ReadResourceResult

Reads a resource from the server by its URI.

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

Removes a single root by URI.

Link copied to clipboard
fun removeRoots(uris: List<String>): Int

Removes multiple roots at once.

Link copied to clipboard
suspend fun <T : RequestResult> request(request: Request, options: RequestOptions?): T
Link copied to clipboard
suspend fun sendRootsListChanged()

Notifies the server that the list of roots has changed. Typically used if the client is managing some form of hierarchical structure.

Link copied to clipboard

Sets the elicitation handler.

Link copied to clipboard
suspend fun setLoggingLevel(level: LoggingLevel, options: RequestOptions? = null): EmptyRequestResult

Sets the logging level on the server.

Link copied to clipboard
fun <T : Notification> setNotificationHandler(method: Method, handler: (notification: T) -> Deferred<Unit>)
Link copied to clipboard
inline fun <T : Request> setRequestHandler(method: Method, noinline block: suspend (T, RequestHandlerExtra) -> RequestResult?)
Link copied to clipboard
suspend fun subscribeResource(request: SubscribeRequest, options: RequestOptions? = null): EmptyRequestResult

Subscribes to resource changes on the server.

Link copied to clipboard

Unsubscribes from resource changes on the server.