mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2025-09-13 08:23:15 +00:00
Compare commits
3 Commits
pathc2
...
dc3e120e97
Author | SHA1 | Date | |
---|---|---|---|
|
dc3e120e97 | ||
|
f33b04a3df | ||
|
ba07925969 |
@@ -64,7 +64,7 @@ var (
|
|||||||
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||||
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path")),
|
mcp.WithString("filePath", mcp.Required(), mcp.Description("file path")),
|
||||||
mcp.WithString("sha", mcp.Required(), mcp.Description("sha is the SHA for the file that already exists")),
|
mcp.WithString("sha", mcp.Required(), mcp.Description("sha is the SHA for the file that already exists")),
|
||||||
mcp.WithString("content", mcp.Required(), mcp.Description("file content, base64 encoded")),
|
mcp.WithString("content", mcp.Required(), mcp.Description("file content")),
|
||||||
mcp.WithString("message", mcp.Required(), mcp.Description("commit message")),
|
mcp.WithString("message", mcp.Required(), mcp.Description("commit message")),
|
||||||
mcp.WithString("branch_name", mcp.Required(), mcp.Description("branch name")),
|
mcp.WithString("branch_name", mcp.Required(), mcp.Description("branch name")),
|
||||||
)
|
)
|
||||||
|
@@ -27,7 +27,7 @@ const (
|
|||||||
var (
|
var (
|
||||||
CreateRepoTool = mcp.NewTool(
|
CreateRepoTool = mcp.NewTool(
|
||||||
CreateRepoToolName,
|
CreateRepoToolName,
|
||||||
mcp.WithDescription("Create repository"),
|
mcp.WithDescription("Create repository in personal account or organization"),
|
||||||
mcp.WithString("name", mcp.Required(), mcp.Description("Name of the repository to create")),
|
mcp.WithString("name", mcp.Required(), mcp.Description("Name of the repository to create")),
|
||||||
mcp.WithString("description", mcp.Description("Description of the repository to create")),
|
mcp.WithString("description", mcp.Description("Description of the repository to create")),
|
||||||
mcp.WithBoolean("private", mcp.Description("Whether the repository is private")),
|
mcp.WithBoolean("private", mcp.Description("Whether the repository is private")),
|
||||||
@@ -38,6 +38,7 @@ var (
|
|||||||
mcp.WithString("license", mcp.Description("License to use")),
|
mcp.WithString("license", mcp.Description("License to use")),
|
||||||
mcp.WithString("readme", mcp.Description("Readme of the repository to create")),
|
mcp.WithString("readme", mcp.Description("Readme of the repository to create")),
|
||||||
mcp.WithString("default_branch", mcp.Description("DefaultBranch of the repository (used when initializes and in template)")),
|
mcp.WithString("default_branch", mcp.Description("DefaultBranch of the repository (used when initializes and in template)")),
|
||||||
|
mcp.WithString("organization", mcp.Description("Organization name to create repository in (optional - defaults to personal account)")),
|
||||||
)
|
)
|
||||||
|
|
||||||
ForkRepoTool = mcp.NewTool(
|
ForkRepoTool = mcp.NewTool(
|
||||||
@@ -120,6 +121,7 @@ func CreateRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolRe
|
|||||||
license, _ := req.GetArguments()["license"].(string)
|
license, _ := req.GetArguments()["license"].(string)
|
||||||
readme, _ := req.GetArguments()["readme"].(string)
|
readme, _ := req.GetArguments()["readme"].(string)
|
||||||
defaultBranch, _ := req.GetArguments()["default_branch"].(string)
|
defaultBranch, _ := req.GetArguments()["default_branch"].(string)
|
||||||
|
organization, _ := req.GetArguments()["organization"].(string)
|
||||||
|
|
||||||
opt := gitea_sdk.CreateRepoOption{
|
opt := gitea_sdk.CreateRepoOption{
|
||||||
Name: name,
|
Name: name,
|
||||||
@@ -133,9 +135,19 @@ func CreateRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolRe
|
|||||||
Readme: readme,
|
Readme: readme,
|
||||||
DefaultBranch: defaultBranch,
|
DefaultBranch: defaultBranch,
|
||||||
}
|
}
|
||||||
repo, _, err := gitea.Client().CreateRepo(opt)
|
|
||||||
if err != nil {
|
var repo *gitea_sdk.Repository
|
||||||
return to.ErrorResult(fmt.Errorf("create repo err: %v", err))
|
var err error
|
||||||
|
if organization != "" {
|
||||||
|
repo, _, err = gitea.Client().CreateOrgRepo(organization, opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create organization repository '%s' in '%s' err: %v", name, organization, err))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
repo, _, err = gitea.Client().CreateRepo(opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("create repository '%s' err: %v", name, err))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return to.TextResult(repo)
|
return to.TextResult(repo)
|
||||||
}
|
}
|
||||||
|
@@ -15,68 +15,94 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
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"
|
GetMyUserInfoToolName = "get_my_user_info"
|
||||||
GetUserOrgsToolName = "get_user_orgs"
|
// 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 Tool = tool.New()
|
||||||
|
|
||||||
var (
|
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(
|
GetMyUserInfoTool = mcp.NewTool(
|
||||||
GetMyUserInfoToolName,
|
GetMyUserInfoToolName,
|
||||||
mcp.WithDescription("Get my user info"),
|
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(
|
GetUserOrgsTool = mcp.NewTool(
|
||||||
GetUserOrgsToolName,
|
GetUserOrgsToolName,
|
||||||
mcp.WithDescription("Get organizations associated with the authenticated user"),
|
mcp.WithDescription("Get organizations associated with the authenticated user"),
|
||||||
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(defaultPage)),
|
||||||
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
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() {
|
func init() {
|
||||||
Tool.RegisterRead(server.ServerTool{
|
registerTools()
|
||||||
Tool: GetMyUserInfoTool,
|
|
||||||
Handler: GetUserInfoFn,
|
|
||||||
})
|
|
||||||
|
|
||||||
Tool.RegisterRead(server.ServerTool{
|
|
||||||
Tool: GetUserOrgsTool,
|
|
||||||
Handler: GetUserOrgsFn,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
val, ok := req.GetArguments()[name].(float64)
|
||||||
|
if !ok || val < 1 {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
return int(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
func GetUserInfoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called GetUserInfoFn")
|
log.Debugf("[User] Called GetUserInfoFn")
|
||||||
user, _, err := gitea.Client().GetMyUserInfo()
|
user, _, err := gitea.Client().GetMyUserInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("get user info err: %v", err))
|
return to.ErrorResult(fmt.Errorf("get user info err: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
return to.TextResult(user)
|
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) {
|
func GetUserOrgsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Debugf("Called GetUserOrgsFn")
|
log.Debugf("[User] Called GetUserOrgsFn")
|
||||||
page, ok := req.GetArguments()["page"].(float64)
|
page := getIntArg(req, "page", defaultPage)
|
||||||
if !ok || page < 1 {
|
pageSize := getIntArg(req, "pageSize", defaultPageSize)
|
||||||
page = 1
|
|
||||||
}
|
|
||||||
pageSize, ok := req.GetArguments()["pageSize"].(float64)
|
|
||||||
if !ok || pageSize < 1 {
|
|
||||||
pageSize = 100
|
|
||||||
}
|
|
||||||
opt := gitea_sdk.ListOrgsOptions{
|
opt := gitea_sdk.ListOrgsOptions{
|
||||||
ListOptions: gitea_sdk.ListOptions{
|
ListOptions: gitea_sdk.ListOptions{
|
||||||
Page: int(page),
|
Page: page,
|
||||||
PageSize: int(pageSize),
|
PageSize: pageSize,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
orgs, _, err := gitea.Client().ListMyOrgs(opt)
|
orgs, _, err := gitea.Client().ListMyOrgs(opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("get user orgs err: %v", err))
|
return to.ErrorResult(fmt.Errorf("get user orgs err: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
return to.TextResult(orgs)
|
return to.TextResult(orgs)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user