Skip to content

Enforce add_comment constraints at MCP server invocation#16011

Merged
pelikhan merged 5 commits intomainfrom
copilot/add-runtime-checks-to-enforce-limits
Feb 16, 2026
Merged

Enforce add_comment constraints at MCP server invocation#16011
pelikhan merged 5 commits intomainfrom
copilot/add-runtime-checks-to-enforce-limits

Conversation

Copy link
Contributor

Copilot AI commented Feb 16, 2026

Plan: Add Runtime Checks to Enforce add_comment Limits in MCP Server

Problem

The add_comment tool enforces limits (max length, mentions, links) only during safe-outputs processing (Phase 6), not during the MCP tool call (Phase 4). This prevents the agent from getting immediate feedback and making corrections.

Requirements (from Safe Outputs Specification Section 8.3)

  • MCE1: Early Validation - Enforce constraints during tool invocation (Phase 4)
  • MCE2: Tool Description Disclosure - Constraints already documented in tool schema ✓
  • MCE3: Actionable Error Responses - Return detailed error messages with guidance
  • MCE4: Dual Enforcement - Keep existing processor checks (defense-in-depth)
  • MCE5: Constraint Consistency - Use same limits across MCP and processor ✓

Implementation Summary

  • Create a dedicated handler for add_comment in MCP server
    • Import and call enforceCommentLimits before recording to NDJSON
    • Catch validation errors and return as MCP error responses
    • Format errors with code -32602 (Invalid params)
  • Register the handler in attachHandlers function
  • Add comprehensive MCP server tests
    • Test valid comments pass through successfully
    • Test E006 error for length violations
    • Test E007 error for mention violations
    • Test E008 error for link violations
    • Test error format matches MCP specification
  • Update specification version (minor increment to 1.12.0)
  • Validate with existing tests
  • Run linting and formatting
  • Refactor limit validation into separate module ✨ NEW

Files Modified

  1. actions/setup/js/safe_outputs_handlers.cjs - Added addCommentHandler
  2. actions/setup/js/safe_outputs_tools_loader.cjs - Registered handler
  3. actions/setup/js/safe_outputs_mcp_add_comment_constraints.test.cjs - Added comprehensive MCP tests
  4. docs/src/content/docs/reference/safe-outputs-specification.md - Incremented version to 1.12.0
  5. actions/setup/js/comment_limit_helpers.cjs - New helper module with validation logic
  6. actions/setup/js/comment_limit_helpers.test.cjs - New test file (15 tests)
  7. actions/setup/js/add_comment.cjs - Updated to import from helper module
  8. actions/setup/setup.sh - Added helper to SAFE_OUTPUTS_FILES list

Test Results

  • ✅ All 16 MCP constraint tests passing
  • ✅ All 45 add_comment tests passing
  • ✅ All 15 new comment_limit_helpers tests passing
  • ✅ Code formatted with prettier

Refactoring Benefits

The new comment_limit_helpers.cjs module:

  • ✅ Centralizes validation logic in one place
  • ✅ Makes it reusable across MCP server and safe output processor
  • ✅ Improves maintainability - single source of truth for limits
  • ✅ Ensures constraint consistency (MCE5) by design
  • ✅ Fully tested with dedicated test suite

Key Design Decisions

  • ✅ Use existing enforceCommentLimits function (no duplication)
  • ✅ Handler validates before appending to NDJSON (early feedback)
  • ✅ Safe output processor still validates (defense-in-depth per MCE4)
  • ✅ Error messages use same E006/E007/E008 codes for consistency
  • ✅ JSON-RPC error code -32602 (Invalid params) per MCP spec
  • Helper module pattern for better code organization
Original prompt

This section details on the original issue you should resolve

<issue_title>add-comment tool enforces rules during safe-outputs instead of during call</issue_title>
<issue_description>Some tools like add-comment enforce some rules like max length, max number of mentions, etc but those rules are only checked when it's time to handle the safe outputs

They are not checked during the agent tool call. If we were able to return feedback to the agent that its comment is too long or has too many mentions in it then the Agent could make a correction.</issue_description>

Comments on the Issue (you are @copilot in this section)

@pelikhan Need to update the spec to enforce limits in the server @pelikhan Follow the specification and add runtime checks with informational error messages in the safe output mcp server.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.


✨ PR Review Safe Output Test - Run 22046490655

💥 [THE END] — Illustrated by Smoke Claude


Changeset

  • Type: patch
  • Description: Enforce add_comment limits during MCP tool calls and reuse the shared helper for limit checks.

Generated by Changeset Generator


✨ PR Review Safe Output Test - Run 22046973195

💥 [THE END] — Illustrated by Smoke Claude

Implement MCE1 (Early Validation) from Safe Outputs Specification:
- Add addCommentHandler in safe_outputs_handlers.cjs
- Validate comment constraints before recording to NDJSON
- Return JSON-RPC error -32602 with actionable messages
- Register handler in safe_outputs_tools_loader.cjs
- Add comprehensive test suite with 16 test cases

Per Requirement MCE4, dual enforcement remains:
- MCP server validates during tool invocation (Phase 4)
- Safe output processor validates before API calls (Phase 6)

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Document implementation of MCE1 (Early Validation):
- Increment version to 1.12.0
- Add changelog entry describing implementation
- Note dual enforcement now operational
- Document JSON-RPC error format compliance

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Add runtime checks to enforce rules during agent tool call Enforce add_comment constraints at MCP server invocation Feb 16, 2026
Copilot AI requested a review from pelikhan February 16, 2026 01:08
@pelikhan pelikhan added the smoke label Feb 16, 2026
@github-actions
Copy link
Contributor

🧪 Smoke Project is now testing project operations...

@github-actions
Copy link
Contributor

github-actions bot commented Feb 16, 2026

🌑 The shadows whisper... Smoke Codex failed. The oracle requires further meditation...

@github-actions
Copy link
Contributor

github-actions bot commented Feb 16, 2026

💫 TO BE CONTINUED... Smoke Claude failed! Our hero faces unexpected challenges...

@github-actions
Copy link
Contributor

📰 BREAKING: Smoke Copilot is now investigating this pull request. Sources say the story is developing...

@github-actions
Copy link
Contributor

github-actions bot commented Feb 16, 2026

Smoke Copilot SDK failed. Please review the logs for details.

@github-actions
Copy link
Contributor

🧪 Smoke Temporary ID is now testing temporary ID functionality...

@github-actions
Copy link
Contributor

Smoke Project encountered failures. Check the logs for details.

@github-actions
Copy link
Contributor

Smoke Temporary ID encountered failures. Check the logs for details.

@github-actions
Copy link
Contributor

📰 DEVELOPING STORY: Smoke Copilot reports failed. Our correspondents are investigating the incident...

const { getCurrentBranch } = require("./get_current_branch.cjs");
const { getBaseBranch } = require("./get_base_branch.cjs");
const { generateGitPatch } = require("./generate_git_patch.cjs");
const { enforceCommentLimits } = require("./add_comment.cjs");
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot refactor limit checkers in separate file.
Update setup.sh to add limit helper to safe outputs file list

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored limit validation into comment_limit_helpers.cjs module and updated setup.sh to include it in safe outputs file list (commit 7a311a7).

The new module exports:

  • MAX_COMMENT_LENGTH, MAX_MENTIONS, MAX_LINKS constants
  • enforceCommentLimits() function

Both add_comment.cjs and safe_outputs_handlers.cjs now import from this shared module, ensuring constraint consistency per MCE5.

Extract enforceCommentLimits and constants into comment_limit_helpers.cjs:
- Create new comment_limit_helpers.cjs module with MAX_COMMENT_LENGTH, MAX_MENTIONS, MAX_LINKS constants
- Move enforceCommentLimits function to new module
- Update add_comment.cjs to import from helper module
- Update safe_outputs_handlers.cjs to import from helper module
- Add comment_limit_helpers.cjs to SAFE_OUTPUTS_FILES in setup.sh
- Add comprehensive test suite for helper module (15 tests)

This refactoring improves code organization by centralizing comment validation
logic in a single reusable module, making it easier to maintain constraint
consistency across MCP server and safe output processor per MCE5.

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
@github-actions
Copy link
Contributor

github-actions bot commented Feb 16, 2026

✨ The prophecy is fulfilled... Smoke Codex has completed its mystical journey. The stars align. 🌟

@github-actions
Copy link
Contributor

github-actions bot commented Feb 16, 2026

🎬 THE ENDSmoke Claude MISSION: ACCOMPLISHED! The hero saves the day! ✨

@github-actions
Copy link
Contributor

github-actions bot commented Feb 16, 2026

Smoke Copilot SDK failed. Please review the logs for details.

@github-actions
Copy link
Contributor

🧪 Smoke Temporary ID is now testing temporary ID functionality...

@github-actions
Copy link
Contributor

📰 BREAKING: Smoke Copilot is now investigating this pull request. Sources say the story is developing...

@github-actions
Copy link
Contributor

🧪 Smoke Project is now testing project operations...

@github-actions
Copy link
Contributor

Smoke Temporary ID completed successfully. Temporary ID validation passed.

@github-actions
Copy link
Contributor

Agent Container Tool Check

Tool Status Version
bash 5.2.21
sh dash
git 2.x
jq 1.7
yq 4.52.2
curl 8.x
gh 2.86.0
node 20.20.0
python3 3.12.3
go 1.24.13
java 21.x
dotnet 10.0.102

Result: 12/12 tools available ✅

All required development tools are present and accessible in the agent container.

🔧 Tool validation by Agent Container Smoke Test for issue #16011

@github-actions
Copy link
Contributor

Smoke Project completed successfully. All project operations validated.

@github-actions
Copy link
Contributor

PRs: #16014 "[docs] Update documentation for logs command and skip-bots/skip-roles features"; #16010 "Update ai-moderator workflow to use codex engine"
GitHub MCP: ✅
Serena MCP: ✅
Playwright: ✅
File write: ✅
Bash cat: ✅
Build: ✅
Overall: PASS

🔮 The oracle has spoken through Smoke Codex for issue #16011

@github-actions
Copy link
Contributor

Smoke Test Results

PRs Reviewed:

✅ GitHub MCP | ✅ GH CLI | ✅ Playwright
✅ File Ops | ✅ Build | ✅ Discussion
✅ Dispatch | ✅ PR Review

Status: PASS

@Copilot @pelikhan

📰 BREAKING: Report filed by Smoke Copilot for issue #16011

@github-actions
Copy link
Contributor

📰 VERDICT: Smoke Copilot has concluded. All systems operational. This is a developing story. 🎤

@pelikhan pelikhan marked this pull request as ready for review February 16, 2026 01:28
Copilot AI review requested due to automatic review settings February 16, 2026 01:28
@pelikhan pelikhan merged commit 96676c9 into main Feb 16, 2026
5 checks passed
@pelikhan pelikhan deleted the copilot/add-runtime-checks-to-enforce-limits branch February 16, 2026 01:28
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

💥 Automated smoke test review - all systems nominal! This refactoring successfully consolidates comment validation logic into a reusable module, improving code quality and maintainability.

💥 [THE END] — Illustrated by Smoke Claude for issue #16011

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements Safe Outputs Specification requirement MCE1 (Early Validation) by enforcing add_comment tool constraints during MCP server invocation (Phase 4) rather than only during safe output processing (Phase 6). This allows the LLM to receive immediate feedback when constraints are violated and make corrections before operations are recorded.

Changes:

  • Created a new comment_limit_helpers.cjs module to centralize validation logic (constants and enforceCommentLimits function) for reuse across MCP server and safe output processor
  • Added addCommentHandler to MCP server that validates constraints before recording to NDJSON and returns JSON-RPC errors (-32602) with actionable messages
  • Refactored add_comment.cjs to import constants and validation function from the shared helper module
  • Added comprehensive test suites for both the helper module (15 tests) and MCP constraint enforcement (16 tests)
  • Updated Safe Outputs Specification to version 1.12.0 with changelog documenting the implementation

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
actions/setup/js/comment_limit_helpers.cjs New shared module defining MAX_COMMENT_LENGTH (65536), MAX_MENTIONS (10), MAX_LINKS (50) constants and enforceCommentLimits validation function
actions/setup/js/comment_limit_helpers.test.cjs Test suite for helper module with 15 tests covering all constraints and edge cases
actions/setup/js/safe_outputs_handlers.cjs Added addCommentHandler that validates constraints and throws JSON-RPC errors before recording operations
actions/setup/js/safe_outputs_tools_loader.cjs Registered addCommentHandler in handlerMap to attach it to add_comment tool
actions/setup/js/safe_outputs_mcp_add_comment_constraints.test.cjs Comprehensive MCP server test suite with 16 tests validating constraint enforcement, error format, and dual enforcement
actions/setup/js/add_comment.cjs Refactored to import constants and enforceCommentLimits from shared helper module, maintaining dual enforcement pattern
actions/setup/setup.sh Added comment_limit_helpers.cjs to SAFE_OUTPUTS_FILES array for deployment
docs/src/content/docs/reference/safe-outputs-specification.md Incremented version to 1.12.0 and added changelog entry documenting MCE1 implementation
.changeset/patch-enforce-add-comment-mcp.md Added changeset describing the patch-level change

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -0,0 +1,99 @@
// @ts-check
import { describe, it, expect } from "vitest";
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

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

The test file uses beforeEach on line 8 but it's not imported from vitest on line 2. This will cause a runtime error when the tests are executed. Add beforeEach to the import statement.

Suggested change
import { describe, it, expect } from "vitest";
import { describe, it, expect, beforeEach } from "vitest";

Copilot uses AI. Check for mistakes.
* 5. Errors use MCP JSON-RPC error format with code -32602
* 6. Error messages are actionable per Requirement MCE3
*
* Related issues: github/gh-aw#<issue_number>
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

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

The comment contains a placeholder <issue_number> that should be replaced with the actual issue number (15976, based on the PR description mentioning "Fixes #15976").

Suggested change
* Related issues: github/gh-aw#<issue_number>
* Related issues: github/gh-aw#15976

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

add-comment tool enforces rules during safe-outputs instead of during call

2 participants