Skip to content

Commit f5f08bc

Browse files
authored
Implement --token and MCP_GITHUB_TOKEN login support (#705)
<!-- Provide a brief summary of your changes --> ## Motivation and Context <!-- Why is this change needed? What problem does it solve? --> The following PR implements the missing support for logging in via `--token` or `MCP_GITHUB_TOKEN` ## How Has This Been Tested? <!-- Have you tested this in a real application? Which scenarios were tested? --> ## Breaking Changes <!-- Will users need to update their code or configurations? --> ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [ ] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [ ] My code follows the repository's style guidelines - [ ] New and existing tests pass locally - [ ] I have added appropriate error handling - [ ] I have added or updated documentation as needed ## Additional context <!-- Add any other context, implementation notes, or design decisions --> Fixes: #698 --------- Signed-off-by: Radoslav Dimitrov <[email protected]>
1 parent b0a79c1 commit f5f08bc

File tree

4 files changed

+69
-19
lines changed

4 files changed

+69
-19
lines changed

cmd/publisher/auth/github-at.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ type StoredRegistryToken struct {
5050

5151
// GitHubATProvider implements the Provider interface using GitHub's device flow
5252
type GitHubATProvider struct {
53-
clientID string
54-
forceLogin bool
55-
registryURL string
53+
clientID string
54+
forceLogin bool
55+
registryURL string
56+
providedToken string // Token provided via --token flag or MCP_GITHUB_TOKEN env var
5657
}
5758

5859
// ServerHealthResponse represents the response from the health endpoint
@@ -62,10 +63,16 @@ type ServerHealthResponse struct {
6263
}
6364

6465
// NewGitHubATProvider creates a new GitHub OAuth provider
65-
func NewGitHubATProvider(forceLogin bool, registryURL string) Provider {
66+
func NewGitHubATProvider(forceLogin bool, registryURL, token string) Provider {
67+
// Check for token from flag or environment variable
68+
if token == "" {
69+
token = os.Getenv("MCP_GITHUB_TOKEN")
70+
}
71+
6672
return &GitHubATProvider{
67-
forceLogin: forceLogin,
68-
registryURL: registryURL,
73+
forceLogin: forceLogin,
74+
registryURL: registryURL,
75+
providedToken: token,
6976
}
7077
}
7178

@@ -100,6 +107,11 @@ func (g *GitHubATProvider) GetToken(ctx context.Context) (string, error) {
100107

101108
// NeedsLogin checks if a new login is required
102109
func (g *GitHubATProvider) NeedsLogin() bool {
110+
// If a token was provided via --token or MCP_GITHUB_TOKEN, no login needed
111+
if g.providedToken != "" {
112+
return false
113+
}
114+
103115
if g.forceLogin {
104116
return true
105117
}
@@ -123,6 +135,15 @@ func (g *GitHubATProvider) NeedsLogin() bool {
123135

124136
// Login performs the GitHub device flow authentication
125137
func (g *GitHubATProvider) Login(ctx context.Context) error {
138+
// If a token was provided via --token or MCP_GITHUB_TOKEN, save it and skip device flow
139+
if g.providedToken != "" {
140+
err := saveToken(g.providedToken)
141+
if err != nil {
142+
return fmt.Errorf("error saving provided token: %w", err)
143+
}
144+
return nil
145+
}
146+
126147
// If clientID is not set, try to retrieve it from the server's health endpoint
127148
if g.clientID == "" {
128149
clientID, err := getClientID(ctx, g.registryURL)

cmd/publisher/commands/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func InitCommand() error {
2929
description := detectDescription()
3030
version := "1.0.0"
3131
repoURL := detectRepoURL()
32-
repoSource := "github"
32+
repoSource := MethodGitHub
3333
if repoURL != "" && !strings.Contains(repoURL, "github.com") {
3434
if strings.Contains(repoURL, "gitlab.com") {
3535
repoSource = "gitlab"

cmd/publisher/commands/login.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ import (
1818
const (
1919
DefaultRegistryURL = "https://registry.modelcontextprotocol.io"
2020
TokenFileName = ".mcp_publisher_token" //nolint:gosec // Not a credential, just a filename
21+
MethodGitHub = "github"
22+
MethodGitHubOIDC = "github-oidc"
23+
MethodDNS = "dns"
24+
MethodHTTP = "http"
25+
MethodNone = "none"
2126
)
2227

2328
type CryptoAlgorithm auth.CryptoAlgorithm
@@ -31,6 +36,7 @@ type LoginFlags struct {
3136
KvVault string
3237
KvKeyName string
3338
KmsResource string
39+
Token Token
3440
CryptoAlgorithm CryptoAlgorithm
3541
SignerType SignerType
3642
ArgOffset int
@@ -56,6 +62,8 @@ func (c *CryptoAlgorithm) Set(v string) error {
5662
return fmt.Errorf("invalid algorithm: %q (allowed: ed25519, ecdsap384)", v)
5763
}
5864

65+
type Token string
66+
5967
func parseLoginFlags(method string, args []string) (LoginFlags, error) {
6068
var flags LoginFlags
6169
loginFlags := flag.NewFlagSet("login", flag.ExitOnError)
@@ -64,6 +72,12 @@ func parseLoginFlags(method string, args []string) (LoginFlags, error) {
6472
flags.ArgOffset = 1
6573
loginFlags.StringVar(&flags.RegistryURL, "registry", DefaultRegistryURL, "Registry URL")
6674

75+
// Add --token flag for GitHub authentication
76+
var token string
77+
if method == MethodGitHub {
78+
loginFlags.StringVar(&token, "token", "", "GitHub Personal Access Token")
79+
}
80+
6781
if method == "dns" || method == "http" {
6882
loginFlags.StringVar(&flags.Domain, "domain", "", "Domain name")
6983
if len(args) > 1 {
@@ -90,6 +104,11 @@ func parseLoginFlags(method string, args []string) (LoginFlags, error) {
90104
flags.RegistryURL = strings.TrimRight(flags.RegistryURL, "/")
91105
}
92106

107+
// Store the token in flags if it was provided
108+
if method == MethodGitHub {
109+
flags.Token = Token(token)
110+
}
111+
93112
return flags, err
94113
}
95114

@@ -108,23 +127,23 @@ func createSigner(flags LoginFlags) (auth.Signer, error) {
108127
}
109128
}
110129

111-
func createAuthProvider(method, registryURL, domain string, signer auth.Signer) (auth.Provider, error) {
130+
func createAuthProvider(method, registryURL, domain string, token Token, signer auth.Signer) (auth.Provider, error) {
112131
switch method {
113-
case "github":
114-
return auth.NewGitHubATProvider(true, registryURL), nil
115-
case "github-oidc":
132+
case MethodGitHub:
133+
return auth.NewGitHubATProvider(true, registryURL, string(token)), nil
134+
case MethodGitHubOIDC:
116135
return auth.NewGitHubOIDCProvider(registryURL), nil
117-
case "dns":
136+
case MethodDNS:
118137
if domain == "" {
119138
return nil, errors.New("dns authentication requires --domain")
120139
}
121140
return auth.NewDNSProvider(registryURL, domain, &signer), nil
122-
case "http":
141+
case MethodHTTP:
123142
if domain == "" {
124143
return nil, errors.New("http authentication requires --domain")
125144
}
126145
return auth.NewHTTPProvider(registryURL, domain, &signer), nil
127-
case "none":
146+
case MethodNone:
128147
return auth.NewNoneProvider(registryURL), nil
129148
default:
130149
return nil, fmt.Errorf("unknown authentication method: %s\nFor a list of available methods, run: mcp-publisher login", method)
@@ -191,11 +210,10 @@ Examples:
191210
}
192211
}
193212

194-
authProvider, err := createAuthProvider(method, flags.RegistryURL, flags.Domain, signer)
213+
authProvider, err := createAuthProvider(method, flags.RegistryURL, flags.Domain, flags.Token, signer)
195214
if err != nil {
196215
return err
197216
}
198-
199217
ctx := context.Background()
200218
_, _ = fmt.Fprintf(os.Stdout, "Logging in with %s...\n", method)
201219

docs/guides/publishing/github-actions.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,25 @@ The workflow runs tests, builds your package, publishes to npm, and publishes to
9898

9999
### GitHub Personal Access Token
100100

101+
You can authenticate using a GitHub Personal Access Token in two ways:
102+
103+
**Option 1: Using the --token flag**
104+
105+
```yaml
106+
- name: Login to MCP Registry
107+
run: mcp-publisher login github --token ${{ secrets.MCP_GITHUB_TOKEN }}
108+
```
109+
110+
**Option 2: Using environment variable**
111+
101112
```yaml
102113
- name: Login to MCP Registry
103-
run: mcp-publisher login github --token ${{ secrets.GITHUB_TOKEN }}
114+
run: mcp-publisher login github
104115
env:
105-
GITHUB_TOKEN: ${{ secrets.MCP_GITHUB_TOKEN }}
116+
MCP_GITHUB_TOKEN: ${{ secrets.MCP_GITHUB_TOKEN }}
106117
```
107118

108-
Add `MCP_GITHUB_TOKEN` secret with a GitHub PAT that has repo access.
119+
**Note:** You must create a custom secret `MCP_GITHUB_TOKEN` with a GitHub Personal Access Token that has `read:org` and `read:user` scopes. The automatic `GITHUB_TOKEN` provided by GitHub Actions does not have these permissions and cannot be used.
109120

110121
### DNS Authentication
111122

0 commit comments

Comments
 (0)