Skip to content

pretty print json #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 45 additions & 9 deletions cmd/github-mcp-server/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
stdlog "log"
Expand Down Expand Up @@ -39,12 +41,20 @@ var (
logFile := viper.GetString("log-file")
readOnly := viper.GetBool("read-only")
exportTranslations := viper.GetBool("export-translations")
prettyPrintJSON := viper.GetBool("pretty-print-json")
logger, err := initLogger(logFile)
if err != nil {
stdlog.Fatal("Failed to initialize logger:", err)
}
logCommands := viper.GetBool("enable-command-logging")
if err := runStdioServer(readOnly, logger, logCommands, exportTranslations); err != nil {
cfg := runConfig{
readOnly: readOnly,
logger: logger,
logCommands: logCommands,
exportTranslations: exportTranslations,
prettyPrintJSON: prettyPrintJSON,
}
if err := runStdioServer(cfg); err != nil {
stdlog.Fatal("failed to run stdio server:", err)
}
},
Expand All @@ -60,13 +70,15 @@ func init() {
rootCmd.PersistentFlags().Bool("enable-command-logging", false, "When enabled, the server will log all command requests and responses to the log file")
rootCmd.PersistentFlags().Bool("export-translations", false, "Save translations to a JSON file")
rootCmd.PersistentFlags().String("gh-host", "", "Specify the GitHub hostname (for GitHub Enterprise etc.)")
rootCmd.PersistentFlags().Bool("pretty-print-json", false, "Pretty print JSON output")

// Bind flag to viper
_ = viper.BindPFlag("read-only", rootCmd.PersistentFlags().Lookup("read-only"))
_ = viper.BindPFlag("log-file", rootCmd.PersistentFlags().Lookup("log-file"))
_ = viper.BindPFlag("enable-command-logging", rootCmd.PersistentFlags().Lookup("enable-command-logging"))
_ = viper.BindPFlag("export-translations", rootCmd.PersistentFlags().Lookup("export-translations"))
_ = viper.BindPFlag("gh-host", rootCmd.PersistentFlags().Lookup("gh-host"))
_ = viper.BindPFlag("pretty-print-json", rootCmd.PersistentFlags().Lookup("pretty-print-json"))

// Add subcommands
rootCmd.AddCommand(stdioCmd)
Expand Down Expand Up @@ -95,15 +107,36 @@ func initLogger(outPath string) (*log.Logger, error) {
return logger, nil
}

func runStdioServer(readOnly bool, logger *log.Logger, logCommands bool, exportTranslations bool) error {
type runConfig struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably just call this config but other than that lgtm!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

going to leave it so you don't have to do another review

readOnly bool
logger *log.Logger
logCommands bool
exportTranslations bool
prettyPrintJSON bool
}

// JSONPrettyPrintWriter is a Writer that pretty prints input to indented JSON
type JSONPrettyPrintWriter struct {
writer io.Writer
}

func (j JSONPrettyPrintWriter) Write(p []byte) (n int, err error) {
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, p, "", "\t"); err != nil {
return 0, err
}
return j.writer.Write(prettyJSON.Bytes())
}

func runStdioServer(cfg runConfig) error {
// Create app context
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()

// Create GH client
token := os.Getenv("GITHUB_PERSONAL_ACCESS_TOKEN")
if token == "" {
logger.Fatal("GITHUB_PERSONAL_ACCESS_TOKEN not set")
cfg.logger.Fatal("GITHUB_PERSONAL_ACCESS_TOKEN not set")
}
ghClient := gogithub.NewClient(nil).WithAuthToken(token)
ghClient.UserAgent = fmt.Sprintf("github-mcp-server/%s", version)
Expand All @@ -125,13 +158,13 @@ func runStdioServer(readOnly bool, logger *log.Logger, logCommands bool, exportT
t, dumpTranslations := translations.TranslationHelper()

// Create
ghServer := github.NewServer(ghClient, readOnly, t)
ghServer := github.NewServer(ghClient, cfg.readOnly, t)
stdioServer := server.NewStdioServer(ghServer)

stdLogger := stdlog.New(logger.Writer(), "stdioserver", 0)
stdLogger := stdlog.New(cfg.logger.Writer(), "stdioserver", 0)
stdioServer.SetErrorLogger(stdLogger)

if exportTranslations {
if cfg.exportTranslations {
// Once server is initialized, all translations are loaded
dumpTranslations()
}
Expand All @@ -141,11 +174,14 @@ func runStdioServer(readOnly bool, logger *log.Logger, logCommands bool, exportT
go func() {
in, out := io.Reader(os.Stdin), io.Writer(os.Stdout)

if logCommands {
loggedIO := iolog.NewIOLogger(in, out, logger)
if cfg.logCommands {
loggedIO := iolog.NewIOLogger(in, out, cfg.logger)
in, out = loggedIO, loggedIO
}

if cfg.prettyPrintJSON {
out = JSONPrettyPrintWriter{writer: out}
}
errC <- stdioServer.Listen(ctx, in, out)
}()

Expand All @@ -155,7 +191,7 @@ func runStdioServer(readOnly bool, logger *log.Logger, logCommands bool, exportT
// Wait for shutdown signal
select {
case <-ctx.Done():
logger.Infof("shutting down server...")
cfg.logger.Infof("shutting down server...")
case err := <-errC:
if err != nil {
return fmt.Errorf("error running server: %w", err)
Expand Down