Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 | 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 12x 12x 15x 7x 2x 5x 2x 3x 15x 14x 41x 38x 4x 7x 7x 3x 9x 7x 4x 34x 15x 36x 26x 26x 4x 4x 4x 4x 5x 5x 5x 1x 5x 1x 5x 3x 2x 2x 2x 1x 1x 10x 10x 10x 10x 10x 1x 10x 1x 10x 10x 10x 1x 10x 6x 10x 2x 10x 6x 6x 6x 30x 6x 10x 2x 2x 2x 2x 2x 2x 2x 3x 3x 5x 5x 5x 3x 3x 3x 1x 1x 1x 1x 14x 14x 14x 2x 2x 2x 7x 1x 6x 2x 2x 2x 2x 4x 2x 2x 2x 6x 6x 6x 6x 1x 5x 5x 5x 1x 4x 4x 4x 4x 2x 2x 2x 2x 1x 1x 1x 1x 14x 14x 14x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 2x 2x 2x 2x 1x 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 6x 6x 6x 2x 2x 2x 2x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 21x 15x 8x 15x 7x 3x 15x 4x | import * as z from "zod";
import picomatch from "picomatch";
import {
BrowseMergeRequestsSchema,
BrowseMrDiscussionsSchema,
LOCKFILE_PATTERNS,
GENERATED_PATTERNS,
} from "./schema-readonly";
import {
ManageMergeRequestSchema,
ManageMrDiscussionSchema,
ManageDraftNotesSchema,
} from "./schema";
import { gitlab, toQuery } from "../../utils/gitlab-api";
import { normalizeProjectId } from "../../utils/projectIdentifier";
import { ToolRegistry, EnhancedToolDefinition } from "../../types";
import { isActionDenied } from "../../config";
/**
* Response shape for MR status check before merge.
* Contains fields needed to determine mergeability.
*/
interface MergeRequestStatusResponse {
detailed_merge_status: string;
merge_status: string;
has_conflicts: boolean;
blocking_discussions_resolved: boolean;
state: string;
draft: boolean;
}
/**
* Structured response when MR cannot be merged.
* Provides actionable information for AI agents.
*/
export interface MergeBlockedResponse {
error: true;
message: string;
detailed_merge_status: string;
merge_status: string;
has_conflicts: boolean;
blocking_discussions_resolved: boolean;
hint: string;
is_retryable: boolean;
can_auto_merge: boolean;
suggested_action: string;
}
/**
* Statuses that are transient and may resolve on their own.
* Agent should wait and retry for these statuses.
*/
export const RETRYABLE_MERGE_STATUSES = [
"checking",
"unchecked",
"ci_still_running",
"ci_must_pass",
"approvals_syncing",
] as const;
/**
* Statuses where auto-merge (merge_when_pipeline_succeeds) is applicable.
* Agent can suggest using auto-merge for these statuses.
*/
export const AUTO_MERGE_ELIGIBLE_STATUSES = ["ci_still_running", "ci_must_pass"] as const;
/**
* Returns actionable hint for a given merge status.
* Helps agents understand why merge failed and what action to take.
*
* @see https://docs.gitlab.com/ee/api/merge_requests.html#merge-status
* @see https://gitlab.com/gitlab-org/gitlab/-/issues/364102
*/
export function getMergeStatusHint(status: string): string {
const hints: Record<string, string> = {
// Async check in progress - agent should wait and retry
checking: "Wait a moment and retry - GitLab is calculating mergeability",
unchecked: "Wait a moment and retry - GitLab has not checked mergeability yet",
// CI/Pipeline related - suggest auto-merge
ci_must_pass:
"Pipeline must pass. Use merge_when_pipeline_succeeds: true for auto-merge, or wait for pipeline",
ci_still_running:
"Pipeline is running. Use merge_when_pipeline_succeeds: true for auto-merge, or wait for completion",
// Approval related
not_approved: "MR requires approval before merging",
approvals_syncing: "Approvals are being synchronized - wait and retry",
// Conflict related
conflict: "Resolve merge conflicts before merging",
need_rebase: "Rebase the source branch before merging",
// Status related
draft_status:
"Remove draft status before merging by updating the title to remove any 'Draft:' or 'WIP:' prefix",
discussions_not_resolved: "Resolve all blocking discussions before merging",
blocked_status: "MR is blocked by another MR or issue",
// External checks
external_status_checks: "External status checks are pending",
jira_association_missing: "Jira issue association is required",
// Other statuses
not_open: "MR is not in open state - cannot merge closed or already merged MRs",
mergeable: "MR is ready to merge",
};
return hints[status] || `Check MR detailed status: ${status}`;
}
/**
* Build suggested action message based on merge status.
*/
export function getSuggestedAction(isRetryable: boolean, canAutoMerge: boolean): string {
if (canAutoMerge) {
return "Consider using merge_when_pipeline_succeeds: true to auto-merge when pipeline passes";
}
if (isRetryable) {
return "Wait a moment and retry the merge";
}
return "Resolve the blocking condition before merging";
}
/**
* Flattens a position object into form-encoded fields with bracket notation.
* GitLab API expects: position[base_sha]=xxx, position[head_sha]=xxx, etc.
* NOT: position={"base_sha":"xxx","head_sha":"xxx"}
*
* @see https://docs.gitlab.com/ee/api/discussions.html#create-a-new-thread-in-the-merge-request-diff
*/
export function flattenPositionToFormFields(
body: Record<string, unknown>,
position: Record<string, unknown>
): void {
for (const [key, value] of Object.entries(position)) {
if (value === undefined || value === null) continue;
if (typeof value === "object" && !Array.isArray(value)) {
// Handle nested objects like line_range.start, line_range.end
for (const [nestedKey, nestedValue] of Object.entries(value as Record<string, unknown>)) {
Iif (nestedValue === undefined || nestedValue === null) continue;
if (typeof nestedValue === "object" && !Array.isArray(nestedValue)) {
// Handle deeply nested (line_range.start.line_code, etc.)
for (const [deepKey, deepValue] of Object.entries(
nestedValue as Record<string, unknown>
)) {
if (deepValue !== undefined && deepValue !== null) {
body[`position[${key}][${nestedKey}][${deepKey}]`] = deepValue;
}
}
} else {
body[`position[${key}][${nestedKey}]`] = nestedValue;
}
}
} else {
body[`position[${key}]`] = value;
}
}
}
/**
* MRS (Merge Requests) tools registry - 5 CQRS tools replacing 20 individual tools
*
* browse_merge_requests (Query): list, get, diffs, compare
* browse_mr_discussions (Query): list, drafts, draft
* manage_merge_request (Command): create, update, merge
* manage_mr_discussion (Command): comment, thread, reply, update, apply_suggestion, apply_suggestions
* manage_draft_notes (Command): create, update, publish, publish_all, delete
*/
export const mrsToolRegistry: ToolRegistry = new Map<string, EnhancedToolDefinition>([
// ============================================================================
// browse_merge_requests - CQRS Query Tool (discriminated union schema)
// TypeScript automatically narrows types in each switch case
// ============================================================================
[
"browse_merge_requests",
{
name: "browse_merge_requests",
description:
"Find and inspect merge requests. Actions: list (filter by state/author/reviewer/labels/branch), get (MR details by IID or source branch), diffs (file-level changes with inline suggestions), compare (diff between any two refs), versions (list diff versions from pushes), version (get specific version with diffs). Related: manage_merge_request to create/update/merge.",
inputSchema: z.toJSONSchema(BrowseMergeRequestsSchema),
gate: { envVar: "USE_MRS", defaultValue: true },
handler: async (args: unknown) => {
const input = BrowseMergeRequestsSchema.parse(args);
// Runtime validation: reject denied actions even if they bypass schema filtering
Iif (isActionDenied("browse_merge_requests", input.action)) {
throw new Error(`Action '${input.action}' is not allowed for browse_merge_requests tool`);
}
switch (input.action) {
case "list": {
// TypeScript knows: input has state, order_by, sort, milestone, etc. (all optional)
const { action: _action, project_id, ...rest } = input;
const query = toQuery(rest, []);
const path = project_id
? `projects/${normalizeProjectId(project_id)}/merge_requests`
: `merge_requests`;
return gitlab.get(path, { query });
}
case "get": {
// TypeScript knows: input has project_id (required), merge_request_iid (optional), branch_name (optional)
const { project_id, merge_request_iid, branch_name } = input;
// Build query params for optional fields
const query: Record<string, boolean | string | undefined> = {};
if (input.include_diverged_commits_count !== undefined)
query.include_diverged_commits_count = input.include_diverged_commits_count;
if (input.include_rebase_in_progress !== undefined)
query.include_rebase_in_progress = input.include_rebase_in_progress;
if (merge_request_iid) {
return gitlab.get(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}`,
Object.keys(query).length > 0 ? { query } : undefined
);
} else Eif (branch_name) {
const result = await gitlab.get<unknown[]>(
`projects/${normalizeProjectId(project_id)}/merge_requests`,
{ query: { source_branch: branch_name, ...query } }
);
if (Array.isArray(result) && result.length > 0) {
return result[0];
}
throw new Error("No merge request found for branch");
}
/* istanbul ignore next -- unreachable: schema validation ensures merge_request_iid or branch_name */
throw new Error("Either merge_request_iid or branch_name must be provided");
}
case "diffs": {
// TypeScript knows: input has project_id (required), merge_request_iid (required)
const {
project_id,
merge_request_iid,
exclude_patterns,
exclude_lockfiles,
exclude_generated,
} = input;
const query: Record<string, number | boolean | undefined> = {};
if (input.page !== undefined) query.page = input.page;
Eif (input.per_page !== undefined) query.per_page = input.per_page;
if (input.include_diverged_commits_count !== undefined)
query.include_diverged_commits_count = input.include_diverged_commits_count;
if (input.include_rebase_in_progress !== undefined)
query.include_rebase_in_progress = input.include_rebase_in_progress;
const response = await gitlab.get<{
changes?: Array<{ new_path: string; old_path: string }>;
[key: string]: unknown;
}>(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/changes`,
{ query }
);
// Build exclusion patterns list
const patterns: string[] = [];
if (exclude_patterns?.length) {
patterns.push(...exclude_patterns);
}
if (exclude_lockfiles) {
patterns.push(...LOCKFILE_PATTERNS);
}
if (exclude_generated) {
patterns.push(...GENERATED_PATTERNS);
}
// Apply filtering if patterns specified and changes array exists
if (patterns.length > 0 && Array.isArray(response.changes)) {
const originalCount = response.changes.length;
const matcher = picomatch(patterns);
response.changes = response.changes.filter(
(diff: { new_path: string; old_path: string }) =>
!matcher(diff.new_path) && !matcher(diff.old_path)
);
// Add metadata about filtering (response type already has index signature)
response._filtered = {
original_count: originalCount,
filtered_count: response.changes.length,
excluded_count: originalCount - response.changes.length,
patterns_applied: patterns,
};
}
return response;
}
case "compare": {
// TypeScript knows: input has project_id (required), from (required), to (required)
const { project_id, from, to, straight } = input;
const query: Record<string, string | boolean | undefined> = {
from,
to,
};
if (straight !== undefined) query.straight = straight;
return gitlab.get(`projects/${normalizeProjectId(project_id)}/repository/compare`, {
query,
});
}
case "versions": {
// TypeScript knows: input has project_id (required), merge_request_iid (required)
const { action: _action, project_id, merge_request_iid, ...rest } = input;
const query = toQuery(rest, []);
return gitlab.get(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/versions`,
{ query }
);
}
case "version": {
// TypeScript knows: input has project_id (required), merge_request_iid (required), version_id (required)
const { project_id, merge_request_iid, version_id } = input;
return gitlab.get(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/versions/${version_id}`
);
}
/* istanbul ignore next -- unreachable with Zod discriminatedUnion */
default:
throw new Error(`Unknown action: ${(input as { action: string }).action}`);
}
},
},
],
// ============================================================================
// browse_mr_discussions - CQRS Query Tool (discriminated union schema)
// TypeScript automatically narrows types in each switch case
// ============================================================================
[
"browse_mr_discussions",
{
name: "browse_mr_discussions",
description:
"Read discussion threads and draft review notes on merge requests. Actions: list (all threads with resolution status), drafts (unpublished draft notes), draft (single draft details). Related: manage_mr_discussion to comment, manage_draft_notes to create drafts.",
inputSchema: z.toJSONSchema(BrowseMrDiscussionsSchema),
gate: { envVar: "USE_MRS", defaultValue: true },
handler: async (args: unknown) => {
const input = BrowseMrDiscussionsSchema.parse(args);
// Runtime validation: reject denied actions even if they bypass schema filtering
Iif (isActionDenied("browse_mr_discussions", input.action)) {
throw new Error(`Action '${input.action}' is not allowed for browse_mr_discussions tool`);
}
switch (input.action) {
case "list": {
// TypeScript knows: input has project_id (required), merge_request_iid (required), per_page, page (optional)
const { action: _action, project_id, merge_request_iid, ...rest } = input;
const query = toQuery(rest, []);
return gitlab.get(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/discussions`,
{ query }
);
}
case "drafts": {
// TypeScript knows: input has project_id (required), merge_request_iid (required)
const { project_id, merge_request_iid } = input;
return gitlab.get(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/draft_notes`
);
}
case "draft": {
// TypeScript knows: input has project_id (required), merge_request_iid (required), draft_note_id (required)
const { project_id, merge_request_iid, draft_note_id } = input;
return gitlab.get(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/draft_notes/${draft_note_id}`
);
}
/* istanbul ignore next -- unreachable with Zod discriminatedUnion */
default:
throw new Error(`Unknown action: ${(input as { action: string }).action}`);
}
},
},
],
// ============================================================================
// manage_merge_request - CQRS Command Tool (discriminated union schema)
// TypeScript automatically narrows types in each switch case
// ============================================================================
[
"manage_merge_request",
{
name: "manage_merge_request",
description:
"Create, update, merge, or approve merge requests. Actions: create (new MR from source to target), update (title/description/assignees/reviewers/labels), merge (into target branch), approve/unapprove (review approval), get_approval_state (current approvals). Related: browse_merge_requests for discovery.",
inputSchema: z.toJSONSchema(ManageMergeRequestSchema),
gate: { envVar: "USE_MRS", defaultValue: true },
handler: async (args: unknown) => {
const input = ManageMergeRequestSchema.parse(args);
// Runtime validation: reject denied actions even if they bypass schema filtering
Iif (isActionDenied("manage_merge_request", input.action)) {
throw new Error(`Action '${input.action}' is not allowed for manage_merge_request tool`);
}
switch (input.action) {
case "create": {
// TypeScript knows: input has source_branch (required), target_branch (required), title (required)
const { action: _action, project_id, ...body } = input;
// Handle array fields - convert to comma-separated strings for form encoding
const processedBody: Record<string, unknown> = {};
for (const [key, value] of Object.entries(body)) {
if (Array.isArray(value)) {
processedBody[key] = value.join(",");
} else {
processedBody[key] = value;
}
}
return gitlab.post(`projects/${normalizeProjectId(project_id)}/merge_requests`, {
body: processedBody,
contentType: "form",
});
}
case "update": {
// TypeScript knows: input has merge_request_iid (required)
const { action: _action, project_id, merge_request_iid, ...body } = input;
// Handle array fields
const processedBody: Record<string, unknown> = {};
for (const [key, value] of Object.entries(body)) {
if (Array.isArray(value)) {
processedBody[key] = value.join(",");
} else {
processedBody[key] = value;
}
}
return gitlab.put(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}`,
{ body: processedBody, contentType: "form" }
);
}
case "merge": {
// TypeScript knows: input has merge_request_iid (required)
const {
action: _action,
project_id,
merge_request_iid,
merge_when_pipeline_succeeds,
...body
} = input;
const projectPath = normalizeProjectId(project_id);
const mergeEndpoint = `projects/${projectPath}/merge_requests/${merge_request_iid}/merge`;
// If auto-merge is explicitly requested, try it directly
// This allows setting auto-merge even when pipeline is running
if (merge_when_pipeline_succeeds) {
return gitlab.put(mergeEndpoint, {
body: { ...body, merge_when_pipeline_succeeds: true },
contentType: "form",
});
}
// Pre-check mergeability to provide actionable errors instead of 405
const mrStatus = await gitlab.get<MergeRequestStatusResponse>(
`projects/${projectPath}/merge_requests/${merge_request_iid}`
);
const detailedStatus = mrStatus.detailed_merge_status;
// If mergeable, proceed with merge
if (detailedStatus === "mergeable") {
return gitlab.put(mergeEndpoint, { body, contentType: "form" });
}
// Determine if status is retryable or eligible for auto-merge
const isRetryable = (RETRYABLE_MERGE_STATUSES as readonly string[]).includes(
detailedStatus
);
const canAutoMerge = (AUTO_MERGE_ELIGIBLE_STATUSES as readonly string[]).includes(
detailedStatus
);
// Return structured error with actionable guidance
const blockedResponse: MergeBlockedResponse = {
error: true,
message: `MR cannot be merged: ${detailedStatus}`,
detailed_merge_status: detailedStatus,
merge_status: mrStatus.merge_status,
has_conflicts: mrStatus.has_conflicts,
blocking_discussions_resolved: mrStatus.blocking_discussions_resolved,
hint: getMergeStatusHint(detailedStatus),
is_retryable: isRetryable,
can_auto_merge: canAutoMerge,
suggested_action: getSuggestedAction(isRetryable, canAutoMerge),
};
return blockedResponse;
}
case "approve": {
// TypeScript knows: input has merge_request_iid (required), sha (optional)
const { project_id, merge_request_iid, sha } = input;
const body: Record<string, unknown> = {};
if (sha) body.sha = sha;
return gitlab.post(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/approve`,
{ body: Object.keys(body).length > 0 ? body : undefined, contentType: "json" }
);
}
case "unapprove": {
// TypeScript knows: input has merge_request_iid (required)
const { project_id, merge_request_iid } = input;
return gitlab.post(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/unapprove`
);
}
case "get_approval_state": {
// TypeScript knows: input has merge_request_iid (required)
const { project_id, merge_request_iid } = input;
return gitlab.get(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/approval_state`
);
}
/* istanbul ignore next -- unreachable with Zod discriminatedUnion */
default:
throw new Error(`Unknown action: ${(input as { action: string }).action}`);
}
},
},
],
// ============================================================================
// manage_mr_discussion - CQRS Command Tool (discriminated union schema)
// TypeScript automatically narrows types in each switch case
// ============================================================================
[
"manage_mr_discussion",
{
name: "manage_mr_discussion",
description:
"Post comments, start threads, and suggest code changes on merge requests. Actions: comment (simple note), thread (line-level discussion), reply (to existing thread), update (edit note text), resolve (toggle thread resolution), suggest (code suggestion block), apply_suggestion/apply_suggestions (accept code suggestions). Related: browse_mr_discussions to read threads.",
inputSchema: z.toJSONSchema(ManageMrDiscussionSchema),
gate: { envVar: "USE_MRS", defaultValue: true },
handler: async (args: unknown) => {
const input = ManageMrDiscussionSchema.parse(args);
// Runtime validation: reject denied actions even if they bypass schema filtering
Iif (isActionDenied("manage_mr_discussion", input.action)) {
throw new Error(`Action '${input.action}' is not allowed for manage_mr_discussion tool`);
}
switch (input.action) {
case "comment": {
// TypeScript knows: input has noteable_type (required), noteable_id (required), body (required)
const {
project_id,
noteable_type,
noteable_id,
body: noteBody,
created_at,
confidential,
} = input;
const body: Record<string, unknown> = { body: noteBody };
Iif (created_at) body.created_at = created_at;
Iif (confidential !== undefined) body.confidential = confidential;
const resourceType = noteable_type === "merge_request" ? "merge_requests" : "issues";
return gitlab.post(
`projects/${normalizeProjectId(project_id)}/${resourceType}/${noteable_id}/notes`,
{ body, contentType: "form" }
);
}
case "thread": {
// TypeScript knows: input has merge_request_iid (required), body (required)
const { project_id, merge_request_iid, body: noteBody, position, commit_id } = input;
const body: Record<string, unknown> = { body: noteBody };
Iif (position) flattenPositionToFormFields(body, position);
Iif (commit_id) body.commit_id = commit_id;
return gitlab.post(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/discussions`,
{ body, contentType: "form" }
);
}
case "reply": {
// TypeScript knows: input has merge_request_iid (required), discussion_id (required), body (required)
const {
project_id,
merge_request_iid,
discussion_id,
body: noteBody,
created_at,
} = input;
const body: Record<string, unknown> = { body: noteBody };
Iif (created_at) body.created_at = created_at;
return gitlab.post(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/discussions/${discussion_id}/notes`,
{ body, contentType: "form" }
);
}
case "update": {
// TypeScript knows: input has merge_request_iid (required), note_id (required), body (required)
const { project_id, merge_request_iid, note_id, body: noteBody } = input;
return gitlab.put(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/notes/${note_id}`,
{ body: { body: noteBody }, contentType: "form" }
);
}
case "apply_suggestion": {
// TypeScript knows: input has suggestion_id (required), commit_message (optional)
// Note: project_id and merge_request_iid are in schema for context but not used in API
// GitLab suggestions API uses global endpoint: PUT /suggestions/:id/apply
const { suggestion_id, commit_message } = input;
const body: Record<string, unknown> = {};
if (commit_message) {
body.commit_message = commit_message;
}
return gitlab.put(`suggestions/${suggestion_id}/apply`, {
body: Object.keys(body).length > 0 ? body : undefined,
contentType: "json",
});
}
case "apply_suggestions": {
// TypeScript knows: input has suggestion_ids (required), commit_message (optional)
// Note: project_id and merge_request_iid are in schema for context but not used in API
// GitLab suggestions API uses global endpoint: PUT /suggestions/batch_apply
const { suggestion_ids, commit_message } = input;
const body: Record<string, unknown> = {
ids: suggestion_ids,
};
if (commit_message) {
body.commit_message = commit_message;
}
return gitlab.put(`suggestions/batch_apply`, { body, contentType: "json" });
}
case "resolve": {
// TypeScript knows: input has discussion_id (required), resolved (required)
const { project_id, merge_request_iid, discussion_id, resolved } = input;
return gitlab.put(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/discussions/${discussion_id}`,
{ body: { resolved }, contentType: "form" }
);
}
case "suggest": {
// TypeScript knows: input has position (required), suggestion (required)
const {
project_id,
merge_request_iid,
position,
suggestion,
comment,
lines_above,
lines_below,
} = input;
// Build markdown suggestion block with line range syntax
// Format: ```suggestion:-N+M where N=lines_above, M=lines_below
const rangeSpec =
lines_above || lines_below ? `:-${lines_above || 0}+${lines_below || 0}` : "";
const suggestionBlock = `\`\`\`suggestion${rangeSpec}\n${suggestion}\n\`\`\``;
// Prepend optional comment
const noteBody = comment ? `${comment}\n\n${suggestionBlock}` : suggestionBlock;
// Create discussion thread with position (same as thread action)
const body: Record<string, unknown> = {
body: noteBody,
};
flattenPositionToFormFields(body, position);
return gitlab.post(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/discussions`,
{ body, contentType: "form" }
);
}
/* istanbul ignore next -- unreachable with Zod discriminatedUnion */
default:
throw new Error(`Unknown action: ${(input as { action: string }).action}`);
}
},
},
],
// ============================================================================
// manage_draft_notes - CQRS Command Tool (discriminated union schema)
// TypeScript automatically narrows types in each switch case
// ============================================================================
[
"manage_draft_notes",
{
name: "manage_draft_notes",
description:
"Create and manage unpublished review comments on merge requests. Actions: create (new draft), update (modify text), publish (make single draft visible), publish_all (submit entire review), delete (discard draft). Related: browse_mr_discussions action 'drafts' to list existing drafts.",
inputSchema: z.toJSONSchema(ManageDraftNotesSchema),
gate: { envVar: "USE_MRS", defaultValue: true },
handler: async (args: unknown) => {
const input = ManageDraftNotesSchema.parse(args);
// Runtime validation: reject denied actions even if they bypass schema filtering
Iif (isActionDenied("manage_draft_notes", input.action)) {
throw new Error(`Action '${input.action}' is not allowed for manage_draft_notes tool`);
}
switch (input.action) {
case "create": {
// TypeScript knows: input has note (required), position, in_reply_to_discussion_id, commit_id (optional)
const {
project_id,
merge_request_iid,
note,
position,
in_reply_to_discussion_id,
commit_id,
} = input;
const body: Record<string, unknown> = { note };
Iif (position) flattenPositionToFormFields(body, position);
if (in_reply_to_discussion_id)
body.in_reply_to_discussion_id = in_reply_to_discussion_id;
Iif (commit_id) body.commit_id = commit_id;
return gitlab.post(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/draft_notes`,
{ body, contentType: "form" }
);
}
case "update": {
// TypeScript knows: input has draft_note_id (required), note (required)
const { project_id, merge_request_iid, draft_note_id, note, position } = input;
const body: Record<string, unknown> = { note };
Iif (position) flattenPositionToFormFields(body, position);
return gitlab.put(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/draft_notes/${draft_note_id}`,
{ body, contentType: "form" }
);
}
case "publish": {
// TypeScript knows: input has draft_note_id (required)
const { project_id, merge_request_iid, draft_note_id } = input;
const result = await gitlab.put<void>(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/draft_notes/${draft_note_id}/publish`
);
// PUT publish returns 204 No Content (undefined) on success
return result ?? { published: true };
}
case "publish_all": {
// TypeScript knows: input has project_id (required), merge_request_iid (required)
const { project_id, merge_request_iid } = input;
const result = await gitlab.post<void>(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/draft_notes/bulk_publish`
);
// POST bulk_publish returns 204 No Content (undefined) on success
return result ?? { published: true };
}
case "delete": {
// TypeScript knows: input has draft_note_id (required)
const { project_id, merge_request_iid, draft_note_id } = input;
await gitlab.delete<void>(
`projects/${normalizeProjectId(project_id)}/merge_requests/${merge_request_iid}/draft_notes/${draft_note_id}`
);
return { success: true, message: "Draft note deleted successfully" };
}
/* istanbul ignore next -- unreachable with Zod discriminatedUnion */
default:
throw new Error(`Unknown action: ${(input as { action: string }).action}`);
}
},
},
],
]);
/**
* Get read-only tool names from the registry
*/
export function getMrsReadOnlyToolNames(): string[] {
return ["browse_merge_requests", "browse_mr_discussions"];
}
/**
* Get all tool definitions from the registry
*/
export function getMrsToolDefinitions(): EnhancedToolDefinition[] {
return Array.from(mrsToolRegistry.values());
}
/**
* Get filtered tools based on read-only mode
*/
export function getFilteredMrsTools(readOnlyMode: boolean = false): EnhancedToolDefinition[] {
if (readOnlyMode) {
const readOnlyNames = getMrsReadOnlyToolNames();
return Array.from(mrsToolRegistry.values()).filter(tool => readOnlyNames.includes(tool.name));
}
return getMrsToolDefinitions();
}
|