Hermolaos.Client.NotificationHandler behaviour (Hermolaos v0.3.0)
View SourceBehaviour for handling MCP server notifications and requests.
When a server sends notifications (like list_changed events) or requests (like ping or sampling), they are dispatched to a notification handler if one is configured.
Implementing a Handler
defmodule MyApp.MCPHandler do
@behaviour Hermolaos.Client.NotificationHandler
@impl true
def handle_notification({:notification, "notifications/tools/list_changed", _params}, state) do
IO.puts("Tools list changed!")
{:ok, state}
end
def handle_notification({:notification, method, params}, state) do
IO.puts("Got notification: #{method}")
{:ok, state}
end
def handle_notification({:request, "ping", _params}, state) do
# ping is handled automatically, but you can observe it
{:ok, state}
end
def handle_notification(_event, state) do
{:ok, state}
end
endUsing the Handler
{:ok, conn} = Hermolaos.Client.Connection.start_link(
transport: :stdio,
command: "my-server",
notification_handler: {MyApp.MCPHandler, %{}}
)Event Types
Events are tuples with one of these formats:
{:notification, method, params}- Server notification{:request, method, params}- Server request (ping, sampling, etc.)
Common Notifications
notifications/tools/list_changed- Available tools changednotifications/resources/list_changed- Available resources changednotifications/resources/updated- A specific resource was updatednotifications/prompts/list_changed- Available prompts changednotifications/progress- Progress update for long operationnotifications/message- Log message from server
Summary
Callbacks
Called when a server notification or request is received.
Types
Callbacks
@callback handle_notification(event :: event(), state :: handler_state()) :: {:ok, handler_state()} | :ok
Called when a server notification or request is received.
Parameters
event- The event tuplestate- Handler state (or nil if no state was provided)
Returns
{:ok, new_state}- Continue with updated state:ok- Continue with unchanged state (convenience for stateless handlers)