@@ -25,6 +25,9 @@ import (
2525 "github.com/shurcooL/githubv4"
2626)
2727
28+ // MCPServerConfig contains all configuration needed to create an MCP server.
29+ // This config struct is used by both NewMCPServer and NewUnauthenticatedMCPServer
30+ // to ensure consistent configuration across authentication modes.
2831type MCPServerConfig struct {
2932 // Version of the server
3033 Version string
@@ -158,6 +161,17 @@ func resolveEnabledToolsets(cfg MCPServerConfig) []string {
158161 return nil
159162}
160163
164+ // NewMCPServer creates a fully initialized MCP server with GitHub API access.
165+ // This constructor is used when a token is available at startup (PAT authentication).
166+ // For OAuth device flow authentication without a pre-configured token, use NewUnauthenticatedMCPServer instead.
167+ //
168+ // The server creation involves several steps:
169+ // 1. Parse API host configuration and create GitHub clients
170+ // 2. Resolve which toolsets to enable
171+ // 3. Create MCP server with appropriate capabilities
172+ // 4. Add middleware for error handling, user agents, and dependency injection
173+ // 5. Build and register the tool/resource/prompt inventory
174+ // 6. Optionally register dynamic toolset management tools
161175func NewMCPServer (cfg MCPServerConfig ) (* mcp.Server , error ) {
162176 apiHost , err := parseAPIHost (cfg .Host )
163177 if err != nil {
@@ -284,8 +298,17 @@ type UnauthenticatedServerResult struct {
284298}
285299
286300// NewUnauthenticatedMCPServer creates an MCP server with only authentication tools available.
287- // After successful authentication via the auth tools, call OnAuthenticated to initialize
288- // GitHub clients and register all other tools.
301+ // This constructor is used for OAuth device flow when no token is available at startup.
302+ // After successful authentication via the auth tools, the OnAuthenticated callback
303+ // initializes GitHub clients and registers all other tools dynamically.
304+ //
305+ // Architecture note: This shares significant setup logic with NewMCPServer. The duplication
306+ // exists because the two modes have different initialization timing:
307+ // - NewMCPServer: All setup happens at construction time (clients + tools)
308+ // - NewUnauthenticatedMCPServer: Setup happens in two phases (auth tools first, then clients + tools after auth)
309+ //
310+ // Future improvement: Consider extracting common setup logic into shared helper functions
311+ // to reduce duplication while maintaining the two-phase initialization pattern.
289312func NewUnauthenticatedMCPServer (cfg MCPServerConfig ) (* UnauthenticatedServerResult , error ) {
290313 // Create OAuth host from the configured GitHub host
291314 oauthHost := github .NewOAuthHostFromAPIHost (cfg .Host )
@@ -387,7 +410,11 @@ func NewUnauthenticatedMCPServer(cfg MCPServerConfig) (*UnauthenticatedServerRes
387410 return nil
388411 },
389412 OnAuthComplete : func () {
390- // Remove auth_login tool now that authentication is complete
413+ // Remove auth tools after authentication completes.
414+ // Note: This manual removal ensures auth tools don't remain available after login.
415+ // The auth_login tool is removed by name here to keep the tool list clean.
416+ // Future improvement: Consider using toolset-based filtering to automatically
417+ // exclude auth toolset after authentication, removing the need for manual cleanup.
391418 ghServer .RemoveTools ("auth_login" )
392419 if cfg .Logger != nil {
393420 cfg .Logger .Info ("auth tools removed after successful authentication" )
0 commit comments