FastestMCP.Server (fastest_mcp v0.1.2)

Copy Markdown View Source

Immutable server definition.

A %FastestMCP.Server{} is the declarative description of everything the runtime should expose:

  • tools
  • resources
  • resource templates
  • prompts
  • middleware
  • transforms
  • providers
  • auth configuration
  • dependency resolvers
  • extra HTTP routes

This module is intentionally pure. Every builder returns a new struct instead of mutating a running process. The runtime only starts later, through FastestMCP.start_server/2 or FastestMCP.ServerModule.

Typical Flow

Most code builds a server in a pipeline:

server =
  FastestMCP.Server.new("docs")
  |> FastestMCP.Server.add_tool("sum", fn %{"a" => a, "b" => b}, _ctx -> a + b end)
  |> FastestMCP.Server.add_dependency(:repo, fn -> MyApp.Repo end)

The same shape is usually reached through the facade:

server =
  FastestMCP.server("docs")
  |> FastestMCP.add_tool("sum", fn %{"a" => a, "b" => b}, _ctx -> a + b end)

Relationship To The Runtime

FastestMCP.Server is the build-time object.

The server runtime is the running process tree built from that object. That split is deliberate: the public builder stays simple, testable, and easy to compose, while runtime concerns stay inside OTP processes.

Duplicate Registration Policy

on_duplicate: controls what happens when the same local component name or URI is registered twice in the same server definition:

  • :error - raise immediately
  • :warn - log a warning and replace the existing definition
  • :ignore - keep the existing definition
  • :replace - replace the existing definition silently

This policy only applies to the local server definition. Provider precedence and mount ordering remain separate runtime concerns.

Summary

Functions

Adds auth configuration to the current definition.

Registers a dependency resolver on the current definition.

Adds an HTTP route to the current definition.

Adds lifespan hooks to the current definition.

Adds middleware to the current definition.

Adds a prompt component to the current definition.

Adds a provider to the current definition.

Adds a resource component to the current definition.

Adds a resource-template component to the current value.

Adds a tool component to the current definition.

Adds a transform to the current definition.

Returns all components attached to the server definition.

Mounts another server or provider-backed definition.

Builds a new value for this module from the supplied options.

Types

middleware()

@type middleware() :: (FastestMCP.Operation.t(),
                 (FastestMCP.Operation.t() -> any()) ->
                   any())

middleware_entry()

@type middleware_entry() :: middleware() | %{middleware: middleware()}

t()

@type t() :: %FastestMCP.Server{
  auth: FastestMCP.Auth.t() | nil,
  dependencies: %{optional(String.t()) => function()},
  http_routes: [tuple()],
  lifespans: [FastestMCP.Lifespan.t()],
  mask_error_details: boolean(),
  metadata: map(),
  middleware: [middleware_entry()],
  name: String.t(),
  on_duplicate: :error | :warn | :ignore | :replace,
  prompts: [struct()],
  providers: [FastestMCP.Provider.t()],
  resource_templates: [struct()],
  resources: [struct()],
  strict_input_validation: boolean(),
  tasks: struct(),
  tools: [struct()],
  transforms: [transform()]
}

transform()

@type transform() :: (struct(), FastestMCP.Operation.t() -> struct() | nil)

Functions

add_auth(server, auth)

Adds auth configuration to the current definition.

add_auth(server, provider, opts \\ [])

add_dependency(server, name, resolver)

Registers a dependency resolver on the current definition.

add_http_route(server, method, path, handler)

Adds an HTTP route to the current definition.

add_lifespan(server, lifespan)

Adds lifespan hooks to the current definition.

add_lifespan(server, enter, exit)

add_middleware(server, middleware)

Adds middleware to the current definition.

add_prompt(server, name, handler, opts \\ [])

Adds a prompt component to the current definition.

add_provider(server, provider)

Adds a provider to the current definition.

add_resource(server, uri, handler, opts \\ [])

Adds a resource component to the current definition.

add_resource_template(server, uri_template, handler, opts \\ [])

Adds a resource-template component to the current value.

add_tool(server, name, handler, opts \\ [])

Adds a tool component to the current definition.

add_transform(server, transform)

Adds a transform to the current definition.

all_components(server)

Returns all components attached to the server definition.

mount(server, mounted_server, opts \\ [])

Mounts another server or provider-backed definition.

new(name, opts \\ [])

Builds a new value for this module from the supplied options.