@@ -41,7 +41,8 @@ import {
41
41
CreateIssueSchema ,
42
42
CreatePullRequestSchema ,
43
43
ForkRepositorySchema ,
44
- CreateBranchSchema
44
+ CreateBranchSchema ,
45
+ ListCommitsSchema
45
46
} from './schemas.js' ;
46
47
import { z } from 'zod' ;
47
48
import { zodToJsonSchema } from 'zod-to-json-schema' ;
@@ -467,6 +468,40 @@ async function createRepository(
467
468
return GitHubRepositorySchema . parse ( await response . json ( ) ) ;
468
469
}
469
470
471
+ async function listCommits (
472
+ owner : string ,
473
+ repo : string ,
474
+ page : number = 1 ,
475
+ perPage : number = 30 ,
476
+ sha ? : string ,
477
+ ) : Promise < GitHubCommit [ ] > {
478
+ const url = new URL ( `https://api.github.com/repos/${ owner } /${ repo } /commits` ) ;
479
+ url . searchParams . append ( "page" , page . toString ( ) ) ;
480
+ url . searchParams . append ( "per_page" , perPage . toString ( ) ) ;
481
+ if ( sha ) {
482
+ url . searchParams . append ( "sha" , sha ) ;
483
+ }
484
+
485
+ const response = await fetch (
486
+ url . toString ( ) ,
487
+ {
488
+ method : "GET" ,
489
+ headers : {
490
+ "Authorization" : `token ${ GITHUB_PERSONAL_ACCESS_TOKEN } ` ,
491
+ "Accept" : "application/vnd.github.v3+json" ,
492
+ "User-Agent" : "github-mcp-server" ,
493
+ "Content-Type" : "application/json"
494
+ } ,
495
+ }
496
+ ) ;
497
+
498
+ if ( ! response . ok ) {
499
+ throw new Error ( `GitHub API error: ${ response . statusText } ` ) ;
500
+ }
501
+
502
+ return GitHubCommitSchema . array ( ) . parse ( await response . json ( ) ) ;
503
+ }
504
+
470
505
server . setRequestHandler ( ListToolsRequestSchema , async ( ) => {
471
506
return {
472
507
tools : [
@@ -514,6 +549,11 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
514
549
name : "create_branch" ,
515
550
description : "Create a new branch in a GitHub repository" ,
516
551
inputSchema : zodToJsonSchema ( CreateBranchSchema )
552
+ } ,
553
+ {
554
+ name : "list_commits" ,
555
+ description : "Get list of commits of a branch in a GitHub repository" ,
556
+ inputSchema : zodToJsonSchema ( ListCommitsSchema )
517
557
}
518
558
]
519
559
} ;
@@ -623,6 +663,12 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
623
663
return { content : [ { type : "text" , text : JSON . stringify ( pullRequest , null , 2 ) } ] } ;
624
664
}
625
665
666
+ case "list_commits" : {
667
+ const args = ListCommitsSchema . parse ( request . params . arguments ) ;
668
+ const results = await listCommits ( args . owner , args . repo , args . page , args . perPage , args . sha ) ;
669
+ return { content : [ { type : "text" , text : JSON . stringify ( results , null , 2 ) } ] } ;
670
+ }
671
+
626
672
default :
627
673
throw new Error ( `Unknown tool: ${ request . params . name } ` ) ;
628
674
}
0 commit comments