package notification import ( "context" "errors" "fmt" "git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea" "git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log" "git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/params" "git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to" "git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/tool" gitea_sdk "code.gitea.io/sdk/gitea" "github.com/mark3labs/mcp-go/mcp" "github.com/mark3labs/mcp-go/server" ) var Tool = tool.New() const ( ListNotificationsToolName = "list_notifications" CheckNewNotificationsToolName = "check_new_notifications" GetNotificationToolName = "get_notification" ReadNotificationToolName = "read_notification" ReadAllNotificationsToolName = "read_all_notifications" ListRepoNotificationsToolName = "list_repo_notifications" ReadRepoNotificationsToolName = "read_repo_notifications" ) var ( ListNotificationsTool = mcp.NewTool( ListNotificationsToolName, mcp.WithDescription("List the authenticated user's notifications"), mcp.WithString("status", mcp.Description("filter by status: read, unread, pinned")), mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)), mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)), ) CheckNewNotificationsTool = mcp.NewTool( CheckNewNotificationsToolName, mcp.WithDescription("Check if there are new notifications"), ) GetNotificationTool = mcp.NewTool( GetNotificationToolName, mcp.WithDescription("Get a notification thread by ID"), mcp.WithNumber("id", mcp.Required(), mcp.Description("notification thread ID")), ) ReadNotificationTool = mcp.NewTool( ReadNotificationToolName, mcp.WithDescription("Mark a notification as read"), mcp.WithNumber("id", mcp.Required(), mcp.Description("notification thread ID")), ) ReadAllNotificationsTool = mcp.NewTool( ReadAllNotificationsToolName, mcp.WithDescription("Mark all notifications as read"), ) ListRepoNotificationsTool = mcp.NewTool( ListRepoNotificationsToolName, mcp.WithDescription("List notifications for a repository"), mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")), mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")), mcp.WithString("status", mcp.Description("filter by status: read, unread, pinned")), mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)), mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)), ) ReadRepoNotificationsTool = mcp.NewTool( ReadRepoNotificationsToolName, mcp.WithDescription("Mark all notifications in a repository as read"), mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")), mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")), ) ) func init() { Tool.RegisterRead(server.ServerTool{Tool: ListNotificationsTool, Handler: ListNotificationsFn}) Tool.RegisterRead(server.ServerTool{Tool: CheckNewNotificationsTool, Handler: CheckNewNotificationsFn}) Tool.RegisterRead(server.ServerTool{Tool: GetNotificationTool, Handler: GetNotificationFn}) Tool.RegisterRead(server.ServerTool{Tool: ListRepoNotificationsTool, Handler: ListRepoNotificationsFn}) Tool.RegisterWrite(server.ServerTool{Tool: ReadNotificationTool, Handler: ReadNotificationFn}) Tool.RegisterWrite(server.ServerTool{Tool: ReadAllNotificationsTool, Handler: ReadAllNotificationsFn}) Tool.RegisterWrite(server.ServerTool{Tool: ReadRepoNotificationsTool, Handler: ReadRepoNotificationsFn}) } func ListNotificationsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { log.Debugf("Called ListNotificationsFn") page := params.GetOptionalInt(req.GetArguments(), "page", 1) pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100) opt := gitea_sdk.ListNotificationOptions{ ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)}, } if v, ok := req.GetArguments()["status"].(string); ok { opt.Status = []gitea_sdk.NotifyStatus{gitea_sdk.NotifyStatus(v)} } client, err := gitea.ClientFromContext(ctx) if err != nil { return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err)) } notifications, _, err := client.ListNotifications(opt) if err != nil { return to.ErrorResult(fmt.Errorf("list notifications err: %v", err)) } return to.TextResult(notifications) } func CheckNewNotificationsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { log.Debugf("Called CheckNewNotificationsFn") client, err := gitea.ClientFromContext(ctx) if err != nil { return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err)) } count, _, err := client.CheckNotifications() if err != nil { return to.ErrorResult(fmt.Errorf("check notifications err: %v", err)) } return to.TextResult(map[string]int64{"new_notifications": count}) } func GetNotificationFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { log.Debugf("Called GetNotificationFn") id, err := params.GetIndex(req.GetArguments(), "id") if err != nil { return to.ErrorResult(err) } client, err := gitea.ClientFromContext(ctx) if err != nil { return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err)) } notification, _, err := client.GetNotification(id) if err != nil { return to.ErrorResult(fmt.Errorf("get notification %d err: %v", id, err)) } return to.TextResult(notification) } func ReadNotificationFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { log.Debugf("Called ReadNotificationFn") id, err := params.GetIndex(req.GetArguments(), "id") if err != nil { return to.ErrorResult(err) } client, err := gitea.ClientFromContext(ctx) if err != nil { return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err)) } notification, _, err := client.ReadNotification(id) if err != nil { return to.ErrorResult(fmt.Errorf("read notification %d err: %v", id, err)) } return to.TextResult(notification) } func ReadAllNotificationsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { log.Debugf("Called ReadAllNotificationsFn") client, err := gitea.ClientFromContext(ctx) if err != nil { return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err)) } notifications, _, err := client.ReadNotifications(gitea_sdk.MarkNotificationOptions{}) if err != nil { return to.ErrorResult(fmt.Errorf("read all notifications err: %v", err)) } return to.TextResult(notifications) } func ListRepoNotificationsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { log.Debugf("Called ListRepoNotificationsFn") owner, ok := req.GetArguments()["owner"].(string) if !ok { return to.ErrorResult(errors.New("owner is required")) } repo, ok := req.GetArguments()["repo"].(string) if !ok { return to.ErrorResult(errors.New("repo is required")) } page := params.GetOptionalInt(req.GetArguments(), "page", 1) pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100) opt := gitea_sdk.ListNotificationOptions{ ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)}, } if v, ok := req.GetArguments()["status"].(string); ok { opt.Status = []gitea_sdk.NotifyStatus{gitea_sdk.NotifyStatus(v)} } client, err := gitea.ClientFromContext(ctx) if err != nil { return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err)) } notifications, _, err := client.ListRepoNotifications(owner, repo, opt) if err != nil { return to.ErrorResult(fmt.Errorf("list %s/%s notifications err: %v", owner, repo, err)) } return to.TextResult(notifications) } func ReadRepoNotificationsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { log.Debugf("Called ReadRepoNotificationsFn") owner, ok := req.GetArguments()["owner"].(string) if !ok { return to.ErrorResult(errors.New("owner is required")) } repo, ok := req.GetArguments()["repo"].(string) if !ok { return to.ErrorResult(errors.New("repo is required")) } client, err := gitea.ClientFromContext(ctx) if err != nil { return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err)) } notifications, _, err := client.ReadRepoNotifications(owner, repo, gitea_sdk.MarkNotificationOptions{}) if err != nil { return to.ErrorResult(fmt.Errorf("read %s/%s notifications err: %v", owner, repo, err)) } return to.TextResult(notifications) }