Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
mcp-tools.ts29.7 KB · 968 lines
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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
/**
 * MCP (Model Context Protocol) tool definitions and implementations.
 *
 * Each tool has:
 *   - name: unique identifier
 *   - description: human/model readable explanation (required by MCP spec)
 *   - inputSchema: JSON Schema for the arguments
 *   - handler: async function that receives (args, user) and returns a result
 *
 * The route (src/routes/mcp.ts) dispatches tools/call to handler() and
 * wraps errors in JSON-RPC error objects.
 */

import { eq, and, desc, inArray } from "drizzle-orm";
import { db } from "../db";
import {
  repositories,
  users,
  issues,
  issueLabels,
  labels,
  pullRequests,
  prComments,
  gateRuns,
} from "../db/schema";
import type { User } from "../db/schema";
import {
  getTree,
  getBlob,
  listCommits,
  searchCode,
  getRepoPath,
} from "../git/repository";
import { loadRepoByPath } from "./namespace";
import { explainCodebase, getCachedExplanation } from "./ai-explain";
import { triggerAiReview } from "./ai-review";

// --------------------------------------------------------------------------
// Shared error helpers
// --------------------------------------------------------------------------

export class McpToolError extends Error {
  constructor(
    public code: number,
    message: string
  ) {
    super(message);
    this.name = "McpToolError";
  }
}

function notFound(msg: string): never {
  throw new McpToolError(-32004, msg);
}

function unauthorized(msg = "Authentication required"): never {
  throw new McpToolError(-32001, msg);
}

function invalidParams(msg: string): never {
  throw new McpToolError(-32602, msg);
}

// --------------------------------------------------------------------------
// Helper: resolve owner username → user row
// --------------------------------------------------------------------------

async function resolveOwnerUser(username: string): Promise<User> {
  const [u] = await db
    .select()
    .from(users)
    .where(eq(users.username, username))
    .limit(1);
  if (!u) notFound(`User '${username}' not found`);
  return u;
}

// --------------------------------------------------------------------------
// Helper: load repo + enforce visibility
// --------------------------------------------------------------------------

async function loadAndCheckRepo(
  owner: string,
  repoName: string,
  caller: User | null
) {
  const repo = await loadRepoByPath(owner, repoName);
  if (!repo) notFound(`Repository '${owner}/${repoName}' not found`);
  if (repo.isPrivate) {
    if (!caller) unauthorized(`Repository '${owner}/${repoName}' is private`);
    // caller must be the owner (org-owned repos not checked here for brevity)
    if (repo.ownerId !== caller.id) {
      unauthorized(`Access denied to private repository '${owner}/${repoName}'`);
    }
  }
  return repo;
}

// --------------------------------------------------------------------------
// Tool type
// --------------------------------------------------------------------------

export interface McpTool {
  name: string;
  description: string;
  inputSchema: Record<string, unknown>;
  handler: (args: Record<string, unknown>, user: User | null) => Promise<unknown>;
}

// --------------------------------------------------------------------------
// Tool: list_repositories
// --------------------------------------------------------------------------

const listRepositoriesTool: McpTool = {
  name: "list_repositories",
  description:
    "List repositories owned by a Gluecron user. If username is omitted the authenticated user's repos are returned.",
  inputSchema: {
    type: "object",
    properties: {
      username: {
        type: "string",
        description: "Username whose repositories to list. Defaults to the authenticated user.",
      },
    },
  },
  async handler(args, user) {
    const usernameArg = args.username as string | undefined;
    let targetUser: User;
    if (usernameArg) {
      targetUser = await resolveOwnerUser(usernameArg);
    } else {
      if (!user) unauthorized("Provide a username or authenticate");
      targetUser = user;
    }

    const rows = await db
      .select({
        id: repositories.id,
        name: repositories.name,
        description: repositories.description,
        isPrivate: repositories.isPrivate,
        defaultBranch: repositories.defaultBranch,
        stars: repositories.starCount,
        createdAt: repositories.createdAt,
        updatedAt: repositories.updatedAt,
        pushedAt: repositories.pushedAt,
      })
      .from(repositories)
      .where(
        and(
          eq(repositories.ownerId, targetUser.id),
          // Only show private repos to the owner themselves
          user?.id === targetUser.id
            ? undefined
            : eq(repositories.isPrivate, false)
        )
      )
      .orderBy(desc(repositories.updatedAt));

    return rows.map((r) => ({
      owner: targetUser.username,
      name: r.name,
      description: r.description ?? null,
      isPrivate: r.isPrivate,
      defaultBranch: r.defaultBranch,
      stars: r.stars,
      createdAt: r.createdAt,
      updatedAt: r.updatedAt,
      pushedAt: r.pushedAt ?? null,
    }));
  },
};

// --------------------------------------------------------------------------
// Tool: get_repository
// --------------------------------------------------------------------------

const getRepositoryTool: McpTool = {
  name: "get_repository",
  description:
    "Get full details for a repository including description, default branch, star/fork/issue counts, and recent activity.",
  inputSchema: {
    type: "object",
    properties: {
      owner: { type: "string", description: "Repository owner username" },
      name: { type: "string", description: "Repository name" },
    },
    required: ["owner", "name"],
  },
  async handler(args, user) {
    const owner = args.owner as string;
    const repoName = args.name as string;
    if (!owner || !repoName) invalidParams("owner and name are required");

    const repo = await loadAndCheckRepo(owner, repoName, user);

    // Recent activity
    const activity = await db
      .select()
      .from(
        (await import("../db/schema")).activityFeed
      )
      .where(eq((await import("../db/schema")).activityFeed.repositoryId, repo.id))
      .orderBy(desc((await import("../db/schema")).activityFeed.createdAt))
      .limit(10);

    return {
      id: repo.id,
      owner,
      name: repo.name,
      description: repo.description ?? null,
      isPrivate: repo.isPrivate,
      isArchived: repo.isArchived,
      isTemplate: repo.isTemplate,
      defaultBranch: repo.defaultBranch,
      stars: repo.starCount,
      forks: repo.forkCount,
      openIssues: repo.issueCount,
      createdAt: repo.createdAt,
      updatedAt: repo.updatedAt,
      pushedAt: repo.pushedAt ?? null,
      recentActivity: activity.map((a) => ({
        action: a.action,
        targetType: a.targetType ?? null,
        targetId: a.targetId ?? null,
        createdAt: a.createdAt,
      })),
    };
  },
};

// --------------------------------------------------------------------------
// Tool: get_file_contents
// --------------------------------------------------------------------------

const getFileContentsTool: McpTool = {
  name: "get_file_contents",
  description: "Get the contents of a file in a repository at a given ref (branch, tag, or commit SHA).",
  inputSchema: {
    type: "object",
    properties: {
      owner: { type: "string", description: "Repository owner username" },
      repo: { type: "string", description: "Repository name" },
      path: { type: "string", description: "File path within the repository" },
      ref: {
        type: "string",
        description: "Branch, tag, or commit SHA. Defaults to the repository default branch.",
      },
    },
    required: ["owner", "repo", "path"],
  },
  async handler(args, user) {
    const owner = args.owner as string;
    const repoName = args.repo as string;
    const filePath = args.path as string;
    if (!owner || !repoName || !filePath) invalidParams("owner, repo, and path are required");

    const repo = await loadAndCheckRepo(owner, repoName, user);
    const ref = (args.ref as string | undefined) || repo.defaultBranch;

    const blob = await getBlob(owner, repoName, ref, filePath);
    if (!blob) notFound(`File '${filePath}' not found at ref '${ref}'`);

    if (blob.isBinary) {
      return {
        content: "",
        encoding: "binary",
        size: blob.size,
        path: filePath,
        isBinary: true,
      };
    }

    return {
      content: blob.content,
      encoding: "utf8",
      size: blob.size,
      path: filePath,
      isBinary: false,
    };
  },
};

// --------------------------------------------------------------------------
// Tool: list_files
// --------------------------------------------------------------------------

const listFilesTool: McpTool = {
  name: "list_files",
  description: "List files and directories in a repository at a given path and ref.",
  inputSchema: {
    type: "object",
    properties: {
      owner: { type: "string", description: "Repository owner username" },
      repo: { type: "string", description: "Repository name" },
      path: {
        type: "string",
        description: "Directory path to list. Defaults to the root directory.",
      },
      ref: {
        type: "string",
        description: "Branch, tag, or commit SHA. Defaults to the repository default branch.",
      },
    },
    required: ["owner", "repo"],
  },
  async handler(args, user) {
    const owner = args.owner as string;
    const repoName = args.repo as string;
    if (!owner || !repoName) invalidParams("owner and repo are required");

    const repo = await loadAndCheckRepo(owner, repoName, user);
    const ref = (args.ref as string | undefined) || repo.defaultBranch;
    const treePath = (args.path as string | undefined) || "";

    const entries = await getTree(owner, repoName, ref, treePath);

    return entries.map((e) => {
      const entryPath = treePath ? `${treePath}/${e.name}` : e.name;
      return {
        name: e.name,
        path: entryPath,
        type: e.type === "tree" ? "dir" : "file",
        size: e.type === "blob" ? (e.size ?? null) : null,
        sha: e.sha,
      };
    });
  },
};

// --------------------------------------------------------------------------
// Tool: search_code
// --------------------------------------------------------------------------

const searchCodeTool: McpTool = {
  name: "search_code",
  description: "Search for a string or pattern in the source code of a repository.",
  inputSchema: {
    type: "object",
    properties: {
      owner: { type: "string", description: "Repository owner username" },
      repo: { type: "string", description: "Repository name" },
      query: { type: "string", description: "Search query string (passed to git grep)" },
      ref: {
        type: "string",
        description: "Branch, tag, or commit SHA. Defaults to the repository default branch.",
      },
    },
    required: ["owner", "repo", "query"],
  },
  async handler(args, user) {
    const owner = args.owner as string;
    const repoName = args.repo as string;
    const query = args.query as string;
    if (!owner || !repoName || !query) invalidParams("owner, repo, and query are required");

    const repo = await loadAndCheckRepo(owner, repoName, user);
    const ref = (args.ref as string | undefined) || repo.defaultBranch;

    const matches = await searchCode(owner, repoName, ref, query);

    return matches.map((m) => ({
      file: m.file,
      line: m.lineNum,
      content: m.line,
    }));
  },
};

// --------------------------------------------------------------------------
// Tool: list_commits
// --------------------------------------------------------------------------

const listCommitsTool: McpTool = {
  name: "list_commits",
  description: "List recent commits on a branch of a repository.",
  inputSchema: {
    type: "object",
    properties: {
      owner: { type: "string", description: "Repository owner username" },
      repo: { type: "string", description: "Repository name" },
      branch: {
        type: "string",
        description: "Branch name. Defaults to the repository default branch.",
      },
      limit: {
        type: "number",
        description: "Maximum number of commits to return (1–100). Defaults to 20.",
        minimum: 1,
        maximum: 100,
      },
    },
    required: ["owner", "repo"],
  },
  async handler(args, user) {
    const owner = args.owner as string;
    const repoName = args.repo as string;
    if (!owner || !repoName) invalidParams("owner and repo are required");

    const repo = await loadAndCheckRepo(owner, repoName, user);
    const branch = (args.branch as string | undefined) || repo.defaultBranch;
    const limit = Math.min(100, Math.max(1, (args.limit as number | undefined) ?? 20));

    const commits = await listCommits(owner, repoName, branch, limit);

    return commits.map((c) => ({
      sha: c.sha,
      message: c.message,
      author: c.author,
      authorEmail: c.authorEmail,
      date: c.date,
      parentShas: c.parentShas,
    }));
  },
};

// --------------------------------------------------------------------------
// Tool: get_gate_status
// --------------------------------------------------------------------------

const getGateStatusTool: McpTool = {
  name: "get_gate_status",
  description:
    "Get the current gate status for a repository — the latest result for each configured gate (GateTest, AI Review, Secret Scan, etc.).",
  inputSchema: {
    type: "object",
    properties: {
      owner: { type: "string", description: "Repository owner username" },
      repo: { type: "string", description: "Repository name" },
    },
    required: ["owner", "repo"],
  },
  async handler(args, user) {
    const owner = args.owner as string;
    const repoName = args.repo as string;
    if (!owner || !repoName) invalidParams("owner and repo are required");

    const repo = await loadAndCheckRepo(owner, repoName, user);

    // Get all gate runs ordered newest-first and deduplicate by gate name
    const runs = await db
      .select()
      .from(gateRuns)
      .where(eq(gateRuns.repositoryId, repo.id))
      .orderBy(desc(gateRuns.createdAt))
      .limit(200);

    // Latest run per gate name
    const latestByGate = new Map<string, typeof runs[0]>();
    for (const run of runs) {
      if (!latestByGate.has(run.gateName)) {
        latestByGate.set(run.gateName, run);
      }
    }

    const gates = Array.from(latestByGate.values()).map((r) => ({
      name: r.gateName,
      status: r.status,
      summary: r.summary ?? null,
      commitSha: r.commitSha,
      updatedAt: r.completedAt ?? r.createdAt,
    }));

    // Derive overall status
    let overall: "green" | "red" | "pending" = "green";
    for (const g of gates) {
      if (g.status === "failed") { overall = "red"; break; }
      if (g.status === "pending" || g.status === "running") overall = "pending";
    }
    if (gates.length === 0) overall = "pending";

    return { overall, gates };
  },
};

// --------------------------------------------------------------------------
// Tool: list_pull_requests
// --------------------------------------------------------------------------

const listPullRequestsTool: McpTool = {
  name: "list_pull_requests",
  description: "List pull requests for a repository, optionally filtered by state.",
  inputSchema: {
    type: "object",
    properties: {
      owner: { type: "string", description: "Repository owner username" },
      repo: { type: "string", description: "Repository name" },
      state: {
        type: "string",
        enum: ["open", "closed", "merged"],
        description: "Filter by state. Defaults to 'open'.",
      },
    },
    required: ["owner", "repo"],
  },
  async handler(args, user) {
    const owner = args.owner as string;
    const repoName = args.repo as string;
    if (!owner || !repoName) invalidParams("owner and repo are required");

    const repo = await loadAndCheckRepo(owner, repoName, user);
    const state = (args.state as string | undefined) || "open";

    const prs = await db
      .select({
        id: pullRequests.id,
        number: pullRequests.number,
        title: pullRequests.title,
        state: pullRequests.state,
        authorId: pullRequests.authorId,
        baseBranch: pullRequests.baseBranch,
        headBranch: pullRequests.headBranch,
        createdAt: pullRequests.createdAt,
        mergedAt: pullRequests.mergedAt,
      })
      .from(pullRequests)
      .where(
        and(
          eq(pullRequests.repositoryId, repo.id),
          eq(pullRequests.state, state)
        )
      )
      .orderBy(desc(pullRequests.createdAt))
      .limit(50);

    if (prs.length === 0) return [];

    // Collect author user data
    const authorIds = [...new Set(prs.map((p) => p.authorId))];
    const authorRows = await db
      .select({ id: users.id, username: users.username })
      .from(users)
      .where(inArray(users.id, authorIds));
    const authorMap = new Map(authorRows.map((u) => [u.id, u.username]));

    // Latest AI review comment per PR
    const prIds = prs.map((p) => p.id);
    const aiComments = await db
      .select({
        pullRequestId: prComments.pullRequestId,
        body: prComments.body,
        createdAt: prComments.createdAt,
      })
      .from(prComments)
      .where(
        and(
          inArray(prComments.pullRequestId, prIds),
          eq(prComments.isAiReview, true)
        )
      )
      .orderBy(desc(prComments.createdAt));

    // Latest AI comment per PR
    const aiReviewMap = new Map<string, string>();
    for (const c of aiComments) {
      if (!aiReviewMap.has(c.pullRequestId)) {
        aiReviewMap.set(c.pullRequestId, c.body);
      }
    }

    return prs.map((p) => ({
      number: p.number,
      title: p.title,
      state: p.state,
      author: authorMap.get(p.authorId) ?? null,
      baseBranch: p.baseBranch,
      headBranch: p.headBranch,
      createdAt: p.createdAt,
      mergedAt: p.mergedAt ?? null,
      aiReviewSummary: aiReviewMap.get(p.id) ?? null,
    }));
  },
};

// --------------------------------------------------------------------------
// Tool: list_issues
// --------------------------------------------------------------------------

const listIssuesTool: McpTool = {
  name: "list_issues",
  description: "List issues for a repository, optionally filtered by state.",
  inputSchema: {
    type: "object",
    properties: {
      owner: { type: "string", description: "Repository owner username" },
      repo: { type: "string", description: "Repository name" },
      state: {
        type: "string",
        enum: ["open", "closed"],
        description: "Filter by state. Defaults to 'open'.",
      },
    },
    required: ["owner", "repo"],
  },
  async handler(args, user) {
    const owner = args.owner as string;
    const repoName = args.repo as string;
    if (!owner || !repoName) invalidParams("owner and repo are required");

    const repo = await loadAndCheckRepo(owner, repoName, user);
    const state = (args.state as string | undefined) || "open";

    const issueRows = await db
      .select({
        id: issues.id,
        number: issues.number,
        title: issues.title,
        state: issues.state,
        authorId: issues.authorId,
        createdAt: issues.createdAt,
      })
      .from(issues)
      .where(
        and(
          eq(issues.repositoryId, repo.id),
          eq(issues.state, state)
        )
      )
      .orderBy(desc(issues.createdAt))
      .limit(50);

    if (issueRows.length === 0) return [];

    // Collect author usernames
    const authorIds = [...new Set(issueRows.map((i) => i.authorId))];
    const authorRows = await db
      .select({ id: users.id, username: users.username })
      .from(users)
      .where(inArray(users.id, authorIds));
    const authorMap = new Map(authorRows.map((u) => [u.id, u.username]));

    // Load labels for all issues in one join
    const issueIds = issueRows.map((i) => i.id);
    const labelJoinRows = await db
      .select({
        issueId: issueLabels.issueId,
        labelName: labels.name,
        labelColor: labels.color,
      })
      .from(issueLabels)
      .innerJoin(labels, eq(issueLabels.labelId, labels.id))
      .where(inArray(issueLabels.issueId, issueIds));

    // Group labels by issue
    const labelsMap = new Map<string, Array<{ name: string; color: string }>>();
    for (const row of labelJoinRows) {
      const existing = labelsMap.get(row.issueId) ?? [];
      existing.push({ name: row.labelName, color: row.labelColor });
      labelsMap.set(row.issueId, existing);
    }

    return issueRows.map((i) => ({
      number: i.number,
      title: i.title,
      state: i.state,
      author: authorMap.get(i.authorId) ?? null,
      labels: labelsMap.get(i.id) ?? [],
      createdAt: i.createdAt,
    }));
  },
};

// --------------------------------------------------------------------------
// Tool: create_issue
// --------------------------------------------------------------------------

const createIssueTool: McpTool = {
  name: "create_issue",
  description: "Create a new issue in a repository. Requires authentication.",
  inputSchema: {
    type: "object",
    properties: {
      owner: { type: "string", description: "Repository owner username" },
      repo: { type: "string", description: "Repository name" },
      title: { type: "string", description: "Issue title" },
      body: { type: "string", description: "Issue body (Markdown)" },
      labels: {
        type: "array",
        items: { type: "string" },
        description: "Label names to apply to the issue",
      },
    },
    required: ["owner", "repo", "title"],
  },
  async handler(args, user) {
    if (!user) unauthorized("Creating issues requires authentication");

    const owner = args.owner as string;
    const repoName = args.repo as string;
    const title = args.title as string;
    if (!owner || !repoName || !title) invalidParams("owner, repo, and title are required");
    if (title.trim().length === 0) invalidParams("title cannot be empty");

    const repo = await loadAndCheckRepo(owner, repoName, user);
    const body = (args.body as string | undefined) ?? null;
    const labelNames = (args.labels as string[] | undefined) ?? [];

    // Insert issue
    const [newIssue] = await db
      .insert(issues)
      .values({
        repositoryId: repo.id,
        authorId: user.id,
        title: title.trim(),
        body,
        state: "open",
      })
      .returning({ id: issues.id, number: issues.number });

    if (!newIssue) throw new McpToolError(-32603, "Failed to create issue");

    // Apply labels if any
    if (labelNames.length > 0) {
      const labelRows = await db
        .select({ id: labels.id, name: labels.name })
        .from(labels)
        .where(
          and(
            eq(labels.repositoryId, repo.id),
            inArray(labels.name, labelNames)
          )
        );

      if (labelRows.length > 0) {
        await db.insert(issueLabels).values(
          labelRows.map((l) => ({ issueId: newIssue.id, labelId: l.id }))
        );
      }
    }

    // Bump issue count on repo (best-effort)
    db.update(repositories)
      .set({ issueCount: repo.issueCount + 1 })
      .where(eq(repositories.id, repo.id))
      .catch(() => {});

    return {
      number: newIssue.number,
      url: `/${owner}/${repoName}/issues/${newIssue.number}`,
    };
  },
};

// --------------------------------------------------------------------------
// Tool: explain_codebase
// --------------------------------------------------------------------------

const explainCodebaseTool: McpTool = {
  name: "explain_codebase",
  description:
    "Generate (or return a cached) AI-powered explanation of what a repository does, its architecture, and key files. Results are cached per commit SHA.",
  inputSchema: {
    type: "object",
    properties: {
      owner: { type: "string", description: "Repository owner username" },
      repo: { type: "string", description: "Repository name" },
    },
    required: ["owner", "repo"],
  },
  async handler(args, user) {
    const owner = args.owner as string;
    const repoName = args.repo as string;
    if (!owner || !repoName) invalidParams("owner and repo are required");

    const repo = await loadAndCheckRepo(owner, repoName, user);

    // Get latest commit SHA on default branch
    const commits = await listCommits(owner, repoName, repo.defaultBranch, 1);
    const commitSha = commits[0]?.sha ?? "HEAD";

    // Check cache first
    const cached = await getCachedExplanation(repo.id, commitSha);
    if (cached) {
      return {
        explanation: cached.markdown,
        summary: cached.summary,
        generatedAt: new Date().toISOString(),
        cached: true,
      };
    }

    // Generate fresh
    const result = await explainCodebase({
      owner,
      repo: repoName,
      repositoryId: repo.id,
      commitSha,
    });

    return {
      explanation: result.markdown,
      summary: result.summary,
      generatedAt: new Date().toISOString(),
      cached: result.cached,
    };
  },
};

// --------------------------------------------------------------------------
// Tool: get_branch_diff
// --------------------------------------------------------------------------

const getBranchDiffTool: McpTool = {
  name: "get_branch_diff",
  description:
    "Get the diff between two branches or refs, including per-file addition/deletion stats and the raw diff (truncated to 50 KB).",
  inputSchema: {
    type: "object",
    properties: {
      owner: { type: "string", description: "Repository owner username" },
      repo: { type: "string", description: "Repository name" },
      base: { type: "string", description: "Base branch or ref" },
      head: { type: "string", description: "Head branch or ref" },
    },
    required: ["owner", "repo", "base", "head"],
  },
  async handler(args, user) {
    const owner = args.owner as string;
    const repoName = args.repo as string;
    const base = args.base as string;
    const head = args.head as string;
    if (!owner || !repoName || !base || !head) {
      invalidParams("owner, repo, base, and head are required");
    }

    const repo = await loadAndCheckRepo(owner, repoName, user);
    const repoDir = getRepoPath(owner, repoName);

    // Raw diff
    const diffProc = Bun.spawn(["git", "diff", `${base}...${head}`], {
      cwd: repoDir,
      stdout: "pipe",
      stderr: "pipe",
    });
    const rawDiffFull = await new Response(diffProc.stdout).text();
    await diffProc.exited;

    // Numstat for per-file summary
    const statProc = Bun.spawn(
      ["git", "diff", "--numstat", `${base}...${head}`],
      { cwd: repoDir, stdout: "pipe", stderr: "pipe" }
    );
    const numstatRaw = await new Response(statProc.stdout).text();
    await statProc.exited;

    const files = numstatRaw
      .trim()
      .split("\n")
      .filter(Boolean)
      .map((line) => {
        const [add, del, filePath] = line.split("\t");
        return {
          path: filePath ?? "",
          additions: add === "-" ? 0 : parseInt(add, 10) || 0,
          deletions: del === "-" ? 0 : parseInt(del, 10) || 0,
        };
      });

    const MAX_DIFF_BYTES = 50 * 1024; // 50 KB
    const rawDiff =
      rawDiffFull.length > MAX_DIFF_BYTES
        ? rawDiffFull.slice(0, MAX_DIFF_BYTES) + "\n... (diff truncated at 50 KB)"
        : rawDiffFull;

    return { files, rawDiff };
  },
};

// --------------------------------------------------------------------------
// Tool: trigger_ai_review
// --------------------------------------------------------------------------

const triggerAiReviewTool: McpTool = {
  name: "trigger_ai_review",
  description:
    "Trigger an AI code review for a pull request. The caller must be authenticated and must be the repository owner or the PR author.",
  inputSchema: {
    type: "object",
    properties: {
      owner: { type: "string", description: "Repository owner username" },
      repo: { type: "string", description: "Repository name" },
      prNumber: { type: "number", description: "Pull request number" },
    },
    required: ["owner", "repo", "prNumber"],
  },
  async handler(args, user) {
    if (!user) unauthorized("Triggering AI review requires authentication");

    const owner = args.owner as string;
    const repoName = args.repo as string;
    const prNumber = args.prNumber as number;
    if (!owner || !repoName || !prNumber) {
      invalidParams("owner, repo, and prNumber are required");
    }

    const repo = await loadAndCheckRepo(owner, repoName, user);

    // Load PR
    const [pr] = await db
      .select()
      .from(pullRequests)
      .where(
        and(
          eq(pullRequests.repositoryId, repo.id),
          eq(pullRequests.number, prNumber)
        )
      )
      .limit(1);

    if (!pr) notFound(`Pull request #${prNumber} not found`);

    // Authorization: repo owner or PR author
    if (user.id !== repo.ownerId && user.id !== pr.authorId) {
      unauthorized("Only the repository owner or PR author can trigger an AI review");
    }

    // Fire-and-forget
    triggerAiReview(
      owner,
      repoName,
      pr.id,
      pr.title,
      pr.body ?? "",
      pr.baseBranch,
      pr.headBranch
    ).catch((err) => {
      console.error("[mcp] triggerAiReview error:", err);
    });

    return { queued: true, prId: pr.id };
  },
};

// --------------------------------------------------------------------------
// Tool registry
// --------------------------------------------------------------------------

export const MCP_TOOLS: McpTool[] = [
  listRepositoriesTool,
  getRepositoryTool,
  getFileContentsTool,
  listFilesTool,
  searchCodeTool,
  listCommitsTool,
  getGateStatusTool,
  listPullRequestsTool,
  listIssuesTool,
  createIssueTool,
  explainCodebaseTool,
  getBranchDiffTool,
  triggerAiReviewTool,
];

export const MCP_TOOL_MAP = new Map<string, McpTool>(
  MCP_TOOLS.map((t) => [t.name, t])
);

/**
 * Serialise a tool for the tools/list response.
 */
export function serializeTool(tool: McpTool) {
  return {
    name: tool.name,
    description: tool.description,
    inputSchema: tool.inputSchema,
  };
}