- Notifications
You must be signed in to change notification settings - Fork3k
Add HTTP server mode with OAuth token support#1216
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
base:main
Are you sure you want to change the base?
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -23,6 +23,7 @@ import ( | ||||||
| "github.com/mark3labs/mcp-go/mcp" | ||||||
| "github.com/mark3labs/mcp-go/server" | ||||||
| "github.com/shurcooL/githubv4" | ||||||
| "github.com/sirupsen/logrus" | ||||||
| ) | ||||||
| type MCPServerConfig struct { | ||||||
| @@ -120,11 +121,39 @@ func NewMCPServer(cfg MCPServerConfig) (*server.MCPServer, error) { | ||||||
| server.WithHooks(hooks), | ||||||
| ) | ||||||
| getClient := func(ctx context.Context) (*gogithub.Client, error) { | ||||||
| if tokenVal := ctx.Value(githubTokenKey{}); tokenVal != nil { | ||||||
| if token, ok := tokenVal.(string); ok && token != "" { | ||||||
| client := gogithub.NewClient(nil).WithAuthToken(token) | ||||||
| client.UserAgent = restClient.UserAgent | ||||||
| client.BaseURL = apiHost.baseRESTURL | ||||||
| client.UploadURL = apiHost.uploadURL | ||||||
| return client, nil | ||||||
| } | ||||||
| } | ||||||
| return restClient, nil // closing over client | ||||||
| } | ||||||
| getGQLClient := func(ctx context.Context) (*githubv4.Client, error) { | ||||||
| if tokenVal := ctx.Value(githubTokenKey{}); tokenVal != nil { | ||||||
| if token, ok := tokenVal.(string); ok && token != "" { | ||||||
| httpClient := &http.Client{ | ||||||
| Transport: &bearerAuthTransport{ | ||||||
| transport: http.DefaultTransport, | ||||||
| token: token, | ||||||
| }, | ||||||
| } | ||||||
| if gqlHTTPClient.Transport != nil { | ||||||
| if uaTransport, ok := gqlHTTPClient.Transport.(*userAgentTransport); ok { | ||||||
| httpClient.Transport = &userAgentTransport{ | ||||||
| transport: httpClient.Transport, | ||||||
| agent: uaTransport.agent, | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| return githubv4.NewEnterpriseClient(apiHost.graphqlURL.String(), httpClient), nil | ||||||
| } | ||||||
| } | ||||||
| return gqlClient, nil // closing over client | ||||||
| } | ||||||
| @@ -155,6 +184,46 @@ func NewMCPServer(cfg MCPServerConfig) (*server.MCPServer, error) { | ||||||
| return ghServer, nil | ||||||
| } | ||||||
| type githubTokenKey struct{} | ||||||
| type HTTPServerConfig struct { | ||||||
| // Version of the server | ||||||
| Version string | ||||||
| // GitHub Host to target for API requests (e.g. github.com or github.enterprise.com) | ||||||
| Host string | ||||||
| // GitHub Token to authenticate with the GitHub API (optional for HTTP mode with OAuth) | ||||||
| Token string | ||||||
| // EnabledToolsets is a list of toolsets to enable | ||||||
| // See: https://github.com/github/github-mcp-server?tab=readme-ov-file#tool-configuration | ||||||
| EnabledToolsets []string | ||||||
| // Whether to enable dynamic toolsets | ||||||
| // See: https://github.com/github/github-mcp-server?tab=readme-ov-file#dynamic-tool-discovery | ||||||
| DynamicToolsets bool | ||||||
| // ReadOnly indicates if we should only register read-only tools | ||||||
| ReadOnly bool | ||||||
| // ExportTranslations indicates if we should export translations | ||||||
| // See: https://github.com/github/github-mcp-server?tab=readme-ov-file#i18n--overriding-descriptions | ||||||
| ExportTranslations bool | ||||||
| // EnableCommandLogging indicates if we should log commands | ||||||
| EnableCommandLogging bool | ||||||
| // Path to the log file if not stderr | ||||||
| LogFilePath string | ||||||
| // Content window size | ||||||
| ContentWindowSize int | ||||||
| // Port to listen on for HTTP server | ||||||
| Port int | ||||||
| } | ||||||
| type StdioServerConfig struct { | ||||||
| // Version of the server | ||||||
| Version string | ||||||
| @@ -190,6 +259,77 @@ type StdioServerConfig struct { | ||||||
| ContentWindowSize int | ||||||
| } | ||||||
| func RunHTTPServer(cfg HTTPServerConfig) error { | ||||||
| ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) | ||||||
| defer stop() | ||||||
| t, dumpTranslations := translations.TranslationHelper() | ||||||
| ghServer, err := NewMCPServer(MCPServerConfig{ | ||||||
| Version: cfg.Version, | ||||||
| Host: cfg.Host, | ||||||
| Token: cfg.Token, | ||||||
| EnabledToolsets: cfg.EnabledToolsets, | ||||||
| DynamicToolsets: cfg.DynamicToolsets, | ||||||
| ReadOnly: cfg.ReadOnly, | ||||||
| Translator: t, | ||||||
| ContentWindowSize: cfg.ContentWindowSize, | ||||||
| }) | ||||||
| if err != nil { | ||||||
| return fmt.Errorf("failed to create MCP server: %w", err) | ||||||
| } | ||||||
| logrusLogger := logrus.New() | ||||||
| if cfg.LogFilePath != "" { | ||||||
| file, err := os.OpenFile(cfg.LogFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600) | ||||||
CopilotAI | ||||||
| file,err:=os.OpenFile(cfg.LogFilePath,os.O_CREATE|os.O_WRONLY|os.O_APPEND,0600) | |
| file,err:=os.OpenFile(cfg.LogFilePath,os.O_CREATE|os.O_WRONLY|os.O_APPEND,0640) |