### Define a server with tool, resource, template, and prompt using FastestMCP.server/2 Source: https://hexdocs.pm/fastest_mcp Composes a server definition with a tool, resource, resource template, and prompt. The tool uses an input schema with an enum for environment, returns a result with structured content, and the prompt takes an argument. The server is started with FastestMCP.start_server/1. Use FastestMCP.ServerModule for supervision tree definitions; use FastestMCP.server/2 for dynamic definitions, tests, and provider composition. ```elixir input_schema = %{ "type" => "object", "properties" => %{ "environment" => %{ "type" => "string", "enum" => ["staging", "production"] } }, "required" => ["environment"] } server = FastestMCP.server("operations") |> FastestMCP.add_tool( "deployment_status", fn %{"environment" => environment}, _ctx -> FastestMCP.Tools.Result.new( "#{environment} is healthy", structured_content: %{environment: environment, status: "healthy"} ) end, description: "Read the current deployment status", input_schema: input_schema ) |> FastestMCP.add_resource("config://environments", fn _arguments, _ctx -> %{environments: ["staging", "production"]} end) |> FastestMCP.add_resource_template( "deployments://{environment}/latest", fn %{"environment" => environment}, _ctx -> %{environment: environment, revision: "2026.08.14", status: "healthy"} end ) |> FastestMCP.add_prompt( "review_deployment", fn %{"environment" => environment}, _ctx -> "Review the latest #{environment} deployment and identify operational risks." end, arguments: [%{name: "environment", required: true}] ) {:ok, _pid} = FastestMCP.start_server(server) ``` -------------------------------- ### Define a module-owned MCP server with tools Source: https://hexdocs.pm/fastest_mcp Defines a module-owned server using FastestMCP.ServerModule, adding a 'sum' tool and a 'visit' tool that uses context state. The server is started as a child in a supervision tree and called with FastestMCP.call_tool. ```elixir defmodule MyApp.MCPServer do use FastestMCP.ServerModule, http: [port: 4100, allowed_hosts: :localhost] alias FastestMCP.Context def server(opts) do base_server(opts) |> FastestMCP.add_tool("sum", fn %{"a" => a, "b" => b}, _ctx -> a + b end) |> FastestMCP.add_tool("visit", fn _arguments, ctx -> visits = Context.get_state(ctx, :visits, 0) + 1 :ok = Context.set_state(ctx, :visits, visits) %{visits: visits, server: ctx.server_name} end) end end children = [ MyApp.MCPServer ] FastestMCP.call_tool(MyApp.MCPServer, "sum", %{"a" => 20, "b" => 22}) # => 42 ``` -------------------------------- ### Supervise a named MCP client connection Source: https://hexdocs.pm/fastest_mcp Starts a supervised, named FastestMCP.Client connection, waits for it to be ready, and lists available tools. Suitable for production applications. ```elixir children = [ {FastestMCP.Client, target: "http://localhost:4100/mcp", name: MyApp.MCPClient, protocol_version: :auto} ] :ok = FastestMCP.Client.await_ready(MyApp.MCPClient, 10_000) %{items: tools} = FastestMCP.Client.list_tools(MyApp.MCPClient) ``` -------------------------------- ### Connect to MCP server over HTTP with automatic protocol negotiation Source: https://hexdocs.pm/fastest_mcp Connects to a running MCP server over HTTP with automatic latest-first protocol negotiation, calls the 'sum' tool, starts and cancels a 'slow_sum' tool result, and disconnects. Demonstrates client operations and cancellation. ```elixir client = FastestMCP.Client.connect!("http://localhost:4100/mcp", protocol_version: :auto, client_info: %{"name" => "my_app", "version" => "1.0.0"} ) FastestMCP.Client.protocol_version(client) # => "2026-07-28" %FastestMCP.Client.ToolResult{structured_content: 42} = FastestMCP.Client.call_tool_result(client, "sum", %{"a" => 20, "b" => 22}) request = FastestMCP.Client.start_tool_result(client, "slow_sum", %{"a" => 20, "b" => 22}) # This remains valid if tools/call becomes a negotiated remote task. :ok = FastestMCP.Client.cancel_tool_result(request, "no longer needed") :ok = FastestMCP.Client.disconnect(client) ``` -------------------------------- ### Define an MCP App with tool and resource Source: https://hexdocs.pm/fastest_mcp Shows how to create an MCP App by adding a tool and a resource with Apps metadata. The tool returns a text fallback and structured content, while the resource serves an HTML document with CSP and border options. Requires the Apps extension to be enabled on the server. ```elixir alias FastestMCP.Apps app_uri = "ui://reports/summary.html" app_options = [ csp: %{"connectDomains" => ["https://api.example.com"]}, prefers_border: true ] server = FastestMCP.server("reports", extensions: %{Apps.extension_id() => %{}} ) |> FastestMCP.add_tool( "show_report", fn _arguments, _ctx -> FastestMCP.Tools.Result.new( "The report is ready.", structured_content: %{status: "ready"} ) end, meta: Apps.tool_meta(app_uri) ) |> FastestMCP.add_resource( app_uri, fn _arguments, _ctx -> Apps.result( app_uri, "