MCP.Server (MCP Elixir SDK v1.0.1)

Copy Markdown View Source

MCP server implementation.

A GenServer that handles incoming MCP client requests via a pluggable transport. Routes requests to a handler module implementing the MCP.Server.Handler behaviour.

Usage

defmodule MyHandler do
  @behaviour MCP.Server.Handler

  @impl true
  def init(_opts), do: {:ok, %{}}

  @impl true
  def handle_list_tools(_cursor, state) do
    tools = [%{"name" => "echo", "description" => "Echoes input"}]
    {:ok, tools, nil, state}
  end

  @impl true
  def handle_call_tool("echo", %{"message" => msg}, state) do
    {:ok, [%{"type" => "text", "text" => msg}], state}
  end
end

{:ok, server} = MCP.Server.start_link(
  transport: {MCP.Transport.Stdio, mode: :server},
  handler: {MyHandler, []},
  server_info: %{name: "my_server", version: "1.0.0"}
)

Options

  • :transport{module, opts} transport spec. The server starts the transport in its init, setting itself as the owner.
  • :handler{module, opts} handler spec. The module must implement MCP.Server.Handler.
  • :server_info%Implementation{} or map with :name and :version.
  • :instructions — optional string instructions for the client.
  • :request_timeout — default timeout in ms for server-initiated requests (default: 30_000).

Summary

Functions

Returns a specification to start this module under a supervisor.

Returns the client capabilities from initialization.

Returns the client info from initialization.

Closes the server and its transport.

Sends a log message notification to the client.

Sends a notifications/prompts/list_changed notification to the client.

Sends a notifications/resources/updated notification for a specific URI.

Sends a notifications/resources/list_changed notification to the client.

Sends a notifications/tools/list_changed notification to the client.

Sends an elicitation/create request to the client.

Sends a roots/list request to the client.

Sends a sampling/createMessage request to the client.

Sends a progress notification to the client.

Starts the server GenServer and its transport.

Returns the current server status.

Returns the transport pid (useful for testing with MockTransport).

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

client_capabilities(server)

Returns the client capabilities from initialization.

client_info(server)

Returns the client info from initialization.

close(server)

Closes the server and its transport.

log(server, level, data, logger_name \\ nil)

Sends a log message notification to the client.

Respects the log level set by the client via logging/setLevel. Messages below the current level are silently dropped.

notify_prompts_changed(server)

Sends a notifications/prompts/list_changed notification to the client.

notify_resource_updated(server, uri)

Sends a notifications/resources/updated notification for a specific URI.

notify_resources_changed(server)

Sends a notifications/resources/list_changed notification to the client.

notify_tools_changed(server)

Sends a notifications/tools/list_changed notification to the client.

request_elicitation(server, params, timeout \\ 60000)

Sends an elicitation/create request to the client.

Returns {:ok, result} or {:error, reason}. Requires the client to have declared elicitation capability.

request_roots(server, timeout \\ 30000)

Sends a roots/list request to the client.

Returns {:ok, result} or {:error, reason}. Requires the client to have declared roots capability.

request_sampling(server, params, timeout \\ 60000)

Sends a sampling/createMessage request to the client.

Returns {:ok, result} or {:error, reason}. Requires the client to have declared sampling capability.

send_progress(server, progress_token, progress, total \\ nil)

Sends a progress notification to the client.

start_link(opts)

Starts the server GenServer and its transport.

status(server)

Returns the current server status.

transport(server)

Returns the transport pid (useful for testing with MockTransport).