Files
Library/src/MCP/Codec.cpp
T

209 lines
5.2 KiB
C++

#include <Void/MCP/Codec.hpp>
#include <format>
#include <string>
#include <utility>
namespace Void::MCP
{
LineFramer::LineFramer(McpLimits limits) : m_limits(std::move(limits))
{
}
std::size_t LineFramer::buffered() const noexcept
{
return m_buffer.size();
}
void LineFramer::reset() noexcept
{
m_buffer.clear();
}
Void::Result::Result<void>
LineFramer::append(std::string_view bytes)
{
m_buffer.append(bytes);
const std::size_t last_newline = m_buffer.rfind('\n');
const std::size_t tail =
last_newline == std::string::npos
? m_buffer.size()
: m_buffer.size() - last_newline - 1U;
if (tail > m_limits.max_line_bytes) {
return std::unexpected{make_mcp_error(
McpErrorCode::payload_too_large,
std::format("MCP: stdio frame exceeds {} bytes",
m_limits.max_line_bytes))};
}
return {};
}
Void::Result::Result<std::optional<std::string>>
LineFramer::next()
{
for (;;) {
const std::size_t newline = m_buffer.find('\n');
if (newline == std::string::npos) {
return std::optional<std::string>{};
}
std::string line = m_buffer.substr(0, newline);
m_buffer.erase(0, newline + 1U);
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
if (line.empty()) {
continue;
}
if (line.size() > m_limits.max_line_bytes) {
return std::unexpected{make_mcp_error(
McpErrorCode::payload_too_large,
std::format("MCP: stdio frame exceeds {} bytes",
m_limits.max_line_bytes))};
}
return std::optional<std::string>{std::move(line)};
}
}
Void::Result::Result<std::string>
frame_line(std::string_view json, const McpLimits& limits)
{
if (json.size() > limits.max_line_bytes) {
return std::unexpected{make_mcp_error(
McpErrorCode::payload_too_large,
std::format("MCP: stdio frame {} bytes exceeds {}",
json.size(), limits.max_line_bytes))};
}
std::string frame{json};
frame.push_back('\n');
return frame;
}
Void::Result::Result<std::string>
encode_sse_event(const SseEvent& event, const McpLimits& limits)
{
std::string out;
if (!event.event.empty()) {
out += "event: ";
out += event.event;
out.push_back('\n');
}
if (!event.id.empty()) {
out += "id: ";
out += event.id;
out.push_back('\n');
}
std::size_t begin = 0;
for (;;) {
const std::size_t newline = event.data.find('\n', begin);
out += "data: ";
if (newline == std::string::npos) {
out.append(event.data, begin, std::string::npos);
out.push_back('\n');
break;
}
out.append(event.data, begin, newline - begin);
out.push_back('\n');
begin = newline + 1U;
}
out.push_back('\n');
if (out.size() > limits.max_sse_event_bytes) {
return std::unexpected{make_mcp_error(
McpErrorCode::payload_too_large,
std::format("MCP: SSE event {} bytes exceeds {}",
out.size(), limits.max_sse_event_bytes))};
}
return out;
}
SseParser::SseParser(McpLimits limits) : m_limits(std::move(limits))
{
}
std::size_t SseParser::buffered() const noexcept
{
return m_buffer.size();
}
void SseParser::reset() noexcept
{
m_buffer.clear();
}
Void::Result::Result<void>
SseParser::append(std::string_view bytes)
{
m_buffer.append(bytes);
if (m_buffer.size() > m_limits.max_sse_event_bytes &&
m_buffer.find("\n\n") == std::string::npos) {
return std::unexpected{make_mcp_error(
McpErrorCode::payload_too_large,
std::format("MCP: SSE buffer exceeds {} bytes",
m_limits.max_sse_event_bytes))};
}
return {};
}
Void::Result::Result<std::optional<SseEvent>>
SseParser::next()
{
for (;;) {
const std::size_t separator = m_buffer.find("\n\n");
if (separator == std::string::npos) {
return std::optional<SseEvent>{};
}
const std::string block = m_buffer.substr(0, separator);
m_buffer.erase(0, separator + 2U);
SseEvent event;
bool has_data = false;
std::size_t begin = 0;
while (begin <= block.size()) {
const std::size_t newline = block.find('\n', begin);
const std::string_view line =
newline == std::string::npos
? std::string_view{block}.substr(begin)
: std::string_view{block}.substr(
begin, newline - begin);
begin = newline == std::string::npos ? block.size() + 1U
: newline + 1U;
if (line.empty() || line.front() == ':') {
continue;
}
const std::size_t colon = line.find(':');
const std::string_view field =
colon == std::string_view::npos
? line
: line.substr(0, colon);
std::string_view value =
colon == std::string_view::npos
? std::string_view{}
: line.substr(colon + 1U);
if (!value.empty() && value.front() == ' ') {
value.remove_prefix(1U);
}
if (field == "event") {
event.event = value;
} else if (field == "id") {
event.id = value;
} else if (field == "data") {
if (has_data) {
event.data.push_back('\n');
}
event.data += value;
has_data = true;
}
}
if (!has_data) {
continue;
}
if (event.data.size() > m_limits.max_sse_event_bytes) {
return std::unexpected{make_mcp_error(
McpErrorCode::payload_too_large,
"MCP: SSE event data exceeds limit")};
}
return std::optional<SseEvent>{std::move(event)};
}
}
} // namespace Void::MCP