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.
118 lines
4.1 KiB
Go
118 lines
4.1 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/pkg/gitea"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/pkg/log"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/pkg/params"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/pkg/to"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/pkg/tool"
|
|
|
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
"github.com/mark3labs/mcp-go/server"
|
|
)
|
|
|
|
const (
|
|
// GetMyUserInfoToolName is the unique tool name used for MCP registration and lookup of the get_my_user_info command.
|
|
GetMyUserInfoToolName = "get_my_user_info"
|
|
// GetUserOrgsToolName is the unique tool name used for MCP registration and lookup of the get_user_orgs command.
|
|
GetUserOrgsToolName = "get_user_orgs"
|
|
|
|
// defaultPage is the default starting page number used for paginated organization listings.
|
|
defaultPage = 1
|
|
// defaultPageSize is the default number of organizations per page for paginated queries.
|
|
defaultPageSize = 100
|
|
)
|
|
|
|
// Tool is the MCP tool manager instance for registering all MCP tools in this package.
|
|
var Tool = tool.New()
|
|
|
|
var (
|
|
// GetMyUserInfoTool is the MCP tool for retrieving the current user's info.
|
|
// It is registered with a specific name and a description string.
|
|
GetMyUserInfoTool = mcp.NewTool(
|
|
GetMyUserInfoToolName,
|
|
mcp.WithDescription("Get my user info"),
|
|
)
|
|
|
|
// GetUserOrgsTool is the MCP tool for listing organizations for the authenticated user.
|
|
// It supports pagination via "page" and "pageSize" arguments with default values specified above.
|
|
GetUserOrgsTool = mcp.NewTool(
|
|
GetUserOrgsToolName,
|
|
mcp.WithDescription("Get organizations associated with the authenticated user"),
|
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(defaultPage)),
|
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(defaultPageSize)),
|
|
)
|
|
)
|
|
|
|
// init registers all MCP tools in Tool at package initialization.
|
|
// This function ensures the handler functions are registered before server usage.
|
|
func init() {
|
|
registerTools()
|
|
}
|
|
|
|
// registerTools registers all local MCP tool definitions and their handler functions.
|
|
// To add new functionality, append your tool/handler pair to the tools slice below.
|
|
func registerTools() {
|
|
tools := []server.ServerTool{
|
|
{Tool: GetMyUserInfoTool, Handler: GetUserInfoFn},
|
|
{Tool: GetUserOrgsTool, Handler: GetUserOrgsFn},
|
|
}
|
|
for _, t := range tools {
|
|
Tool.RegisterRead(t)
|
|
}
|
|
}
|
|
|
|
// getIntArg parses an integer argument from the MCP request arguments map.
|
|
// Returns def if missing, not a number, or less than 1. Used for pagination arguments.
|
|
func getIntArg(req mcp.CallToolRequest, name string, def int) int {
|
|
v := params.GetOptionalInt(req.GetArguments(), name, int64(def))
|
|
if v < 1 {
|
|
return def
|
|
}
|
|
return int(v)
|
|
}
|
|
|
|
// GetUserInfoFn is the handler for "get_my_user_info" MCP tool requests.
|
|
// Logs invocation, fetches current user info from gitea, wraps result for MCP.
|
|
func GetUserInfoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
log.Debugf("[User] Called GetUserInfoFn")
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
user, _, err := client.GetMyUserInfo()
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get user info err: %v", err))
|
|
}
|
|
return to.TextResult(user)
|
|
}
|
|
|
|
// GetUserOrgsFn is the handler for "get_user_orgs" MCP tool requests.
|
|
// Logs invocation, pulls validated pagination arguments from request,
|
|
// performs Gitea organization listing, and wraps the result for MCP.
|
|
func GetUserOrgsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
log.Debugf("[User] Called GetUserOrgsFn")
|
|
page := getIntArg(req, "page", defaultPage)
|
|
pageSize := getIntArg(req, "pageSize", defaultPageSize)
|
|
|
|
opt := gitea_sdk.ListOrgsOptions{
|
|
ListOptions: gitea_sdk.ListOptions{
|
|
Page: page,
|
|
PageSize: pageSize,
|
|
},
|
|
}
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
orgs, _, err := client.ListMyOrgs(opt)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get user orgs err: %v", err))
|
|
}
|
|
return to.TextResult(orgs)
|
|
}
|