mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-03-20 03:45:13 +00:00
Consolidate tools from 110 to 45 using method dispatch (#143)
Consolidate 110 individual MCP tools down to 45 using a method dispatch pattern, aligning tool names with the GitHub MCP server conventions. **Motivation:** LLMs work better with fewer, well-organized tools. The method dispatch pattern (used by GitHub's MCP server) groups related operations under read/write tools with a `method` parameter. **Changes:** - Group related tools into `_read`/`_write` pairs with method dispatch (e.g. `issue_read`, `issue_write`, `pull_request_read`, `pull_request_write`) - Rename tools to match GitHub MCP naming (`get_file_contents`, `create_or_update_file`, `list_issues`, `list_pull_requests`, etc.) - Rename `pageSize` to `perPage` for GitHub MCP compat - Move issue label ops (`add_labels`, `remove_label`, etc.) into `issue_write` - Merge `create_file`/`update_file` into `create_or_update_file` with optional `sha` - Make `delete_file` require `sha` - Add `get_labels` method to `issue_read` - Add shared helpers: `GetInt64Slice`, `GetStringSlice`, `GetPagination` in params package - Unexport all dispatch handler functions - Fix: pass assignees/milestone in `CreateIssue`, bounds check in `GetFileContent` Reviewed-on: https://gitea.com/gitea/gitea-mcp/pulls/143 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: silverwind <me@silverwind.io> Co-committed-by: silverwind <me@silverwind.io>
This commit is contained in:
@@ -4,9 +4,10 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"gitea.com/gitea/gitea-mcp/pkg/gitea"
|
||||
@@ -19,114 +20,88 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
ListRepoActionWorkflowsToolName = "list_repo_action_workflows"
|
||||
GetRepoActionWorkflowToolName = "get_repo_action_workflow"
|
||||
DispatchRepoActionWorkflowToolName = "dispatch_repo_action_workflow"
|
||||
|
||||
ListRepoActionRunsToolName = "list_repo_action_runs"
|
||||
GetRepoActionRunToolName = "get_repo_action_run"
|
||||
CancelRepoActionRunToolName = "cancel_repo_action_run"
|
||||
RerunRepoActionRunToolName = "rerun_repo_action_run"
|
||||
|
||||
ListRepoActionJobsToolName = "list_repo_action_jobs"
|
||||
ListRepoActionRunJobsToolName = "list_repo_action_run_jobs"
|
||||
ActionsRunReadToolName = "actions_run_read"
|
||||
ActionsRunWriteToolName = "actions_run_write"
|
||||
)
|
||||
|
||||
var (
|
||||
ListRepoActionWorkflowsTool = mcp.NewTool(
|
||||
ListRepoActionWorkflowsToolName,
|
||||
mcp.WithDescription("List repository Actions workflows"),
|
||||
ActionsRunReadTool = mcp.NewTool(
|
||||
ActionsRunReadToolName,
|
||||
mcp.WithDescription("Read Actions workflow, run, and job data. Use method 'list_workflows'/'get_workflow' for workflows, 'list_runs'/'get_run' for runs, 'list_jobs'/'list_run_jobs' for jobs, 'get_job_log_preview'/'download_job_log' for logs."),
|
||||
mcp.WithString("method", mcp.Required(), mcp.Description("operation to perform"), mcp.Enum("list_workflows", "get_workflow", "list_runs", "get_run", "list_jobs", "list_run_jobs", "get_job_log_preview", "download_job_log")),
|
||||
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||
mcp.WithString("workflow_id", mcp.Description("workflow ID or filename (required for 'get_workflow')")),
|
||||
mcp.WithNumber("run_id", mcp.Description("run ID (required for 'get_run', 'list_run_jobs')")),
|
||||
mcp.WithNumber("job_id", mcp.Description("job ID (required for 'get_job_log_preview', 'download_job_log')")),
|
||||
mcp.WithString("status", mcp.Description("optional status filter (for 'list_runs', 'list_jobs')")),
|
||||
mcp.WithNumber("tail_lines", mcp.Description("number of lines from end of log (for 'get_job_log_preview')"), mcp.DefaultNumber(200), mcp.Min(1)),
|
||||
mcp.WithNumber("max_bytes", mcp.Description("max bytes to return (for 'get_job_log_preview')"), mcp.DefaultNumber(65536), mcp.Min(1024)),
|
||||
mcp.WithString("output_path", mcp.Description("output file path (for 'download_job_log')")),
|
||||
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(30), mcp.Min(1)),
|
||||
mcp.WithNumber("perPage", mcp.Description("results per page"), mcp.DefaultNumber(30), mcp.Min(1)),
|
||||
)
|
||||
|
||||
GetRepoActionWorkflowTool = mcp.NewTool(
|
||||
GetRepoActionWorkflowToolName,
|
||||
mcp.WithDescription("Get a repository Actions workflow by ID"),
|
||||
ActionsRunWriteTool = mcp.NewTool(
|
||||
ActionsRunWriteToolName,
|
||||
mcp.WithDescription("Trigger, cancel, or rerun Actions workflows."),
|
||||
mcp.WithString("method", mcp.Required(), mcp.Description("operation to perform"), mcp.Enum("dispatch_workflow", "cancel_run", "rerun_run")),
|
||||
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||
mcp.WithString("workflow_id", mcp.Required(), mcp.Description("workflow ID or filename (e.g. 'my-workflow.yml')")),
|
||||
)
|
||||
|
||||
DispatchRepoActionWorkflowTool = mcp.NewTool(
|
||||
DispatchRepoActionWorkflowToolName,
|
||||
mcp.WithDescription("Trigger (dispatch) a repository Actions workflow"),
|
||||
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||
mcp.WithString("workflow_id", mcp.Required(), mcp.Description("workflow ID or filename (e.g. 'my-workflow.yml')")),
|
||||
mcp.WithString("ref", mcp.Required(), mcp.Description("git ref (branch or tag)")),
|
||||
mcp.WithObject("inputs", mcp.Description("workflow inputs object")),
|
||||
)
|
||||
|
||||
ListRepoActionRunsTool = mcp.NewTool(
|
||||
ListRepoActionRunsToolName,
|
||||
mcp.WithDescription("List repository Actions workflow runs"),
|
||||
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(30), mcp.Min(1)),
|
||||
mcp.WithString("status", mcp.Description("optional status filter")),
|
||||
)
|
||||
|
||||
GetRepoActionRunTool = mcp.NewTool(
|
||||
GetRepoActionRunToolName,
|
||||
mcp.WithDescription("Get a repository Actions run by ID"),
|
||||
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||
mcp.WithNumber("run_id", mcp.Required(), mcp.Description("run ID")),
|
||||
)
|
||||
|
||||
CancelRepoActionRunTool = mcp.NewTool(
|
||||
CancelRepoActionRunToolName,
|
||||
mcp.WithDescription("Cancel a repository Actions run"),
|
||||
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||
mcp.WithNumber("run_id", mcp.Required(), mcp.Description("run ID")),
|
||||
)
|
||||
|
||||
RerunRepoActionRunTool = mcp.NewTool(
|
||||
RerunRepoActionRunToolName,
|
||||
mcp.WithDescription("Rerun a repository Actions run"),
|
||||
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||
mcp.WithNumber("run_id", mcp.Required(), mcp.Description("run ID")),
|
||||
)
|
||||
|
||||
ListRepoActionJobsTool = mcp.NewTool(
|
||||
ListRepoActionJobsToolName,
|
||||
mcp.WithDescription("List repository Actions jobs"),
|
||||
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(30), mcp.Min(1)),
|
||||
mcp.WithString("status", mcp.Description("optional status filter")),
|
||||
)
|
||||
|
||||
ListRepoActionRunJobsTool = mcp.NewTool(
|
||||
ListRepoActionRunJobsToolName,
|
||||
mcp.WithDescription("List Actions jobs for a specific run"),
|
||||
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
||||
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
||||
mcp.WithNumber("run_id", mcp.Required(), mcp.Description("run ID")),
|
||||
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)),
|
||||
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(30), mcp.Min(1)),
|
||||
mcp.WithString("workflow_id", mcp.Description("workflow ID or filename (required for 'dispatch_workflow')")),
|
||||
mcp.WithString("ref", mcp.Description("git ref branch or tag (required for 'dispatch_workflow')")),
|
||||
mcp.WithObject("inputs", mcp.Description("workflow inputs object (for 'dispatch_workflow')")),
|
||||
mcp.WithNumber("run_id", mcp.Description("run ID (required for 'cancel_run', 'rerun_run')")),
|
||||
)
|
||||
)
|
||||
|
||||
func init() {
|
||||
Tool.RegisterRead(server.ServerTool{Tool: ListRepoActionWorkflowsTool, Handler: ListRepoActionWorkflowsFn})
|
||||
Tool.RegisterRead(server.ServerTool{Tool: GetRepoActionWorkflowTool, Handler: GetRepoActionWorkflowFn})
|
||||
Tool.RegisterWrite(server.ServerTool{Tool: DispatchRepoActionWorkflowTool, Handler: DispatchRepoActionWorkflowFn})
|
||||
Tool.RegisterRead(server.ServerTool{Tool: ActionsRunReadTool, Handler: runReadFn})
|
||||
Tool.RegisterWrite(server.ServerTool{Tool: ActionsRunWriteTool, Handler: runWriteFn})
|
||||
}
|
||||
|
||||
Tool.RegisterRead(server.ServerTool{Tool: ListRepoActionRunsTool, Handler: ListRepoActionRunsFn})
|
||||
Tool.RegisterRead(server.ServerTool{Tool: GetRepoActionRunTool, Handler: GetRepoActionRunFn})
|
||||
Tool.RegisterWrite(server.ServerTool{Tool: CancelRepoActionRunTool, Handler: CancelRepoActionRunFn})
|
||||
Tool.RegisterWrite(server.ServerTool{Tool: RerunRepoActionRunTool, Handler: RerunRepoActionRunFn})
|
||||
func runReadFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
method, err := params.GetString(req.GetArguments(), "method")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
switch method {
|
||||
case "list_workflows":
|
||||
return listRepoActionWorkflowsFn(ctx, req)
|
||||
case "get_workflow":
|
||||
return getRepoActionWorkflowFn(ctx, req)
|
||||
case "list_runs":
|
||||
return listRepoActionRunsFn(ctx, req)
|
||||
case "get_run":
|
||||
return getRepoActionRunFn(ctx, req)
|
||||
case "list_jobs":
|
||||
return listRepoActionJobsFn(ctx, req)
|
||||
case "list_run_jobs":
|
||||
return listRepoActionRunJobsFn(ctx, req)
|
||||
case "get_job_log_preview":
|
||||
return getRepoActionJobLogPreviewFn(ctx, req)
|
||||
case "download_job_log":
|
||||
return downloadRepoActionJobLogFn(ctx, req)
|
||||
default:
|
||||
return to.ErrorResult(fmt.Errorf("unknown method: %s", method))
|
||||
}
|
||||
}
|
||||
|
||||
Tool.RegisterRead(server.ServerTool{Tool: ListRepoActionJobsTool, Handler: ListRepoActionJobsFn})
|
||||
Tool.RegisterRead(server.ServerTool{Tool: ListRepoActionRunJobsTool, Handler: ListRepoActionRunJobsFn})
|
||||
func runWriteFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
method, err := params.GetString(req.GetArguments(), "method")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
switch method {
|
||||
case "dispatch_workflow":
|
||||
return dispatchRepoActionWorkflowFn(ctx, req)
|
||||
case "cancel_run":
|
||||
return cancelRepoActionRunFn(ctx, req)
|
||||
case "rerun_run":
|
||||
return rerunRepoActionRunFn(ctx, req)
|
||||
default:
|
||||
return to.ErrorResult(fmt.Errorf("unknown method: %s", method))
|
||||
}
|
||||
}
|
||||
|
||||
func doJSONWithFallback(ctx context.Context, method string, paths []string, query url.Values, body, respOut any) error {
|
||||
@@ -146,8 +121,8 @@ func doJSONWithFallback(ctx context.Context, method string, paths []string, quer
|
||||
return lastErr
|
||||
}
|
||||
|
||||
func ListRepoActionWorkflowsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called ListRepoActionWorkflowsFn")
|
||||
func listRepoActionWorkflowsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called listRepoActionWorkflowsFn")
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil || owner == "" {
|
||||
return to.ErrorResult(errors.New("owner is required"))
|
||||
@@ -174,8 +149,8 @@ func ListRepoActionWorkflowsFn(ctx context.Context, req mcp.CallToolRequest) (*m
|
||||
return to.TextResult(slimActionWorkflows(result))
|
||||
}
|
||||
|
||||
func GetRepoActionWorkflowFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called GetRepoActionWorkflowFn")
|
||||
func getRepoActionWorkflowFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called getRepoActionWorkflowFn")
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil || owner == "" {
|
||||
return to.ErrorResult(errors.New("owner is required"))
|
||||
@@ -202,8 +177,8 @@ func GetRepoActionWorkflowFn(ctx context.Context, req mcp.CallToolRequest) (*mcp
|
||||
return to.TextResult(slimActionWorkflow(result))
|
||||
}
|
||||
|
||||
func DispatchRepoActionWorkflowFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called DispatchRepoActionWorkflowFn")
|
||||
func dispatchRepoActionWorkflowFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called dispatchRepoActionWorkflowFn")
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil || owner == "" {
|
||||
return to.ErrorResult(errors.New("owner is required"))
|
||||
@@ -225,9 +200,6 @@ func DispatchRepoActionWorkflowFn(ctx context.Context, req mcp.CallToolRequest)
|
||||
if raw, exists := req.GetArguments()["inputs"]; exists {
|
||||
if m, ok := raw.(map[string]any); ok {
|
||||
inputs = m
|
||||
} else if m, ok := raw.(map[string]any); ok {
|
||||
inputs = make(map[string]any, len(m))
|
||||
maps.Copy(inputs, m)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -255,8 +227,8 @@ func DispatchRepoActionWorkflowFn(ctx context.Context, req mcp.CallToolRequest)
|
||||
return to.TextResult(map[string]any{"message": "workflow dispatched"})
|
||||
}
|
||||
|
||||
func ListRepoActionRunsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called ListRepoActionRunsFn")
|
||||
func listRepoActionRunsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called listRepoActionRunsFn")
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil || owner == "" {
|
||||
return to.ErrorResult(errors.New("owner is required"))
|
||||
@@ -288,8 +260,8 @@ func ListRepoActionRunsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.Ca
|
||||
return to.TextResult(slimActionRuns(result))
|
||||
}
|
||||
|
||||
func GetRepoActionRunFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called GetRepoActionRunFn")
|
||||
func getRepoActionRunFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called getRepoActionRunFn")
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil || owner == "" {
|
||||
return to.ErrorResult(errors.New("owner is required"))
|
||||
@@ -316,8 +288,8 @@ func GetRepoActionRunFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.Call
|
||||
return to.TextResult(slimActionRun(result))
|
||||
}
|
||||
|
||||
func CancelRepoActionRunFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called CancelRepoActionRunFn")
|
||||
func cancelRepoActionRunFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called cancelRepoActionRunFn")
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil || owner == "" {
|
||||
return to.ErrorResult(errors.New("owner is required"))
|
||||
@@ -343,8 +315,8 @@ func CancelRepoActionRunFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.C
|
||||
return to.TextResult(map[string]any{"message": "run cancellation requested"})
|
||||
}
|
||||
|
||||
func RerunRepoActionRunFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called RerunRepoActionRunFn")
|
||||
func rerunRepoActionRunFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called rerunRepoActionRunFn")
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil || owner == "" {
|
||||
return to.ErrorResult(errors.New("owner is required"))
|
||||
@@ -375,8 +347,8 @@ func RerunRepoActionRunFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.Ca
|
||||
return to.TextResult(map[string]any{"message": "run rerun requested"})
|
||||
}
|
||||
|
||||
func ListRepoActionJobsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called ListRepoActionJobsFn")
|
||||
func listRepoActionJobsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called listRepoActionJobsFn")
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil || owner == "" {
|
||||
return to.ErrorResult(errors.New("owner is required"))
|
||||
@@ -408,8 +380,8 @@ func ListRepoActionJobsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.Ca
|
||||
return to.TextResult(slimActionJobs(result))
|
||||
}
|
||||
|
||||
func ListRepoActionRunJobsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called ListRepoActionRunJobsFn")
|
||||
func listRepoActionRunJobsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called listRepoActionRunJobsFn")
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil || owner == "" {
|
||||
return to.ErrorResult(errors.New("owner is required"))
|
||||
@@ -440,3 +412,138 @@ func ListRepoActionRunJobsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp
|
||||
}
|
||||
return to.TextResult(slimActionJobs(result))
|
||||
}
|
||||
|
||||
// Log functions (merged from logs.go)
|
||||
|
||||
func logPaths(owner, repo string, jobID int64) []string {
|
||||
return []string{
|
||||
fmt.Sprintf("repos/%s/%s/actions/jobs/%d/logs", url.PathEscape(owner), url.PathEscape(repo), jobID),
|
||||
fmt.Sprintf("repos/%s/%s/actions/jobs/%d/log", url.PathEscape(owner), url.PathEscape(repo), jobID),
|
||||
fmt.Sprintf("repos/%s/%s/actions/tasks/%d/log", url.PathEscape(owner), url.PathEscape(repo), jobID),
|
||||
fmt.Sprintf("repos/%s/%s/actions/task/%d/log", url.PathEscape(owner), url.PathEscape(repo), jobID),
|
||||
}
|
||||
}
|
||||
|
||||
func fetchJobLogBytes(ctx context.Context, owner, repo string, jobID int64) ([]byte, string, error) {
|
||||
var lastErr error
|
||||
for _, p := range logPaths(owner, repo, jobID) {
|
||||
b, _, err := gitea.DoBytes(ctx, "GET", p, nil, nil, "text/plain")
|
||||
if err == nil {
|
||||
return b, p, nil
|
||||
}
|
||||
lastErr = err
|
||||
var httpErr *gitea.HTTPError
|
||||
if errors.As(err, &httpErr) && (httpErr.StatusCode == http.StatusNotFound || httpErr.StatusCode == http.StatusMethodNotAllowed) {
|
||||
continue
|
||||
}
|
||||
return nil, p, err
|
||||
}
|
||||
return nil, "", lastErr
|
||||
}
|
||||
|
||||
func tailByLines(data []byte, tailLines int) []byte {
|
||||
if tailLines <= 0 || len(data) == 0 {
|
||||
return data
|
||||
}
|
||||
lines := 0
|
||||
i := len(data) - 1
|
||||
for i >= 0 {
|
||||
if data[i] == '\n' {
|
||||
lines++
|
||||
if lines > tailLines {
|
||||
return data[i+1:]
|
||||
}
|
||||
}
|
||||
i--
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func limitBytes(data []byte, maxBytes int) ([]byte, bool) {
|
||||
if maxBytes <= 0 {
|
||||
return data, false
|
||||
}
|
||||
if len(data) <= maxBytes {
|
||||
return data, false
|
||||
}
|
||||
return data[len(data)-maxBytes:], true
|
||||
}
|
||||
|
||||
func getRepoActionJobLogPreviewFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called getRepoActionJobLogPreviewFn")
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
jobID, err := params.GetIndex(req.GetArguments(), "job_id")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
tailLines := int(params.GetOptionalInt(req.GetArguments(), "tail_lines", 200))
|
||||
maxBytes := int(params.GetOptionalInt(req.GetArguments(), "max_bytes", 65536))
|
||||
raw, usedPath, err := fetchJobLogBytes(ctx, owner, repo, jobID)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("get job log err: %v", err))
|
||||
}
|
||||
|
||||
tailed := tailByLines(raw, tailLines)
|
||||
limited, truncated := limitBytes(tailed, maxBytes)
|
||||
|
||||
return to.TextResult(map[string]any{
|
||||
"endpoint": usedPath,
|
||||
"job_id": jobID,
|
||||
"bytes": len(raw),
|
||||
"tail_lines": tailLines,
|
||||
"max_bytes": maxBytes,
|
||||
"truncated": truncated,
|
||||
"log": string(limited),
|
||||
})
|
||||
}
|
||||
|
||||
func downloadRepoActionJobLogFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
log.Debugf("Called downloadRepoActionJobLogFn")
|
||||
owner, err := params.GetString(req.GetArguments(), "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(req.GetArguments(), "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
jobID, err := params.GetIndex(req.GetArguments(), "job_id")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
outputPath, _ := req.GetArguments()["output_path"].(string)
|
||||
|
||||
raw, usedPath, err := fetchJobLogBytes(ctx, owner, repo, jobID)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("download job log err: %v", err))
|
||||
}
|
||||
|
||||
if outputPath == "" {
|
||||
home, _ := os.UserHomeDir()
|
||||
if home == "" {
|
||||
home = os.TempDir()
|
||||
}
|
||||
outputPath = filepath.Join(home, ".gitea-mcp", "artifacts", "actions-logs", owner, repo, fmt.Sprintf("%d.log", jobID))
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(outputPath), 0o700); err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("create output dir err: %v", err))
|
||||
}
|
||||
if err := os.WriteFile(outputPath, raw, 0o600); err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("write log file err: %v", err))
|
||||
}
|
||||
|
||||
return to.TextResult(map[string]any{
|
||||
"endpoint": usedPath,
|
||||
"job_id": jobID,
|
||||
"path": outputPath,
|
||||
"bytes": len(raw),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user