Files
Library/src/MCP/Legacy.cpp
T

171 lines
4.6 KiB
C++

#include <Void/MCP/Transport.hpp>
#include <Void/MCP/Envelope.hpp>
#include <algorithm>
#include <atomic>
#include <format>
#include <map>
#include <string>
#include <utility>
#include <variant>
namespace Void::MCP
{
namespace
{
[[nodiscard]] std::string default_session_id()
{
static std::atomic<std::uint64_t> counter{1};
return std::format("mcp-session-{}",
counter.fetch_add(1,
std::memory_order_relaxed));
}
[[nodiscard]] const JsonValue::Object*
as_object(const JsonValue& value) noexcept
{
return std::get_if<JsonValue::Object>(&value.value);
}
[[nodiscard]] std::string
param_string(const Request& request, std::string_view key)
{
if (!request.params.has_value()) {
return {};
}
const auto* object = as_object(*request.params);
if (object == nullptr) {
return {};
}
const auto field = object->find(key);
if (field == object->end()) {
return {};
}
if (const auto* text = std::get_if<std::string>(&field->second.value);
text != nullptr) {
return *text;
}
return {};
}
[[nodiscard]] bool contains(const std::vector<std::string>& values,
std::string_view wanted) noexcept
{
return std::ranges::any_of(values, [&](const std::string& value) {
return value == wanted;
});
}
} // namespace
struct LegacySessionAdapter::Impl {
LegacySessionOptions options;
std::map<std::string, LegacySession, std::less<>> sessions;
Impl() = default;
};
LegacySessionAdapter::LegacySessionAdapter(LegacySessionOptions options)
: m_impl(std::make_unique<Impl>())
{
m_impl->options = std::move(options);
if (m_impl->options.supported_versions.empty()) {
m_impl->options.supported_versions.push_back("2025-11-25");
}
if (!m_impl->options.session_id_factory) {
m_impl->options.session_id_factory = default_session_id;
}
}
LegacySessionAdapter::~LegacySessionAdapter() = default;
Void::Result::Result<std::optional<Message>>
LegacySessionAdapter::handle(const Message& inbound,
std::string_view session_id)
{
auto& impl = *m_impl;
if (const auto* request = std::get_if<Request>(&inbound);
request != nullptr) {
if (request->method == "initialize") {
if (!session_id.empty() &&
impl.sessions.contains(session_id)) {
return std::unexpected{make_mcp_error(
McpErrorCode::invalid_request,
"MCP: session is already initialized")};
}
const std::string requested =
param_string(*request, "protocolVersion");
const std::string negotiated =
contains(impl.options.supported_versions, requested)
? requested
: impl.options.supported_versions.front();
LegacySession session;
session.id = impl.options.session_id_factory();
session.protocol_version = negotiated;
impl.sessions.emplace(session.id, session);
JsonValue::Object capabilities;
JsonValue::Object server_info;
server_info["name"] =
JsonValue{impl.options.server_name};
server_info["version"] =
JsonValue{impl.options.server_version};
JsonValue::Object result;
result["protocolVersion"] = JsonValue{negotiated};
result["capabilities"] =
JsonValue{std::move(capabilities)};
result["serverInfo"] = JsonValue{std::move(server_info)};
return std::optional<Message>{Response{
request->id, JsonValue{std::move(result)},
std::nullopt}};
}
const auto session = impl.sessions.find(session_id);
if (session == impl.sessions.end() ||
!session->second.initialized) {
return std::unexpected{make_mcp_error(
McpErrorCode::session_not_found,
"MCP: no initialized legacy session")};
}
return std::optional<Message>{inbound};
}
if (const auto* notification = std::get_if<Notification>(&inbound);
notification != nullptr) {
if (notification->method == "initialized") {
const auto session = impl.sessions.find(session_id);
if (session == impl.sessions.end()) {
return std::unexpected{make_mcp_error(
McpErrorCode::session_not_found,
"MCP: initialized without a legacy session")};
}
session->second.initialized = true;
}
return std::optional<Message>{};
}
return std::optional<Message>{};
}
const LegacySession*
LegacySessionAdapter::find(std::string_view session_id) const
{
const auto session =
m_impl->sessions.find(std::string{session_id});
if (session == m_impl->sessions.end()) {
return nullptr;
}
return &session->second;
}
bool LegacySessionAdapter::terminate(std::string_view session_id)
{
return m_impl->sessions.erase(std::string{session_id}) > 0;
}
std::size_t LegacySessionAdapter::session_count() const
{
return m_impl->sessions.size();
}
} // namespace Void::MCP