CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
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 | /**
* MCP tool handlers — read-only v1 set.
*
* Each handler returns either a string (auto-wrapped to text content)
* or the full MCP `{content: [...]}` shape. Errors throw `McpError` from
* `mcp.ts` so the router can surface them as JSON-RPC -32xxx codes.
*
* Tool surface (v1, all read-only):
* - gluecron_repo_search — search public repos by keyword
* - gluecron_repo_read_file — read a file from a repo at a ref
* - gluecron_repo_list_issues — list open issues for a repo
* - gluecron_repo_explain_codebase — return cached AI explanation
*
* v2 will add write tools (create_issue, post_comment, run_workflow)
* gated on `userId` + write-access on the target repo.
*/
import { and, asc, desc, eq, like, or } from "drizzle-orm";
import { db } from "../db";
import {
issues,
repositories,
users,
codebaseExplanations,
} from "../db/schema";
import { getBlob, repoExists } from "../git/repository";
import { computeHealthScore } from "./intelligence";
import { McpError, ERR_INVALID_PARAMS, ERR_METHOD_NOT_FOUND } from "./mcp";
import type { McpContext } from "./mcp";
export type McpTool = {
name: string;
description: string;
inputSchema: {
type: "object";
properties: Record<string, { type: string; description?: string }>;
required?: string[];
};
};
export type McpToolHandler = {
tool: McpTool;
run: (
args: Record<string, unknown>,
ctx: McpContext
) => Promise<unknown>;
};
const argString = (
args: Record<string, unknown>,
key: string,
fallback?: string
): string => {
const v = args[key];
if (typeof v === "string" && v.trim().length > 0) return v.trim();
if (fallback !== undefined) return fallback;
throw new McpError(ERR_INVALID_PARAMS, `argument '${key}' is required`);
};
const argNumber = (
args: Record<string, unknown>,
key: string,
fallback?: number
): number => {
const v = args[key];
if (typeof v === "number" && Number.isFinite(v)) return v;
if (typeof v === "string" && /^\d+$/.test(v)) return Number.parseInt(v, 10);
if (fallback !== undefined) return fallback;
throw new McpError(ERR_INVALID_PARAMS, `argument '${key}' must be a number`);
};
// ---------------------------------------------------------------------------
// gluecron_repo_search
// ---------------------------------------------------------------------------
const repoSearch: McpToolHandler = {
tool: {
name: "gluecron_repo_search",
description:
"Search public Gluecron repositories by keyword. Matches against name + description. Returns up to 20 results.",
inputSchema: {
type: "object",
properties: {
query: { type: "string", description: "Search keyword (1-100 chars)" },
limit: { type: "number", description: "Max results, default 20" },
},
required: ["query"],
},
},
async run(args) {
const q = argString(args, "query");
if (q.length > 100) {
throw new McpError(ERR_INVALID_PARAMS, "query too long (max 100 chars)");
}
const limit = Math.max(1, Math.min(50, argNumber(args, "limit", 20)));
const pattern = `%${q.replace(/[%_]/g, (m) => "\\" + m)}%`;
const rows = await db
.select({
id: repositories.id,
name: repositories.name,
description: repositories.description,
ownerName: users.username,
stars: repositories.starCount,
})
.from(repositories)
.innerJoin(users, eq(repositories.ownerId, users.id))
.where(
and(
eq(repositories.isPrivate, false),
or(
like(repositories.name, pattern),
like(repositories.description, pattern)
)
)
)
.orderBy(desc(repositories.starCount))
.limit(limit);
return {
total: rows.length,
repos: rows.map((r) => ({
fullName: `${r.ownerName}/${r.name}`,
description: r.description || "",
stars: r.stars,
})),
};
},
};
// ---------------------------------------------------------------------------
// gluecron_repo_read_file
// ---------------------------------------------------------------------------
const repoReadFile: McpToolHandler = {
tool: {
name: "gluecron_repo_read_file",
description:
"Read a single file from a public repository at a given ref (branch / tag / commit). Returns the text content (binary files rejected).",
inputSchema: {
type: "object",
properties: {
owner: { type: "string", description: "Repo owner username" },
repo: { type: "string", description: "Repo name" },
ref: { type: "string", description: "Branch / tag / commit (default: main)" },
path: { type: "string", description: "File path within the repo" },
},
required: ["owner", "repo", "path"],
},
},
async run(args) {
const owner = argString(args, "owner");
const repo = argString(args, "repo");
const ref = argString(args, "ref", "main");
const path = argString(args, "path");
// Visibility check — public-only for v1. (Authed users see private
// repos in v2 once we extend the args.)
const [r] = await db
.select({ isPrivate: repositories.isPrivate })
.from(repositories)
.innerJoin(users, eq(repositories.ownerId, users.id))
.where(and(eq(users.username, owner), eq(repositories.name, repo)))
.limit(1);
if (!r) throw new McpError(ERR_METHOD_NOT_FOUND, `repo not found: ${owner}/${repo}`);
if (r.isPrivate) {
throw new McpError(
ERR_METHOD_NOT_FOUND,
`${owner}/${repo} is private; v1 MCP read tool is public-only`
);
}
const blob = await getBlob(owner, repo, ref, path);
if (!blob) {
throw new McpError(
ERR_METHOD_NOT_FOUND,
`path not found: ${owner}/${repo}@${ref}:${path}`
);
}
return {
content: [
{
type: "text",
text: blob.content,
},
],
};
},
};
// ---------------------------------------------------------------------------
// gluecron_repo_list_issues
// ---------------------------------------------------------------------------
const repoListIssues: McpToolHandler = {
tool: {
name: "gluecron_repo_list_issues",
description:
"List open issues for a public repository. Returns up to 50 ordered by most-recent.",
inputSchema: {
type: "object",
properties: {
owner: { type: "string", description: "Repo owner username" },
repo: { type: "string", description: "Repo name" },
limit: { type: "number", description: "Max results, default 25" },
},
required: ["owner", "repo"],
},
},
async run(args) {
const owner = argString(args, "owner");
const repo = argString(args, "repo");
const limit = Math.max(1, Math.min(50, argNumber(args, "limit", 25)));
const [r] = await db
.select({ id: repositories.id, isPrivate: repositories.isPrivate })
.from(repositories)
.innerJoin(users, eq(repositories.ownerId, users.id))
.where(and(eq(users.username, owner), eq(repositories.name, repo)))
.limit(1);
if (!r) throw new McpError(ERR_METHOD_NOT_FOUND, `repo not found: ${owner}/${repo}`);
if (r.isPrivate) {
throw new McpError(
ERR_METHOD_NOT_FOUND,
`${owner}/${repo} is private; v1 MCP read tool is public-only`
);
}
const rows = await db
.select({
number: issues.number,
title: issues.title,
body: issues.body,
state: issues.state,
createdAt: issues.createdAt,
})
.from(issues)
.where(and(eq(issues.repositoryId, r.id), eq(issues.state, "open")))
.orderBy(desc(issues.createdAt))
.limit(limit);
return {
total: rows.length,
issues: rows.map((i) => ({
number: i.number,
title: i.title,
body: i.body || "",
state: i.state,
createdAt: i.createdAt,
})),
};
},
};
// ---------------------------------------------------------------------------
// gluecron_repo_explain_codebase
// ---------------------------------------------------------------------------
const repoExplain: McpToolHandler = {
tool: {
name: "gluecron_repo_explain_codebase",
description:
"Return the cached AI 'explain this codebase' Markdown for a public repo (most recent commit). Returns null when no cached explanation exists yet.",
inputSchema: {
type: "object",
properties: {
owner: { type: "string", description: "Repo owner username" },
repo: { type: "string", description: "Repo name" },
},
required: ["owner", "repo"],
},
},
async run(args) {
const owner = argString(args, "owner");
const repo = argString(args, "repo");
const [r] = await db
.select({ id: repositories.id, isPrivate: repositories.isPrivate })
.from(repositories)
.innerJoin(users, eq(repositories.ownerId, users.id))
.where(and(eq(users.username, owner), eq(repositories.name, repo)))
.limit(1);
if (!r) throw new McpError(ERR_METHOD_NOT_FOUND, `repo not found: ${owner}/${repo}`);
if (r.isPrivate) {
throw new McpError(
ERR_METHOD_NOT_FOUND,
`${owner}/${repo} is private; v1 MCP read tool is public-only`
);
}
const [row] = await db
.select({
commitSha: codebaseExplanations.commitSha,
markdown: codebaseExplanations.markdown,
generatedAt: codebaseExplanations.generatedAt,
})
.from(codebaseExplanations)
.where(eq(codebaseExplanations.repositoryId, r.id))
.orderBy(desc(codebaseExplanations.generatedAt))
.limit(1);
if (!row) {
return { explanation: null };
}
return {
commitSha: row.commitSha,
generatedAt: row.generatedAt,
markdown: row.markdown,
};
},
};
// ---------------------------------------------------------------------------
// gluecron_repo_health
// ---------------------------------------------------------------------------
const repoHealth: McpToolHandler = {
tool: {
name: "gluecron_repo_health",
description:
"Compute the current health report for a public repo: overall score (0-100), letter grade, per-category breakdown (security/testing/complexity/dependencies/documentation/activity), and a list of insights to fix next. Backed by computeHealthScore in src/lib/intelligence.ts.",
inputSchema: {
type: "object",
properties: {
owner: { type: "string", description: "Repo owner username" },
repo: { type: "string", description: "Repo name" },
},
required: ["owner", "repo"],
},
},
async run(args) {
const owner = argString(args, "owner");
const repo = argString(args, "repo");
const [r] = await db
.select({ id: repositories.id, isPrivate: repositories.isPrivate })
.from(repositories)
.innerJoin(users, eq(repositories.ownerId, users.id))
.where(and(eq(users.username, owner), eq(repositories.name, repo)))
.limit(1);
if (!r) throw new McpError(ERR_METHOD_NOT_FOUND, `repo not found: ${owner}/${repo}`);
if (r.isPrivate) {
throw new McpError(
ERR_METHOD_NOT_FOUND,
`${owner}/${repo} is private; v1 MCP tools are public-only`
);
}
if (!(await repoExists(owner, repo))) {
throw new McpError(
ERR_METHOD_NOT_FOUND,
`${owner}/${repo} has no on-disk git data yet`
);
}
const report = await computeHealthScore(owner, repo);
return {
score: report.score,
grade: report.grade,
breakdown: report.breakdown,
insights: report.insights,
generatedAt: report.generatedAt,
};
},
};
// ---------------------------------------------------------------------------
// Default tool registry
// ---------------------------------------------------------------------------
export function defaultTools(): Record<string, McpToolHandler> {
return {
[repoSearch.tool.name]: repoSearch,
[repoReadFile.tool.name]: repoReadFile,
[repoListIssues.tool.name]: repoListIssues,
[repoExplain.tool.name]: repoExplain,
[repoHealth.tool.name]: repoHealth,
};
}
/** Test-only export of internal helpers + per-tool handlers. */
export const __test = {
argString,
argNumber,
repoSearch,
repoReadFile,
repoListIssues,
repoExplain,
repoHealth,
};
|