ExMCP.Server.Context (ex_mcp v1.3.0)

Copy Markdown View Source

Access to the validated context of the currently executing server callback.

This is primarily useful for MRTR-aware handlers that retain their existing callback arity. The value is scoped to the callback invocation and must not be read later from a spawned process.

cancelled?/0 reports whether the current request id has been cancelled.

Summary

Functions

Returns true if the current request id has been cancelled.

Reports progress for the currently executing request callback.

Sends a log notification on the currently executing request's HTTP stream.

Functions

cancelled?()

@spec cancelled?() :: boolean()

Returns true if the current request id has been cancelled.

Safe to call from inside a running handler: cancel is recorded out of band when notifications/cancelled is accepted, so this does not wait for the server GenServer to finish the current callback.

Returns false when there is no request context or the current id has not been cancelled. The server MAY stop work when this is true; ExMCP does not automatically abort the JSON-RPC request.

current()

@spec current() :: ExMCP.Server.RequestContext.t() | nil

input_responses()

@spec input_responses() :: map() | nil

progress_token()

@spec progress_token() :: ExMCP.Types.progress_token() | nil

report_progress(progress, total \\ nil, message \\ nil)

@spec report_progress(number(), number() | nil, String.t() | nil) ::
  :ok
  | {:error,
     :no_request_context
     | :progress_not_requested
     | :request_not_streaming
     | :stream_closed
     | :stream_timeout}

Reports progress for the currently executing request callback.

Modern streamable-HTTP handlers use this request-scoped helper instead of a connection-wide server process. The request must include _meta.progressToken, and its HTTP response must be an active SSE stream. Delivery is acknowledged before this function returns, which guarantees the notification is written before the request's final JSON-RPC response.

request_state()

@spec request_state() :: term()

send_log_message(level, message, data \\ %{})

@spec send_log_message(atom() | String.t(), String.t(), map()) ::
  :ok
  | {:error,
     :no_request_context
     | :logging_not_requested
     | :invalid_log_level
     | :request_not_streaming
     | :stream_closed
     | :stream_timeout}

Sends a log notification on the currently executing request's HTTP stream.

Request-scoped log delivery is available only while that request owns an active SSE response. It never falls back to another request or subscription stream.

MCP protocol Logging is deprecated as of 2026-07-28 and retained throughout ExMCP 1.x. Prefer stderr for stdio diagnostics or OpenTelemetry for new structured-observability integrations.