Support move semantics in mcp_write_result and optimize MCP notifications and headers
HA
14m ago+231+231+012345678901234567890123456789-27-27-01234567890123456789
Summary
This PR addresses three performance and safety optimizations in the Model Context Protocol (MCP) implementation:
-
Move Semantics for MCP Result Writing:
- Added an overload \mcp_write_result(..., JSON &&result, ...)\ in \src/core/mcp/include/sourcemeta/core/mcp_results.h\ and \src/core/mcp/results.cc.
- Modifies and decorates the result payload in-place without deep cloning when the caller provides an rvalue, eliminating multi-megabyte allocations on large
esources/read\ or \ ools/list\ payloads. - Preserves backward compatibility by delegating the \const JSON &result\ lvalue overload to create a copy and forward via move.
-
Eliminate Duplicate Log Level Enum Resolution:
- In \src/core/mcp/notifications.cc, resolve the log level string once into a local \std::optional\ variable instead of parsing multiple times.
- Guard and safely dereference the cached
esolved_level\ when checking \mcp_log_level_enabled, avoiding redundant lookups and unverified pointer dereferences.
-
Avoid Temporary JSON Allocations in Parameter Headers:
- In \src/core/mcp/parameter_headers.cc, replaced repeated dynamic allocations \JSON{INT64_C(-9007199254740991)}\ and \JSON{INT64_C(9007199254740991)}\ with scalar comparisons (\alue.to_integer()) when \alue.is_integer().
- Used static constants (\MIN_SAFE_INTEGER, \MAX_SAFE_INTEGER) as a fallback for non-scalar integral types.
Testing
- Added unit tests in \ est/mcp/mcp_results_test.cc\ verifying move semantics and const lvalue preservation.
- Added unit tests in \ est/mcp/mcp_headers_test.cc\ verifying safe integer boundary checking.
- Ran test suite \sourcemeta_core_mcp_unit: all 1,413 tests passed, 0 failed.
- Verified code formatting with CMake \clang_format_test\ target.