MeshLib C++ Docs
Loading...
Searching...
No Matches
MR::McpGateway Namespace Reference

Classes

struct  Config
class  MLClientTransport

Functions

std::atomic< bool > & getBackendAlive ()
bool probeBackendAlive (const std::string &targetUrl)
void updateBackendAliveAndNotify (bool nowAlive)
bool probeAndTrackBackend (const std::string &targetUrl)
std::mutex & gatewayStdoutMutex ()
void registerLocalTools (fastmcpp::ProxyApp &proxy, const Config &cfg)
 Registers the gateway-local launch and status tools on proxy.
std::vector< nlohmann::json > & getCachedTools ()
void ensureFreshCache (const Config &cfg)
void loadCachedTools (const Config &cfg)
bool spawnDetached (const std::filesystem::path &exe, const std::vector< std::string > &args)
bool spawnAndWait (const std::filesystem::path &exe, const std::vector< std::string > &args, std::chrono::seconds timeout)
std::filesystem::path pathFromUtf8 (const std::string &s)
std::string pathToUtf8 (const std::filesystem::path &p)

Function Documentation

◆ ensureFreshCache()

void MR::McpGateway::ensureFreshCache ( const Config & cfg)

If the cache file is missing or its build stamp doesn't match this binary, and the backend isn't already running, spawn the backend hidden so it dumps its tool schemas, then embed the build stamp into the just-written file. Best-effort — failures are logged to stderr but non-fatal.

◆ gatewayStdoutMutex()

std::mutex & MR::McpGateway::gatewayStdoutMutex ( )

Mutex held during direct gateway-side writes to stdout (notifications emitted from background threads). Protects against emit-vs-emit interleaving. Does NOT cover fastmcpp's own response writes — fastmcpp's stdio_server does not expose its writer mutex; treat emit-vs-response interleaving as a pre-existing fastmcpp limitation.

◆ getBackendAlive()

std::atomic< bool > & MR::McpGateway::getBackendAlive ( )

Single source of truth for "is the backend currently alive?". Updated by updateBackendAliveAndNotify (from probeAndTrackBackend and from the persistent transport's SSE connect/error callbacks).

◆ getCachedTools()

std::vector< nlohmann::json > & MR::McpGateway::getCachedTools ( )

Tool-schema entries loaded from the on-disk cache at startup. Spliced into tools/list responses by the gateway's handler when the backend is offline.

◆ loadCachedTools()

void MR::McpGateway::loadCachedTools ( const Config & cfg)

Parses mcp_tools_cache.json and populates getCachedTools(). Errors are non-fatal (logged + leaves the cache empty).

◆ pathFromUtf8()

std::filesystem::path MR::McpGateway::pathFromUtf8 ( const std::string & s)

UTF-8 std::string -> std::filesystem::path. On Windows this is non-trivial: path( std::string ) interprets the bytes as the system code page, which silently loses any character not representable in the current locale (e.g. CJK on US-Windows). On POSIX, paths are UTF-8 by convention so this is essentially a passthrough.

◆ pathToUtf8()

std::string MR::McpGateway::pathToUtf8 ( const std::filesystem::path & p)

std::filesystem::path -> UTF-8 std::string. Mirror of pathFromUtf8 — on Windows, path::string() narrows via the system code page, which corrupts non-ASCII chars before we hand the string off to spawn args / log output / cerr.

◆ probeAndTrackBackend()

bool MR::McpGateway::probeAndTrackBackend ( const std::string & targetUrl)

Probes the backend and routes the result through updateBackendAliveAndNotify, so a state transition synchronously emits notifications/tools/list_changed.

◆ probeBackendAlive()

bool MR::McpGateway::probeBackendAlive ( const std::string & targetUrl)

Raw HTTP liveness probe. No state updates, no transition events. Used at startup (before any client is connected, so transitions would be spurious).

◆ registerLocalTools()

void MR::McpGateway::registerLocalTools ( fastmcpp::ProxyApp & proxy,
const Config & cfg )

Registers the gateway-local launch and status tools on proxy.

◆ spawnAndWait()

bool MR::McpGateway::spawnAndWait ( const std::filesystem::path & exe,
const std::vector< std::string > & args,
std::chrono::seconds timeout )

Spawns exe with args as an attached child process and blocks until it exits or timeout elapses. On timeout the child is force-killed. Returns true iff the child ran to completion within the timeout (regardless of its exit code — caller decides what counts as success based on side effects, e.g. a written cache file).

◆ spawnDetached()

bool MR::McpGateway::spawnDetached ( const std::filesystem::path & exe,
const std::vector< std::string > & args )

Spawns exe with args as a detached child process and returns immediately. The child survives this process exiting (Windows: DETACHED_PROCESS; POSIX: setsid + double-fork). Returns true if the child was launched successfully (does not verify it then ran to completion).

◆ updateBackendAliveAndNotify()

void MR::McpGateway::updateBackendAliveAndNotify ( bool nowAlive)

Updates getBackendAlive() and emits notifications/tools/list_changed on a real alive↔dead transition. Suppresses the very first call (baseline-set, not a transition the client cares about). Safe to call from any thread: emit goes through gatewayStdoutMutex().