mirror of
				https://gitea.com/gitea/gitea-mcp.git
				synced 2025-10-30 01:41:50 +00:00 
			
		
		
		
	Update
This commit is contained in:
		| @@ -2,10 +2,10 @@ package issue | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"encoding/json" |  | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  |  | ||||||
| 	"gitea.com/gitea/gitea-mcp/pkg/gitea" | 	"gitea.com/gitea/gitea-mcp/pkg/gitea" | ||||||
|  | 	"gitea.com/gitea/gitea-mcp/pkg/to" | ||||||
| 	"github.com/mark3labs/mcp-go/mcp" | 	"github.com/mark3labs/mcp-go/mcp" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| @@ -16,6 +16,10 @@ const ( | |||||||
| var ( | var ( | ||||||
| 	GetIssueByIndexTool = mcp.NewTool( | 	GetIssueByIndexTool = mcp.NewTool( | ||||||
| 		GetIssueByIndexToolName, | 		GetIssueByIndexToolName, | ||||||
|  | 		GetIssueByIndexOpt..., | ||||||
|  | 	) | ||||||
|  |  | ||||||
|  | 	GetIssueByIndexOpt = []mcp.ToolOption{ | ||||||
| 		mcp.WithDescription("get issue by index"), | 		mcp.WithDescription("get issue by index"), | ||||||
| 		mcp.WithString( | 		mcp.WithString( | ||||||
| 			"owner", | 			"owner", | ||||||
| @@ -32,21 +36,17 @@ var ( | |||||||
| 			mcp.Required(), | 			mcp.Required(), | ||||||
| 			mcp.Description("repository issue index"), | 			mcp.Description("repository issue index"), | ||||||
| 		), | 		), | ||||||
| 	) | 	} | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func GetIssueByIndexFn(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | func GetIssueByIndexFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||||||
| 	owner := request.Params.Arguments["owner"].(string) | 	owner := req.Params.Arguments["owner"].(string) | ||||||
| 	repo := request.Params.Arguments["repo"].(string) | 	repo := req.Params.Arguments["repo"].(string) | ||||||
| 	index := request.Params.Arguments["index"].(float64) | 	index := req.Params.Arguments["index"].(float64) | ||||||
| 	issue, _, err := gitea.Client().GetIssue(owner, repo, int64(index)) | 	issue, _, err := gitea.Client().GetIssue(owner, repo, int64(index)) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return mcp.NewToolResultError(fmt.Sprintf("get %v/%v/issue/%v err", owner, repo, int64(index))), err | 		return mcp.NewToolResultError(fmt.Sprintf("get %v/%v/issue/%v err", owner, repo, int64(index))), err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	result, err := json.Marshal(issue) | 	return to.TextResult(issue) | ||||||
| 	if err != nil { |  | ||||||
| 		return mcp.NewToolResultError("marshal issue err"), err |  | ||||||
| 	} |  | ||||||
| 	return mcp.NewToolResultText(string(result)), nil |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -21,6 +21,7 @@ func RegisterTool(s *server.MCPServer) { | |||||||
| 	s.AddTool(user.GetMyUserInfoTool, user.GetUserInfoFn) | 	s.AddTool(user.GetMyUserInfoTool, user.GetUserInfoFn) | ||||||
|  |  | ||||||
| 	// Repo Tool | 	// Repo Tool | ||||||
|  | 	s.AddTool(repo.CreateRepoTool, repo.CreateRepoFn) | ||||||
| 	s.AddTool(repo.ListMyReposTool, repo.ListMyReposFn) | 	s.AddTool(repo.ListMyReposTool, repo.ListMyReposFn) | ||||||
|  |  | ||||||
| 	// Issue Tool | 	// Issue Tool | ||||||
|   | |||||||
| @@ -2,20 +2,44 @@ package repo | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"encoding/json" |  | ||||||
|  |  | ||||||
| 	"code.gitea.io/sdk/gitea" | 	"code.gitea.io/sdk/gitea" | ||||||
| 	giteaPkg "gitea.com/gitea/gitea-mcp/pkg/gitea" | 	giteaPkg "gitea.com/gitea/gitea-mcp/pkg/gitea" | ||||||
|  | 	"gitea.com/gitea/gitea-mcp/pkg/to" | ||||||
| 	"github.com/mark3labs/mcp-go/mcp" | 	"github.com/mark3labs/mcp-go/mcp" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| const ( | const ( | ||||||
|  | 	CreateRepoToolName  = "create_repo" | ||||||
| 	ListMyReposToolName = "list_my_repos" | 	ListMyReposToolName = "list_my_repos" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| var ( | var ( | ||||||
|  | 	CreateRepoTool = mcp.NewTool( | ||||||
|  | 		CreateRepoToolName, | ||||||
|  | 		CreateRepoOpt..., | ||||||
|  | 	) | ||||||
|  |  | ||||||
|  | 	CreateRepoOpt = []mcp.ToolOption{ | ||||||
|  | 		mcp.WithDescription("Create repository"), | ||||||
|  | 		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.WithBoolean("private", mcp.Description("Whether the repository is private")), | ||||||
|  | 		mcp.WithString("issue_labels", mcp.Description("Issue Label set to use")), | ||||||
|  | 		mcp.WithBoolean("auto_init", mcp.Description("Whether the repository should be auto-intialized?")), | ||||||
|  | 		mcp.WithBoolean("template", mcp.Description("Whether the repository is template")), | ||||||
|  | 		mcp.WithString("gitignores", mcp.Description("Gitignores to use")), | ||||||
|  | 		mcp.WithString("license", mcp.Description("License to use")), | ||||||
|  | 		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)")), | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	ListMyReposTool = mcp.NewTool( | 	ListMyReposTool = mcp.NewTool( | ||||||
| 		ListMyReposToolName, | 		ListMyReposToolName, | ||||||
|  | 		ListMyReposOpt..., | ||||||
|  | 	) | ||||||
|  |  | ||||||
|  | 	ListMyReposOpt = []mcp.ToolOption{ | ||||||
| 		mcp.WithDescription("List my repositories"), | 		mcp.WithDescription("List my repositories"), | ||||||
| 		mcp.WithNumber( | 		mcp.WithNumber( | ||||||
| 			"page", | 			"page", | ||||||
| @@ -29,15 +53,46 @@ var ( | |||||||
| 			mcp.DefaultNumber(10), | 			mcp.DefaultNumber(10), | ||||||
| 			mcp.Min(1), | 			mcp.Min(1), | ||||||
| 		), | 		), | ||||||
| 	) | 	} | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func ListMyReposFn(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | func CreateRepoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||||||
| 	page, ok := request.Params.Arguments["page"].(float64) | 	name := req.Params.Arguments["name"].(string) | ||||||
|  | 	description := req.Params.Arguments["description"].(string) | ||||||
|  | 	private := req.Params.Arguments["private"].(bool) | ||||||
|  | 	issueLabels := req.Params.Arguments["issue_labels"].(string) | ||||||
|  | 	autoInit := req.Params.Arguments["auto_init"].(bool) | ||||||
|  | 	template := req.Params.Arguments["template"].(bool) | ||||||
|  | 	gitignores := req.Params.Arguments["gitignores"].(string) | ||||||
|  | 	license := req.Params.Arguments["license"].(string) | ||||||
|  | 	readme := req.Params.Arguments["readme"].(string) | ||||||
|  | 	defaultBranch := req.Params.Arguments["default_branch"].(string) | ||||||
|  |  | ||||||
|  | 	opt := gitea.CreateRepoOption{ | ||||||
|  | 		Name:          name, | ||||||
|  | 		Description:   description, | ||||||
|  | 		Private:       private, | ||||||
|  | 		IssueLabels:   issueLabels, | ||||||
|  | 		AutoInit:      autoInit, | ||||||
|  | 		Template:      template, | ||||||
|  | 		Gitignores:    gitignores, | ||||||
|  | 		License:       license, | ||||||
|  | 		Readme:        readme, | ||||||
|  | 		DefaultBranch: defaultBranch, | ||||||
|  | 	} | ||||||
|  | 	repo, _, err := giteaPkg.Client().CreateRepo(opt) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return nil, err | ||||||
|  | 	} | ||||||
|  | 	return to.TextResult(repo) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func ListMyReposFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||||||
|  | 	page, ok := req.Params.Arguments["page"].(float64) | ||||||
| 	if !ok { | 	if !ok { | ||||||
| 		return mcp.NewToolResultError("get page number error"), nil | 		return mcp.NewToolResultError("get page number error"), nil | ||||||
| 	} | 	} | ||||||
| 	size, ok := request.Params.Arguments["pageSize"].(float64) | 	size, ok := req.Params.Arguments["pageSize"].(float64) | ||||||
| 	if !ok { | 	if !ok { | ||||||
| 		return mcp.NewToolResultError("get page size number error"), nil | 		return mcp.NewToolResultError("get page size number error"), nil | ||||||
| 	} | 	} | ||||||
| @@ -52,9 +107,5 @@ func ListMyReposFn(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallT | |||||||
| 		return mcp.NewToolResultError("List my repositories error"), err | 		return mcp.NewToolResultError("List my repositories error"), err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	result, err := json.Marshal(repos) | 	return to.TextResult(repos) | ||||||
| 	if err != nil { |  | ||||||
| 		return mcp.NewToolResultError("marshal repository list error"), err |  | ||||||
| 	} |  | ||||||
| 	return mcp.NewToolResultText(string(result)), nil |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -2,9 +2,9 @@ package user | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"encoding/json" |  | ||||||
|  |  | ||||||
| 	"gitea.com/gitea/gitea-mcp/pkg/gitea" | 	"gitea.com/gitea/gitea-mcp/pkg/gitea" | ||||||
|  | 	"gitea.com/gitea/gitea-mcp/pkg/to" | ||||||
|  |  | ||||||
| 	"github.com/mark3labs/mcp-go/mcp" | 	"github.com/mark3labs/mcp-go/mcp" | ||||||
| ) | ) | ||||||
| @@ -20,15 +20,11 @@ var ( | |||||||
| 	) | 	) | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func GetUserInfoFn(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | func GetUserInfoFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||||||
| 	user, _, err := gitea.Client().GetMyUserInfo() | 	user, _, err := gitea.Client().GetMyUserInfo() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return mcp.NewToolResultError("Get My User Info Error"), err | 		return mcp.NewToolResultError("Get My User Info Error"), err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	result, err := json.Marshal(user) | 	return to.TextResult(user) | ||||||
| 	if err != nil { |  | ||||||
| 		return mcp.NewToolResultError("marshal my user info error"), err |  | ||||||
| 	} |  | ||||||
| 	return mcp.NewToolResultText(string(result)), nil |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -5,6 +5,7 @@ import ( | |||||||
| 	"fmt" | 	"fmt" | ||||||
|  |  | ||||||
| 	"gitea.com/gitea/gitea-mcp/pkg/flag" | 	"gitea.com/gitea/gitea-mcp/pkg/flag" | ||||||
|  | 	"gitea.com/gitea/gitea-mcp/pkg/to" | ||||||
| 	"github.com/mark3labs/mcp-go/mcp" | 	"github.com/mark3labs/mcp-go/mcp" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| @@ -19,10 +20,10 @@ var ( | |||||||
| 	) | 	) | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func GetGiteaMCPServerVersionFn(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | func GetGiteaMCPServerVersionFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||||||
| 	version := flag.Version | 	version := flag.Version | ||||||
| 	if version == "" { | 	if version == "" { | ||||||
| 		version = "dev" | 		version = "dev" | ||||||
| 	} | 	} | ||||||
| 	return mcp.NewToolResultText(fmt.Sprintf("Gitea MCP Server version: %v", version)), nil | 	return to.TextResult(fmt.Sprintf("Gitea MCP Server version: %v", version)) | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										15
									
								
								pkg/to/to.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								pkg/to/to.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | |||||||
|  | package to | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"encoding/json" | ||||||
|  |  | ||||||
|  | 	"github.com/mark3labs/mcp-go/mcp" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | func TextResult(v any) (*mcp.CallToolResult, error) { | ||||||
|  | 	result, err := json.Marshal(v) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return mcp.NewToolResultError("marshal result error"), err | ||||||
|  | 	} | ||||||
|  | 	return mcp.NewToolResultText(string(result)), nil | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user