Boost C++ Libraries

PrevUpHomeNext

Class request_scope

boost::taar::jsonrpc::request_scope — Per-request state a JSON-RPC method or MCP tool can ask for by declaring a request_scope const& (or std::shared_ptr<request_scope>) parameter.

Synopsis

// In header: <boost/taar/jsonrpc/request_scope.hpp>


class request_scope : public std::enable_shared_from_this< request_scope > {
public:

  // public member functions
  request_scope(boost::json::value, boost::asio::any_io_executor);
  boost::json::value const & id() const noexcept;
  boost::asio::cancellation_slot slot();
  bool cancelled() const noexcept;
  void cancel(boost::asio::cancellation_type = boost::asio::cancellation_type::all);
  void send_notification(std::string_view, boost::json::value = {}) const;
  void report_progress(double, std::optional< double > = std::nullopt, 
                       std::string = {}) const;

  // public data members
  std::optional< boost::json::value > progress_token;  // MCP _meta.progressToken of the request, when the client sent one. 
  std::function< void(boost::json::value)> notify;  // Where notifications go (set by the endpoint); empty on transports without a stream. 
};

Description

It carries the request id, a cancellation slot that the endpoint fires when the request is cancelled (asynchronous operations the callable awaits are interrupted; a synchronous callable can poll cancelled), and a sink for server-to-client notifications such as MCP progress.

mcp.tool("export", "Long running", [](jsonrpc::request_scope const& scope, int items)
    -> taar::awaitable<std::string>
{
    for (int i = 0; i < items && !scope.cancelled(); ++i)
    {
        co_await do_one(i);
        scope.report_progress(i + 1, items);
    }
    co_return "done";
}, mcp_arg("items"));

request_scope public member functions

  1. request_scope(boost::json::value id, boost::asio::any_io_executor executor);
  2. boost::json::value const & id() const noexcept;
    The request's id (null for notifications).
  3. boost::asio::cancellation_slot slot();
    The slot the endpoint binds to the method's coroutine.

    Not for callables: the coroutine's own cancellation state already interrupts every co_await inside it when the request is cancelled, and assigning this slot would replace that binding.

  4. bool cancelled() const noexcept;
    True once cancel was called.
  5. void cancel(boost::asio::cancellation_type type = boost::asio::cancellation_type::all);
    Cancel the request: marks it and emits the signal on the request's executor.
  6. void send_notification(std::string_view method, 
                           boost::json::value params = {}) const;
    Send a JSON-RPC notification to the client, if the transport supports it.
  7. void report_progress(double progress, 
                         std::optional< double > total = std::nullopt, 
                         std::string message = {}) const;
    Send an MCP notifications/progress for this request.

    No-op when the client sent no progressToken or the transport has no stream.


PrevUpHomeNext