Skip to content

Commit 52bed28

Browse files
committed
create a config struct for running the server
1 parent 6716423 commit 52bed28

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

cmd/github-mcp-server/main.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ var (
4444
stdlog.Fatal("Failed to initialize logger:", err)
4545
}
4646
logCommands := viper.GetBool("enable-command-logging")
47-
if err := runStdioServer(readOnly, logger, logCommands, exportTranslations); err != nil {
47+
cfg := runConfig{
48+
readOnly: readOnly,
49+
logger: logger,
50+
logCommands: logCommands,
51+
exportTranslations: exportTranslations,
52+
}
53+
if err := runStdioServer(cfg); err != nil {
4854
stdlog.Fatal("failed to run stdio server:", err)
4955
}
5056
},
@@ -95,15 +101,22 @@ func initLogger(outPath string) (*log.Logger, error) {
95101
return logger, nil
96102
}
97103

98-
func runStdioServer(readOnly bool, logger *log.Logger, logCommands bool, exportTranslations bool) error {
104+
type runConfig struct {
105+
readOnly bool
106+
logger *log.Logger
107+
logCommands bool
108+
exportTranslations bool
109+
}
110+
111+
func runStdioServer(cfg runConfig) error {
99112
// Create app context
100113
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
101114
defer stop()
102115

103116
// Create GH client
104117
token := os.Getenv("GITHUB_PERSONAL_ACCESS_TOKEN")
105118
if token == "" {
106-
logger.Fatal("GITHUB_PERSONAL_ACCESS_TOKEN not set")
119+
cfg.logger.Fatal("GITHUB_PERSONAL_ACCESS_TOKEN not set")
107120
}
108121
ghClient := gogithub.NewClient(nil).WithAuthToken(token)
109122
ghClient.UserAgent = fmt.Sprintf("github-mcp-server/%s", version)
@@ -125,13 +138,13 @@ func runStdioServer(readOnly bool, logger *log.Logger, logCommands bool, exportT
125138
t, dumpTranslations := translations.TranslationHelper()
126139

127140
// Create
128-
ghServer := github.NewServer(ghClient, readOnly, t)
141+
ghServer := github.NewServer(ghClient, cfg.readOnly, t)
129142
stdioServer := server.NewStdioServer(ghServer)
130143

131-
stdLogger := stdlog.New(logger.Writer(), "stdioserver", 0)
144+
stdLogger := stdlog.New(cfg.logger.Writer(), "stdioserver", 0)
132145
stdioServer.SetErrorLogger(stdLogger)
133146

134-
if exportTranslations {
147+
if cfg.exportTranslations {
135148
// Once server is initialized, all translations are loaded
136149
dumpTranslations()
137150
}
@@ -141,8 +154,8 @@ func runStdioServer(readOnly bool, logger *log.Logger, logCommands bool, exportT
141154
go func() {
142155
in, out := io.Reader(os.Stdin), io.Writer(os.Stdout)
143156

144-
if logCommands {
145-
loggedIO := iolog.NewIOLogger(in, out, logger)
157+
if cfg.logCommands {
158+
loggedIO := iolog.NewIOLogger(in, out, cfg.logger)
146159
in, out = loggedIO, loggedIO
147160
}
148161

@@ -155,7 +168,7 @@ func runStdioServer(readOnly bool, logger *log.Logger, logCommands bool, exportT
155168
// Wait for shutdown signal
156169
select {
157170
case <-ctx.Done():
158-
logger.Infof("shutting down server...")
171+
cfg.logger.Infof("shutting down server...")
159172
case err := <-errC:
160173
if err != nil {
161174
return fmt.Errorf("error running server: %w", err)

0 commit comments

Comments
 (0)