Skip to content

Support move semantics in mcp_write_result and optimize MCP notifications and headers - #3025

Open
HarshPopat23 wants to merge 1 commit into
sourcemeta:mainfrom
HarshPopat23:perf/mcp-write-result-move-semantics
Open

HarshPopat23 wants to merge 1 commit into
sourcemeta:mainfrom
HarshPopat23:perf/mcp-write-result-move-semantics

Conversation

@HarshPopat23

@HarshPopat23 HarshPopat23 commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR addresses three performance and safety optimizations in the Model Context Protocol (MCP) implementation:

  1. 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.
  2. 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.
  3. 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.

…ions and headers

- Provide an overload mcp_write_result(..., JSON &&result, ...) in mcp_results.h and results.cc to eliminate deep clones of multi-megabyte payloads when callers pass an rvalue.

- In notifications.cc, resolve the log level enum once into a local variable instead of parsing twice and unconditionally dereferencing mcp_resolve_log_level.

- In parameter_headers.cc, eliminate repeated temporary JSON allocations for JavaScript safe integer boundaries by using scalar comparisons for integers and static constants for integral fallback.

- Add unit tests verifying move semantics and safe integer boundary validations.

Signed-off-by: HarshPopat23 <musichk61@gmail.com>
@augmentcode

augmentcode Bot commented Oct 16, 2026

Copy link
Copy Markdown
🤖 Augment PR Summary

Adds a move-aware mcp_write_result overload for large MCP response payloads.
Decorates rvalue results in place and moves them into the JSON-RPC envelope.
Preserves lvalue behavior through a copy-and-forward overload.
Reuses resolved notification log levels for validation and threshold checks.
Uses scalar safe-integer checks for native integers, retaining JSON comparison fallbacks.
Adds tests covering rvalue calls, lvalue preservation, and integer boundaries.

🤖 Was this summary useful? React with 👍 or 👎

@augmentcode augmentcode Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. No suggestions at this time.

Comment augment review to trigger a new review at any time.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 6 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/core/mcp/results.cc">

<violation number="1" location="src/core/mcp/results.cc:1167">
P2: In-place decoration can reallocate `result`, invalidating `identifier` when it aliases a result property; `jsonrpc_make_success` then reads a dangling reference. Copy and use the identifier before decorating.</violation>

<violation number="2" location="src/core/mcp/results.cc:1180">
P3: The lvalue compatibility wrapper deep-copies the entire result before the existing validation path runs. Large invalid results now incur unnecessary allocation/traversal and can fail during copying before reporting the invalid identifier, method, or result; validate first, then copy only after validation succeeds.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread src/core/mcp/results.cc
}
auto payload{result};
decorate_result(version, payload, server_info);
decorate_result(version, result, server_info);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: In-place decoration can reallocate result, invalidating identifier when it aliases a result property; jsonrpc_make_success then reads a dangling reference. Copy and use the identifier before decorating.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/core/mcp/results.cc, line 1167:

<comment>In-place decoration can reallocate `result`, invalidating `identifier` when it aliases a result property; `jsonrpc_make_success` then reads a dangling reference. Copy and use the identifier before decorating.</comment>

<file context>
@@ -1164,11 +1164,21 @@ auto mcp_write_result(std::ostream &stream, const MCPProtocolVersion version,
   }
-  auto payload{result};
-  decorate_result(version, payload, server_info);
+  decorate_result(version, result, server_info);
   if (cache_policy) {
-    decorate_cacheable_result(version, payload, *cache_policy, method);
</file context>

Comment thread src/core/mcp/results.cc
const std::optional<MCPCachePolicy> cache_policy,
const std::optional<MCPImplementation> server_info)
-> void {
auto payload{result};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: The lvalue compatibility wrapper deep-copies the entire result before the existing validation path runs. Large invalid results now incur unnecessary allocation/traversal and can fail during copying before reporting the invalid identifier, method, or result; validate first, then copy only after validation succeeds.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/core/mcp/results.cc, line 1180:

<comment>The lvalue compatibility wrapper deep-copies the entire result before the existing validation path runs. Large invalid results now incur unnecessary allocation/traversal and can fail during copying before reporting the invalid identifier, method, or result; validate first, then copy only after validation succeeds.</comment>

<file context>
@@ -1164,11 +1164,21 @@ auto mcp_write_result(std::ostream &stream, const MCPProtocolVersion version,
+                      const std::optional<MCPCachePolicy> cache_policy,
+                      const std::optional<MCPImplementation> server_info)
+    -> void {
+  auto payload{result};
+  mcp_write_result(stream, version, method, identifier, std::move(payload),
+                   cache_policy, server_info);
</file context>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant