cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
roots.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
11
12#include <optional>
13#include <string>
14#include <utility>
15#include <vector>
16
20
21namespace mcp::protocol {
22
24struct Root {
26 std::string uri;
28 std::optional<std::string> name;
30 std::optional<Json> meta;
32 Json extensions = Json::object();
33};
34
35template <>
36struct Reflect<Root> {
37 static constexpr bool defined = true;
38 static auto fields() {
39 return std::make_tuple(
40 field("uri", &Root::uri), field("name", &Root::name),
41 field("_meta", &Root::meta),
42 extensions_field(&Root::extensions, {"uri", "name", "_meta"}));
43 }
44 static std::vector<std::string> known_keys() {
45 return {"uri", "name", "_meta"};
46 }
47};
48
52 std::vector<Root> roots;
54 std::optional<Json> meta;
56 Json extensions = Json::object();
57};
58
60inline core::Error roots_json_error(std::string message) {
61 return core::Error{
62 static_cast<int>(ErrorCode::InvalidRequest), std::move(message), {}};
63}
64
66inline Json root_to_json(const Root& root) { return reflect_to_json(root); }
67
71 return reflect_from_json<Root>(json);
72}
73
76 Json json = Json::object();
77 json["roots"] = Json::array();
78 for (const auto& root : result.roots) {
79 json["roots"].push_back(root_to_json(root));
80 }
81 if (result.meta.has_value()) {
82 json["_meta"] = *result.meta;
83 }
85 return json;
86}
87
91 const Json& json) {
92 if (!json.is_object()) {
93 return mcp::core::unexpected(
94 roots_json_error("roots/list result must be an object"));
95 }
96 if (!json.contains("roots") || !json.at("roots").is_array()) {
97 return mcp::core::unexpected(
98 roots_json_error("roots/list result requires a roots array"));
99 }
100
101 RootsListResult result;
102 for (const auto& item : json.at("roots")) {
103 const auto root = root_from_json(item);
104 if (!root) {
105 return mcp::core::unexpected(root.error());
106 }
107 result.roots.push_back(*root);
108 }
109 if (json.contains("_meta")) {
110 if (!json.at("_meta").is_object()) {
111 return mcp::core::unexpected(
112 roots_json_error("roots/list result _meta must be an object"));
113 }
114 result.meta = json.at("_meta");
115 }
116 result.extensions = collect_json_extensions(json, {"roots", "_meta"});
117 return result;
118}
119
120} // namespace mcp::protocol
Shared JSON, JSON-RPC, error, cancellation, and progress model types.
void append_json_extensions(Json &json, const Json &extensions)
Flattens extension members into a JSON object without overwriting typed fields.
Definition types.hpp:358
nlohmann::json Json
JSON value type used by all protocol DTOs.
Definition types.hpp:28
Json collect_json_extensions(const Json &json, std::initializer_list< std::string_view > known_keys)
Collects unknown object members so typed DTOs can preserve future protocol fields and vendor extensio...
Definition types.hpp:320
C++17 tuple-reflection infrastructure for zero-boilerplate DTO serialization.
constexpr FieldDescriptor< Struct, Field > field(const char *wire_name, Field Struct::*pointer)
Creates a FieldDescriptor with wire name and pointer-to-member.
Definition reflect.hpp:75
ExtensionsField< Struct > extensions_field(Json Struct::*pointer, std::vector< std::string > own_keys)
Creates an ExtensionsField descriptor.
Definition reflect.hpp:90
Json reflect_to_json(const T &obj)
Serializes a DTO to JSON using its Reflect<T> trait.
Definition reflect.hpp:701
Shared result and error primitives used by the public cxxmcp SDK.
tl::expected< T, Error > Result
Alias for the SDK result type.
Definition result.hpp:64
core::Result< RootsListResult > roots_list_result_from_json(const Json &json)
Parses a roots/list result.
Definition roots.hpp:90
Json root_to_json(const Root &root)
Serializes a root entry.
Definition roots.hpp:66
Json roots_list_result_to_json(const RootsListResult &result)
Serializes a roots/list result.
Definition roots.hpp:75
core::Error roots_json_error(std::string message)
Builds an InvalidRequest error for roots JSON validation failures.
Definition roots.hpp:60
core::Result< Root > root_from_json(const Json &json)
Parses a root entry.
Definition roots.hpp:70
Structured error returned by fallible SDK operations.
Definition result.hpp:35
Primary template.
Definition reflect.hpp:199
One client root entry.
Definition roots.hpp:24
std::optional< Json > meta
Optional _meta extension object preserved on the wire.
Definition roots.hpp:30
Json extensions
Unknown JSON members preserved for forward-compatible round trips.
Definition roots.hpp:32
std::optional< std::string > name
Optional human-readable name for display.
Definition roots.hpp:28
std::string uri
Root URI. The interpretation is transport/client specific.
Definition roots.hpp:26
Result object for roots/list.
Definition roots.hpp:50
std::optional< Json > meta
Optional _meta extension object preserved on the wire.
Definition roots.hpp:54
std::vector< Root > roots
Roots currently available to the server.
Definition roots.hpp:52
Json extensions
Unknown JSON members preserved for forward-compatible round trips.
Definition roots.hpp:56