ExMCP.ACP.Client.Handler behaviour (ex_mcp v0.9.0)

View Source

Behaviour for handling ACP session events and agent requests.

Implement this behaviour to customize how your application responds to streaming session updates, permission requests, and file access requests from ACP agents.

See ExMCP.ACP.Client.DefaultHandler for a reference implementation.

Summary

Callbacks

Called when the agent requests to read a file.

Called when the agent requests to write a file.

Called when the agent requests permission to use a tool.

Called for each session/update notification from the agent.

Called when the handler is initialized.

Called when the handler is being terminated.

Types

state()

@type state() :: any()

Callbacks

handle_file_read(session_id, path, opts, state)

(optional)
@callback handle_file_read(
  session_id :: String.t(),
  path :: String.t(),
  opts :: map(),
  state()
) ::
  {:ok, content :: String.t(), state()}
  | {:error, reason :: String.t(), state()}

Called when the agent requests to read a file.

Return {:ok, content, state} with the file contents, or {:error, reason, state} to deny access.

handle_file_write(session_id, path, content, state)

(optional)
@callback handle_file_write(
  session_id :: String.t(),
  path :: String.t(),
  content :: String.t(),
  state()
) :: {:ok, state()} | {:error, reason :: String.t(), state()}

Called when the agent requests to write a file.

Return {:ok, state} to allow the write, or {:error, reason, state} to deny it.

handle_permission_request(session_id, tool_call, options, state)

@callback handle_permission_request(
  session_id :: String.t(),
  tool_call :: map(),
  options :: [map()],
  state()
) :: {:ok, outcome :: map(), state()}

Called when the agent requests permission to use a tool.

Must return an outcome map with an "optionId" matching one of the provided options.

handle_session_update(session_id, update, state)

@callback handle_session_update(session_id :: String.t(), update :: map(), state()) ::
  {:ok, state()}

Called for each session/update notification from the agent.

The update map contains a "sessionUpdate" discriminator field indicating the update type (e.g., "agent_message_chunk", "tool_call", "plan", etc.).

init(opts)

@callback init(opts :: keyword()) :: {:ok, state()}

Called when the handler is initialized.

terminate(reason, state)

(optional)
@callback terminate(reason :: any(), state()) :: :ok

Called when the handler is being terminated.