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:
+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