ExMCP.Client (ex_mcp v0.1.0)

View Source

MCP client for connecting to Model Context Protocol servers.

The client handles:

  • Connection management with automatic reconnection
  • Request/response correlation
  • Protocol message encoding/decoding
  • Concurrent request handling

Example

# Connect to a filesystem server
{:ok, client} = ExMCP.Client.start_link(
  transport: :stdio,
  command: ["npx", "-y", "@modelcontextprotocol/server-filesystem", "/tmp"],
  name: :fs_client
)

# List available tools
{:ok, tools} = ExMCP.Client.list_tools(client)

# Call a tool
{:ok, result} = ExMCP.Client.call_tool(client, "read_file", %{
  "path" => "/tmp/example.txt"
})

Summary

Functions

Calls a tool with the given arguments.

Returns a specification to start this module under a supervisor.

Gets a prompt with the given arguments.

Lists available prompts from the server.

Lists available resources from the server.

Lists available tools from the server.

Gets server capabilities.

Gets server information.

Starts an MCP client.

Functions

call_tool(client, name, arguments, timeout \\ 30000)

@spec call_tool(GenServer.server(), String.t(), map(), timeout()) ::
  {:ok, ExMCP.Types.tool_result()} | {:error, any()}

Calls a tool with the given arguments.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_prompt(client, name, arguments \\ %{}, timeout \\ 30000)

@spec get_prompt(GenServer.server(), String.t(), map(), timeout()) ::
  {:ok, ExMCP.Types.prompt_message()} | {:error, any()}

Gets a prompt with the given arguments.

list_prompts(client, timeout \\ 30000)

@spec list_prompts(GenServer.server(), timeout()) ::
  {:ok, [ExMCP.Types.prompt()]} | {:error, any()}

Lists available prompts from the server.

list_resources(client, timeout \\ 30000)

@spec list_resources(GenServer.server(), timeout()) ::
  {:ok, [ExMCP.Types.resource()]} | {:error, any()}

Lists available resources from the server.

list_tools(client, timeout \\ 30000)

@spec list_tools(GenServer.server(), timeout()) ::
  {:ok, [ExMCP.Types.tool()]} | {:error, any()}

Lists available tools from the server.

read_resource(client, uri, timeout \\ 30000)

@spec read_resource(GenServer.server(), String.t(), timeout()) ::
  {:ok, ExMCP.Types.resource_content()} | {:error, any()}

Reads a resource by URI.

server_capabilities(client)

@spec server_capabilities(GenServer.server()) ::
  {:ok, ExMCP.Types.capabilities()} | {:error, :not_initialized}

Gets server capabilities.

server_info(client)

@spec server_info(GenServer.server()) ::
  {:ok, ExMCP.Types.server_info()} | {:error, :not_initialized}

Gets server information.

start_link(opts)

Starts an MCP client.

Options

  • :transport - Transport type (:stdio, :sse, or module)
  • :name - GenServer name (optional)
  • :client_info - Client information map with :name and :version

Transport-specific options are passed through.