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.

@augmentcode

augmentcode Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
🤖 Augment PR Summary

Adds move-aware MCP result writing and reduces redundant MCP work.

  • Introduces a public mcp_write_result overload accepting JSON &&.
  • Decorates rvalue payloads in place and moves them into the JSON-RPC envelope.
  • Avoids deep-copying large response payloads.
  • Preserves lvalue inputs through the existing const-reference overload.
  • Shares validation and serialization logic between both overloads.
  • Validates cache TTL before copying or decorating payloads.
  • Copies identifiers before mutation to support aliased IDs.
  • Resolves notification log levels once for validation and threshold checks.
  • Uses scalar safe-integer checks for native integer parameter headers.
  • Retains JSON-comparison fallbacks for other integral representations.
  • Adds tests for moves, lvalue preservation, aliased IDs, validation, 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.

All reported issues were addressed across 6 files

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

View guided diff | Re-trigger cubic

Comment thread src/core/mcp/results.cc
Comment thread src/core/mcp/results.cc
@HarshPopat23
HarshPopat23 force-pushed the perf/mcp-write-result-move-semantics branch from d768565 to 0e08943 Compare October 16, 2026 15:23

@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.

All reported issues were addressed across 2 files (changes from recent commits).

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

View guided diff | Re-trigger cubic

Comment thread test/mcp/mcp_results_test.cc Outdated
@HarshPopat23

Copy link
Copy Markdown
Contributor Author

augment review

@HarshPopat23
HarshPopat23 force-pushed the perf/mcp-write-result-move-semantics branch from 0e08943 to 8b1f2c7 Compare October 16, 2026 15:32

@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. 1 suggestion posted.

Fix All in Augment

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

Comment thread src/core/mcp/results.cc
const std::optional<MCPImplementation> &server_info)
-> void {
const auto envelope_identifier{identifier};
decorate_result(version, result, server_info);

@augmentcode augmentcode Bot Oct 16, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could you check the remaining borrowed inputs at src/core/mcp/results.cc:1180–1182 when they alias result? Adding resultType can reallocate the object's storage, leaving a short-string method view (e.g. result.at("method").to_string() containing "tools/list") or server_info.icons pointing to a top-level array dangling before decorate_cacheable_result/attach_server_info reads it, causing a use-after-free.

Severity: medium

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

@HarshPopat23
HarshPopat23 force-pushed the perf/mcp-write-result-move-semantics branch from 8b1f2c7 to b9078a6 Compare October 16, 2026 15:47

@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.

All reported issues were addressed across 2 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

View guided diff | Re-trigger cubic

Comment thread test/mcp/mcp_results_test.cc Outdated
Comment thread src/core/mcp/results.cc Outdated
@HarshPopat23
HarshPopat23 force-pushed the perf/mcp-write-result-move-semantics branch from b9078a6 to 2fa1c09 Compare October 16, 2026 15:57
…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. Validate results prior to copying in the lvalue wrapper, and copy or materialize borrowed inputs (identifiers, methods, server information) before in-place decoration.

- 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, safe integer boundary validations, and aliased inputs.

Signed-off-by: HarshPopat23 <musichk61@gmail.com>
@HarshPopat23
HarshPopat23 force-pushed the perf/mcp-write-result-move-semantics branch from 2fa1c09 to cf4d34e Compare October 16, 2026 16:22

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