Hermes.Client (hermes_mcp v0.3.3)
A GenServer implementation of an MCP (Model Context Protocol) client.
This module handles the client-side implementation of the MCP protocol, including initialization, request/response handling, and maintaining protocol state.
Examples
# Start a client process
{:ok, client} = Hermes.Client.start_link(
name: MyApp.MCPClient,
transport: Hermes.Transport.STDIO,
client_info: %{"name" => "MyApp", "version" => "1.0.0"},
capabilities: %{"resources" => %{}, "tools" => %{}}
)
# List available resources
{:ok, resources} = Hermes.Client.list_resources(client)
Notes
The initial client <> server handshake is performed automatically when the client is started.
Summary
Functions
Calls a tool on the server.
Cancels all pending requests.
Cancels an in-progress request.
Returns a specification to start this module under a supervisor.
Closes the client connection and terminates the process.
Gets a specific prompt from the server.
Gets the server's capabilities as reported during initialization.
Gets the server's information as reported during initialization.
Lists available prompts from the server.
Lists available resources from the server.
Lists available tools from the server.
Merges additional capabilities into the client's capabilities.
Sends a ping request to the server to check connection health. Returns :pong
if successful.
Reads a specific resource from the server.
Registers a callback function to be called when log messages are received.
Registers a callback function to be called when progress notifications are received for the specified progress token.
Sends a progress notification to the server for a long-running operation.
Sets the minimum log level for the server to send log messages.
Starts a new MCP client process.
Unregisters a previously registered log callback.
Unregisters a previously registered progress callback for the specified token.
Types
Functions
Calls a tool on the server.
Options
:timeout
- Request timeout in milliseconds:progress
- Progress tracking options:token
- A unique token to track progress (string or integer):callback
- A function to call when progress updates are received
@spec cancel_all_requests(GenServer.server(), String.t()) :: {:ok, [Hermes.Client.Request.t()]} | {:error, Hermes.MCP.Error.t()}
Cancels all pending requests.
Parameters
client
- The client processreason
- Optional reason for cancellation (defaults to "client_cancelled")
Returns
{:ok, requests}
- A list of the Request structs that were cancelled{:error, reason}
- If an error occurred
@spec cancel_request(GenServer.server(), String.t(), String.t()) :: :ok | {:error, Hermes.MCP.Error.t()} | {:not_found, String.t()}
Cancels an in-progress request.
Parameters
client
- The client processrequest_id
- The ID of the request to cancelreason
- Optional reason for cancellation
Returns
:ok
if the cancellation was successful{:error, reason}
if an error occurred{:not_found, request_id}
if the request ID was not found
Returns a specification to start this module under a supervisor.
See Supervisor
.
Closes the client connection and terminates the process.
Gets a specific prompt from the server.
Options
:timeout
- Request timeout in milliseconds:progress
- Progress tracking options:token
- A unique token to track progress (string or integer):callback
- A function to call when progress updates are received
Gets the server's capabilities as reported during initialization.
Returns nil
if the client has not been initialized yet.
Gets the server's information as reported during initialization.
Returns nil
if the client has not been initialized yet.
Lists available prompts from the server.
Options
:cursor
- Pagination cursor for continuing a previous request:timeout
- Request timeout in milliseconds:progress
- Progress tracking options:token
- A unique token to track progress (string or integer):callback
- A function to call when progress updates are received
Lists available resources from the server.
Options
:cursor
- Pagination cursor for continuing a previous request:timeout
- Request timeout in milliseconds:progress
- Progress tracking options:token
- A unique token to track progress (string or integer):callback
- A function to call when progress updates are received
Lists available tools from the server.
Options
:cursor
- Pagination cursor for continuing a previous request:timeout
- Request timeout in milliseconds:progress
- Progress tracking options:token
- A unique token to track progress (string or integer):callback
- A function to call when progress updates are received
Merges additional capabilities into the client's capabilities.
Sends a ping request to the server to check connection health. Returns :pong
if successful.
Reads a specific resource from the server.
Options
:timeout
- Request timeout in milliseconds:progress
- Progress tracking options:token
- A unique token to track progress (string or integer):callback
- A function to call when progress updates are received
@spec register_log_callback(GenServer.server(), log_callback()) :: :ok
Registers a callback function to be called when log messages are received.
Parameters
client
- The client processcallback
- A function that takes three arguments: level, data, and logger name
The callback function will be called whenever a log message notification is received.
@spec register_progress_callback( GenServer.server(), String.t() | integer(), progress_callback() ) :: :ok
Registers a callback function to be called when progress notifications are received for the specified progress token.
Parameters
client
- The client processprogress_token
- The progress token to watch for (string or integer)callback
- A function that takes three arguments: progress_token, progress, and total
The callback function will be called whenever a progress notification with the matching token is received.
@spec send_progress( GenServer.server(), String.t() | integer(), number(), number() | nil ) :: :ok | {:error, term()}
Sends a progress notification to the server for a long-running operation.
Parameters
client
- The client processprogress_token
- The progress token provided in the original request (string or integer)progress
- The current progress value (number)total
- The optional total value for the operation (number)
Returns :ok
if notification was sent successfully, or {:error, reason}
otherwise.
@spec set_log_level(GenServer.server(), String.t()) :: {:ok, map()} | {:error, term()}
Sets the minimum log level for the server to send log messages.
Parameters
client
- The client processlevel
- The minimum log level (debug, info, notice, warning, error, critical, alert, emergency)
Returns {:ok, result} if successful, {:error, reason} otherwise.
@spec start_link([option()]) :: Supervisor.on_start()
Starts a new MCP client process.
Options
:name
- Optional name to register the client process:transport
- The transport process or name to use (required):client_info
- Information about the client (required):capabilities
- Client capabilities to advertise:protocol_version
- Protocol version to use (defaults to "2024-11-05"):request_timeout
- Default timeout for requests in milliseconds (default: 30s)
@spec unregister_log_callback(GenServer.server()) :: :ok
Unregisters a previously registered log callback.
Parameters
client
- The client processcallback
- The callback function to unregister
@spec unregister_progress_callback(GenServer.server(), String.t() | integer()) :: :ok
Unregisters a previously registered progress callback for the specified token.
Parameters
client
- The client processprogress_token
- The progress token to stop watching (string or integer)