Skip to content

Commit 69e3e5f

Browse files
committed
Don't use a middleware. Not sure if this is better or worse.
1 parent 7feaee3 commit 69e3e5f

File tree

3 files changed

+42
-36
lines changed

3 files changed

+42
-36
lines changed

pkg/github/dependencies.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"errors"
66
"fmt"
77
"net/http"
8+
"strings"
89

9-
ghcontext "github.com/github/github-mcp-server/pkg/context"
1010
"github.com/github/github-mcp-server/pkg/http/transport"
1111
"github.com/github/github-mcp-server/pkg/inventory"
1212
"github.com/github/github-mcp-server/pkg/lockdown"
@@ -213,6 +213,7 @@ type RequestDeps struct {
213213
RepoAccessCache *lockdown.RepoAccessCache
214214

215215
// Static dependencies
216+
token string
216217
apiHosts *utils.ApiHost
217218
version string
218219
lockdownMode bool
@@ -224,23 +225,29 @@ type RequestDeps struct {
224225

225226
// NewRequestDeps creates a RequestDeps with the provided clients and configuration.
226227
func NewRequestDeps(
228+
token string,
227229
apiHosts *utils.ApiHost,
228230
version string,
229231
lockdownMode bool,
230232
repoAccessOpts []lockdown.RepoAccessOption,
231233
t translations.TranslationHelperFunc,
232234
flags FeatureFlags,
233235
contentWindowSize int,
234-
) *RequestDeps {
236+
) (*RequestDeps, error) {
237+
if strings.TrimSpace(token) == "" {
238+
return nil, fmt.Errorf("token must be provided")
239+
}
240+
235241
return &RequestDeps{
242+
token: token,
236243
apiHosts: apiHosts,
237244
version: version,
238245
lockdownMode: lockdownMode,
239246
RepoAccessOpts: repoAccessOpts,
240247
T: t,
241248
Flags: flags,
242249
ContentWindowSize: contentWindowSize,
243-
}
250+
}, nil
244251
}
245252

246253
// GetClient implements ToolDependencies.
@@ -249,11 +256,8 @@ func (d *RequestDeps) GetClient(ctx context.Context) (*gogithub.Client, error) {
249256
return d.Client, nil
250257
}
251258

252-
// extract the token from the context
253-
token, _ := ghcontext.GetTokenInfo(ctx)
254-
255259
// Construct REST client
256-
restClient := gogithub.NewClient(nil).WithAuthToken(token)
260+
restClient := gogithub.NewClient(nil).WithAuthToken(d.token)
257261
restClient.UserAgent = fmt.Sprintf("github-mcp-server/%s", d.version)
258262
restClient.BaseURL = d.apiHosts.BaseRESTURL
259263
restClient.UploadURL = d.apiHosts.UploadURL
@@ -266,15 +270,12 @@ func (d *RequestDeps) GetGQLClient(ctx context.Context) (*githubv4.Client, error
266270
return d.GQLClient, nil
267271
}
268272

269-
// extract the token from the context
270-
token, _ := ghcontext.GetTokenInfo(ctx)
271-
272273
// Construct GraphQL client
273274
// We use NewEnterpriseClient unconditionally since we already parsed the API host
274275
gqlHTTPClient := &http.Client{
275276
Transport: &transport.BearerAuthTransport{
276277
Transport: http.DefaultTransport,
277-
Token: token,
278+
Token: d.token,
278279
},
279280
}
280281
gqlClient := githubv4.NewEnterpriseClient(d.apiHosts.GraphqlURL.String(), gqlHTTPClient)

pkg/http/handler.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package http
22

33
import (
4+
"errors"
45
"log/slog"
56
"net/http"
67

78
"github.com/github/github-mcp-server/pkg/github"
8-
"github.com/github/github-mcp-server/pkg/http/middleware"
99
"github.com/github/github-mcp-server/pkg/lockdown"
1010
"github.com/github/github-mcp-server/pkg/translations"
1111
"github.com/github/github-mcp-server/pkg/utils"
@@ -35,8 +35,20 @@ func NewHttpMcpHandler(cfg *HTTPServerConfig,
3535
}
3636

3737
func (s *HttpMcpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
38+
token, err := ExtractUserToken(r)
39+
if err != nil {
40+
if errors.Is(err, errMissingAuthorizationHeader) {
41+
// sendAuthChallenge(w, r, cfg, obsv)
42+
return
43+
}
44+
// For other auth errors (bad format, unsupported), return 400
45+
http.Error(w, err.Error(), http.StatusBadRequest)
46+
return
47+
}
48+
3849
// Set up repo access cache for lockdown mode
39-
deps := github.NewRequestDeps(
50+
deps, err := github.NewRequestDeps(
51+
token,
4052
&s.apiHosts,
4153
s.config.Version,
4254
s.config.LockdownMode,
@@ -47,6 +59,11 @@ func (s *HttpMcpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4759
},
4860
s.config.ContentWindowSize,
4961
)
62+
if err != nil {
63+
s.logger.Error("failed to create request dependencies", "error", err)
64+
w.WriteHeader(http.StatusInternalServerError)
65+
return
66+
}
5067

5168
ghServer, err := github.NewMCPServer(&github.MCPServerConfig{
5269
Version: s.config.Version,
@@ -71,5 +88,5 @@ func (s *HttpMcpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7188
Stateless: true,
7289
})
7390

74-
middleware.ExtractUserToken()(mcpHandler).ServeHTTP(w, r)
91+
mcpHandler.ServeHTTP(w, r)
7592
}
Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
package middleware
1+
package http
22

33
import (
4-
"errors"
54
"fmt"
65
"net/http"
76
"regexp"
@@ -40,28 +39,17 @@ var supportedThirdPartyTokenPrefixes = []string{
4039
// were 40 characters long and only contained the characters a-f and 0-9.
4140
var oldPatternRegexp = regexp.MustCompile(`\A[a-f0-9]{40}\z`)
4241

43-
func ExtractUserToken() func(next http.Handler) http.Handler {
44-
return func(next http.Handler) http.Handler {
45-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
46-
_, token, err := parseAuthorizationHeader(r)
47-
if err != nil {
48-
// For missing Authorization header, return 401 with WWW-Authenticate header per MCP spec
49-
if errors.Is(err, errMissingAuthorizationHeader) {
50-
// sendAuthChallenge(w, r, cfg, obsv)
51-
return
52-
}
53-
// For other auth errors (bad format, unsupported), return 400
54-
http.Error(w, err.Error(), http.StatusBadRequest)
55-
return
56-
}
42+
func ExtractUserToken(r *http.Request) (string, error) {
43+
_, token, err := parseAuthorizationHeader(r)
44+
if err != nil {
45+
return "", err
46+
}
5747

58-
ctx := r.Context()
59-
ctx = ghcontext.WithTokenInfo(ctx, token)
60-
r = r.WithContext(ctx)
48+
ctx := r.Context()
49+
ctx = ghcontext.WithTokenInfo(ctx, token)
50+
r = r.WithContext(ctx)
6151

62-
next.ServeHTTP(w, r)
63-
})
64-
}
52+
return token, nil
6553
}
6654
func parseAuthorizationHeader(req *http.Request) (authType authType, token string, _ error) {
6755
authHeader := req.Header.Get(httpheaders.AuthorizationHeader)

0 commit comments

Comments
 (0)