MCPClientImpl

org.llm4s.mcp.MCPClientImpl
See theMCPClientImpl companion object
class MCPClientImpl(config: MCPServerConfig) extends MCPClient

MCPClient implementation that connects to an MCP server over JSON-RPC.

== Transport auto-detection == For HTTP-based server configs (StreamableHTTPTransport or SSETransport), this client first attempts to connect using the MCP 2025-06-18 Streamable HTTP protocol. If the server responds with HTTP 404 or 405, it automatically falls back to the older 2024-11-05 HTTP+SSE protocol. Stdio servers always use the 2024-11-05 protocol.

== Error swallowing in getTools == getTools() never returns a Left: any failure during tool discovery (network error, parse error, missing transport) is logged and an empty sequence is returned instead. Callers cannot distinguish "server has no tools" from "server could not be reached".

== Thread safety == This class is not thread-safe. Concurrent calls to initialize, getTools, or close from different threads require external synchronisation.

Value parameters

config

Server configuration including transport type, URL/command, and timeout.

Attributes

Companion
object
Graph
Supertypes
trait MCPClient
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

override def close(): Unit

Closes the MCP client connection and releases resources. Should be called when done with the client.

Closes the MCP client connection and releases resources. Should be called when done with the client.

Attributes

Definition Classes
override def getTools(): Either[String, Seq[ToolFunction[_, _]]]

Retrieves all tools advertised by the MCP server, converting them to ToolFunction instances that the agent framework can invoke.

Retrieves all tools advertised by the MCP server, converting them to ToolFunction instances that the agent framework can invoke.

Calls initialize() automatically if not already connected. Any error during transport initialisation, tool listing, or JSON parsing is logged and swallowed: this method always returns Right(tools) where tools is the (possibly empty) sequence of successfully parsed tool definitions.

Attributes

Returns

always Right; Right(Seq.empty) on any communication or parse failure

Definition Classes
override def initialize(): Either[String, Unit]

Initializes the MCP connection with handshake protocol. Must be called before other operations.

Initializes the MCP connection with handshake protocol. Must be called before other operations.

Attributes

Returns

Either error message or successful initialization

Definition Classes
def trySendingRequest(transportImpl: MCPTransportImpl): Either[String, Seq[ToolFunction[_, _]]]