|
| 1 | +import type { FastMCP } from "fastmcp"; |
| 2 | +import { z } from "zod"; |
| 3 | + |
| 4 | +import { isSafeGitUpstreamToken, spawnGitAsync } from "./git.js"; |
| 5 | +import { jsonRespond } from "./json.js"; |
| 6 | +import { requireSingleRepo } from "./roots.js"; |
| 7 | +import { WorkspacePickSchema } from "./schemas.js"; |
| 8 | + |
| 9 | +// --------------------------------------------------------------------------- |
| 10 | +// Types |
| 11 | +// --------------------------------------------------------------------------- |
| 12 | + |
| 13 | +interface TagResult { |
| 14 | + tag: string; |
| 15 | + type: "annotated" | "lightweight" | "deleted"; |
| 16 | + sha: string; |
| 17 | +} |
| 18 | + |
| 19 | +// --------------------------------------------------------------------------- |
| 20 | +// Helpers |
| 21 | +// --------------------------------------------------------------------------- |
| 22 | + |
| 23 | +/** |
| 24 | + * Get the SHA of a given ref (tag, commit, branch, etc). |
| 25 | + */ |
| 26 | +async function getRefSha(gitTop: string, ref: string): Promise<string | null> { |
| 27 | + const result = await spawnGitAsync(gitTop, ["rev-parse", ref]); |
| 28 | + if (!result.ok) return null; |
| 29 | + return result.stdout.trim(); |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Check if a tag is annotated or lightweight. |
| 34 | + */ |
| 35 | +async function getTagType( |
| 36 | + gitTop: string, |
| 37 | + tag: string, |
| 38 | +): Promise<"annotated" | "lightweight" | null> { |
| 39 | + // For annotated tags, `git cat-file -t <tag>` returns "tag" |
| 40 | + // For lightweight tags, it returns "commit" |
| 41 | + const result = await spawnGitAsync(gitTop, ["cat-file", "-t", tag]); |
| 42 | + if (!result.ok) return null; |
| 43 | + const type = result.stdout.trim(); |
| 44 | + if (type === "tag") return "annotated"; |
| 45 | + if (type === "commit") return "lightweight"; |
| 46 | + return null; |
| 47 | +} |
| 48 | + |
| 49 | +// --------------------------------------------------------------------------- |
| 50 | +// Tool registration |
| 51 | +// --------------------------------------------------------------------------- |
| 52 | + |
| 53 | +export function registerGitTagTool(server: FastMCP): void { |
| 54 | + server.addTool({ |
| 55 | + name: "git_tag", |
| 56 | + description: |
| 57 | + "Create, delete, or inspect git tags. Create annotated tags (with message) or lightweight tags (ref only). " + |
| 58 | + "Returns tag name, type, and SHA.", |
| 59 | + annotations: { |
| 60 | + readOnlyHint: false, |
| 61 | + destructiveHint: true, |
| 62 | + }, |
| 63 | + parameters: WorkspacePickSchema.omit({ absoluteGitRoots: true }).extend({ |
| 64 | + tag: z.string().min(1).describe("Tag name (e.g. 'v1.2.3')."), |
| 65 | + message: z |
| 66 | + .string() |
| 67 | + .optional() |
| 68 | + .describe( |
| 69 | + "If provided, create an annotated tag with this message. If absent, create a lightweight tag.", |
| 70 | + ), |
| 71 | + ref: z |
| 72 | + .string() |
| 73 | + .optional() |
| 74 | + .describe("Commit/ref to tag (default: HEAD). Ignored if `delete` is true."), |
| 75 | + delete: z |
| 76 | + .boolean() |
| 77 | + .optional() |
| 78 | + .default(false) |
| 79 | + .describe("If true, delete the named tag instead of creating it."), |
| 80 | + }), |
| 81 | + execute: async (args) => { |
| 82 | + const pre = requireSingleRepo(server, args); |
| 83 | + if (!pre.ok) return jsonRespond(pre.error); |
| 84 | + const gitTop = pre.gitTop; |
| 85 | + |
| 86 | + const tag = args.tag.trim(); |
| 87 | + if (!tag) { |
| 88 | + return jsonRespond({ error: "tag_empty" }); |
| 89 | + } |
| 90 | + |
| 91 | + // Validate tag name: no shell metacharacters |
| 92 | + if (!isSafeGitUpstreamToken(tag)) { |
| 93 | + return jsonRespond({ error: "tag_unsafe", tag }); |
| 94 | + } |
| 95 | + |
| 96 | + // Handle deletion |
| 97 | + if (args.delete === true) { |
| 98 | + const delResult = await spawnGitAsync(gitTop, ["tag", "-d", tag]); |
| 99 | + if (!delResult.ok) { |
| 100 | + return jsonRespond({ |
| 101 | + error: "tag_delete_failed", |
| 102 | + detail: (delResult.stderr || delResult.stdout).trim(), |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + if (args.format === "json") { |
| 107 | + return jsonRespond({ |
| 108 | + tag, |
| 109 | + type: "deleted", |
| 110 | + sha: "", // Deleted tags have no SHA |
| 111 | + } as unknown as Record<string, unknown>); |
| 112 | + } |
| 113 | + |
| 114 | + return `Deleted tag: ${tag}`; |
| 115 | + } |
| 116 | + |
| 117 | + // Determine the ref to tag (default HEAD) |
| 118 | + const ref = (args.ref ?? "HEAD").trim(); |
| 119 | + if (!isSafeGitUpstreamToken(ref)) { |
| 120 | + return jsonRespond({ error: "ref_unsafe", ref }); |
| 121 | + } |
| 122 | + |
| 123 | + // Get the SHA of the ref to tag |
| 124 | + const sha = await getRefSha(gitTop, ref); |
| 125 | + if (!sha) { |
| 126 | + return jsonRespond({ |
| 127 | + error: "ref_not_found", |
| 128 | + ref, |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + // Create tag (annotated or lightweight) |
| 133 | + const tagArgs: string[] = ["tag"]; |
| 134 | + |
| 135 | + if (args.message) { |
| 136 | + // Annotated tag |
| 137 | + tagArgs.push("-a", "-m", args.message); |
| 138 | + } else { |
| 139 | + // Lightweight tag (just the tag name and ref) |
| 140 | + } |
| 141 | + |
| 142 | + tagArgs.push(tag, ref); |
| 143 | + |
| 144 | + const createResult = await spawnGitAsync(gitTop, tagArgs); |
| 145 | + if (!createResult.ok) { |
| 146 | + return jsonRespond({ |
| 147 | + error: "tag_create_failed", |
| 148 | + detail: (createResult.stderr || createResult.stdout).trim(), |
| 149 | + }); |
| 150 | + } |
| 151 | + |
| 152 | + // Verify the tag was created and get its type |
| 153 | + const tagType = await getTagType(gitTop, tag); |
| 154 | + if (!tagType) { |
| 155 | + return jsonRespond({ |
| 156 | + error: "tag_verification_failed", |
| 157 | + tag, |
| 158 | + }); |
| 159 | + } |
| 160 | + |
| 161 | + const result: TagResult = { |
| 162 | + tag, |
| 163 | + type: tagType, |
| 164 | + sha, |
| 165 | + }; |
| 166 | + |
| 167 | + if (args.format === "json") { |
| 168 | + return jsonRespond(result as unknown as Record<string, unknown>); |
| 169 | + } |
| 170 | + |
| 171 | + // Markdown output |
| 172 | + const lines: string[] = []; |
| 173 | + lines.push(`# Tag: ${tag}`); |
| 174 | + lines.push(""); |
| 175 | + lines.push(`**Type:** ${tagType}`); |
| 176 | + lines.push(`**SHA:** \`${sha}\``); |
| 177 | + if (args.message) { |
| 178 | + lines.push(""); |
| 179 | + lines.push("**Message:**"); |
| 180 | + lines.push(""); |
| 181 | + lines.push("```"); |
| 182 | + lines.push(args.message); |
| 183 | + lines.push("```"); |
| 184 | + } |
| 185 | + |
| 186 | + return lines.join("\n"); |
| 187 | + }, |
| 188 | + }); |
| 189 | +} |
0 commit comments