MeshLib C++ Docs
Loading...
Searching...
No Matches
MRMcp.h
Go to the documentation of this file.
1#pragma once
2
3#include "exports.h"
4
5#include "MRMesh/MRExpected.h"
6
7#include <nlohmann/json.hpp>
8
9#include <filesystem>
10#include <memory>
11#include <string>
12#include <utility>
13#include <vector>
14
15namespace MR::Mcp
16{
17
19namespace Schema
20{
22 struct Base
23 {
24 protected:
25 nlohmann::json json;
26 Base( nlohmann::json json ) : json( std::move( json ) ) {}
27
28 public:
29 [[nodiscard]] const nlohmann::json& asJson() const & { return json; }
30 [[nodiscard]] nlohmann::json&& asJson() && { return std::move( json ); }
31 };
32
34 struct Number : Base
35 {
37 : Base( nlohmann::json::object( {
38 { "type", "number" },
39 } ) )
40 {}
41 };
42
44 struct String : Base
45 {
47 : Base( nlohmann::json::object( {
48 { "type", "string" },
49 } ) )
50 {}
51
53 explicit String( std::vector<std::string> allowedValues )
54 : String()
55 {
56 json["enum"] = std::move( allowedValues );
57 }
58 };
59
61 struct Bool : Base
62 {
64 : Base( nlohmann::json::object( {
65 { "type", "boolean" },
66 } ) )
67 {}
68 };
69
73 struct Array : Base
74 {
75 Array( Base elemSchema )
76 : Base( nlohmann::json::object( {
77 { "type", "array" },
78 { "items", std::move( elemSchema ).asJson() },
79 } ) )
80 {}
81 };
82
84 struct AnyOf : Base
85 {
86 AnyOf( std::vector<Base> alternatives )
87 : Base( nlohmann::json::object( {
88 { "anyOf", nlohmann::json::array() },
89 } ) )
90 {
91 for ( auto& alt : alternatives )
92 json["anyOf"].push_back( std::move( alt ).asJson() );
93 }
94 };
95
98 struct Object : Base
99 {
101 : Base( nlohmann::json::object( {
102 { "type", "object" },
103 { "properties", nlohmann::json::object() },
104 { "required", nlohmann::json::array() },
105 } ) )
106 {}
107
109 Object &addMember( std::string name, Base schema ) &
110 {
111 json.at("required").push_back( name );
112 addMemberOpt( std::move( name ), std::move( schema ) );
113 return *this;
114 }
115
116 Object &addMemberOpt( std::string name, Base schema ) &
117 {
118 json.at( "properties" ).push_back( nlohmann::json::object_t::value_type( std::move( name ), std::move( schema ).asJson() ) );
119 return *this;
120 }
121
123 [[nodiscard]] Object&& addMember( std::string name, Base schema ) &&
124 {
125 addMember( std::move( name ), std::move( schema ) );
126 return std::move( *this );
127 }
128
129 [[nodiscard]] Object&& addMemberOpt( std::string name, Base schema ) &&
130 {
131 addMemberOpt( std::move( name ), std::move( schema ) );
132 return std::move( *this );
133 }
134 };
135}
136
139{
140public:
141 struct Params
142 {
143 std::string address = "127.0.0.1";
144 int port = 7887;
145 std::string name;
146 std::string version;
147
148 friend bool operator==( const Params&, const Params& ) = default;
149
150 MRMCP_API Params();
151 };
152
153 MRMCP_API Server();
154 MRMCP_API Server( Server&& );
155 MRMCP_API Server& operator=( Server&& );
156 MRMCP_API ~Server();
157
159 using ToolFunc = std::function<nlohmann::json( const nlohmann::json& args )>;
160
173 MRMCP_API bool addTool( std::string id, std::string name, std::string desc, Schema::Base inputSchema, Schema::Base outputSchema, ToolFunc func,
174 bool readOnly = false );
175
176 [[nodiscard]] MRMCP_API const Params& getParams() const;
177
179 MRMCP_API void setParams( Params params );
180
181 [[nodiscard]] MRMCP_API bool isRunning() const;
184 MRMCP_API bool setRunning( bool enable );
185
190 MRMCP_API void shutdown();
191
195 [[nodiscard]] MRMCP_API nlohmann::json dumpToolsAsJson() const;
196
199 MRMCP_API Expected<void> saveToolsCache( const std::filesystem::path& path ) const;
200
205 using ToolValidator = std::function<Expected<void>( const std::string& toolId )>;
206 MRMCP_API void setToolValidator( ToolValidator validator );
207
208private:
209 struct State;
210
212 std::unique_ptr<State> state_;
213
214 Params params_;
215};
216
218[[nodiscard]] MRMCP_API Server& getDefaultServer();
219
220}
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.
bool isRunning() const
Server & operator=(Server &&)
Expected< void > saveToolsCache(const std::filesystem::path &path) const
const Params & getParams() const
Server(Server &&)
bool setRunning(bool enable)
tl::expected< T, E > Expected
Definition MRExpected.h:31
This is used to build json schemas.
Definition MRMcp.h:20
Definition MRMcp.h:16
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
Definition MRMcp.h:142
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