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

Copy Markdown 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.

Deprecated API

ExMCP.Server.Tools (and Tools.Simplified) are deprecated, retained throughout 1.x, and planned for removal in 2.0.0. Prefer ExMCP.Server.DSL for new code.

Protocol-deprecated features

MCP 2026-07-28 deprecated Roots, Sampling, and protocol Logging. ExMCP retains their public APIs throughout 1.x for legacy and 2026-07-28 compatibility. New implementations should pass directories through tool parameters, resource URIs, or server configuration; call LLM provider APIs directly; and use stderr or OpenTelemetry for operational logs.

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 to streamable-HTTP clients subscribed to uri.

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.

MCP Sampling is deprecated as of 2026-07-28. This API remains available throughout ExMCP 1.x for compatibility. New implementations should integrate directly with an LLM provider API.

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.

MCP Roots is deprecated as of 2026-07-28. This API remains available throughout ExMCP 1.x for compatibility. New implementations should pass directories or files via tool parameters, resource URIs, or server configuration.

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(uri)

@spec notify_resource_update(String.t()) :: %{
  subscribers: non_neg_integer(),
  delivered: non_neg_integer()
}

Sends a resource update to streamable-HTTP clients subscribed to uri.

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.

MCP Roots is deprecated as of 2026-07-28 and retained throughout ExMCP 1.x. Prefer explicit tool parameters, resource URIs, or server configuration for new implementations.

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.

MCP protocol Logging is deprecated as of 2026-07-28. This API remains available throughout ExMCP 1.x for compatibility. Prefer stderr on stdio or OpenTelemetry for new observability integrations.