ExMCP.Server (ex_mcp v1.0.0-rc.0)

View Source

Public server-side helpers for running MCP servers.

Server implementations should use ExMCP.Server.Handler directly. For a declarative handler DSL, combine it with ExMCP.Server.DSL:

defmodule MyServer do
  use ExMCP.Server.Handler
  use ExMCP.Server.DSL

  tool "echo", "Echo input" do
    param :message, :string, required: true

    run fn %{message: message}, state ->
      {:ok, %{text: message}, state}
    end
  end
end

Use ExMCP.Server.HandlerServer.start_link/1 when you need a transport-aware process for a handler module.

Summary

Functions

Sends a cancellation notification to the server.

Sends a sampling/createMessage request to the connected client.

Gets the list of pending request IDs on the server.

Lists roots available from the connected client.

Sends a progress notification to the client.

Sends a progress notification with a total to the client.

Notifies subscribed clients that the prompts list has changed.

Sends a resource update notification for subscribed clients.

Notifies subscribed clients that the resource list has changed.

Notifies the client that the server's roots have changed.

Notifies subscribed clients that the tools list has changed.

Sends a ping request to the connected client.

Sends a log message through the server.

Functions

cancel_request(server, request_id, reason \\ nil)

@spec cancel_request(GenServer.server(), ExMCP.Types.request_id(), String.t() | nil) ::
  :ok

Sends a cancellation notification to the server.

create_message(server, params)

@spec create_message(GenServer.server(), map()) :: {:ok, map()} | {:error, term()}

Sends a sampling/createMessage request to the connected client.

get_pending_requests(server)

@spec get_pending_requests(GenServer.server()) :: [ExMCP.Types.request_id()]

Gets the list of pending request IDs on the server.

list_roots(server, timeout \\ 5000)

@spec list_roots(GenServer.server(), timeout()) ::
  {:ok, %{roots: [map()]}} | {:error, any()}

Lists roots available from the connected client.

notify_progress(server, progress_token, progress)

@spec notify_progress(GenServer.server(), any(), number()) :: :ok

Sends a progress notification to the client.

notify_progress(server, progress_token, progress, total)

@spec notify_progress(GenServer.server(), any(), number(), number()) :: :ok

Sends a progress notification with a total to the client.

notify_prompts_changed(server)

@spec notify_prompts_changed(GenServer.server()) :: :ok

Notifies subscribed clients that the prompts list has changed.

notify_resource_update(server, uri)

@spec notify_resource_update(GenServer.server(), String.t()) :: :ok

Sends a resource update notification for subscribed clients.

notify_resources_changed(server)

@spec notify_resources_changed(GenServer.server()) :: :ok

Notifies subscribed clients that the resource list has changed.

notify_roots_changed(server)

@spec notify_roots_changed(GenServer.server()) :: :ok

Notifies the client that the server's roots have changed.

notify_tools_changed(server)

@spec notify_tools_changed(GenServer.server()) :: :ok

Notifies subscribed clients that the tools list has changed.

ping(server, timeout \\ 5000)

@spec ping(GenServer.server(), timeout()) :: {:ok, map()} | {:error, any()}

Sends a ping request to the connected client.

send_log_message(server, level, message, data)

@spec send_log_message(GenServer.server(), atom() | String.t(), String.t(), map()) ::
  :ok

Sends a log message through the server.