@@ -24,18 +24,19 @@ import (
2424 "github.com/github/github-mcp-server/pkg/scopes"
2525 "github.com/github/github-mcp-server/pkg/translations"
2626 "github.com/github/github-mcp-server/pkg/utils"
27- gogithub "github.com/google/go-github/v86 /github"
27+ gogithub "github.com/google/go-github/v87 /github"
2828 "github.com/modelcontextprotocol/go-sdk/mcp"
2929 "github.com/shurcooL/githubv4"
3030)
3131
3232// githubClients holds all the GitHub API clients created for a server instance.
3333type githubClients struct {
34- rest * gogithub.Client
35- gql * githubv4.Client
36- gqlHTTP * http.Client // retained for middleware to modify transport
37- raw * raw.Client
38- repoAccess * lockdown.RepoAccessCache
34+ rest * gogithub.Client
35+ restUATransp * transport.UserAgentTransport // retained for middleware to modify user agent
36+ gql * githubv4.Client
37+ gqlHTTP * http.Client // retained for middleware to modify transport
38+ raw * raw.Client
39+ repoAccess * lockdown.RepoAccessCache
3940}
4041
4142// createGitHubClients creates all the GitHub API clients needed by the server.
@@ -60,11 +61,22 @@ func createGitHubClients(cfg github.MCPServerConfig, apiHost utils.APIHostResolv
6061 return nil , fmt .Errorf ("failed to get Raw URL: %w" , err )
6162 }
6263
64+ // Create a mutable user agent transport for the REST client so we can
65+ // update the user agent after MCP initialization.
66+ restUATransport := & transport.UserAgentTransport {
67+ Transport : http .DefaultTransport ,
68+ Agent : fmt .Sprintf ("github-mcp-server/%s" , cfg .Version ),
69+ }
70+
6371 // Construct REST client
64- restClient := gogithub .NewClient (nil ).WithAuthToken (cfg .Token )
65- restClient .UserAgent = fmt .Sprintf ("github-mcp-server/%s" , cfg .Version )
66- restClient .BaseURL = restURL
67- restClient .UploadURL = uploadURL
72+ restClient , err := gogithub .NewClient (
73+ gogithub .WithHTTPClient (& http.Client {Transport : restUATransport }),
74+ gogithub .WithAuthToken (cfg .Token ),
75+ gogithub .WithEnterpriseURLs (restURL .String (), uploadURL .String ()),
76+ )
77+ if err != nil {
78+ return nil , fmt .Errorf ("failed to create REST client: %w" , err )
79+ }
6880
6981 // Construct GraphQL client
7082 // We use NewEnterpriseClient unconditionally since we already parsed the API host
@@ -80,7 +92,10 @@ func createGitHubClients(cfg github.MCPServerConfig, apiHost utils.APIHostResolv
8092 gqlClient := githubv4 .NewEnterpriseClient (graphQLURL .String (), gqlHTTPClient )
8193
8294 // Create raw content client (shares REST client's HTTP transport)
83- rawClient := raw .NewClient (restClient , rawURL )
95+ rawClient , err := raw .NewClient (restClient , rawURL )
96+ if err != nil {
97+ return nil , fmt .Errorf ("failed to create raw client: %w" , err )
98+ }
8499
85100 // Set up repo access cache for lockdown mode
86101 var repoAccessCache * lockdown.RepoAccessCache
@@ -95,11 +110,12 @@ func createGitHubClients(cfg github.MCPServerConfig, apiHost utils.APIHostResolv
95110 }
96111
97112 return & githubClients {
98- rest : restClient ,
99- gql : gqlClient ,
100- gqlHTTP : gqlHTTPClient ,
101- raw : rawClient ,
102- repoAccess : repoAccessCache ,
113+ rest : restClient ,
114+ restUATransp : restUATransport ,
115+ gql : gqlClient ,
116+ gqlHTTP : gqlHTTPClient ,
117+ raw : rawClient ,
118+ repoAccess : repoAccessCache ,
103119 }, nil
104120}
105121
@@ -170,7 +186,7 @@ func NewStdioMCPServer(ctx context.Context, cfg github.MCPServerConfig) (*mcp.Se
170186 github .RegisterUIResources (ghServer )
171187 }
172188
173- ghServer .AddReceivingMiddleware (addUserAgentsMiddleware (cfg , clients .rest , clients .gqlHTTP ))
189+ ghServer .AddReceivingMiddleware (addUserAgentsMiddleware (cfg , clients .restUATransp , clients .gqlHTTP ))
174190
175191 return ghServer , nil
176192}
@@ -345,7 +361,7 @@ func createFeatureChecker(enabledFeatures []string, insidersMode bool) inventory
345361 }
346362}
347363
348- func addUserAgentsMiddleware (cfg github.MCPServerConfig , restClient * gogithub. Client , gqlHTTPClient * http.Client ) func (next mcp.MethodHandler ) mcp.MethodHandler {
364+ func addUserAgentsMiddleware (cfg github.MCPServerConfig , restUATransport * transport. UserAgentTransport , gqlHTTPClient * http.Client ) func (next mcp.MethodHandler ) mcp.MethodHandler {
349365 return func (next mcp.MethodHandler ) mcp.MethodHandler {
350366 return func (ctx context.Context , method string , request mcp.Request ) (result mcp.Result , err error ) {
351367 if method != "initialize" {
@@ -368,7 +384,7 @@ func addUserAgentsMiddleware(cfg github.MCPServerConfig, restClient *gogithub.Cl
368384 userAgent += " (insiders)"
369385 }
370386
371- restClient . UserAgent = userAgent
387+ restUATransport . Agent = userAgent
372388
373389 gqlHTTPClient .Transport = & transport.UserAgentTransport {
374390 Transport : gqlHTTPClient .Transport ,
0 commit comments