ExAgent. MCP. Server
(ExAgent v3.0.0)
View Source
A host-owned MCP tools server for the Streamable HTTP Plug.
Stability — Stable.
Supervise one server per static catalogue. Legacy transports pass decoded JSON to
handle/4. Each successful initialization creates a principal-bound session.
Calls execute in linked workers so another request can ping, cancel or delete
a session while a tool runs. Losing an HTTP connection does not replay or
cancel a call. Restarting the server discards its sessions.
A 2026-07-28 Plug dispatches independently of sessions and applies the tool
deadline, payload limits and concurrency controls on its own replica.
Export ExAgent.Tool function tools with :tools. Set a tool's
:result_schema with explicit "type": "object" to return validated
structuredContent alongside JSON text. Invalid x-mcp-header declarations
in the input schema raise at startup. Supply :capabilities for before-
and after-tool hooks (including ExAgent.Tool.OutputLimit), ExAgent.Guardrail
tool stages and :permissions for admission by name and validated arguments. A tool-input tripwire
returns isError without running the tool; a tool-output tripwire withholds a
confirmed result behind a JSON-RPC error. Input and output guardrails raise. The authenticated actor becomes RunContext.deps.
With :elicitation enabled, the authenticated MCP client may authorize a
single legacy call on its live POST stream.
To request approval on the stateless Plug, configure the candidate
:mrtr_approval option with a scoped atomic :store, a stable endpoint
:id, at least 32 secret bytes as :key, and optional :ttl_ms (300,000).
Replicas share these settings. The Store's admission CAS allows a fresh
approval receipt to authorize one dispatch. Use a durable Store across nodes.
Each principal has 256 live receipts within an endpoint, and the endpoint
has a ceiling of 1,024. At capacity the Plug returns HTTP 400 with JSON-RPC
-32040 and data.scope ("principal" or "endpoint") plus
data.retryable: true. Capacity expires with the receipts' signed states.
Supply static text :resources and :prompts to publish their catalogues.
Set :catalog_policy to a host callback that returns the names or resource
URIs visible to the authenticated principal for each catalogue request.
See catalog_policy/0 for the callback contract. The default is nil,
which exposes all configured entries. :catalog_policy_timeout (1,000 ms)
bounds each check. Up to :max_calls checks may run alongside the tool pool.
A host can set :on_validation_error to a function of the tool name and the
full argument validation diagnostic. It runs in the tool worker. Diagnostics
can include private values introduced by hooks.
Options also include OTP :name, :max_sessions (128), :session_ttl
(absolute lifetime, 300_000 ms), :max_calls (32 concurrent calls across
sessions), :max_calls_per_minute (120 admissions per session window),
:timeout (30_000 ms, shortened by a tool timeout),
:page_size (64 tools) and :max_payload_bytes (1 MiB post-decode).
Saturation rejects immediately. An exclusive tool requires an idle server.
Summary
Types
A per-request policy for an authenticated principal and one catalogue kind.
Return {:ok, selectors} with tool names, resource URIs or prompt names.
Each selector is a nonempty UTF-8 string. An empty list hides all entries.
Unknown selectors are ignored. A failure rejects the request before lookup.
The deadline is absolute monotonic milliseconds. Use it to bound policy IO.
Functions
Export a one-shot agent as a function tool accepting a string prompt.
Its configured usage limits apply. An agent without a finite request limit
receives a limit of eight model requests. :timeout bounds the entire call.
:permissions applies to the agent's own tools. A single JSON Schema
branch without output validators or a function supplies the default
:result_schema. Pass it explicitly for a transformed result or union.
The run receives the authenticated actor in its deps.
Returns a specification to start this module under a supervisor.
Close a principal's session, stopping any active calls.
Handle one decoded message. Pass nil for the first session ID and use the
returned ID on subsequent calls. A nil response acknowledges a notification
or a cancelled request. Supply the authenticated principal on every call.
Start the session and call owner. Invalid options raise before the process starts.
Types
@type catalog_policy() :: (%{ principal: term(), kind: :tools | :resources | :prompts, deadline: integer() } -> {:ok, [String.t()]} | {:error, term()})
A per-request policy for an authenticated principal and one catalogue kind.
Return {:ok, selectors} with tool names, resource URIs or prompt names.
Each selector is a nonempty UTF-8 string. An empty list hides all entries.
Unknown selectors are ignored. A failure rejects the request before lookup.
The deadline is absolute monotonic milliseconds. Use it to bound policy IO.
@type session() :: binary() | nil
Functions
@spec agent_tool(ExAgent.t(), keyword()) :: ExAgent.Tool.t()
Export a one-shot agent as a function tool accepting a string prompt.
Its configured usage limits apply. An agent without a finite request limit
receives a limit of eight model requests. :timeout bounds the entire call.
:permissions applies to the agent's own tools. A single JSON Schema
branch without output validators or a function supplies the default
:result_schema. Pass it explicitly for a transformed result or union.
The run receives the authenticated actor in its deps.
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec close(GenServer.server(), binary(), term(), keyword()) :: :ok | {:error, :unknown_session | :session_required | :unavailable | :result_unconfirmed}
Close a principal's session, stopping any active calls.
The relative :timeout (5,000 ms by default) includes owner lookup.
Absence or expiry before dispatch returns {:error, :unavailable}.
Losing the acknowledgement after dispatch returns {:error, :result_unconfirmed}.
@spec handle(GenServer.server(), session(), term(), term()) :: reply()
Handle one decoded message. Pass nil for the first session ID and use the
returned ID on subsequent calls. A nil response acknowledges a notification
or a cancelled request. Supply the authenticated principal on every call.
If the owner cannot receive the message, returns {:error, :unavailable}.
Losing the owner after dispatch returns {:error, :result_unconfirmed}.
Reconcile any possible effect before sending another command.
@spec start_link(keyword()) :: GenServer.on_start()
Start the session and call owner. Invalid options raise before the process starts.