|
| 1 | +import { z } from "zod"; |
| 2 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 3 | +import { githubGet, GitHubApiError, REPO_URL } from "../github.js"; |
| 4 | +import { matchAreas } from "../data/sourceMap.js"; |
| 5 | + |
| 6 | +interface GitHubCommit { |
| 7 | + sha: string; |
| 8 | + commit: { |
| 9 | + message: string; |
| 10 | + author: { |
| 11 | + date: string; |
| 12 | + }; |
| 13 | + }; |
| 14 | + author: { |
| 15 | + login: string; |
| 16 | + } | null; |
| 17 | + html_url: string; |
| 18 | +} |
| 19 | + |
| 20 | +export function registerChangelogTool(server: McpServer): void { |
| 21 | + server.registerTool( |
| 22 | + "get_recent_changes", |
| 23 | + { |
| 24 | + title: "Get Recent Changes", |
| 25 | + description: |
| 26 | + "Fetches the most recent commits to the main branch of vscode-cmake-tools. " + |
| 27 | + "Each commit is annotated with affected areas (derived from keywords in the commit message " + |
| 28 | + "matched against the codebase source map). Includes a summary of the most active areas " + |
| 29 | + "to help new contributors understand what's currently in flux.", |
| 30 | + inputSchema: z.object({ |
| 31 | + limit: z |
| 32 | + .number() |
| 33 | + .int() |
| 34 | + .min(1) |
| 35 | + .max(30) |
| 36 | + .default(10) |
| 37 | + .describe("Number of recent commits to fetch (default 10, max 30).") |
| 38 | + }) |
| 39 | + }, |
| 40 | + async ({ limit }) => { |
| 41 | + const effectiveLimit = Math.min(limit ?? 10, 30); |
| 42 | + |
| 43 | + try { |
| 44 | + const raw = await githubGet<GitHubCommit[]>( |
| 45 | + `/commits?per_page=${effectiveLimit}` |
| 46 | + ); |
| 47 | + |
| 48 | + // Track area frequencies for the summary |
| 49 | + const areaFrequency = new Map<string, number>(); |
| 50 | + |
| 51 | + const commits = raw.map((c) => { |
| 52 | + const firstLine = c.commit.message.split("\n")[0].trim(); |
| 53 | + const areas = matchAreas(c.commit.message); |
| 54 | + |
| 55 | + for (const area of areas) { |
| 56 | + areaFrequency.set(area, (areaFrequency.get(area) ?? 0) + 1); |
| 57 | + } |
| 58 | + |
| 59 | + return { |
| 60 | + sha: c.sha.slice(0, 7), |
| 61 | + message: firstLine, |
| 62 | + author: c.author?.login ?? "unknown", |
| 63 | + date: c.commit.author.date, |
| 64 | + url: c.html_url, |
| 65 | + affectedAreas: areas |
| 66 | + }; |
| 67 | + }); |
| 68 | + |
| 69 | + // Top 3 most active areas |
| 70 | + const mostActiveAreas = [...areaFrequency.entries()] |
| 71 | + .sort((a, b) => b[1] - a[1]) |
| 72 | + .slice(0, 3) |
| 73 | + .map(([area]) => area); |
| 74 | + |
| 75 | + const areaList = mostActiveAreas.join(", ") || "general"; |
| 76 | + const tip = |
| 77 | + mostActiveAreas.length > 0 && !mostActiveAreas.includes("general") |
| 78 | + ? `Recent activity is concentrated in ${areaList} — if you're new, these areas may have more in-flux context to catch up on.` |
| 79 | + : "Recent commits span a broad range of areas. Check the affectedAreas on each commit to find patterns."; |
| 80 | + |
| 81 | + const result = { |
| 82 | + commits, |
| 83 | + summary: { |
| 84 | + fetchedAt: new Date().toISOString(), |
| 85 | + repoUrl: REPO_URL, |
| 86 | + totalReturned: commits.length, |
| 87 | + mostActiveAreas, |
| 88 | + tip |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + return { |
| 93 | + content: [{ type: "text" as const, text: JSON.stringify(result, null, 2) }] |
| 94 | + }; |
| 95 | + } catch (error) { |
| 96 | + const message = |
| 97 | + error instanceof GitHubApiError |
| 98 | + ? error.message |
| 99 | + : `Failed to fetch commits: ${error instanceof Error ? error.message : String(error)}`; |
| 100 | + |
| 101 | + return { |
| 102 | + content: [ |
| 103 | + { |
| 104 | + type: "text" as const, |
| 105 | + text: JSON.stringify({ error: message }, null, 2) |
| 106 | + } |
| 107 | + ], |
| 108 | + isError: true |
| 109 | + }; |
| 110 | + } |
| 111 | + } |
| 112 | + ); |
| 113 | +} |
0 commit comments