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

MRMCP_API Server ()
MRMCP_API Server (Server &&)
MRMCP_API Serveroperator= (Server &&)
MRMCP_API ~Server ()
MRMCP_API bool addTool (std::string id, std::string name, std::string desc, Schema::Base inputSchema, Schema::Base outputSchema, ToolFunc func)
MRMCP_API const ParamsgetParams () const
MRMCP_API void setParams (Params params)
 This restarts the server if necessary.
MRMCP_API bool isRunning () const
MRMCP_API bool setRunning (bool enable)
MRMCP_API void shutdown ()
MRMCP_API nlohmann::json dumpToolsAsJson () const
MRMCP_API Expected< void > saveToolsCache (const std::filesystem::path &path) const
MRMCP_API 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]

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

◆ Server() [2/2]

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

◆ ~Server()

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

Member Function Documentation

◆ addTool()

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

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. 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()

MRMCP_API 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()

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

◆ isRunning()

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

◆ operator=()

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

◆ saveToolsCache()

MRMCP_API 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()

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

This restarts the server if necessary.

◆ setRunning()

MRMCP_API 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()

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

◆ shutdown()

MRMCP_API 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: