9
9
"os/signal"
10
10
"syscall"
11
11
12
+ "github.com/cli/go-gh/pkg/auth"
12
13
"github.com/github/github-mcp-server/pkg/github"
13
14
iolog "github.com/github/github-mcp-server/pkg/log"
14
15
"github.com/github/github-mcp-server/pkg/translations"
@@ -24,9 +25,12 @@ var version = "version"
24
25
var commit = "commit"
25
26
var date = "date"
26
27
28
+ var root_command_name = "github-mcp-server"
29
+ var default_token_source = "env"
30
+
27
31
var (
28
32
rootCmd = & cobra.Command {
29
- Use : "server" ,
33
+ Use : root_command_name ,
30
34
Short : "GitHub MCP Server" ,
31
35
Long : `A GitHub MCP server that handles various tools and resources.` ,
32
36
Version : fmt .Sprintf ("Version: %s\n Commit: %s\n Build Date: %s" , version , commit , date ),
@@ -75,6 +79,7 @@ func init() {
75
79
rootCmd .PersistentFlags ().Bool ("enable-command-logging" , false , "When enabled, the server will log all command requests and responses to the log file" )
76
80
rootCmd .PersistentFlags ().Bool ("export-translations" , false , "Save translations to a JSON file" )
77
81
rootCmd .PersistentFlags ().String ("gh-host" , "" , "Specify the GitHub hostname (for GitHub Enterprise etc.)" )
82
+ rootCmd .PersistentFlags ().String ("token-source" , default_token_source , "Authentication token source (e.g. env, gh)" )
78
83
79
84
// Bind flag to viper
80
85
_ = viper .BindPFlag ("toolsets" , rootCmd .PersistentFlags ().Lookup ("toolsets" ))
@@ -84,6 +89,7 @@ func init() {
84
89
_ = viper .BindPFlag ("enable-command-logging" , rootCmd .PersistentFlags ().Lookup ("enable-command-logging" ))
85
90
_ = viper .BindPFlag ("export-translations" , rootCmd .PersistentFlags ().Lookup ("export-translations" ))
86
91
_ = viper .BindPFlag ("host" , rootCmd .PersistentFlags ().Lookup ("gh-host" ))
92
+ _ = viper .BindPFlag ("token-source" , rootCmd .PersistentFlags ().Lookup ("token-source" ))
87
93
88
94
// Add subcommands
89
95
rootCmd .AddCommand (stdioCmd )
@@ -126,21 +132,9 @@ func runStdioServer(cfg runConfig) error {
126
132
defer stop ()
127
133
128
134
// Create GH client
129
- token := viper .GetString ("personal_access_token" )
130
- if token == "" {
131
- cfg .logger .Fatal ("GITHUB_PERSONAL_ACCESS_TOKEN not set" )
132
- }
133
- ghClient := gogithub .NewClient (nil ).WithAuthToken (token )
134
- ghClient .UserAgent = fmt .Sprintf ("github-mcp-server/%s" , version )
135
-
136
- host := viper .GetString ("host" )
137
-
138
- if host != "" {
139
- var err error
140
- ghClient , err = ghClient .WithEnterpriseURLs (host , host )
141
- if err != nil {
142
- return fmt .Errorf ("failed to create GitHub client with host: %w" , err )
143
- }
135
+ ghClient , err := newGitHubClient ()
136
+ if err != nil {
137
+ cfg .logger .Fatalf ("failed to create GitHub client: %v" , err )
144
138
}
145
139
146
140
t , dumpTranslations := translations .TranslationHelper ()
@@ -229,6 +223,50 @@ func runStdioServer(cfg runConfig) error {
229
223
return nil
230
224
}
231
225
226
+ func getToken (host string ) (string , error ) {
227
+ token_source := viper .GetString ("token-source" )
228
+ switch token_source {
229
+ case "env" :
230
+ token := os .Getenv ("GITHUB_PERSONAL_ACCESS_TOKEN" )
231
+ if token == "" {
232
+ return "" , fmt .Errorf ("GITHUB_PERSONAL_ACCESS_TOKEN not set" )
233
+ }
234
+ return token , nil
235
+ case "gh" :
236
+ token , err := auth .TokenForHost (host )
237
+ if err == "default" {
238
+ return "" , fmt .Errorf ("no token found for host: %s" , host )
239
+ }
240
+ return token , nil
241
+ }
242
+ return "" , fmt .Errorf ("unknown token source: %s" , token_source )
243
+ }
244
+
245
+ func getHost () string {
246
+ host := os .Getenv ("GH_HOST" )
247
+ if host == "" {
248
+ host = viper .GetString ("gh-host" )
249
+ }
250
+ return host
251
+ }
252
+
253
+ func newGitHubClient () (* gogithub.Client , error ) {
254
+ host := getHost ()
255
+ token , err := getToken (host )
256
+ if err != nil {
257
+ return nil , fmt .Errorf ("failed to get token: %w" , err )
258
+ }
259
+ ghClient := gogithub .NewClient (nil ).WithAuthToken (token )
260
+ if host != "" {
261
+ ghClient , err = ghClient .WithEnterpriseURLs (host , host )
262
+ if err != nil {
263
+ return nil , fmt .Errorf ("failed to create GitHub client with host: %w" , err )
264
+ }
265
+ }
266
+ ghClient .UserAgent = fmt .Sprintf ("github-mcp-server/%s" , version )
267
+ return ghClient , nil
268
+ }
269
+
232
270
func main () {
233
271
if err := rootCmd .Execute (); err != nil {
234
272
fmt .Println (err )
0 commit comments