Some checks failed
release-nightly / release-image (push) Failing after 2m17s
Fork the official gitea-mcp and massively extend API coverage: - Organization: orgs, members, teams, hooks, blocks, activity (34 tools) - User: profile, followers, keys, emails, repos, blocks (30 tools) - Repository: collaborators, webhooks, branch/tag protection, deploy keys, topics, git refs/trees/notes, commit status, stars/watchers, forks, transfers, mirrors, templates (53 tools) - Issue: reactions, pins, subscriptions, timeline, templates (16 tools) - Notifications: list, check, read, repo-scoped (7 tools) - Settings: API, attachment, repo, UI settings (4 tools) - Packages: list, get, delete, files, latest, link/unlink (7 tools) - Miscellaneous: server version, gitignore/label/license templates, markdown/markup rendering, node info, signing keys (12 tools) - Admin: user/org/repo CRUD, system webhooks, cron tasks, unadopted repos, emails, badges (23 tools) Module path updated to git.lethalbits.com/lethalbits/gitea-mcp.
182 lines
4.7 KiB
Go
182 lines
4.7 KiB
Go
package operation
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/operation/actions"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/operation/admin"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/operation/issue"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/operation/label"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/operation/milestone"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/operation/miscellaneous"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/operation/notification"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/operation/organization"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/operation/packages"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/operation/pull"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/operation/repo"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/operation/search"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/operation/settings"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/operation/timetracking"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/operation/user"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/operation/version"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/operation/wiki"
|
|
mcpContext "git.lethalbits.com/lethalbits/gitea-mcp/pkg/context"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/pkg/flag"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/pkg/log"
|
|
|
|
"github.com/mark3labs/mcp-go/server"
|
|
)
|
|
|
|
var mcpServer *server.MCPServer
|
|
|
|
func RegisterTool(s *server.MCPServer) {
|
|
// User Tool
|
|
s.AddTools(user.Tool.Tools()...)
|
|
|
|
// Actions Tool
|
|
s.AddTools(actions.Tool.Tools()...)
|
|
|
|
// Repo Tool
|
|
s.AddTools(repo.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(organization.Tool.Tools()...)
|
|
|
|
// Notification Tool
|
|
s.AddTools(notification.Tool.Tools()...)
|
|
|
|
// Settings Tool
|
|
s.AddTools(settings.Tool.Tools()...)
|
|
|
|
// Package Tool
|
|
s.AddTools(packages.Tool.Tools()...)
|
|
|
|
// Miscellaneous Tool
|
|
s.AddTools(miscellaneous.Tool.Tools()...)
|
|
|
|
// Admin Tool
|
|
s.AddTools(admin.Tool.Tools()...)
|
|
|
|
s.DeleteTools("")
|
|
}
|
|
|
|
// parseAuthToken extracts the token from an Authorization header.
|
|
// Supports "Bearer <token>" (case-insensitive per RFC 7235) and
|
|
// Gitea-style "token <token>" formats.
|
|
// Returns the token and true if valid, empty string and false otherwise.
|
|
func parseAuthToken(authHeader string) (string, bool) {
|
|
if len(authHeader) > 7 && strings.EqualFold(authHeader[:7], "Bearer ") {
|
|
token := strings.TrimSpace(authHeader[7:])
|
|
if token != "" {
|
|
return token, true
|
|
}
|
|
}
|
|
if len(authHeader) > 6 && strings.EqualFold(authHeader[:6], "token ") {
|
|
token := strings.TrimSpace(authHeader[6:])
|
|
if token != "" {
|
|
return token, true
|
|
}
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
func getContextWithToken(ctx context.Context, r *http.Request) context.Context {
|
|
authHeader := r.Header.Get("Authorization")
|
|
if authHeader == "" {
|
|
return ctx
|
|
}
|
|
|
|
token, ok := parseAuthToken(authHeader)
|
|
if !ok {
|
|
return ctx
|
|
}
|
|
|
|
return context.WithValue(ctx, mcpContext.TokenContextKey, token)
|
|
}
|
|
|
|
func Run() error {
|
|
mcpServer = newMCPServer(flag.Version)
|
|
RegisterTool(mcpServer)
|
|
switch flag.Mode {
|
|
case "stdio":
|
|
if err := server.ServeStdio(
|
|
mcpServer,
|
|
); err != nil {
|
|
return err
|
|
}
|
|
case "http":
|
|
httpServer := server.NewStreamableHTTPServer(
|
|
mcpServer,
|
|
server.WithLogger(log.New()),
|
|
server.WithHeartbeatInterval(30*time.Second),
|
|
server.WithHTTPContextFunc(getContextWithToken),
|
|
)
|
|
log.Infof("Gitea MCP HTTP server listening on :%d", flag.Port)
|
|
|
|
// Graceful shutdown setup
|
|
sigCh := make(chan os.Signal, 1)
|
|
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
|
|
shutdownDone := make(chan struct{})
|
|
|
|
go func() {
|
|
<-sigCh
|
|
log.Infof("Shutdown signal received, gracefully stopping HTTP server...")
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if err := httpServer.Shutdown(shutdownCtx); err != nil {
|
|
log.Errorf("HTTP server shutdown error: %v", err)
|
|
}
|
|
close(shutdownDone)
|
|
}()
|
|
|
|
if err := httpServer.Start(fmt.Sprintf(":%d", flag.Port)); err != nil {
|
|
return err
|
|
}
|
|
<-shutdownDone // Wait for shutdown to finish
|
|
default:
|
|
return fmt.Errorf("invalid transport type: %s. Must be 'stdio' or 'http'", flag.Mode)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func newMCPServer(version string) *server.MCPServer {
|
|
return server.NewMCPServer(
|
|
"Gitea MCP Server",
|
|
version,
|
|
server.WithToolCapabilities(true),
|
|
server.WithLogging(),
|
|
server.WithRecovery(),
|
|
)
|
|
}
|