Skip to content

Commit e9accbd

Browse files
committed
phase 3
1 parent 8b4775d commit e9accbd

File tree

8 files changed

+267
-254
lines changed

8 files changed

+267
-254
lines changed

src/mcp/http-server.ts

Lines changed: 207 additions & 217 deletions
Large diffs are not rendered by default.

src/mcp/server.ts

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ import {
1111
McpError,
1212
} from '@modelcontextprotocol/sdk/types.js';
1313
import { getConfig, Config } from '../config.js';
14-
import { GitHubClient } from '../github/client.js';
1514
import { leaveGeneralComment } from './tools/leave_comment.js';
1615
import { leaveInlineComment } from './tools/leave_inline_comment.js';
1716
import { createCheckRun } from './tools/create_check_run.js';
1817
import { getPRInfo } from './tools/get_pr_info.js';
1918
import { triggerReview } from './tools/trigger_review.js';
2019
import { getPRComments } from './tools/get_pr_comments.js';
21-
import {
20+
import {
2221
validateLeaveGeneralCommentArgs,
2322
validateLeaveInlineCommentArgs,
2423
validateCreateCheckRunArgs,
@@ -30,7 +29,6 @@ import {
3029
class GitHubMCPServer {
3130
private server: Server;
3231
private config: Config;
33-
private githubClient: GitHubClient;
3432

3533
constructor() {
3634
console.log('🚀 Initializing GitHub MCP Server...');
@@ -47,16 +45,9 @@ class GitHubMCPServer {
4745
);
4846

4947
this.config = getConfig();
50-
// Initialize with a default client, will be updated in run()
51-
this.githubClient = new GitHubClient(this.config);
5248
this.setupToolHandlers();
5349
}
5450

55-
private async setupGitHubClient() {
56-
console.log('🔑 Initializing GitHub client with static token');
57-
this.githubClient = new GitHubClient(this.config);
58-
}
59-
6051
private setupToolHandlers(): void {
6152
console.log('🔨 Setting up tool handlers...');
6253
this.server.setRequestHandler(ListToolsRequestSchema, async () => {
@@ -268,8 +259,7 @@ class GitHubMCPServer {
268259
const validatedArgs = validateLeaveGeneralCommentArgs(args);
269260
const result = await leaveGeneralComment(
270261
validatedArgs,
271-
this.config,
272-
this.githubClient
262+
this.config
273263
);
274264
console.log(`✅ leave_general_comment completed in ${Date.now() - startTime}ms`);
275265
return {
@@ -287,8 +277,7 @@ class GitHubMCPServer {
287277
const validatedArgs = validateLeaveInlineCommentArgs(args);
288278
const result = await leaveInlineComment(
289279
validatedArgs,
290-
this.config,
291-
this.githubClient
280+
this.config
292281
);
293282
console.log(`✅ leave_inline_comment completed in ${Date.now() - startTime}ms`);
294283
return {
@@ -306,8 +295,7 @@ class GitHubMCPServer {
306295
const validatedArgs = validateCreateCheckRunArgs(args);
307296
const result = await createCheckRun(
308297
validatedArgs,
309-
this.config,
310-
this.githubClient
298+
this.config
311299
);
312300
console.log(`✅ create_check_run completed in ${Date.now() - startTime}ms`);
313301
return {
@@ -325,8 +313,7 @@ class GitHubMCPServer {
325313
const validatedArgs = validateGetPRInfoArgs(args);
326314
const result = await getPRInfo(
327315
validatedArgs,
328-
this.config,
329-
this.githubClient
316+
this.config
330317
);
331318
console.log(`✅ get_pr_info completed in ${Date.now() - startTime}ms`);
332319
return {
@@ -344,8 +331,7 @@ class GitHubMCPServer {
344331
const validatedArgs = validateTriggerReviewArgs(args);
345332
const result = await triggerReview(
346333
validatedArgs,
347-
this.config,
348-
this.githubClient
334+
this.config
349335
);
350336
console.log(`✅ trigger_review completed in ${Date.now() - startTime}ms`);
351337
return {
@@ -363,8 +349,7 @@ class GitHubMCPServer {
363349
const validatedArgs = validateGetPRCommentsArgs(args);
364350
const result = await getPRComments(
365351
validatedArgs,
366-
this.config,
367-
this.githubClient
352+
this.config
368353
);
369354
console.log(`✅ get_pr_comments completed in ${Date.now() - startTime}ms`);
370355
return {
@@ -397,9 +382,6 @@ class GitHubMCPServer {
397382

398383
async run(): Promise<void> {
399384
console.log('🔌 Starting MCP server connection...');
400-
// Setup the proper GitHub client before starting
401-
await this.setupGitHubClient();
402-
403385
console.log('📡 Creating stdio transport...');
404386
const transport = new StdioServerTransport();
405387
console.log('🔗 Connecting to transport...');

src/mcp/tools/create_check_run.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ export interface CreateCheckRunArgs {
1414

1515
export async function createCheckRun(
1616
args: CreateCheckRunArgs,
17-
config: Config,
18-
githubClient: GitHubClient
17+
config: Config
1918
): Promise<{ success: boolean; check_run_id?: number; error?: string }> {
2019
try {
2120
const { owner, repo, commit_sha, status, conclusion, title, summary, details_url } = args;
2221

22+
// Get installation ID from environment
23+
const installationId = parseInt(process.env.GITHUB_INSTALLATION_ID || '0');
24+
if (!installationId) {
25+
throw new Error('GITHUB_INSTALLATION_ID environment variable is required');
26+
}
27+
28+
const githubClient = GitHubClient.forInstallation(config, installationId);
29+
2330
const checkOptions: {
2431
name: string;
2532
status: 'queued' | 'in_progress' | 'completed';

src/mcp/tools/get_pr_comments.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ export interface GetPRCommentsArgs {
99

1010
export async function getPRComments(
1111
args: GetPRCommentsArgs,
12-
config: Config,
13-
githubClient: GitHubClient
12+
config: Config
1413
): Promise<{
1514
success: boolean;
1615
comments?: unknown[];
@@ -20,6 +19,14 @@ export async function getPRComments(
2019
try {
2120
const { owner, repo, pr_number } = args;
2221

22+
// Get installation ID from environment
23+
const installationId = parseInt(process.env.GITHUB_INSTALLATION_ID || '0');
24+
if (!installationId) {
25+
throw new Error('GITHUB_INSTALLATION_ID environment variable is required');
26+
}
27+
28+
const githubClient = GitHubClient.forInstallation(config, installationId);
29+
2330
const comments = await githubClient.getPRComments(owner, repo, pr_number);
2431

2532
return {

src/mcp/tools/get_pr_info.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ export interface GetPRInfoArgs {
1010

1111
export async function getPRInfo(
1212
args: GetPRInfoArgs,
13-
config: Config,
14-
githubClient: GitHubClient
13+
config: Config
1514
): Promise<{
1615
success: boolean;
1716
pr_info?: unknown;
@@ -22,6 +21,14 @@ export async function getPRInfo(
2221
try {
2322
const { owner, repo, pr_number, include_diff = false } = args;
2423

24+
// Get installation ID from environment
25+
const installationId = parseInt(process.env.GITHUB_INSTALLATION_ID || '0');
26+
if (!installationId) {
27+
throw new Error('GITHUB_INSTALLATION_ID environment variable is required');
28+
}
29+
30+
const githubClient = GitHubClient.forInstallation(config, installationId);
31+
2532
// Get PR info
2633
const prInfo = await githubClient.getPRInfo(owner, repo, pr_number);
2734

src/mcp/tools/leave_comment.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ export interface LeaveGeneralCommentArgs {
1010

1111
export async function leaveGeneralComment(
1212
args: LeaveGeneralCommentArgs,
13-
config: Config,
14-
githubClient: GitHubClient
13+
config: Config
1514
): Promise<{ success: boolean; comment_id?: number; error?: string }> {
1615
try {
1716
const { message, owner, repo, pr_number } = args;
1817

18+
// Get installation ID from environment
19+
const installationId = parseInt(process.env.GITHUB_INSTALLATION_ID || '0');
20+
if (!installationId) {
21+
throw new Error('GITHUB_INSTALLATION_ID environment variable is required');
22+
}
23+
24+
const githubClient = GitHubClient.forInstallation(config, installationId);
1925
const response = await githubClient.createPRComment(owner, repo, pr_number, message);
2026

2127
return {

src/mcp/tools/leave_inline_comment.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,19 @@ export interface LeaveInlineCommentArgs {
1313

1414
export async function leaveInlineComment(
1515
args: LeaveInlineCommentArgs,
16-
config: Config,
17-
githubClient: GitHubClient
16+
config: Config
1817
): Promise<{ success: boolean; review_id?: number; error?: string }> {
1918
try {
2019
const { message, owner, repo, pr_number, path, line, commit_sha } = args;
2120

21+
// Get installation ID from environment
22+
const installationId = parseInt(process.env.GITHUB_INSTALLATION_ID || '0');
23+
if (!installationId) {
24+
throw new Error('GITHUB_INSTALLATION_ID environment variable is required');
25+
}
26+
27+
const githubClient = GitHubClient.forInstallation(config, installationId);
28+
2229
console.log('🎯 Creating inline comment via PR review:', { path, line });
2330

2431
// Get commit SHA if not provided

src/mcp/tools/trigger_review.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@ export interface TriggerReviewArgs {
1111

1212
export async function triggerReview(
1313
args: TriggerReviewArgs,
14-
config: Config,
15-
githubClient: GitHubClient
14+
config: Config
1615
): Promise<{ success: boolean; review_id?: string; check_run_id?: number; error?: string }> {
1716
try {
1817
const { owner, repo, pr_number, commit_sha } = args;
1918

19+
// Get installation ID from environment
20+
const installationId = parseInt(process.env.GITHUB_INSTALLATION_ID || '0');
21+
if (!installationId) {
22+
throw new Error('GITHUB_INSTALLATION_ID environment variable is required');
23+
}
24+
25+
const githubClient = GitHubClient.forInstallation(config, installationId);
26+
2027
// Get PR info to get the latest commit if not provided
2128
const prInfo = await githubClient.getPRInfo(owner, repo, pr_number);
2229
const targetCommitSha = commit_sha || prInfo.head.sha;

0 commit comments

Comments
 (0)