OzMcp.Testing (OzMcp v0.9.0)

Copy Markdown View Source

A client for the tests of an MCP server. It starts the server in the test, opens a session through OzMcp.Router and gives the answers as a client reads them.

setup do
  {:ok, client} = OzMcp.Testing.start(MyApp.McpServer)
  %{client: client}
end

test "gives the weather", %{client: client} do
  assert {:ok, %{"content" => [%{"text" => "Sunny"}]}} =
           OzMcp.Testing.call_tool(client, "weather", %{"city" => "Paris"})
end

The guide Testing shows the setup and each call.

Summary

Functions

Calls the tool name with arguments. The options are :meta, :id and :timeout.

Completes the reference ref with argument. The options are :meta, :id and :timeout.

Gets the prompt name with arguments. The options are :meta, :id and :timeout.

The result of the initialize of the client, with string keys, as the server sent it.

The name of the instance of the client, for OzMcp.Notify: OzMcp.Notify.resource_updated(OzMcp.Testing.instance(client), uri).

Lists the entities of kind. The option :cursor continues a paginated list.

Sends the notification method with params. The options are :meta and :timeout.

Reads the resource uri. The options are :meta, :id and :timeout.

Sends the request method with params and gives its answer. The options are :meta, :id and :timeout.

Starts the server server and one client of it, under the supervisor of the test. The options of the client are :token (a bearer token), :capabilities (the client capabilities of initialize, default %{}), :handle (a function of method and params that answers a request of the server with {:ok, result} or {:error, error}), :client_info and :timeout (milliseconds, default 5_000). Every other option is an option of the instance.

Ends the session of the client and stops it; the end of the test stops it without the DELETE.

Types

client()

@type client() :: pid()

error()

@type error() :: %{required(String.t()) => term()}

kind()

@type kind() :: :tools | :prompts | :resources | :resource_templates

Functions

call_tool(client, name, arguments \\ %{}, opts \\ [])

@spec call_tool(client(), String.t(), map(), keyword()) ::
  {:ok, map()} | {:error, error()}

Calls the tool name with arguments. The options are :meta, :id and :timeout.

complete(client, ref, argument, opts \\ [])

@spec complete(client(), map(), map(), keyword()) :: {:ok, map()} | {:error, error()}

Completes the reference ref with argument. The options are :meta, :id and :timeout.

get_prompt(client, name, arguments \\ %{}, opts \\ [])

@spec get_prompt(client(), String.t(), map(), keyword()) ::
  {:ok, map()} | {:error, error()}

Gets the prompt name with arguments. The options are :meta, :id and :timeout.

initialize_result(client)

@spec initialize_result(client()) :: map()

The result of the initialize of the client, with string keys, as the server sent it.

instance(client)

@spec instance(client()) :: atom()

The name of the instance of the client, for OzMcp.Notify: OzMcp.Notify.resource_updated(OzMcp.Testing.instance(client), uri).

list(client, kind, opts \\ [])

@spec list(client(), kind(), keyword()) :: {:ok, map()} | {:error, error()}

Lists the entities of kind. The option :cursor continues a paginated list.

notify(client, method, params \\ %{}, opts \\ [])

@spec notify(client(), String.t(), map(), keyword()) :: :ok | {:error, error()}

Sends the notification method with params. The options are :meta and :timeout.

read_resource(client, uri, opts \\ [])

@spec read_resource(client(), String.t(), keyword()) ::
  {:ok, map()} | {:error, error()}

Reads the resource uri. The options are :meta, :id and :timeout.

request(client, method, params \\ %{}, opts \\ [])

@spec request(client(), String.t(), map(), keyword()) ::
  {:ok, map()} | {:error, error()}

Sends the request method with params and gives its answer. The options are :meta, :id and :timeout.

start(server, opts \\ [])

@spec start(module(), keyword()) :: {:ok, client()} | {:error, error()}

Starts the server server and one client of it, under the supervisor of the test. The options of the client are :token (a bearer token), :capabilities (the client capabilities of initialize, default %{}), :handle (a function of method and params that answers a request of the server with {:ok, result} or {:error, error}), :client_info and :timeout (milliseconds, default 5_000). Every other option is an option of the instance.

It runs in a test process only: it calls ExUnit.Callbacks.start_supervised!/1. A protected server needs a :resource_url unless its test verifier gives the audience http://localhost/mcp.

stop(client)

@spec stop(client()) :: :ok

Ends the session of the client and stops it; the end of the test stops it without the DELETE.