forgejo-mcp/operation/user/user.go
Byte Flavour 78916f6ef1
feat(notifications): implement complete notification API
- added get_notification_thread
- added mark_notification_read
- added mark_all_notifications_read
- added list_repo_notifications
- added mark_repo_notifications_read
- added mock tests for all notification endpoints
- updated README.md and added demo documentation
2026-03-08 10:25:22 +01:00

61 lines
1.7 KiB
Go

package user
import (
"context"
"fmt"
"time"
"codeberg.org/goern/forgejo-mcp/v2/pkg/forgejo"
"codeberg.org/goern/forgejo-mcp/v2/pkg/log"
"codeberg.org/goern/forgejo-mcp/v2/pkg/to"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
)
const (
GetMyUserInfoToolName = "get_my_user_info"
)
var (
GetMyUserInfoTool = mcp.NewTool(
GetMyUserInfoToolName,
mcp.WithDescription("Get user info"),
)
)
func RegisterTool(s *server.MCPServer) {
s.AddTool(GetMyUserInfoTool, GetUserInfoFn)
s.AddTool(CheckNotificationsTool, CheckNotificationsFn)
s.AddTool(GetNotificationThreadTool, GetNotificationThreadFn)
s.AddTool(MarkNotificationReadTool, MarkNotificationReadFn)
s.AddTool(MarkAllNotificationsReadTool, MarkAllNotificationsReadFn)
s.AddTool(ListRepoNotificationsTool, ListRepoNotificationsFn)
s.AddTool(MarkRepoNotificationsReadTool, MarkRepoNotificationsReadFn)
}
func GetUserInfoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
ctx, _ = log.WithMCPContext(ctx, GetMyUserInfoToolName)
start := time.Now()
log.LogMCPToolStart(ctx, GetMyUserInfoToolName, map[string]interface{}{})
user, resp, err := forgejo.Client().GetMyUserInfo()
duration := time.Since(start)
// Log API call details — guard resp against nil (e.g. network error)
statusCode := 0
if resp != nil {
statusCode = resp.StatusCode
}
forgejo.LogAPICall(ctx, "GET", "/user", duration, statusCode, err)
if err != nil {
log.LogMCPToolError(ctx, GetMyUserInfoToolName, duration, err)
return to.ErrorResult(fmt.Errorf("get user info err: %v", err))
}
log.LogMCPToolComplete(ctx, GetMyUserInfoToolName, duration, fmt.Sprintf("Retrieved info for user: %s", user.UserName))
return to.TextResult(user)
}