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 };
52
54 struct Bool : Base
55 {
57 : Base( nlohmann::json::object( {
58 { "type", "boolean" },
59 } ) )
60 {}
61 };
62
64 struct Array : Base
65 {
66 Array( Base elemSchema )
67 : Base( nlohmann::json::object( {
68 { "type", "array" },
69 { "items", std::move( elemSchema ).asJson() },
70 } ) )
71 {}
72 };
73
76 struct Object : Base
77 {
79 : Base( nlohmann::json::object( {
80 { "type", "object" },
81 { "properties", nlohmann::json::object() },
82 { "required", nlohmann::json::array() },
83 } ) )
84 {}
85
87 Object &addMember( std::string name, Base schema ) &
88 {
89 json.at("required").push_back( name );
90 addMemberOpt( std::move( name ), std::move( schema ) );
91 return *this;
92 }
93
94 Object &addMemberOpt( std::string name, Base schema ) &
95 {
96 json.at( "properties" ).push_back( nlohmann::json::object_t::value_type( std::move( name ), std::move( schema ).asJson() ) );
97 return *this;
98 }
99
101 [[nodiscard]] Object&& addMember( std::string name, Base schema ) &&
102 {
103 addMember( std::move( name ), std::move( schema ) );
104 return std::move( *this );
105 }
106
107 [[nodiscard]] Object&& addMemberOpt( std::string name, Base schema ) &&
108 {
109 addMemberOpt( std::move( name ), std::move( schema ) );
110 return std::move( *this );
111 }
112 };
113}
114
117{
118public:
119 struct Params
120 {
121 std::string address = "127.0.0.1";
122 int port = 7887;
123 std::string name;
124 std::string version;
125
126 friend bool operator==( const Params&, const Params& ) = default;
127
128 MRMCP_API Params();
129 };
130
131 MRMCP_API Server();
132 MRMCP_API Server( Server&& );
133 MRMCP_API Server& operator=( Server&& );
134 MRMCP_API ~Server();
135
137 using ToolFunc = std::function<nlohmann::json( const nlohmann::json& args )>;
138
149 MRMCP_API bool addTool( std::string id, std::string name, std::string desc, Schema::Base inputSchema, Schema::Base outputSchema, ToolFunc func );
150
151 [[nodiscard]] MRMCP_API const Params& getParams() const;
152
154 MRMCP_API void setParams( Params params );
155
156 [[nodiscard]] MRMCP_API bool isRunning() const;
159 MRMCP_API bool setRunning( bool enable );
160
165 MRMCP_API void shutdown();
166
170 [[nodiscard]] MRMCP_API nlohmann::json dumpToolsAsJson() const;
171
174 MRMCP_API Expected<void> saveToolsCache( const std::filesystem::path& path ) const;
175
180 using ToolValidator = std::function<Expected<void>( const std::string& toolId )>;
181 MRMCP_API void setToolValidator( ToolValidator validator );
182
183private:
184 struct State;
185
187 std::unique_ptr<State> state_;
188
189 Params params_;
190};
191
193[[nodiscard]] MRMCP_API Server& getDefaultServer();
194
195}
Owns a HTTP MCP server (using the SSE protocol).
Definition MRMcp.h:117
MRMCP_API bool addTool(std::string id, std::string name, std::string desc, Schema::Base inputSchema, Schema::Base outputSchema, ToolFunc func)
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:137
MRMCP_API void setParams(Params params)
This restarts the server if necessary.
MRMCP_API Server(Server &&)
std::function< Expected< void >(const std::string &toolId)> ToolValidator
Definition MRMcp.h:180
MRMCP_API Expected< void > saveToolsCache(const std::filesystem::path &path) const
MRMCP_API Server & operator=(Server &&)
MRMCP_API const Params & getParams() const
MRMCP_API void shutdown()
MRMCP_API void setToolValidator(ToolValidator validator)
MRMCP_API Server()
MRMCP_API ~Server()
MRMCP_API nlohmann::json dumpToolsAsJson() const
MRMCP_API bool setRunning(bool enable)
MRMCP_API bool isRunning() const
tl::expected< T, E > Expected
Definition MRExpected.h:31
This is used to build json schemas.
Definition MRMcp.h:20
Definition MRMcp.h:16
MRMCP_API Server & getDefaultServer()
The global instance of the MCP server.
Array(Base elemSchema)
Definition MRMcp.h:66
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:56
Number()
Definition MRMcp.h:36
Object & addMemberOpt(std::string name, Base schema) &
Add optional member. Returns a reference to *this.
Definition MRMcp.h:94
Object && addMember(std::string name, Base schema) &&
Add required member. Returns a reference to *this.
Definition MRMcp.h:101
Object && addMemberOpt(std::string name, Base schema) &&
Add optional member. Returns a reference to *this.
Definition MRMcp.h:107
Object & addMember(std::string name, Base schema) &
Add required member. Returns a reference to *this.
Definition MRMcp.h:87
Object()
Definition MRMcp.h:78
String()
Definition MRMcp.h:46
Definition MRMcp.h:120
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:121
std::string name
A default string is set in the constructor.
Definition MRMcp.h:123
std::string version
A default string is set in the constructor.
Definition MRMcp.h:124
int port
Definition MRMcp.h:122