Skip to content

Commit 32d2342

Browse files
committed
Move http stuff into its own package
1 parent f3802d5 commit 32d2342

File tree

7 files changed

+91
-88
lines changed

7 files changed

+91
-88
lines changed

cmd/github-mcp-server/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/github/github-mcp-server/internal/ghmcp"
1111
"github.com/github/github-mcp-server/pkg/github"
12+
ghhttp "github.com/github/github-mcp-server/pkg/http"
1213
"github.com/spf13/cobra"
1314
"github.com/spf13/pflag"
1415
"github.com/spf13/viper"
@@ -94,7 +95,7 @@ var (
9495
Short: "Start HTTP server",
9596
Long: `Start an HTTP server that listens for MCP requests over HTTP.`,
9697
RunE: func(_ *cobra.Command, _ []string) error {
97-
httpConfig := ghmcp.HTTPServerConfig{
98+
httpConfig := ghhttp.HTTPServerConfig{
9899
Version: version,
99100
Host: viper.GetString("host"),
100101
ExportTranslations: viper.GetBool("export-translations"),
@@ -103,7 +104,7 @@ var (
103104
ContentWindowSize: viper.GetInt("content-window-size"),
104105
}
105106

106-
return ghmcp.RunHTTPServer(httpConfig)
107+
return ghhttp.RunHTTPServer(httpConfig)
107108
},
108109
}
109110
)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ require (
3131
require (
3232
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
3333
github.com/fsnotify/fsnotify v1.9.0 // indirect
34+
github.com/go-chi/chi/v5 v5.2.3
3435
github.com/go-viper/mapstructure/v2 v2.4.0
3536
github.com/google/go-querystring v1.1.0 // indirect
3637
github.com/inconshreveable/mousetrap v1.1.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
1010
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
1111
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
1212
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
13+
github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE=
14+
github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
1315
github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY=
1416
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
1517
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=

pkg/context/lockdown.go

Lines changed: 0 additions & 19 deletions
This file was deleted.

pkg/github/dependencies.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ type RequestDeps struct {
215215
// Static dependencies
216216
apiHosts *utils.ApiHost
217217
version string
218+
lockdownMode bool
218219
RepoAccessOpts []lockdown.RepoAccessOption
219220
T translations.TranslationHelperFunc
220221
Flags FeatureFlags
@@ -225,6 +226,7 @@ type RequestDeps struct {
225226
func NewRequestDeps(
226227
apiHosts *utils.ApiHost,
227228
version string,
229+
lockdownMode bool,
228230
repoAccessOpts []lockdown.RepoAccessOption,
229231
t translations.TranslationHelperFunc,
230232
flags FeatureFlags,
@@ -233,6 +235,7 @@ func NewRequestDeps(
233235
return &RequestDeps{
234236
apiHosts: apiHosts,
235237
version: version,
238+
lockdownMode: lockdownMode,
236239
RepoAccessOpts: repoAccessOpts,
237240
T: t,
238241
Flags: flags,
@@ -298,7 +301,7 @@ func (d *RequestDeps) GetRawClient(ctx context.Context) (*raw.Client, error) {
298301

299302
// GetRepoAccessCache implements ToolDependencies.
300303
func (d *RequestDeps) GetRepoAccessCache(ctx context.Context) (*lockdown.RepoAccessCache, error) {
301-
if !ghcontext.IsLockdownMode(ctx) {
304+
if !d.lockdownMode {
302305
return nil, nil
303306
}
304307

pkg/http/handler.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package http
2+
3+
import (
4+
"log/slog"
5+
"net/http"
6+
7+
"github.com/github/github-mcp-server/pkg/github"
8+
"github.com/github/github-mcp-server/pkg/http/middleware"
9+
"github.com/github/github-mcp-server/pkg/lockdown"
10+
"github.com/github/github-mcp-server/pkg/translations"
11+
"github.com/github/github-mcp-server/pkg/utils"
12+
"github.com/modelcontextprotocol/go-sdk/mcp"
13+
)
14+
15+
type HttpMcpHandler struct {
16+
config *HTTPServerConfig
17+
apiHosts utils.ApiHost
18+
logger *slog.Logger
19+
t translations.TranslationHelperFunc
20+
repoAccessOpts []lockdown.RepoAccessOption
21+
}
22+
23+
func NewHttpMcpHandler(cfg *HTTPServerConfig,
24+
t translations.TranslationHelperFunc,
25+
apiHosts *utils.ApiHost,
26+
repoAccessOptions []lockdown.RepoAccessOption,
27+
logger *slog.Logger) *HttpMcpHandler {
28+
return &HttpMcpHandler{
29+
config: cfg,
30+
apiHosts: *apiHosts,
31+
logger: logger,
32+
t: t,
33+
repoAccessOpts: repoAccessOptions,
34+
}
35+
}
36+
37+
func (s *HttpMcpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
38+
// Set up repo access cache for lockdown mode
39+
deps := github.NewRequestDeps(
40+
&s.apiHosts,
41+
s.config.Version,
42+
s.config.LockdownMode,
43+
s.repoAccessOpts,
44+
s.t,
45+
github.FeatureFlags{
46+
LockdownMode: s.config.LockdownMode,
47+
},
48+
s.config.ContentWindowSize,
49+
)
50+
51+
ghServer, err := github.NewMcpServer(&github.MCPServerConfig{
52+
Version: s.config.Version,
53+
Host: s.config.Host,
54+
EnabledToolsets: s.config.EnabledToolsets,
55+
EnabledTools: s.config.EnabledTools,
56+
EnabledFeatures: s.config.EnabledFeatures,
57+
DynamicToolsets: s.config.DynamicToolsets,
58+
ReadOnly: s.config.ReadOnly,
59+
Translator: s.t,
60+
ContentWindowSize: s.config.ContentWindowSize,
61+
Logger: s.logger,
62+
RepoAccessTTL: s.config.RepoAccessCacheTTL,
63+
}, deps)
64+
if err != nil {
65+
w.WriteHeader(http.StatusInternalServerError)
66+
}
67+
68+
mcpHandler := mcp.NewStreamableHTTPHandler(func(r *http.Request) *mcp.Server {
69+
return ghServer
70+
}, &mcp.StreamableHTTPOptions{
71+
Stateless: true,
72+
})
73+
74+
middleware.ExtractUserToken()(mcpHandler).ServeHTTP(w, r)
75+
}
Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ghmcp
1+
package http
22

33
import (
44
"context"
@@ -11,12 +11,10 @@ import (
1111
"syscall"
1212
"time"
1313

14-
"github.com/github/github-mcp-server/pkg/github"
15-
"github.com/github/github-mcp-server/pkg/http/middleware"
1614
"github.com/github/github-mcp-server/pkg/lockdown"
1715
"github.com/github/github-mcp-server/pkg/translations"
1816
"github.com/github/github-mcp-server/pkg/utils"
19-
"github.com/modelcontextprotocol/go-sdk/mcp"
17+
"github.com/go-chi/chi/v5"
2018
)
2119

2220
type HTTPServerConfig struct {
@@ -102,9 +100,12 @@ func RunHTTPServer(cfg HTTPServerConfig) error {
102100

103101
handler := NewHttpMcpHandler(&cfg, t, &apiHost, repoAccessOpts, logger)
104102

103+
r := chi.NewRouter()
104+
r.Mount("/", handler)
105+
105106
httpSvr := http.Server{
106107
Addr: ":8082",
107-
Handler: handler,
108+
Handler: r,
108109
}
109110

110111
go func() {
@@ -130,64 +131,3 @@ func RunHTTPServer(cfg HTTPServerConfig) error {
130131
logger.Info("server stopped gracefully")
131132
return nil
132133
}
133-
134-
type HttpMcpHandler struct {
135-
config *HTTPServerConfig
136-
apiHosts utils.ApiHost
137-
logger *slog.Logger
138-
t translations.TranslationHelperFunc
139-
repoAccessOpts []lockdown.RepoAccessOption
140-
}
141-
142-
func NewHttpMcpHandler(cfg *HTTPServerConfig,
143-
t translations.TranslationHelperFunc,
144-
apiHosts *utils.ApiHost,
145-
repoAccessOptions []lockdown.RepoAccessOption,
146-
logger *slog.Logger) *HttpMcpHandler {
147-
return &HttpMcpHandler{
148-
config: cfg,
149-
apiHosts: *apiHosts,
150-
logger: logger,
151-
t: t,
152-
repoAccessOpts: repoAccessOptions,
153-
}
154-
}
155-
156-
func (s *HttpMcpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
157-
// Set up repo access cache for lockdown mode
158-
deps := github.NewRequestDeps(
159-
&s.apiHosts,
160-
s.config.Version,
161-
s.repoAccessOpts,
162-
s.t,
163-
github.FeatureFlags{
164-
LockdownMode: s.config.LockdownMode,
165-
},
166-
s.config.ContentWindowSize,
167-
)
168-
169-
ghServer, err := github.NewMcpServer(&github.MCPServerConfig{
170-
Version: s.config.Version,
171-
Host: s.config.Host,
172-
EnabledToolsets: s.config.EnabledToolsets,
173-
EnabledTools: s.config.EnabledTools,
174-
EnabledFeatures: s.config.EnabledFeatures,
175-
DynamicToolsets: s.config.DynamicToolsets,
176-
ReadOnly: s.config.ReadOnly,
177-
Translator: s.t,
178-
ContentWindowSize: s.config.ContentWindowSize,
179-
Logger: s.logger,
180-
RepoAccessTTL: s.config.RepoAccessCacheTTL,
181-
}, deps)
182-
if err != nil {
183-
w.WriteHeader(http.StatusInternalServerError)
184-
}
185-
186-
mcpHandler := mcp.NewStreamableHTTPHandler(func(r *http.Request) *mcp.Server {
187-
return ghServer
188-
}, &mcp.StreamableHTTPOptions{
189-
Stateless: true,
190-
})
191-
192-
middleware.ExtractUserToken()(mcpHandler).ServeHTTP(w, r)
193-
}

0 commit comments

Comments
 (0)