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 | 3x 3x 3x 3x 3x 3x 9x 9x 9x 6x 6x 3x 3x 1x 2x 1x 1x 1x 5x 3x 3x 3x 1x 1x 2x 3x 1x 3x 7x 7x 7x 3x 3x 3x 3x 1x 1x 2x 3x 2x 2x 2x 2x 1x 2x 2x 2x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 3x 7x 3x 2x 3x 2x 1x 1x 2x 1x | import * as z from "zod";
import { BrowseSnippetsSchema } from "./schema-readonly";
import { ManageSnippetSchema } from "./schema";
import { gitlab, toQuery } from "../../utils/gitlab-api";
import { ToolRegistry, EnhancedToolDefinition } from "../../types";
import { isActionDenied } from "../../config";
/**
* Snippets tools registry - 2 CQRS tools replacing 5 individual tools
*
* browse_snippets (Query): list, get
* manage_snippet (Command): create, update, delete
*/
export const snippetsToolRegistry: ToolRegistry = new Map<string, EnhancedToolDefinition>([
// ============================================================================
// browse_snippets - CQRS Query Tool (discriminated union schema)
// TypeScript automatically narrows types in each switch case
// ============================================================================
[
"browse_snippets",
{
name: "browse_snippets",
description:
'BROWSE GitLab code snippets. Actions: "list" shows snippets by scope (personal/project/public) with filtering, "get" retrieves single snippet metadata or raw content. Snippets are reusable code blocks, configs, or text with versioning support.',
inputSchema: z.toJSONSchema(BrowseSnippetsSchema),
gate: { envVar: "USE_SNIPPETS", defaultValue: true },
handler: async (args: unknown): Promise<unknown> => {
const input = BrowseSnippetsSchema.parse(args);
// Runtime validation: reject denied actions even if they bypass schema filtering
Iif (isActionDenied("browse_snippets", input.action)) {
throw new Error(`Action '${input.action}' is not allowed for browse_snippets tool`);
}
switch (input.action) {
case "list": {
// TypeScript knows: input has scope (required), projectId, visibility, etc. (optional)
const { action: _action, scope, projectId, ...queryOptions } = input;
// Build the path based on scope
let path: string;
if (scope === "personal") {
path = "snippets";
} else if (scope === "public") {
path = "snippets/public";
} else {
// project scope - requires projectId
if (!projectId) {
throw new Error("projectId is required when scope is 'project'");
}
const encodedProjectId = encodeURIComponent(projectId);
path = `projects/${encodedProjectId}/snippets`;
}
return gitlab.get(path, {
query: toQuery(queryOptions, []),
});
}
case "get": {
// TypeScript knows: input has id (required), projectId, raw (optional)
const { id, projectId, raw } = input;
const encodedId = id.toString();
let path: string;
if (projectId) {
const encodedProjectId = encodeURIComponent(projectId);
path = `projects/${encodedProjectId}/snippets/${encodedId}`;
} else {
path = `snippets/${encodedId}`;
}
// If raw content is requested, append /raw to the path
if (raw) {
path = `${path}/raw`;
}
return gitlab.get(path);
}
/* istanbul ignore next -- unreachable with Zod discriminatedUnion */
default:
throw new Error(`Unknown action: ${(input as { action: string }).action}`);
}
},
},
],
// ============================================================================
// manage_snippet - CQRS Command Tool (discriminated union schema)
// TypeScript automatically narrows types in each switch case
// ============================================================================
[
"manage_snippet",
{
name: "manage_snippet",
description:
'MANAGE GitLab snippets. Actions: "create" creates new snippet with multiple files and visibility control, "update" modifies title/description/visibility/files (supports file create/update/delete/move), "delete" permanently removes snippet. Supports personal and project snippets.',
inputSchema: z.toJSONSchema(ManageSnippetSchema),
gate: { envVar: "USE_SNIPPETS", defaultValue: true },
handler: async (args: unknown): Promise<unknown> => {
const input = ManageSnippetSchema.parse(args);
// Runtime validation: reject denied actions even if they bypass schema filtering
Iif (isActionDenied("manage_snippet", input.action)) {
throw new Error(`Action '${input.action}' is not allowed for manage_snippet tool`);
}
switch (input.action) {
case "create": {
// TypeScript knows: input has title, files (required), projectId, description, visibility (optional)
const { projectId, title, description, visibility, files } = input;
const body: Record<string, unknown> = {
title,
visibility,
files,
};
Iif (description) {
body.description = description;
}
let path: string;
if (projectId) {
const encodedProjectId = encodeURIComponent(projectId);
path = `projects/${encodedProjectId}/snippets`;
} else {
path = "snippets";
}
return gitlab.post(path, {
body,
contentType: "json",
});
}
case "update": {
// TypeScript knows: input has id (required), projectId, title, description, visibility, files (optional)
const { id, projectId, title, description, visibility, files } = input;
const encodedId = id.toString();
const body: Record<string, unknown> = {};
if (title !== undefined) {
body.title = title;
}
Iif (description !== undefined) {
body.description = description;
}
Iif (visibility !== undefined) {
body.visibility = visibility;
}
if (files !== undefined) {
body.files = files;
}
let path: string;
Iif (projectId) {
const encodedProjectId = encodeURIComponent(projectId);
path = `projects/${encodedProjectId}/snippets/${encodedId}`;
} else {
path = `snippets/${encodedId}`;
}
return gitlab.put(path, {
body,
contentType: "json",
});
}
case "delete": {
// TypeScript knows: input has id (required), projectId (optional)
const { id, projectId } = input;
const encodedId = id.toString();
let path: string;
if (projectId) {
const encodedProjectId = encodeURIComponent(projectId);
path = `projects/${encodedProjectId}/snippets/${encodedId}`;
} else {
path = `snippets/${encodedId}`;
}
await gitlab.delete(path);
return { deleted: true, id };
}
/* 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 getSnippetsReadOnlyToolNames(): string[] {
return ["browse_snippets"];
}
/**
* Get all tool definitions from the registry
*/
export function getSnippetsToolDefinitions(): EnhancedToolDefinition[] {
return Array.from(snippetsToolRegistry.values());
}
/**
* Get filtered tools based on read-only mode
*/
export function getFilteredSnippetsTools(readOnlyMode: boolean = false): EnhancedToolDefinition[] {
if (readOnlyMode) {
const readOnlyNames = getSnippetsReadOnlyToolNames();
return Array.from(snippetsToolRegistry.values()).filter(tool =>
readOnlyNames.includes(tool.name)
);
}
return getSnippetsToolDefinitions();
}
|