Unravel Engine C++ Reference
Loading...
Searching...
No Matches
mcp_manager.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <atomic>
6#include <base/basetypes.hpp>
7#include <chrono>
8#include <context/context.hpp>
9#include <deque>
10#include <functional>
11#include <memory>
12#include <mutex>
13#include <optional>
14#include <string>
15#include <thread>
16#include <type_traits>
17#include <utility>
18#include <vector>
19
20#include <threadpp/future.hpp>
21#include <threadpp/thread.h>
22
23namespace httplib
24{
25class Server;
26}
27
28namespace unravel
29{
30
32{
33 std::chrono::system_clock::time_point timestamp{};
34 std::string category;
35 std::string message;
36 bool is_error{false};
37};
38
40{
41public:
42 static constexpr const char* k_host = "127.0.0.1";
43 static constexpr int k_default_port = 27182;
44 static constexpr size_t k_max_activity_entries = 256;
45
48
49 auto init(rtti::context& ctx) -> bool;
50 auto deinit(rtti::context& ctx) -> bool;
51
52 void start();
53 void stop();
54 auto is_running() const -> bool;
55 auto is_enabled() const -> bool;
56 auto get_host() const -> const char*;
57 auto get_port() const -> int;
58 auto get_endpoint_url() const -> std::string;
59 auto get_health_url() const -> std::string;
60 auto get_tool_count() const -> size_t;
61 auto get_request_count() const -> uint64_t;
62 auto get_tool_call_count() const -> uint64_t;
63 auto get_error_count() const -> uint64_t;
64
65 auto snapshot_activity() const -> std::vector<mcp_activity_entry>;
66 void clear_activity();
67
70 template<typename Fn>
71 auto invoke_on_main(Fn&& fn, std::chrono::milliseconds timeout = std::chrono::milliseconds(5000))
72 -> std::optional<std::invoke_result_t<std::decay_t<Fn>>>
73 {
74 using result_t = std::invoke_result_t<std::decay_t<Fn>>;
75 static_assert(!std::is_void_v<result_t>, "invoke_on_main requires a non-void return type");
76
77 tpp::this_thread::register_this_thread();
78 auto future = tpp::async(tpp::main_thread::get_id(), std::forward<Fn>(fn));
79 if(future.wait_for(timeout) != std::future_status::ready)
80 {
81 error_count_.fetch_add(1);
82 log_activity("rpc", "Timed out waiting for main thread", true);
83 return std::nullopt;
84 }
85
86 try
87 {
88 return future.get();
89 }
90 catch(const std::exception& ex)
91 {
92 error_count_.fetch_add(1);
93 log_activity("rpc", std::string("Exception on main thread: ") + ex.what(), true);
94 return std::nullopt;
95 }
96 catch(...)
97 {
98 error_count_.fetch_add(1);
99 log_activity("rpc", "Exception on main thread", true);
100 return std::nullopt;
101 }
102 }
103
104private:
105 using job_fn = std::function<std::string()>;
106
107 auto run_on_main_thread(job_fn job, std::chrono::milliseconds timeout = std::chrono::milliseconds(5000))
108 -> std::string;
109 auto handle_http_request(const std::string& body) -> std::string;
110 auto dispatch_json_rpc(const std::string& body) -> std::string;
111 auto execute_tool_call(const std::string& tool_name,
112 const std::string& args_json,
113 const std::optional<std::string>& id) -> std::string;
114 void log_activity(std::string category, std::string message, bool is_error = false);
115
116 rtti::context* ctx_{nullptr};
117 mcp::mcp_tool_registry registry_;
118 std::unique_ptr<httplib::Server> server_;
119 std::thread server_thread_;
120 std::atomic_bool running_{false};
121 bool enabled_{true};
122 int port_{k_default_port};
123
124 mutable std::mutex activity_mutex_;
125 std::deque<mcp_activity_entry> activity_;
126 std::atomic_uint64_t request_count_{0};
127 std::atomic_uint64_t tool_call_count_{0};
128 std::atomic_uint64_t error_count_{0};
129};
130
131} // namespace unravel
auto get_request_count() const -> uint64_t
static constexpr const char * k_host
Definition mcp_manager.h:42
auto get_error_count() const -> uint64_t
static constexpr int k_default_port
Definition mcp_manager.h:43
auto get_tool_call_count() const -> uint64_t
auto get_health_url() const -> std::string
auto snapshot_activity() const -> std::vector< mcp_activity_entry >
static constexpr size_t k_max_activity_entries
Definition mcp_manager.h:44
auto get_host() const -> const char *
auto get_endpoint_url() const -> std::string
auto invoke_on_main(Fn &&fn, std::chrono::milliseconds timeout=std::chrono::milliseconds(5000)) -> std::optional< std::invoke_result_t< std::decay_t< Fn > > >
Definition mcp_manager.h:71
auto is_enabled() const -> bool
auto deinit(rtti::context &ctx) -> bool
auto get_tool_count() const -> size_t
auto is_running() const -> bool
auto init(rtti::context &ctx) -> bool
auto get_port() const -> int
Hash specialization for batch_key to enable use in std::unordered_map.
Definition mcp_manager.h:32
bool is_error
Definition mcp_manager.h:36
std::string category
Definition mcp_manager.h:34
std::chrono::system_clock::time_point timestamp
Definition mcp_manager.h:33
std::string message
Definition mcp_manager.h:35