feat: expand MCP tool coverage from 93 to 299 tools
Some checks failed
release-nightly / release-image (push) Failing after 2m17s
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.
This commit is contained in:
319
operation/organization/org.go
Normal file
319
operation/organization/org.go
Normal file
@@ -0,0 +1,319 @@
|
||||
package organization
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"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"
|
||||
)
|
||||
|
||||
var Tool = tool.New()
|
||||
|
||||
const (
|
||||
ListOrgsToolName = "list_orgs"
|
||||
GetOrgToolName = "get_org"
|
||||
CreateOrgToolName = "create_org"
|
||||
EditOrgToolName = "edit_org"
|
||||
DeleteOrgToolName = "delete_org"
|
||||
RenameOrgToolName = "rename_org"
|
||||
ListOrgReposToolName = "list_org_repos"
|
||||
CreateOrgRepoToolName = "create_org_repo"
|
||||
)
|
||||
|
||||
var (
|
||||
ListOrgsTool = mcp.NewTool(
|
||||
ListOrgsToolName,
|
||||
mcp.WithDescription("List all visible organizations"),
|
||||
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||
)
|
||||
|
||||
GetOrgTool = mcp.NewTool(
|
||||
GetOrgToolName,
|
||||
mcp.WithDescription("Get an organization by name"),
|
||||
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||
)
|
||||
|
||||
CreateOrgTool = mcp.NewTool(
|
||||
CreateOrgToolName,
|
||||
mcp.WithDescription("Create an organization"),
|
||||
mcp.WithString("username", mcp.Required(), mcp.Description("organization username")),
|
||||
mcp.WithString("full_name", mcp.Description("organization full name")),
|
||||
mcp.WithString("description", mcp.Description("organization description")),
|
||||
mcp.WithString("website", mcp.Description("organization website")),
|
||||
mcp.WithString("location", mcp.Description("organization location")),
|
||||
mcp.WithString("visibility", mcp.Description("visibility: public, limited, or private"), mcp.DefaultString("public")),
|
||||
)
|
||||
|
||||
EditOrgTool = mcp.NewTool(
|
||||
EditOrgToolName,
|
||||
mcp.WithDescription("Edit an organization"),
|
||||
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||
mcp.WithString("full_name", mcp.Description("organization full name")),
|
||||
mcp.WithString("description", mcp.Description("organization description")),
|
||||
mcp.WithString("website", mcp.Description("organization website")),
|
||||
mcp.WithString("location", mcp.Description("organization location")),
|
||||
mcp.WithString("visibility", mcp.Description("visibility: public, limited, or private")),
|
||||
)
|
||||
|
||||
DeleteOrgTool = mcp.NewTool(
|
||||
DeleteOrgToolName,
|
||||
mcp.WithDescription("Delete an organization"),
|
||||
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||
)
|
||||
|
||||
RenameOrgTool = mcp.NewTool(
|
||||
RenameOrgToolName,
|
||||
mcp.WithDescription("Rename an organization"),
|
||||
mcp.WithString("org", mcp.Required(), mcp.Description("current organization name")),
|
||||
mcp.WithString("new_name", mcp.Required(), mcp.Description("new organization name")),
|
||||
)
|
||||
|
||||
ListOrgReposTool = mcp.NewTool(
|
||||
ListOrgReposToolName,
|
||||
mcp.WithDescription("List an organization's repositories"),
|
||||
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
||||
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
||||
)
|
||||
|
||||
CreateOrgRepoTool = mcp.NewTool(
|
||||
CreateOrgRepoToolName,
|
||||
mcp.WithDescription("Create a repository in an organization"),
|
||||
mcp.WithString("org", mcp.Required(), mcp.Description("organization name")),
|
||||
mcp.WithString("name", mcp.Required(), mcp.Description("repository name")),
|
||||
mcp.WithString("description", mcp.Description("repository description")),
|
||||
mcp.WithBoolean("private", mcp.Description("whether the repository is private")),
|
||||
mcp.WithBoolean("auto_init", mcp.Description("whether to auto-initialize with README")),
|
||||
mcp.WithString("default_branch", mcp.Description("default branch name")),
|
||||
mcp.WithString("gitignores", mcp.Description("gitignore template")),
|
||||
mcp.WithString("license", mcp.Description("license template")),
|
||||
)
|
||||
)
|
||||
|
||||
func init() {
|
||||
Tool.RegisterRead(server.ServerTool{Tool: ListOrgsTool, Handler: ListOrgsFn})
|
||||
Tool.RegisterRead(server.ServerTool{Tool: GetOrgTool, Handler: GetOrgFn})
|
||||
Tool.RegisterRead(server.ServerTool{Tool: ListOrgReposTool, Handler: ListOrgReposFn})
|
||||
Tool.RegisterWrite(server.ServerTool{Tool: CreateOrgTool, Handler: CreateOrgFn})
|
||||
Tool.RegisterWrite(server.ServerTool{Tool: EditOrgTool, Handler: EditOrgFn})
|
||||
Tool.RegisterWrite(server.ServerTool{Tool: DeleteOrgTool, Handler: DeleteOrgFn})
|
||||
Tool.RegisterWrite(server.ServerTool{Tool: RenameOrgTool, Handler: RenameOrgFn})
|
||||
Tool.RegisterWrite(server.ServerTool{Tool: CreateOrgRepoTool, Handler: CreateOrgRepoFn})
|
||||
}
|
||||
|
||||
func ListOrgsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called ListOrgsFn")
|
||||
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||
client, err := gitea.ClientFromContext(ctx)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||
}
|
||||
orgs, _, err := client.ListOrgs(gitea_sdk.ListOrgsOptions{
|
||||
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||
})
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("list orgs err: %v", err))
|
||||
}
|
||||
return to.TextResult(orgs)
|
||||
}
|
||||
|
||||
func GetOrgFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called GetOrgFn")
|
||||
org, ok := req.GetArguments()["org"].(string)
|
||||
if !ok {
|
||||
return to.ErrorResult(errors.New("org is required"))
|
||||
}
|
||||
client, err := gitea.ClientFromContext(ctx)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||
}
|
||||
organization, _, err := client.GetOrg(org)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("get org %s err: %v", org, err))
|
||||
}
|
||||
return to.TextResult(organization)
|
||||
}
|
||||
|
||||
func CreateOrgFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called CreateOrgFn")
|
||||
username, ok := req.GetArguments()["username"].(string)
|
||||
if !ok {
|
||||
return to.ErrorResult(errors.New("username is required"))
|
||||
}
|
||||
opt := gitea_sdk.CreateOrgOption{
|
||||
Name: username,
|
||||
}
|
||||
if v, ok := req.GetArguments()["full_name"].(string); ok {
|
||||
opt.FullName = v
|
||||
}
|
||||
if v, ok := req.GetArguments()["description"].(string); ok {
|
||||
opt.Description = v
|
||||
}
|
||||
if v, ok := req.GetArguments()["website"].(string); ok {
|
||||
opt.Website = v
|
||||
}
|
||||
if v, ok := req.GetArguments()["location"].(string); ok {
|
||||
opt.Location = v
|
||||
}
|
||||
if v, ok := req.GetArguments()["visibility"].(string); ok {
|
||||
opt.Visibility = gitea_sdk.VisibleType(v)
|
||||
}
|
||||
client, err := gitea.ClientFromContext(ctx)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||
}
|
||||
organization, _, err := client.CreateOrg(opt)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("create org %s err: %v", username, err))
|
||||
}
|
||||
return to.TextResult(organization)
|
||||
}
|
||||
|
||||
func EditOrgFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called EditOrgFn")
|
||||
org, ok := req.GetArguments()["org"].(string)
|
||||
if !ok {
|
||||
return to.ErrorResult(errors.New("org is required"))
|
||||
}
|
||||
opt := gitea_sdk.EditOrgOption{}
|
||||
if v, ok := req.GetArguments()["full_name"].(string); ok {
|
||||
opt.FullName = v
|
||||
}
|
||||
if v, ok := req.GetArguments()["description"].(string); ok {
|
||||
opt.Description = v
|
||||
}
|
||||
if v, ok := req.GetArguments()["website"].(string); ok {
|
||||
opt.Website = v
|
||||
}
|
||||
if v, ok := req.GetArguments()["location"].(string); ok {
|
||||
opt.Location = v
|
||||
}
|
||||
if v, ok := req.GetArguments()["visibility"].(string); ok {
|
||||
opt.Visibility = gitea_sdk.VisibleType(v)
|
||||
}
|
||||
client, err := gitea.ClientFromContext(ctx)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||
}
|
||||
_, err = client.EditOrg(org, opt)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("edit org %s err: %v", org, err))
|
||||
}
|
||||
updated, _, err := client.GetOrg(org)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("get org %s after edit err: %v", org, err))
|
||||
}
|
||||
return to.TextResult(updated)
|
||||
}
|
||||
|
||||
func DeleteOrgFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called DeleteOrgFn")
|
||||
org, ok := req.GetArguments()["org"].(string)
|
||||
if !ok {
|
||||
return to.ErrorResult(errors.New("org is required"))
|
||||
}
|
||||
client, err := gitea.ClientFromContext(ctx)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||
}
|
||||
_, err = client.DeleteOrg(org)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("delete org %s err: %v", org, err))
|
||||
}
|
||||
return to.TextResult(map[string]string{"status": "deleted", "org": org})
|
||||
}
|
||||
|
||||
func RenameOrgFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called RenameOrgFn")
|
||||
org, ok := req.GetArguments()["org"].(string)
|
||||
if !ok {
|
||||
return to.ErrorResult(errors.New("org is required"))
|
||||
}
|
||||
newName, ok := req.GetArguments()["new_name"].(string)
|
||||
if !ok {
|
||||
return to.ErrorResult(errors.New("new_name is required"))
|
||||
}
|
||||
client, err := gitea.ClientFromContext(ctx)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||
}
|
||||
_, err = client.RenameOrg(org, gitea_sdk.RenameOrgOption{NewName: newName})
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("rename org %s to %s err: %v", org, newName, err))
|
||||
}
|
||||
return to.TextResult(map[string]string{"status": "renamed", "old_name": org, "new_name": newName})
|
||||
}
|
||||
|
||||
func ListOrgReposFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called ListOrgReposFn")
|
||||
org, ok := req.GetArguments()["org"].(string)
|
||||
if !ok {
|
||||
return to.ErrorResult(errors.New("org is required"))
|
||||
}
|
||||
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
||||
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
||||
client, err := gitea.ClientFromContext(ctx)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||
}
|
||||
repos, _, err := client.ListOrgRepos(org, gitea_sdk.ListOrgReposOptions{
|
||||
ListOptions: gitea_sdk.ListOptions{Page: int(page), PageSize: int(pageSize)},
|
||||
})
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("list org %s repos err: %v", org, err))
|
||||
}
|
||||
return to.TextResult(repos)
|
||||
}
|
||||
|
||||
func CreateOrgRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called CreateOrgRepoFn")
|
||||
org, ok := req.GetArguments()["org"].(string)
|
||||
if !ok {
|
||||
return to.ErrorResult(errors.New("org is required"))
|
||||
}
|
||||
name, ok := req.GetArguments()["name"].(string)
|
||||
if !ok {
|
||||
return to.ErrorResult(errors.New("name is required"))
|
||||
}
|
||||
opt := gitea_sdk.CreateRepoOption{
|
||||
Name: name,
|
||||
}
|
||||
if v, ok := req.GetArguments()["description"].(string); ok {
|
||||
opt.Description = v
|
||||
}
|
||||
if v, ok := req.GetArguments()["private"].(bool); ok {
|
||||
opt.Private = v
|
||||
}
|
||||
if v, ok := req.GetArguments()["auto_init"].(bool); ok {
|
||||
opt.AutoInit = v
|
||||
}
|
||||
if v, ok := req.GetArguments()["default_branch"].(string); ok {
|
||||
opt.DefaultBranch = v
|
||||
}
|
||||
if v, ok := req.GetArguments()["gitignores"].(string); ok {
|
||||
opt.Gitignores = v
|
||||
}
|
||||
if v, ok := req.GetArguments()["license"].(string); ok {
|
||||
opt.License = v
|
||||
}
|
||||
client, err := gitea.ClientFromContext(ctx)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||
}
|
||||
repo, _, err := client.CreateOrgRepo(org, opt)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("create repo %s/%s err: %v", org, name, err))
|
||||
}
|
||||
return to.TextResult(repo)
|
||||
}
|
||||
Reference in New Issue
Block a user