ExAgent.MCP.Server.Plug (ExAgent v3.0.0)

View Source

Streamable HTTP endpoint for a supervised ExAgent.MCP.Server. Mount at a dedicated path before a body parser. The host supplies the HTTP listener.

Stability — Stable.

Options are :server (registered server or pid), :authorize (required function of the connection returning {:ok, actor} or {:error, reason}), :origins (exact allowed Origin strings, default empty) and :max_body_bytes (default 1 MiB). Requests without Origin still require authorization. Return a stable principal identifier as the actor. All operations, including cancellation and deletion, authenticate independently. The server's optional :catalog_policy receives that actor on each catalogue request. POST responses use Cache-Control: no-store.

To publish OAuth Protected Resource Metadata, set the candidate :protected_resource option to a keyword list with the public :resource URL, a non-empty :authorization_servers list of issuer URLs and optional :scopes_supported. Mount this Plug for both the MCP endpoint and its well-known GET path, for example /mcp and /.well-known/oauth-protected-resource/mcp. Host-only resources use /.well-known/oauth-protected-resource. URLs use HTTPS, with HTTP accepted on loopback for local testing. The metadata GET is public.

For protected requests the host's :authorize verifies the Bearer token, including its issuer, expiry, intended audience and scopes. Return {:ok, actor} on success, {:error, :unauthenticated} for absent credentials, {:error, :invalid_token} for an invalid token and {:error, {:insufficient_scope, scopes}} for a valid token lacking the listed scopes. Use {:error, :insufficient_scope} if the scope names are unavailable. Either 401 reason can also be paired with a list of required scopes, as in {:error, {:unauthenticated, ["notes:read"]}}. The Plug sends the corresponding Bearer challenge before dispatch.

Set :trace to :local for OpenTelemetry MCP server spans under the local context, :trusted to accept authenticated peers' W3C Trace Context, or a one-argument function returning true for trusted authenticated actors. The default is false. Propagation reads params._meta after authentication and discards baggage. Missing or invalid context uses the local context.

Tool calls return JSON or stream JSON-RPC over SSE on POST when progress or form elicitation is needed. Discovery and initialization return JSON. Notifications and approval replies receive 202. GET returns 405 for an active session or a request without a session ID. An unknown or other-principal session receives 404. Streams are ephemeral. DELETE closes the current session and stops its active calls.

Set :protocol_version to "2026-07-28" for a stateless POST endpoint. It supports discovery and JSON responses for the static catalogue and synchronous tools. Configure :mrtr_approval on the server for signed form approval across requests. The default "2025-06-18" profile supports session quotas and live elicitation. Mount each protocol at its own path to serve both. The host's authorizer validates credentials on every MCP request.