89 lines
2.5 KiB
C++
89 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include <Void/Data/StrictJson.hpp>
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/MCP/Types.hpp>
|
|
|
|
#include <cstdint>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <variant>
|
|
|
|
namespace Void::MCP
|
|
{
|
|
using JsonValue = Void::Json::Strict::Value;
|
|
|
|
/// JSON-RPC request id: null, an integer, or a string.
|
|
struct JsonRpcId {
|
|
std::variant<std::monostate, std::int64_t, std::string> value{};
|
|
|
|
[[nodiscard]] bool is_null() const noexcept
|
|
{
|
|
return std::holds_alternative<std::monostate>(value);
|
|
}
|
|
|
|
[[nodiscard]] bool
|
|
operator==(const JsonRpcId&) const noexcept = default;
|
|
};
|
|
|
|
struct ErrorObject {
|
|
std::int32_t code{0};
|
|
std::string message{};
|
|
std::optional<JsonValue> data{};
|
|
};
|
|
|
|
struct Request {
|
|
JsonRpcId id{};
|
|
std::string method{};
|
|
std::optional<JsonValue> params{};
|
|
RequestMetadata metadata{};
|
|
};
|
|
|
|
struct Notification {
|
|
std::string method{};
|
|
std::optional<JsonValue> params{};
|
|
RequestMetadata metadata{};
|
|
};
|
|
|
|
struct Response {
|
|
JsonRpcId id{};
|
|
std::optional<JsonValue> result{};
|
|
std::optional<ErrorObject> error{};
|
|
};
|
|
|
|
using Message = std::variant<Request, Notification, Response>;
|
|
|
|
/// Encode one JSON-RPC message. Request/notification metadata is merged
|
|
/// into `params._meta`; responses carry exactly one of result or error.
|
|
[[nodiscard]] Void::Result::Result<std::string>
|
|
encode_message(const Message& message, const McpLimits& limits = {});
|
|
|
|
/// Decode exactly one JSON-RPC message, rejecting malformed envelopes,
|
|
/// duplicate keys, unknown id types, and limit violations.
|
|
[[nodiscard]] Void::Result::Result<Message>
|
|
decode_message(std::string_view text, const McpLimits& limits = {});
|
|
|
|
/// Parse `_meta` metadata out of a params object (or a `_meta` object).
|
|
[[nodiscard]] Void::Result::Result<RequestMetadata>
|
|
parse_metadata(const JsonValue& params, const McpLimits& limits = {});
|
|
|
|
/// Build the `_meta` object for a request metadata value.
|
|
[[nodiscard]] Void::Result::Result<JsonValue>
|
|
metadata_to_json(const RequestMetadata& metadata,
|
|
const McpLimits& limits = {});
|
|
|
|
[[nodiscard]] Void::Result::Result<std::string>
|
|
encode_json(const JsonValue& value, const McpLimits& limits = {});
|
|
|
|
[[nodiscard]] Void::Result::Result<JsonValue>
|
|
decode_json(std::string_view text, const McpLimits& limits = {});
|
|
|
|
/// Error response helper (null id is used for transport-level failures).
|
|
[[nodiscard]] Response
|
|
make_error_response(const JsonRpcId& id, std::int32_t code,
|
|
std::string message,
|
|
std::optional<JsonValue> data = {});
|
|
|
|
} // namespace Void::MCP
|