MeshLib C++ Docs
Loading...
Searching...
No Matches
MR::Mcp::Server Class Reference

Owns a HTTP MCP server (using the SSE protocol). More...

#include <MRMcp/MRMcp.h>

Classes

struct  Params

Public Types

using ToolFunc = std::function<nlohmann::json( const nlohmann::json& args )>
 Those functions are allowed to throw, that's how you report errors to the MCP.
using ToolValidator = std::function<Expected<void>( const std::string& toolId )>

Public Member Functions

 Server ()
 Server (Server &&)
Server & operator= (Server &&)
 ~Server ()
bool addTool (std::string id, std::string name, std::string desc, Schema::Base inputSchema, Schema::Base outputSchema, ToolFunc func, bool readOnly=false)
const Params & getParams () const
void setParams (Params params)
 This restarts the server if necessary.
bool isRunning () const
bool setRunning (bool enable)
void shutdown ()
nlohmann::json dumpToolsAsJson () const
Expected< void > saveToolsCache (const std::filesystem::path &path) const
void setToolValidator (ToolValidator validator)

Detailed Description

Owns a HTTP MCP server (using the SSE protocol).

Member Typedef Documentation

◆ ToolFunc

using MR::Mcp::Server::ToolFunc = std::function<nlohmann::json( const nlohmann::json& args )>

Those functions are allowed to throw, that's how you report errors to the MCP.

◆ ToolValidator

using MR::Mcp::Server::ToolValidator = std::function<Expected<void>( const std::string& toolId )>

Optional predicate consulted before every tool dispatch, given the tool's id. Return {} to allow; return unexpected("reason") to block — the reason surfaces to the MCP client as the tool-call error. Evaluated per call, so changes (e.g. user sign-in) take effect immediately.

Constructor & Destructor Documentation

◆ Server() [1/2]

MR::Mcp::Server::Server ( )

◆ Server() [2/2]

MR::Mcp::Server::Server ( Server && )

◆ ~Server()

MR::Mcp::Server::~Server ( )

Member Function Documentation

◆ addTool()

bool MR::Mcp::Server::addTool ( std::string id,
std::string name,
std::string desc,
Schema::Base inputSchema,
Schema::Base outputSchema,
ToolFunc func,
bool readOnly = false )

Registers a new tool.

Parameters
idAn arbitrary function name, e.g. foo.bar.
nameA human/ai-readable name.
descA human/ai-readable explanation of what the tool does.
inputSchemaDescribes the arguments. Normally it should be Schema::Object{} with some fields added.
outputSchemaDescribes the returned JSON.
readOnlyThe tool does not change the application's state; clients get it as the readOnlyHint annotation, so they may e.g. run such calls concurrently or approve them more easily. Fails if the tool with this id already exists. Must be called early, before setRunning(true) is called for the first time, otherwise fails. Returns true on success. Asserts when returning false, so you don't have to check the return value. NOTE: Consult docs/testing_mcp.md for how to test your tool.

◆ dumpToolsAsJson()

nlohmann::json MR::Mcp::Server::dumpToolsAsJson ( ) const
nodiscard

Returns the list of currently-registered tools as a JSON array of MCP tool entries (name, optional title/description, inputSchema, outputSchema). Suitable for splicing into a tools/list response or persisting to a cache file.

◆ getParams()

const Params & MR::Mcp::Server::getParams ( ) const
nodiscard

◆ isRunning()

bool MR::Mcp::Server::isRunning ( ) const
nodiscard

◆ operator=()

Server & MR::Mcp::Server::operator= ( Server && )

◆ saveToolsCache()

Expected< void > MR::Mcp::Server::saveToolsCache ( const std::filesystem::path & path) const

Atomically writes { "tools": dumpToolsAsJson() } to path, creating parent directories as needed. Returns an error message on I/O failure.

◆ setParams()

void MR::Mcp::Server::setParams ( Params params)

This restarts the server if necessary.

◆ setRunning()

bool MR::Mcp::Server::setRunning ( bool enable)

Returns true on success, including if the server is already running and you're trying to start it again. Stopping always returns true.

◆ setToolValidator()

void MR::Mcp::Server::setToolValidator ( ToolValidator validator)

◆ shutdown()

void MR::Mcp::Server::shutdown ( )

Tears down the running asio server (if any) and clears all registered tools. Use before unloading DLLs whose translation units called addTool — their captured std::function deleters dangle once those DLLs are unmapped, so a later ~Server would segfault. Idempotent. Safe to call when nothing was registered.


The documentation for this class was generated from the following file: