McpClient

class McpClient(val url: String, headers: Map<String, String> = emptyMap(), auth: McpAuth? = null, http: OkHttpClient = DefaultHttpClient, clientInfo: McpImplementation = McpImplementation("ai-elements-kotlin", "0.3.0"), capabilities: JsonObject = JsonObject(emptyMap()))(source)

A Model Context Protocol client over the Streamable HTTP transport.

It speaks the current revision (MODERN_VERSION, stateless: every request carries its protocol version, client info and capabilities in _meta, and the MCP-Protocol-Version, Mcp-Method, Mcp-Name and Mcp-Param-* headers mirror them) and falls back to the legacy, session-based revisions (initialize handshake, Mcp-Session-Id) when the server turns out to be one — the "dual-era" client of the spec. The era is detected on the first request and cached. The deprecated HTTP+SSE transport is not supported.

Each request is its own POST; the reply is a JSON object or an SSE stream carrying notifications/progress before the response. Cancelling the calling coroutine closes the stream, which is the cancellation signal.

It declares elicitation (form and URL modes): a server that needs the user's input mid-call — InputRequiredResult on 2026-07-28 (SEP-2322: the client asks, then retries with inputResponses and the echoed requestState), elicitation/create on a legacy session stream — is answered through callTool's onInput; without one the answer is cancel. Sampling and roots are not declared. capabilities can add extensions, e.g. McpApps.CLIENT_CAPABILITIES for MCP Apps.

Parameters

headers

sent with every request, e.g. a static API key header.

auth

supplies (and refreshes) bearer tokens; see McpAuth.

Constructors

Link copied to clipboard
constructor(url: String, headers: Map<String, String> = emptyMap(), auth: McpAuth? = null, http: OkHttpClient = DefaultHttpClient, clientInfo: McpImplementation = McpImplementation("ai-elements-kotlin", "0.3.0"), capabilities: JsonObject = JsonObject(emptyMap()))

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Usage hints the server gives the model, if any.

Link copied to clipboard

The negotiated protocol version, once known.

Link copied to clipboard

Name and version the server reported (legacy initialize or server/discover).

Link copied to clipboard
val url: String

Functions

Link copied to clipboard
suspend fun callTool(tool: McpTool, arguments: JsonObject, onInput: McpInputHandler? = null, onProgress: suspend (McpProgress) -> Unit? = null): McpToolResult

Call tool with arguments. onProgress receives the server's progress notifications while it runs.

suspend fun callTool(name: String, arguments: JsonObject, onInput: McpInputHandler? = null, onProgress: suspend (McpProgress) -> Unit? = null): McpToolResult

Call a tool by name (no x-mcp-header mirroring; prefer the McpTool overload).

Link copied to clipboard
suspend fun close()

Ends a legacy session (DELETE); a no-op for the stateless revision.

Link copied to clipboard
suspend fun getPrompt(name: String, arguments: Map<String, String> = emptyMap()): List<Pair<String, McpContent>>

Render prompt name; returns its messages as (role, content) pairs.

Link copied to clipboard
suspend fun listPrompts(): List<McpPrompt>
Link copied to clipboard
Link copied to clipboard
suspend fun listTools(): List<McpTool>

All tools, following pagination. Tools with invalid x-mcp-header annotations are dropped, as the spec requires.

Link copied to clipboard

The contents of the resource at uri (text and/or base64 blobs).