MCP client: connect to an MCP server over a transport and call its tools, resources, and prompts.
children = [
{Noizu.MCP.Client,
name: MyApp.FS,
transport:
{:stdio,
command: "npx",
args: ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"]},
client_info: %{name: "my_app", version: "1.0.0"},
handler: MyApp.MCPHandler,
roots: [Noizu.MCP.Types.Root.new("file:///workspace", name: "Workspace")]}
]
{:ok, tools} = Noizu.MCP.Client.list_tools(MyApp.FS)
{:ok, result} = Noizu.MCP.Client.call_tool(MyApp.FS, "read_file", %{"path" => "/tmp/a"})Options
:transport(required) —{:stdio, command: "...", args: [...], env: %{}, cd: "..."}to spawn a subprocess,{:test, server: MyServer}for an in-memory connection to aNoizu.MCP.Serverin the same VM, or{module, opts}for a customNoizu.MCP.Transport.Clientimplementation:name— optional registered name:client_info—%{name: _, version: _}advertised to the server:handler—moduleor{module, arg}implementingNoizu.MCP.Client.Handler; implemented callbacks advertise thesampling/elicitationcapabilities:roots— initialNoizu.MCP.Types.Rootlist (advertises therootscapability); update later withset_roots/2:on_notification— pid mirrored every server notification as{:mcp_notification, method, params}:request_timeout— default per-request timeout in ms (30_000)
Calls made before the handshake completes are queued and dispatched once the
connection is ready. Per-call :timeout overrides auto-cancel the request
(notifications/cancelled) on expiry. Pass progress: fun/1 to receive
progress notifications for a call.
Summary
Functions
Await an async/4 request.
Block until the initialize handshake completes.
Call a tool. Returns {:ok, %ToolResult{}}. Args use string keys.
Cancel an async/4 request (notifications/cancelled).
Returns a specification to start this module under a supervisor.
Close the connection.
Request completion values. ref is {:prompt, name} or
{:resource_template, uri_template}.
Get a prompt. Returns {:ok, %{description: _, messages: [%PromptMessage{}]}}.
The server's instructions string, if any.
List all prompts (auto-paginates).
List all resource templates (auto-paginates).
List all resources (auto-paginates).
List all tools (auto-paginates; pass page: cursor for manual paging).
Send a one-way notification to the server.
Ping the server.
Read a resource. Returns {:ok, [%ResourceContents{}]}.
Issue a raw MCP request. Returns {:ok, result_map} | {:error, reason}
where reason is a Noizu.MCP.Error, :timeout, or :closed.
The server's negotiated capabilities (wire-format map).
The server's Implementation info (after ready).
Set the server's log level for this session.
Replace the advertised roots and emit notifications/roots/list_changed.
Subscribe to update notifications for a resource URI.
Unsubscribe from update notifications for a resource URI.
Functions
@spec async(GenServer.server(), String.t(), map() | nil, keyword()) :: {:ok, term()}
Issue a request without blocking; returns a reference for await/3 / cancel/3.
@spec await(GenServer.server(), term(), timeout()) :: {:ok, map()} | {:error, term()}
Await an async/4 request.
@spec await_ready(GenServer.server(), timeout()) :: :ok | {:error, term()}
Block until the initialize handshake completes.
@spec call_tool(GenServer.server(), String.t(), map(), keyword()) :: {:ok, Noizu.MCP.Types.ToolResult.t()} | {:error, term()}
Call a tool. Returns {:ok, %ToolResult{}}. Args use string keys.
@spec cancel(GenServer.server(), term(), String.t() | nil) :: :ok
Cancel an async/4 request (notifications/cancelled).
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec close(GenServer.server()) :: :ok
Close the connection.
@spec complete(GenServer.server(), tuple(), String.t(), String.t()) :: {:ok, map()} | {:error, term()}
Request completion values. ref is {:prompt, name} or
{:resource_template, uri_template}.
@spec get_prompt(GenServer.server(), String.t(), map(), keyword()) :: {:ok, map()} | {:error, term()}
Get a prompt. Returns {:ok, %{description: _, messages: [%PromptMessage{}]}}.
@spec instructions(GenServer.server()) :: String.t() | nil
The server's instructions string, if any.
@spec list_prompts( GenServer.server(), keyword() ) :: {:ok, [Noizu.MCP.Types.Prompt.t()]} | {:error, term()}
List all prompts (auto-paginates).
@spec list_resource_templates( GenServer.server(), keyword() ) :: {:ok, [Noizu.MCP.Types.ResourceTemplate.t()]} | {:error, term()}
List all resource templates (auto-paginates).
@spec list_resources( GenServer.server(), keyword() ) :: {:ok, [Noizu.MCP.Types.Resource.t()]} | {:error, term()}
List all resources (auto-paginates).
@spec list_tools( GenServer.server(), keyword() ) :: {:ok, [Noizu.MCP.Types.Tool.t()]} | {:error, term()}
List all tools (auto-paginates; pass page: cursor for manual paging).
@spec notify(GenServer.server(), String.t(), map() | nil) :: :ok
Send a one-way notification to the server.
@spec ping(GenServer.server()) :: :ok | {:error, term()}
Ping the server.
@spec read_resource(GenServer.server(), String.t(), keyword()) :: {:ok, [Noizu.MCP.Types.ResourceContents.t()]} | {:error, term()}
Read a resource. Returns {:ok, [%ResourceContents{}]}.
@spec request(GenServer.server(), String.t(), map() | nil, keyword()) :: {:ok, map()} | {:error, term()}
Issue a raw MCP request. Returns {:ok, result_map} | {:error, reason}
where reason is a Noizu.MCP.Error, :timeout, or :closed.
@spec server_capabilities(GenServer.server()) :: map() | nil
The server's negotiated capabilities (wire-format map).
@spec server_info(GenServer.server()) :: Noizu.MCP.Types.Implementation.t() | nil
The server's Implementation info (after ready).
@spec set_log_level(GenServer.server(), atom() | String.t()) :: :ok | {:error, term()}
Set the server's log level for this session.
@spec set_roots(GenServer.server(), [Noizu.MCP.Types.Root.t()]) :: :ok
Replace the advertised roots and emit notifications/roots/list_changed.
@spec subscribe_resource(GenServer.server(), String.t()) :: :ok | {:error, term()}
Subscribe to update notifications for a resource URI.
@spec unsubscribe_resource(GenServer.server(), String.t()) :: :ok | {:error, term()}
Unsubscribe from update notifications for a resource URI.