ExMCP.Service behaviour (ex_mcp v0.9.0)
View SourceBehaviour and macro for creating MCP services with automatic registration.
This module provides a convenient way to create MCP services that automatically register themselves with ExMCP.Native on startup and unregister on shutdown.
Usage
defmodule MyToolService do
use ExMCP.Service, name: :my_tools
@impl true
def handle_mcp_request("list_tools", _params, state) do
tools = [
%{
"name" => "ping",
"description" => "Test tool",
"inputSchema" => %{"type" => "object", "properties" => %{}}
}
]
{:ok, %{"tools" => tools}, state}
end
@impl true
def handle_mcp_request("tools/call", %{"name" => "ping"} = params, state) do
{:ok, %{"content" => [%{"type" => "text", "text" => "Pong!"}]}, state}
end
def handle_mcp_request(method, _params, state) do
{:error, %{"code" => ErrorCodes.method_not_found(), "message" => "Method not found: #{method}"}, state}
end
endCallbacks
The service must implement the handle_mcp_request/3 callback to process
MCP method calls.
Summary
Callbacks
Handles an MCP request.
Callbacks
@callback handle_mcp_request(method :: String.t(), params :: map(), state :: term()) :: {:ok, result :: term(), new_state :: term()} | {:error, error :: term(), new_state :: term()}
Handles an MCP request.
Parameters
method- The MCP method name (e.g., "list_tools", "tools/call")params- The request parameters as a mapstate- The current GenServer state
Returns
{:ok, result, new_state}- Success with result data{:error, error, new_state}- Error with error details