7#include <nlohmann/json.hpp>
29 [[nodiscard]]
const nlohmann::json&
asJson() const & {
return json; }
30 [[nodiscard]] nlohmann::json&&
asJson() && {
return std::move(
json ); }
53 explicit String( std::vector<std::string> allowedValues )
56 json[
"enum"] = std::move( allowedValues );
65 {
"type",
"boolean" },
78 {
"items", std::move( elemSchema ).asJson() },
86 AnyOf( std::vector<Base> alternatives )
88 {
"anyOf", nlohmann::json::array() },
91 for (
auto& alt : alternatives )
92 json[
"anyOf"].push_back( std::move( alt ).
asJson() );
102 {
"type",
"object" },
103 {
"properties", nlohmann::json::object() },
104 {
"required", nlohmann::json::array() },
111 json.at(
"required").push_back( name );
118 json.at(
"properties" ).push_back( nlohmann::json::object_t::value_type( std::move( name ), std::move( schema ).
asJson() ) );
125 addMember( std::move( name ), std::move( schema ) );
126 return std::move( *
this );
132 return std::move( *
this );
159 using ToolFunc = std::function<nlohmann::json(
const nlohmann::json& args )>;
174 bool readOnly =
false );
205 using ToolValidator = std::function<Expected<void>(
const std::string& toolId )>;
212 std::unique_ptr<State> state_;
Owns a HTTP MCP server (using the SSE protocol).
Definition MRMcp.h:139
std::function< nlohmann::json(const nlohmann::json &args)> ToolFunc
Those functions are allowed to throw, that's how you report errors to the MCP.
Definition MRMcp.h:159
bool addTool(std::string id, std::string name, std::string desc, Schema::Base inputSchema, Schema::Base outputSchema, ToolFunc func, bool readOnly=false)
std::function< Expected< void >(const std::string &toolId)> ToolValidator
Definition MRMcp.h:205
nlohmann::json dumpToolsAsJson() const
void setToolValidator(ToolValidator validator)
void setParams(Params params)
This restarts the server if necessary.
Server & operator=(Server &&)
Expected< void > saveToolsCache(const std::filesystem::path &path) const
const Params & getParams() const
bool setRunning(bool enable)
tl::expected< T, E > Expected
Definition MRExpected.h:31
This is used to build json schemas.
Definition MRMcp.h:20
Server & getDefaultServer()
The global instance of the MCP server.
AnyOf(std::vector< Base > alternatives)
Definition MRMcp.h:86
Array(Base elemSchema)
Definition MRMcp.h:75
A common base class for the different schemas. Functions can accept this by value,...
Definition MRMcp.h:23
nlohmann::json && asJson() &&
Definition MRMcp.h:30
Base(nlohmann::json json)
Definition MRMcp.h:26
const nlohmann::json & asJson() const &
Definition MRMcp.h:29
nlohmann::json json
Definition MRMcp.h:25
Bool()
Definition MRMcp.h:63
Number()
Definition MRMcp.h:36
Object & addMemberOpt(std::string name, Base schema) &
Add optional member. Returns a reference to *this.
Definition MRMcp.h:116
Object && addMember(std::string name, Base schema) &&
Add required member. Returns a reference to *this.
Definition MRMcp.h:123
Object && addMemberOpt(std::string name, Base schema) &&
Add optional member. Returns a reference to *this.
Definition MRMcp.h:129
Object & addMember(std::string name, Base schema) &
Add required member. Returns a reference to *this.
Definition MRMcp.h:109
Object()
Definition MRMcp.h:100
String(std::vector< std::string > allowedValues)
A string limited to one of allowedValues.
Definition MRMcp.h:53
String()
Definition MRMcp.h:46
friend bool operator==(const Params &, const Params &)=default
std::string address
You don't need to change this, unless you want to accept connections from the outside world.
Definition MRMcp.h:143
std::string name
A default string is set in the constructor.
Definition MRMcp.h:145
std::string version
A default string is set in the constructor.
Definition MRMcp.h:146
int port
Definition MRMcp.h:144