Client

open class Client(clientInfo: Implementation, options: ClientOptions = ClientOptions()) : Protocol<ClientRequest, ClientNotification, ClientResult> (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, getServerCapabilities and getServerVersion 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?

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

Functions

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

A method to check if a capability is supported by the remote side, for the given method to be called.

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

A method to check if a request handler is supported by the local side, for the given method to be handled.

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()

Closes the connection.

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

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
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: ClientNotification)

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

Link copied to clipboard
open fun onclose()

Callback for when the connection is closed for any reason.

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

Callback for when an error occurs.

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

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: ClientRequest, options: RequestOptions? = null): T

Sends a request and wait for a response.

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
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>)

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) -> ClientResult?)

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

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.