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 implementMCP.Server.Handler.:server_info—%Implementation{}or map with:nameand: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
Returns a specification to start this module under a supervisor.
See 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.
Respects the log level set by the client via logging/setLevel.
Messages below the current level are silently dropped.
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.
Returns {:ok, result} or {:error, reason}.
Requires the client to have declared elicitation capability.
Sends a roots/list request to the client.
Returns {:ok, result} or {:error, reason}.
Requires the client to have declared roots capability.
Sends a sampling/createMessage request to the client.
Returns {:ok, result} or {:error, reason}.
Requires the client to have declared sampling capability.
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).