MCPEx.Client (MCPEx v0.1.0)
An implementation of the MCP client for Elixir.
This client provides a complete, spec-compliant implementation of the Model Context Protocol (MCP) client side, supporting version 2025-03-26 of the specification.
Features
- Full protocol implementation with capability negotiation
- Multiple transport options (stdio, HTTP)
- Resource handling with subscriptions
- Tool invocation support
- Prompt template handling
- Sampling interface for LLM interaction
Usage
# Create a new client connected to a server via stdio
{:ok, client} = MCPEx.Client.start_link(
transport: :stdio,
command: "/path/to/server",
capabilities: [:sampling, :roots]
)
# Or with HTTP transport
{:ok, client} = MCPEx.Client.start_link(
transport: :http,
url: "https://example.com/mcp",
capabilities: [:sampling, :roots]
)
# List available resources
{:ok, %{resources: resources}} = MCPEx.Client.list_resources(client)
# Read a specific resource
{:ok, %{contents: contents}} = MCPEx.Client.read_resource(client, "file:///path/to/resource")
# Call a tool
{:ok, result} = MCPEx.Client.call_tool(client, "get_weather", %{location: "New York"})
Summary
Functions
Calls a tool on the server.
Returns a specification to start this module under a supervisor.
Gets a specific prompt by name with optional parameters.
Returns the server capabilities negotiated during initialization.
Returns information about the connected server.
Lists available prompts from the server.
Lists available resource templates from the server.
Lists available resources from the server.
Lists available tools from the server.
Sends a ping to the server to check if it's still alive.
Reads the contents of a specific resource from the server.
Starts a new MCP client as a linked process.
Stops the client and terminates the connection.
Subscribes to updates for a specific resource.
Unsubscribes from updates for a specific resource.
Types
Functions
Calls a tool on the server.
Parameters
name
- The name of the tool to callargs
- The arguments to pass to the tool
Returns
{:ok, result}
- The result of the tool call{:error, reason}
- Failed to call the tool
Returns a specification to start this module under a supervisor.
See Supervisor
.
Gets a specific prompt by name with optional parameters.
Parameters
name
- The name of the prompt to getargs
- Optional arguments to apply to the prompt template
Returns
{:ok, %{description: String.t(), messages: [...]}}
- Successfully fetched prompt with messages{:error, reason}
- Failed to fetch prompt
Returns the server capabilities negotiated during initialization.
Returns
{:ok, map()}
- Server capabilities{:error, reason}
- Failed to get capabilities
Returns information about the connected server.
Returns
{:ok, map()}
- Server information{:error, reason}
- Failed to get server information
Lists available prompts from the server.
Parameters
cursor
- An optional pagination cursor for fetching the next page of results
Returns
{:ok, %{prompts: [...], next_cursor: cursor}}
- Successfully fetched prompts{:error, reason}
- Failed to fetch prompts
Lists available resource templates from the server.
Parameters
cursor
- An optional pagination cursor for fetching the next page of results
Returns
{:ok, %{resource_templates: [...], next_cursor: cursor}}
- Successfully fetched templates{:error, reason}
- Failed to fetch templates
Lists available resources from the server.
Parameters
cursor
- An optional pagination cursor for fetching the next page of results
Returns
{:ok, %{resources: [...], next_cursor: cursor}}
- Successfully fetched resources{:error, reason}
- Failed to fetch resources
Lists available tools from the server.
Parameters
cursor
- An optional pagination cursor for fetching the next page of results
Returns
{:ok, %{tools: [...], next_cursor: cursor}}
- Successfully fetched tools{:error, reason}
- Failed to fetch tools
Sends a ping to the server to check if it's still alive.
Parameters
client
- The client processtimeout
- Optional timeout in milliseconds (default: 5000)
Returns
{:ok, _}
- The server responded to the ping{:error, reason}
- The ping failed
Reads the contents of a specific resource from the server.
Parameters
uri
- The URI of the resource to read
Returns
{:ok, %{contents: [...]}}
- Successfully read the resource{:error, reason}
- Failed to read the resource
Starts a new MCP client as a linked process.
Options
:transport
- The transport mechanism to use (:stdio
or:http
):command
- The command to run for stdio transport (required for stdio):url
- The URL to connect to for HTTP transport (required for http):capabilities
- A list of capabilities to advertise to the server:client_info
- Information about the client implementation (name, version)
Returns
{:ok, pid}
- The client was started successfully{:error, reason}
- The client failed to start
@spec stop(t()) :: :ok
Stops the client and terminates the connection.
Returns
:ok
- The client was stopped successfully
Subscribes to updates for a specific resource.
Parameters
uri
- The URI of the resource to subscribe to
Returns
:ok
- Successfully subscribed{:error, reason}
- Failed to subscribe
Unsubscribes from updates for a specific resource.
Parameters
uri
- The URI of the resource to unsubscribe from
Returns
:ok
- Successfully unsubscribed{:error, reason}
- Failed to unsubscribe