feat: add multi-server support via GITEA_SERVERS env var
Allows connecting to multiple Gitea instances from a single MCP server.
Set GITEA_SERVERS as a JSON object mapping names to {host, token, insecure}
configs. Every tool gets an optional "server" parameter, and a new
"list_servers" tool is added to discover available connections.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,5 +3,7 @@ package context
|
||||
type contextKey string
|
||||
|
||||
const (
|
||||
TokenContextKey = contextKey("token")
|
||||
TokenContextKey = contextKey("token")
|
||||
HostContextKey = contextKey("host")
|
||||
InsecureContextKey = contextKey("insecure")
|
||||
)
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
package flag
|
||||
|
||||
// ServerConfig holds connection details for a named Gitea server.
|
||||
type ServerConfig struct {
|
||||
Host string `json:"host"`
|
||||
Token string `json:"token"`
|
||||
Insecure bool `json:"insecure,omitempty"`
|
||||
}
|
||||
|
||||
var (
|
||||
Host string
|
||||
Port int
|
||||
@@ -10,4 +17,8 @@ var (
|
||||
Insecure bool
|
||||
ReadOnly bool
|
||||
Debug bool
|
||||
|
||||
// Servers maps a friendly name to its connection config.
|
||||
// Populated from the GITEA_SERVERS env var (JSON object).
|
||||
Servers map[string]ServerConfig
|
||||
)
|
||||
|
||||
+24
-9
@@ -12,16 +12,16 @@ import (
|
||||
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
||||
)
|
||||
|
||||
func NewClient(token string) (*gitea.Client, error) {
|
||||
func newClientForHost(host, token string, insecure bool) (*gitea.Client, error) {
|
||||
httpClient := &http.Client{
|
||||
Transport: http.DefaultTransport,
|
||||
Transport: http.DefaultTransport.(*http.Transport).Clone(),
|
||||
CheckRedirect: checkRedirect,
|
||||
}
|
||||
|
||||
opts := []gitea.ClientOption{
|
||||
gitea.SetToken(token),
|
||||
}
|
||||
if flag.Insecure {
|
||||
if insecure {
|
||||
httpClient.Transport.(*http.Transport).TLSClientConfig = &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
}
|
||||
@@ -30,16 +30,20 @@ func NewClient(token string) (*gitea.Client, error) {
|
||||
if flag.Debug {
|
||||
opts = append(opts, gitea.SetDebugMode())
|
||||
}
|
||||
client, err := gitea.NewClient(flag.Host, opts...)
|
||||
client, err := gitea.NewClient(host, opts...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create gitea client err: %w", err)
|
||||
}
|
||||
|
||||
// Set user agent for the client
|
||||
client.SetUserAgent("gitea-mcp-server/" + flag.Version)
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// NewClient creates a Gitea client using the default host and the given token.
|
||||
func NewClient(token string) (*gitea.Client, error) {
|
||||
return newClientForHost(flag.Host, token, flag.Insecure)
|
||||
}
|
||||
|
||||
// checkRedirect prevents Go from silently changing mutating requests (POST, PATCH, etc.)
|
||||
// to GET when following 301/302/303 redirects, which would drop the request body and
|
||||
// make writes appear to succeed when they didn't.
|
||||
@@ -53,10 +57,21 @@ func checkRedirect(_ *http.Request, via []*http.Request) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ClientFromContext creates a Gitea client using connection details from the
|
||||
// context (set by the multi-server wrapper) or falling back to global defaults.
|
||||
func ClientFromContext(ctx context.Context) (*gitea.Client, error) {
|
||||
token, ok := ctx.Value(mcpContext.TokenContextKey).(string)
|
||||
if !ok {
|
||||
token = flag.Token
|
||||
host := flag.Host
|
||||
token := flag.Token
|
||||
insecure := flag.Insecure
|
||||
|
||||
if h, ok := ctx.Value(mcpContext.HostContextKey).(string); ok && h != "" {
|
||||
host = h
|
||||
}
|
||||
return NewClient(token)
|
||||
if t, ok := ctx.Value(mcpContext.TokenContextKey).(string); ok && t != "" {
|
||||
token = t
|
||||
}
|
||||
if i, ok := ctx.Value(mcpContext.InsecureContextKey).(bool); ok {
|
||||
insecure = i
|
||||
}
|
||||
return newClientForHost(host, token, insecure)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user