Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mntlty merged 3 commits intomainfrompretty_print_json
Apr 5, 2025
Merged
Changes fromall 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 deletionscmd/github-mcp-server/main.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
stdlog "log"
Expand DownExpand 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 DownExpand 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 thisconfig but other than that lgtm!

Copy link
CollaboratorAuthor

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 {
ifcfg.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)
ifcfg.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

[8]ページ先頭

©2009-2025 Movatter.jp