mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-03-15 09:25:12 +00:00
Reduce token usage by slimming tool responses. Instead of returning full Gitea SDK objects (with nested user/repo objects, avatars, permissions, etc.), each operation now has a colocated `slim.go` that extracts only the fields an LLM needs. List endpoints return even fewer fields than single-item endpoints.
Other changes:
- Add `params` helpers to DRY parameter extraction across 40+ handlers
- Remove `{"Result": ...}` wrapper for flatter responses
- Reduce default pageSize from 100 to 30
Fixes: https://gitea.com/gitea/gitea-mcp/issues/128
*Created by Claude on behalf of @silverwind*
Reviewed-on: https://gitea.com/gitea/gitea-mcp/pulls/141
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
Co-committed-by: silverwind <me@silverwind.io>
151 lines
4.2 KiB
Go
151 lines
4.2 KiB
Go
package repo
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"gitea.com/gitea/gitea-mcp/pkg/gitea"
|
|
"gitea.com/gitea/gitea-mcp/pkg/log"
|
|
"gitea.com/gitea/gitea-mcp/pkg/params"
|
|
"gitea.com/gitea/gitea-mcp/pkg/to"
|
|
|
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
"github.com/mark3labs/mcp-go/server"
|
|
)
|
|
|
|
const (
|
|
CreateBranchToolName = "create_branch"
|
|
DeleteBranchToolName = "delete_branch"
|
|
ListBranchesToolName = "list_branches"
|
|
)
|
|
|
|
var (
|
|
CreateBranchTool = mcp.NewTool(
|
|
CreateBranchToolName,
|
|
mcp.WithDescription("Create branch"),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
|
mcp.WithString("branch", mcp.Required(), mcp.Description("Name of the branch to create")),
|
|
mcp.WithString("old_branch", mcp.Required(), mcp.Description("Name of the old branch to create from")),
|
|
)
|
|
|
|
DeleteBranchTool = mcp.NewTool(
|
|
DeleteBranchToolName,
|
|
mcp.WithDescription("Delete branch"),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
|
mcp.WithString("branch", mcp.Required(), mcp.Description("Name of the branch to delete")),
|
|
)
|
|
|
|
ListBranchesTool = mcp.NewTool(
|
|
ListBranchesToolName,
|
|
mcp.WithDescription("List branches"),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
|
)
|
|
)
|
|
|
|
func init() {
|
|
Tool.RegisterWrite(server.ServerTool{
|
|
Tool: CreateBranchTool,
|
|
Handler: CreateBranchFn,
|
|
})
|
|
Tool.RegisterWrite(server.ServerTool{
|
|
Tool: DeleteBranchTool,
|
|
Handler: DeleteBranchFn,
|
|
})
|
|
Tool.RegisterRead(server.ServerTool{
|
|
Tool: ListBranchesTool,
|
|
Handler: ListBranchesFn,
|
|
})
|
|
}
|
|
|
|
func CreateBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
log.Debugf("Called CreateBranchFn")
|
|
args := req.GetArguments()
|
|
owner, err := params.GetString(args, "owner")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
repo, err := params.GetString(args, "repo")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
branch, err := params.GetString(args, "branch")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
oldBranch, _ := args["old_branch"].(string)
|
|
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
_, _, err = client.CreateBranch(owner, repo, gitea_sdk.CreateBranchOption{
|
|
BranchName: branch,
|
|
OldBranchName: oldBranch,
|
|
})
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("create branch error: %v", err))
|
|
}
|
|
|
|
return mcp.NewToolResultText("Branch Created"), nil
|
|
}
|
|
|
|
func DeleteBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
log.Debugf("Called DeleteBranchFn")
|
|
args := req.GetArguments()
|
|
owner, err := params.GetString(args, "owner")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
repo, err := params.GetString(args, "repo")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
branch, err := params.GetString(args, "branch")
|
|
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))
|
|
}
|
|
_, _, err = client.DeleteRepoBranch(owner, repo, branch)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("delete branch error: %v", err))
|
|
}
|
|
|
|
return to.TextResult("Branch Deleted")
|
|
}
|
|
|
|
func ListBranchesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
log.Debugf("Called ListBranchesFn")
|
|
args := req.GetArguments()
|
|
owner, err := params.GetString(args, "owner")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
repo, err := params.GetString(args, "repo")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
opt := gitea_sdk.ListRepoBranchesOptions{
|
|
ListOptions: gitea_sdk.ListOptions{
|
|
Page: 1,
|
|
PageSize: 30,
|
|
},
|
|
}
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
branches, _, err := client.ListRepoBranches(owner, repo, opt)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("list branches error: %v", err))
|
|
}
|
|
|
|
return to.TextResult(slimBranches(branches))
|
|
}
|