- Notifications
You must be signed in to change notification settings - Fork1.4k
Split PR review creation, commenting, submission and deletion#410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -5,8 +5,11 @@ import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"os/signal" | ||
"strings" | ||
"syscall" | ||
"github.com/github/github-mcp-server/pkg/github" | ||
@@ -15,6 +18,7 @@ import ( | ||
gogithub"github.com/google/go-github/v69/github" | ||
"github.com/mark3labs/mcp-go/mcp" | ||
"github.com/mark3labs/mcp-go/server" | ||
"github.com/shurcooL/githubv4" | ||
"github.com/sirupsen/logrus" | ||
) | ||
@@ -44,25 +48,43 @@ type MCPServerConfig struct { | ||
} | ||
funcNewMCPServer(cfgMCPServerConfig) (*server.MCPServer,error) { | ||
apiHost,err:=parseAPIHost(cfg.Host) | ||
iferr!=nil { | ||
returnnil,fmt.Errorf("failed to parse API host: %w",err) | ||
} | ||
// Construct our REST client | ||
restClient:=gogithub.NewClient(nil).WithAuthToken(cfg.Token) | ||
restClient.UserAgent=fmt.Sprintf("github-mcp-server/%s",cfg.Version) | ||
restClient.BaseURL=apiHost.baseRESTURL | ||
CollaboratorAuthor
| ||
restClient.UploadURL=apiHost.uploadURL | ||
// Construct our GraphQL client | ||
// We're using NewEnterpriseClient here unconditionally as opposed to NewClient because we already | ||
// did the necessary API host parsing so that github.com will return the correct URL anyway. | ||
gqlHTTPClient:=&http.Client{ | ||
Transport:&bearerAuthTransport{ | ||
transport:http.DefaultTransport, | ||
token:cfg.Token, | ||
}, | ||
}// We're going to wrap the Transport later in beforeInit | ||
gqlClient:=githubv4.NewEnterpriseClient(apiHost.graphqlURL.String(),gqlHTTPClient) | ||
// When a client send an initialize request, update the user agent to include the client info. | ||
beforeInit:=func(_ context.Context,_any,message*mcp.InitializeRequest) { | ||
userAgent:=fmt.Sprintf( | ||
"github-mcp-server/%s (%s/%s)", | ||
cfg.Version, | ||
message.Params.ClientInfo.Name, | ||
message.Params.ClientInfo.Version, | ||
) | ||
restClient.UserAgent=userAgent | ||
gqlHTTPClient.Transport=&userAgentTransport{ | ||
transport:gqlHTTPClient.Transport, | ||
agent:userAgent, | ||
} | ||
} | ||
hooks:=&server.Hooks{ | ||
@@ -83,14 +105,19 @@ func NewMCPServer(cfg MCPServerConfig) (*server.MCPServer, error) { | ||
} | ||
getClient:=func(_ context.Context) (*gogithub.Client,error) { | ||
returnrestClient,nil// closing over client | ||
} | ||
getGQLClient:=func(_ context.Context) (*githubv4.Client,error) { | ||
returngqlClient,nil// closing over client | ||
} | ||
// Create default toolsets | ||
toolsets,err:=github.InitToolsets( | ||
enabledToolsets, | ||
cfg.ReadOnly, | ||
getClient, | ||
getGQLClient, | ||
cfg.Translator, | ||
) | ||
iferr!=nil { | ||
@@ -213,3 +240,141 @@ func RunStdioServer(cfg StdioServerConfig) error { | ||
returnnil | ||
} | ||
typeapiHoststruct { | ||
baseRESTURL*url.URL | ||
graphqlURL*url.URL | ||
uploadURL*url.URL | ||
} | ||
funcnewDotcomHost() (apiHost,error) { | ||
baseRestURL,err:=url.Parse("https://api.github.com/") | ||
iferr!=nil { | ||
returnapiHost{},fmt.Errorf("failed to parse dotcom REST URL: %w",err) | ||
} | ||
gqlURL,err:=url.Parse("https://api.github.com/graphql") | ||
iferr!=nil { | ||
returnapiHost{},fmt.Errorf("failed to parse dotcom GraphQL URL: %w",err) | ||
} | ||
uploadURL,err:=url.Parse("https://uploads.github.com") | ||
iferr!=nil { | ||
returnapiHost{},fmt.Errorf("failed to parse dotcom Upload URL: %w",err) | ||
} | ||
returnapiHost{ | ||
baseRESTURL:baseRestURL, | ||
graphqlURL:gqlURL, | ||
uploadURL:uploadURL, | ||
},nil | ||
} | ||
funcnewGHECHost(hostnamestring) (apiHost,error) { | ||
u,err:=url.Parse(hostname) | ||
iferr!=nil { | ||
returnapiHost{},fmt.Errorf("failed to parse GHEC URL: %w",err) | ||
} | ||
// Unsecured GHEC would be an error | ||
ifu.Scheme=="http" { | ||
returnapiHost{},fmt.Errorf("GHEC URL must be HTTPS") | ||
} | ||
restURL,err:=url.Parse(fmt.Sprintf("https://api.%s/",u.Hostname())) | ||
iferr!=nil { | ||
returnapiHost{},fmt.Errorf("failed to parse GHEC REST URL: %w",err) | ||
} | ||
gqlURL,err:=url.Parse(fmt.Sprintf("https://api.%s/graphql",u.Hostname())) | ||
iferr!=nil { | ||
returnapiHost{},fmt.Errorf("failed to parse GHEC GraphQL URL: %w",err) | ||
} | ||
uploadURL,err:=url.Parse(fmt.Sprintf("https://uploads.%s",u.Hostname())) | ||
iferr!=nil { | ||
returnapiHost{},fmt.Errorf("failed to parse GHEC Upload URL: %w",err) | ||
} | ||
returnapiHost{ | ||
baseRESTURL:restURL, | ||
graphqlURL:gqlURL, | ||
uploadURL:uploadURL, | ||
},nil | ||
} | ||
funcnewGHESHost(hostnamestring) (apiHost,error) { | ||
u,err:=url.Parse(hostname) | ||
iferr!=nil { | ||
returnapiHost{},fmt.Errorf("failed to parse GHES URL: %w",err) | ||
} | ||
restURL,err:=url.Parse(fmt.Sprintf("%s://%s/api/v3/",u.Scheme,u.Hostname())) | ||
iferr!=nil { | ||
returnapiHost{},fmt.Errorf("failed to parse GHES REST URL: %w",err) | ||
} | ||
gqlURL,err:=url.Parse(fmt.Sprintf("%s://%s/api/graphql",u.Scheme,u.Hostname())) | ||
iferr!=nil { | ||
returnapiHost{},fmt.Errorf("failed to parse GHES GraphQL URL: %w",err) | ||
} | ||
uploadURL,err:=url.Parse(fmt.Sprintf("%s://%s/api/uploads/",u.Scheme,u.Hostname())) | ||
iferr!=nil { | ||
returnapiHost{},fmt.Errorf("failed to parse GHES Upload URL: %w",err) | ||
} | ||
returnapiHost{ | ||
baseRESTURL:restURL, | ||
graphqlURL:gqlURL, | ||
uploadURL:uploadURL, | ||
},nil | ||
} | ||
// Note that this does not handle ports yet, so development environments are out. | ||
funcparseAPIHost(sstring) (apiHost,error) { | ||
ifs=="" { | ||
returnnewDotcomHost() | ||
} | ||
u,err:=url.Parse(s) | ||
iferr!=nil { | ||
returnapiHost{},fmt.Errorf("could not parse host as URL: %s",s) | ||
} | ||
ifu.Scheme=="" { | ||
returnapiHost{},fmt.Errorf("host must have a scheme (http or https): %s",s) | ||
} | ||
ifstrings.HasSuffix(u.Hostname(),"github.com") { | ||
returnnewDotcomHost() | ||
} | ||
ifstrings.HasSuffix(u.Hostname(),"ghe.com") { | ||
returnnewGHECHost(s) | ||
SamMorrowDrums marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
returnnewGHESHost(s) | ||
} | ||
typeuserAgentTransportstruct { | ||
transport http.RoundTripper | ||
agentstring | ||
} | ||
func (t*userAgentTransport)RoundTrip(req*http.Request) (*http.Response,error) { | ||
req=req.Clone(req.Context()) | ||
req.Header.Set("User-Agent",t.agent) | ||
returnt.transport.RoundTrip(req) | ||
} | ||
typebearerAuthTransportstruct { | ||
transport http.RoundTripper | ||
tokenstring | ||
} | ||
func (t*bearerAuthTransport)RoundTrip(req*http.Request) (*http.Response,error) { | ||
req=req.Clone(req.Context()) | ||
req.Header.Set("Authorization","Bearer "+t.token) | ||
returnt.transport.RoundTrip(req) | ||
} |
Uh oh!
There was an error while loading.Please reload this page.