126 lines
2.9 KiB
Go
126 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
stdlog "log"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
|
|
"github.com/github/github-mcp-server/pkg/github"
|
|
gogithub "github.com/google/go-github/v69/github"
|
|
"github.com/mark3labs/mcp-go/server"
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var (
|
|
rootCmd = &cobra.Command{
|
|
Use: "server",
|
|
Short: "GitHub MCP Server",
|
|
Long: `A GitHub MCP server that handles various tools and resources.`,
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
|
// Bind flag to viper
|
|
viper.BindPFlag("log-file", cmd.PersistentFlags().Lookup("log-file"))
|
|
},
|
|
}
|
|
|
|
stdioCmd = &cobra.Command{
|
|
Use: "stdio",
|
|
Short: "Start stdio server",
|
|
Long: `Start a server that communicates via standard input/output streams using JSON-RPC messages.`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
logFile := viper.GetString("log-file")
|
|
logger, err := initLogger(logFile)
|
|
if err != nil {
|
|
stdlog.Fatal("Failed to initialize logger:", err)
|
|
}
|
|
if err := runStdioServer(logger); err != nil {
|
|
stdlog.Fatal("failed to run stdio server:", err)
|
|
}
|
|
},
|
|
}
|
|
)
|
|
|
|
func init() {
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
// Add global flags that will be shared by all commands
|
|
rootCmd.PersistentFlags().String("log-file", "", "Path to log file")
|
|
|
|
// Add subcommands
|
|
rootCmd.AddCommand(stdioCmd)
|
|
}
|
|
|
|
func initConfig() {
|
|
// Initialize Viper configuration
|
|
viper.SetEnvPrefix("APP")
|
|
viper.AutomaticEnv()
|
|
}
|
|
|
|
func initLogger(outPath string) (*log.Logger, error) {
|
|
if outPath == "" {
|
|
return log.New(), nil
|
|
}
|
|
|
|
file, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to open log file: %w", err)
|
|
}
|
|
|
|
logger := log.New()
|
|
logger.SetOutput(file)
|
|
|
|
return logger, nil
|
|
}
|
|
|
|
func runStdioServer(logger *log.Logger) 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")
|
|
}
|
|
ghClient := gogithub.NewClient(nil).WithAuthToken(token)
|
|
|
|
// Create server
|
|
ghServer := github.NewServer(ghClient)
|
|
stdioServer := server.NewStdioServer(ghServer)
|
|
|
|
stdLogger := stdlog.New(logger.Writer(), "stdioserver", 0)
|
|
stdioServer.SetErrorLogger(stdLogger)
|
|
|
|
// Start listening for messages
|
|
errC := make(chan error, 1)
|
|
go func() {
|
|
errC <- stdioServer.Listen(ctx, os.Stdin, os.Stdout)
|
|
}()
|
|
|
|
// Output github-mcp-server string
|
|
_, _ = fmt.Fprintf(os.Stderr, "GitHub MCP Server running on stdio\n")
|
|
|
|
// Wait for shutdown signal
|
|
select {
|
|
case <-ctx.Done():
|
|
logger.Infof("shutting down server...")
|
|
case err := <-errC:
|
|
if err != nil {
|
|
return fmt.Errorf("error running server: %w", err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func main() {
|
|
if err := rootCmd.Execute(); err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
}
|