Skip to content

Commit 0c378c8

Browse files
feat: read-only flag
1 parent fca7cd7 commit 0c378c8

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

cmd/github-mcp-server/main.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ var (
3131
Long: `Start a server that communicates via standard input/output streams using JSON-RPC messages.`,
3232
Run: func(cmd *cobra.Command, args []string) {
3333
logFile := viper.GetString("log-file")
34+
readOnly := viper.GetBool("read-only")
3435
logger, err := initLogger(logFile)
3536
if err != nil {
3637
stdlog.Fatal("Failed to initialize logger:", err)
3738
}
3839
logCommands := viper.GetBool("enable-command-logging")
39-
if err := runStdioServer(logger, logCommands); err != nil {
40+
if err := runStdioServer(readOnly, logger, logCommands); err != nil {
4041
stdlog.Fatal("failed to run stdio server:", err)
4142
}
4243
},
@@ -47,10 +48,12 @@ func init() {
4748
cobra.OnInitialize(initConfig)
4849

4950
// Add global flags that will be shared by all commands
51+
rootCmd.PersistentFlags().Bool("read-only", false, "Restrict the server to read-only operations")
5052
rootCmd.PersistentFlags().String("log-file", "", "Path to log file")
5153
rootCmd.PersistentFlags().Bool("enable-command-logging", false, "When enabled, the server will log all command requests and responses to the log file")
5254

5355
// Bind flag to viper
56+
viper.BindPFlag("read-only", rootCmd.PersistentFlags().Lookup("read-only"))
5457
viper.BindPFlag("log-file", rootCmd.PersistentFlags().Lookup("log-file"))
5558
viper.BindPFlag("enable-command-logging", rootCmd.PersistentFlags().Lookup("enable-command-logging"))
5659

@@ -81,7 +84,7 @@ func initLogger(outPath string) (*log.Logger, error) {
8184
return logger, nil
8285
}
8386

84-
func runStdioServer(logger *log.Logger, logCommands bool) error {
87+
func runStdioServer(readOnly bool, logger *log.Logger, logCommands bool) error {
8588
// Create app context
8689
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
8790
defer stop()
@@ -93,8 +96,8 @@ func runStdioServer(logger *log.Logger, logCommands bool) error {
9396
}
9497
ghClient := gogithub.NewClient(nil).WithAuthToken(token)
9598

96-
// Create server
97-
ghServer := github.NewServer(ghClient)
99+
// Create
100+
ghServer := github.NewServer(ghClient, readOnly)
98101
stdioServer := server.NewStdioServer(ghServer)
99102

100103
stdLogger := stdlog.New(logger.Writer(), "stdioserver", 0)

pkg/github/server.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
// NewServer creates a new GitHub MCP server with the specified GH client and logger.
17-
func NewServer(client *github.Client) *server.MCPServer {
17+
func NewServer(client *github.Client, readOnly bool) *server.MCPServer {
1818
// Create a new MCP server
1919
s := server.NewMCPServer(
2020
"github-mcp-server",
@@ -33,27 +33,33 @@ func NewServer(client *github.Client) *server.MCPServer {
3333

3434
// Add GitHub tools - Issues
3535
s.AddTool(getIssue(client))
36-
s.AddTool(addIssueComment(client))
3736
s.AddTool(searchIssues(client))
37+
if !readOnly {
38+
s.AddTool(addIssueComment(client))
39+
}
3840

3941
// Add GitHub tools - Pull Requests
4042
s.AddTool(getPullRequest(client))
4143
s.AddTool(listPullRequests(client))
42-
s.AddTool(mergePullRequest(client))
4344
s.AddTool(getPullRequestFiles(client))
4445
s.AddTool(getPullRequestStatus(client))
45-
s.AddTool(updatePullRequestBranch(client))
4646
s.AddTool(getPullRequestComments(client))
4747
s.AddTool(getPullRequestReviews(client))
48+
if !readOnly {
49+
s.AddTool(mergePullRequest(client))
50+
s.AddTool(updatePullRequestBranch(client))
51+
}
4852

4953
// Add GitHub tools - Repositories
50-
s.AddTool(createOrUpdateFile(client))
5154
s.AddTool(searchRepositories(client))
52-
s.AddTool(createRepository(client))
5355
s.AddTool(getFileContents(client))
54-
s.AddTool(forkRepository(client))
55-
s.AddTool(createBranch(client))
5656
s.AddTool(listCommits(client))
57+
if !readOnly {
58+
s.AddTool(createOrUpdateFile(client))
59+
s.AddTool(createRepository(client))
60+
s.AddTool(forkRepository(client))
61+
s.AddTool(createBranch(client))
62+
}
5763

5864
// Add GitHub tools - Search
5965
s.AddTool(searchCode(client))

0 commit comments

Comments
 (0)