Skip to content

Commit f6d4337

Browse files
committed
Linter fixes and renamed scope challenge to PAT scope filter
1 parent 2b016e5 commit f6d4337

File tree

9 files changed

+22
-26
lines changed

9 files changed

+22
-26
lines changed

pkg/context/mcp_info.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ type MCPMethodInfo struct {
2525
Arguments map[string]any
2626
}
2727

28-
// ContextWithMCPMethodInfo stores the MCPMethodInfo in the context.
29-
func ContextWithMCPMethodInfo(ctx context.Context, info *MCPMethodInfo) context.Context {
28+
// WithMCPMethodInfo stores the MCPMethodInfo in the context.
29+
func WithMCPMethodInfo(ctx context.Context, info *MCPMethodInfo) context.Context {
3030
return context.WithValue(ctx, mcpMethodInfoCtxKey, info)
3131
}
3232

pkg/context/token.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ type tokenCtx string
1212
var tokenCtxKey tokenCtx = "tokenctx"
1313

1414
type TokenInfo struct {
15-
Token string
16-
TokenType utils.TokenType
17-
Scopes []string
15+
Token string
16+
TokenType utils.TokenType
17+
ScopesFetched bool
18+
Scopes []string
1819
}
1920

2021
// WithTokenInfo adds TokenInfo to the context
2122
func WithTokenInfo(ctx context.Context, tokenInfo *TokenInfo) context.Context {
2223
return context.WithValue(ctx, tokenCtxKey, tokenInfo)
2324
}
2425

25-
func SetTokenScopes(ctx context.Context, scopes []string) context.Context {
26+
func SetTokenScopes(ctx context.Context, scopes []string) {
2627
if tokenInfo, ok := GetTokenInfo(ctx); ok {
2728
tokenInfo.Scopes = scopes
28-
return WithTokenInfo(ctx, tokenInfo)
29+
tokenInfo.ScopesFetched = true
2930
}
30-
return ctx
3131
}
3232

3333
// GetTokenInfo retrieves the authentication token from the context

pkg/http/handler.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,7 @@ func DefaultInventoryFactory(cfg *ServerConfig, t translations.TranslationHelper
209209
WithFeatureChecker(featureChecker)
210210

211211
b = InventoryFiltersForRequest(r, b)
212-
213-
if cfg.ScopeChallenge {
214-
b = ScopeChallengeFilter(b, r, scopeFetcher)
215-
}
212+
b = PATScopeFilter(b, r, scopeFetcher)
216213

217214
b.WithServerInstructions()
218215

@@ -243,7 +240,7 @@ func InventoryFiltersForRequest(r *http.Request, builder *inventory.Builder) *in
243240
return builder
244241
}
245242

246-
func ScopeChallengeFilter(b *inventory.Builder, r *http.Request, fetcher scopes.FetcherInterface) *inventory.Builder {
243+
func PATScopeFilter(b *inventory.Builder, r *http.Request, fetcher scopes.FetcherInterface) *inventory.Builder {
247244
ctx := r.Context()
248245

249246
tokenInfo, ok := ghcontext.GetTokenInfo(ctx)
@@ -260,6 +257,9 @@ func ScopeChallengeFilter(b *inventory.Builder, r *http.Request, fetcher scopes.
260257
return b
261258
}
262259

260+
// Store fetched scopes in context for downstream use
261+
ghcontext.SetTokenScopes(ctx, scopesList)
262+
263263
return b.WithFilter(github.CreateToolScopeFilter(scopesList))
264264
}
265265

pkg/http/middleware/mcp_parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func WithMCPParse() func(http.Handler) http.Handler {
117117
}
118118

119119
// Store the parsed info in context
120-
ctx = ghcontext.ContextWithMCPMethodInfo(ctx, methodInfo)
120+
ctx = ghcontext.WithMCPMethodInfo(ctx, methodInfo)
121121

122122
next.ServeHTTP(w, r.WithContext(ctx))
123123
}

pkg/http/middleware/scope_challenge.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import (
2020
// a HEAD request and reading the X-OAuth-Scopes header. This is used as a fallback
2121
// when scopes are not provided in the token info header.
2222
func FetchScopesFromGitHubAPI(ctx context.Context, token string, apiHost utils.APIHostResolver) ([]string, error) {
23-
baseUrl, err := apiHost.BaseRESTURL(ctx)
23+
baseURL, err := apiHost.BaseRESTURL(ctx)
2424
if err != nil {
2525
return nil, err
2626
}
2727

28-
req, err := http.NewRequestWithContext(ctx, http.MethodHead, strings.TrimSuffix(baseUrl.String(), "/")+"/user", http.NoBody)
28+
req, err := http.NewRequestWithContext(ctx, http.MethodHead, strings.TrimSuffix(baseURL.String(), "/")+"/user", http.NoBody)
2929
if err != nil {
3030
return nil, err
3131
}

pkg/http/oauth/oauth.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strings"
99

1010
"github.com/github/github-mcp-server/pkg/http/headers"
11-
"github.com/github/github-mcp-server/pkg/utils"
1211
"github.com/go-chi/chi/v5"
1312
"github.com/modelcontextprotocol/go-sdk/auth"
1413
"github.com/modelcontextprotocol/go-sdk/oauthex"
@@ -40,8 +39,6 @@ var SupportedScopes = []string{
4039

4140
// Config holds the OAuth configuration for the MCP server.
4241
type Config struct {
43-
ApiHosts utils.APIHostResolver
44-
4542
// BaseURL is the publicly accessible URL where this server is hosted.
4643
// This is used to construct the OAuth resource URL.
4744
BaseURL string

pkg/http/server.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ func RunHTTPServer(cfg ServerConfig) error {
128128
// Register OAuth protected resource metadata endpoints
129129
oauthCfg := &oauth.Config{
130130
BaseURL: cfg.BaseURL,
131-
ApiHosts: apiHost,
132131
ResourcePath: cfg.ResourcePath,
133132
}
134133

pkg/scopes/fetcher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ func NewFetcher(opts FetcherOptions) *Fetcher {
6767
// Note: Fine-grained PATs don't return the X-OAuth-Scopes header, so an empty
6868
// slice is returned for those tokens.
6969
func (f *Fetcher) FetchTokenScopes(ctx context.Context, token string) ([]string, error) {
70-
apiHostUrl, err := f.apiHost.BaseRESTURL(ctx)
70+
apiHostURL, err := f.apiHost.BaseRESTURL(ctx)
7171
if err != nil {
7272
return nil, fmt.Errorf("failed to get API host URL: %w", err)
7373
}
7474

7575
// Use a lightweight endpoint that requires authentication
76-
endpoint, err := url.JoinPath(apiHostUrl.String(), "/")
76+
endpoint, err := url.JoinPath(apiHostURL.String(), "/")
7777
if err != nil {
7878
return nil, fmt.Errorf("failed to construct API URL: %w", err)
7979
}

pkg/scopes/fetcher_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ type testApiHostResolver struct {
1616
baseURL string
1717
}
1818

19-
func (t testApiHostResolver) BaseRESTURL(ctx context.Context) (*url.URL, error) {
19+
func (t testApiHostResolver) BaseRESTURL(_ context.Context) (*url.URL, error) {
2020
return url.Parse(t.baseURL)
2121
}
22-
func (t testApiHostResolver) GraphqlURL(ctx context.Context) (*url.URL, error) {
22+
func (t testApiHostResolver) GraphqlURL(_ context.Context) (*url.URL, error) {
2323
return nil, nil
2424
}
25-
func (t testApiHostResolver) UploadURL(ctx context.Context) (*url.URL, error) {
25+
func (t testApiHostResolver) UploadURL(_ context.Context) (*url.URL, error) {
2626
return nil, nil
2727
}
28-
func (t testApiHostResolver) RawURL(ctx context.Context) (*url.URL, error) {
28+
func (t testApiHostResolver) RawURL(_ context.Context) (*url.URL, error) {
2929
return nil, nil
3030
}
3131

0 commit comments

Comments
 (0)