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:
+95
-36
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sort"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
@@ -28,51 +29,109 @@ import (
|
||||
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
||||
"gitea.com/gitea/gitea-mcp/pkg/log"
|
||||
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
"github.com/mark3labs/mcp-go/server"
|
||||
)
|
||||
|
||||
var mcpServer *server.MCPServer
|
||||
|
||||
// serverNames returns a sorted list of configured server names for use in
|
||||
// enum validation and the list_servers tool.
|
||||
func serverNames() []string {
|
||||
names := make([]string, 0, len(flag.Servers))
|
||||
for name := range flag.Servers {
|
||||
names = append(names, name)
|
||||
}
|
||||
// Deterministic ordering for tool schemas.
|
||||
sort.Strings(names)
|
||||
return names
|
||||
}
|
||||
|
||||
// injectServerParam adds an optional "server" parameter to a tool and wraps
|
||||
// its handler so the chosen server's host/token/insecure are injected into
|
||||
// the context before the original handler runs.
|
||||
func injectServerParam(tools []server.ServerTool) []server.ServerTool {
|
||||
names := serverNames()
|
||||
for i, st := range tools {
|
||||
// Add "server" to the JSON Schema properties.
|
||||
if st.Tool.InputSchema.Properties == nil {
|
||||
st.Tool.InputSchema.Properties = make(map[string]any)
|
||||
}
|
||||
st.Tool.InputSchema.Properties["server"] = map[string]any{
|
||||
"type": "string",
|
||||
"description": "Target Gitea server name. Available: " + strings.Join(names, ", ") + ". Omit to use the default server.",
|
||||
"enum": names,
|
||||
}
|
||||
tools[i].Tool = st.Tool
|
||||
|
||||
// Wrap the handler to resolve server config → context values.
|
||||
origHandler := st.Handler
|
||||
tools[i].Handler = func(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
if serverName, ok := req.GetArguments()["server"].(string); ok && serverName != "" {
|
||||
cfg, exists := flag.Servers[serverName]
|
||||
if !exists {
|
||||
return mcp.NewToolResultText("unknown server: " + serverName + ". Available: " + strings.Join(serverNames(), ", ")), nil
|
||||
}
|
||||
ctx = context.WithValue(ctx, mcpContext.HostContextKey, cfg.Host)
|
||||
ctx = context.WithValue(ctx, mcpContext.TokenContextKey, cfg.Token)
|
||||
ctx = context.WithValue(ctx, mcpContext.InsecureContextKey, cfg.Insecure)
|
||||
}
|
||||
return origHandler(ctx, req)
|
||||
}
|
||||
}
|
||||
return tools
|
||||
}
|
||||
|
||||
// listServersFn handles the "list_servers" tool — returns the configured servers.
|
||||
func listServersFn(_ context.Context, _ mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
if len(flag.Servers) == 0 {
|
||||
defaultInfo := fmt.Sprintf("default: %s (no additional servers configured)", flag.Host)
|
||||
return mcp.NewToolResultText(defaultInfo), nil
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString(fmt.Sprintf("default: %s\n", flag.Host))
|
||||
for _, name := range serverNames() {
|
||||
cfg := flag.Servers[name]
|
||||
b.WriteString(fmt.Sprintf("%s: %s", name, cfg.Host))
|
||||
if cfg.Insecure {
|
||||
b.WriteString(" (insecure)")
|
||||
}
|
||||
b.WriteString("\n")
|
||||
}
|
||||
return mcp.NewToolResultText(b.String()), nil
|
||||
}
|
||||
|
||||
func RegisterTool(s *server.MCPServer) {
|
||||
// User Tool
|
||||
s.AddTools(user.Tool.Tools()...)
|
||||
// Collect all tools from every domain package.
|
||||
allTools := make([]server.ServerTool, 0, 200)
|
||||
allTools = append(allTools, user.Tool.Tools()...)
|
||||
allTools = append(allTools, actions.Tool.Tools()...)
|
||||
allTools = append(allTools, repo.Tool.Tools()...)
|
||||
allTools = append(allTools, notification.Tool.Tools()...)
|
||||
allTools = append(allTools, issue.Tool.Tools()...)
|
||||
allTools = append(allTools, label.Tool.Tools()...)
|
||||
allTools = append(allTools, milestone.Tool.Tools()...)
|
||||
allTools = append(allTools, pull.Tool.Tools()...)
|
||||
allTools = append(allTools, search.Tool.Tools()...)
|
||||
allTools = append(allTools, version.Tool.Tools()...)
|
||||
allTools = append(allTools, wiki.Tool.Tools()...)
|
||||
allTools = append(allTools, timetracking.Tool.Tools()...)
|
||||
allTools = append(allTools, org.Tool.Tools()...)
|
||||
|
||||
// Actions Tool
|
||||
s.AddTools(actions.Tool.Tools()...)
|
||||
// If multiple servers are configured, inject the "server" parameter into every tool.
|
||||
if len(flag.Servers) > 0 {
|
||||
allTools = injectServerParam(allTools)
|
||||
|
||||
// Repo Tool
|
||||
s.AddTools(repo.Tool.Tools()...)
|
||||
|
||||
// Notification Tool
|
||||
s.AddTools(notification.Tool.Tools()...)
|
||||
|
||||
// Issue Tool
|
||||
s.AddTools(issue.Tool.Tools()...)
|
||||
|
||||
// Label Tool
|
||||
s.AddTools(label.Tool.Tools()...)
|
||||
|
||||
// Milestone Tool
|
||||
s.AddTools(milestone.Tool.Tools()...)
|
||||
|
||||
// Pull Tool
|
||||
s.AddTools(pull.Tool.Tools()...)
|
||||
|
||||
// Search Tool
|
||||
s.AddTools(search.Tool.Tools()...)
|
||||
|
||||
// Version Tool
|
||||
s.AddTools(version.Tool.Tools()...)
|
||||
|
||||
// Wiki Tool
|
||||
s.AddTools(wiki.Tool.Tools()...)
|
||||
|
||||
// Time Tracking Tool
|
||||
s.AddTools(timetracking.Tool.Tools()...)
|
||||
|
||||
// Organization Tool
|
||||
s.AddTools(org.Tool.Tools()...)
|
||||
// Add a list_servers meta-tool so callers can discover available servers.
|
||||
allTools = append(allTools, server.ServerTool{
|
||||
Tool: mcp.NewTool("list_servers",
|
||||
mcp.WithDescription("List all configured Gitea server connections"),
|
||||
),
|
||||
Handler: listServersFn,
|
||||
})
|
||||
}
|
||||
|
||||
s.AddTools(allTools...)
|
||||
s.DeleteTools("")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user