McpConnection

Minimal MCP client implementing the tools-related subset of the protocol on top of McpTransport (Streamable HTTP by default).

Responsibilities:

  • initialize handshake + notifications/initialized;

  • session management: Mcp-Session-Id header capture and automatic re-initialization when the server answers 404 (session expired);

  • tools/list with pagination and caching - see listTools;

  • tools/call with textual result rendering - see callTool.

Server-initiated features (sampling, roots, elicitation) are not supported: empty client capabilities are declared, so a compliant server will not send such requests. Incoming messages that do not match our request id (notifications, server requests) are skipped while scanning SSE streams.

Properties

Link copied to clipboard
val url: String

Functions

Link copied to clipboard
suspend fun callTool(name: String, arguments: JsonObject): String

Calls a remote tool and returns its textual result.

Link copied to clipboard
fun close()
Link copied to clipboard
suspend fun fetch(): List<McpToolDefinition>

Re-fetches the tool list, discarding the cache.

Link copied to clipboard
Link copied to clipboard
suspend fun listTools(force: Boolean = false): List<McpToolDefinition>

Lists tools exposed by the server, following nextCursor pagination. The result is cached after the first call; pass force = true to re-fetch it.

Link copied to clipboard

Re-fetches the tool list, discarding the cache.