|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "github.com/github/github-mcp-server/internal/profiler" |
| 11 | + "github.com/github/github-mcp-server/pkg/translations" |
| 12 | + "github.com/mark3labs/mcp-go/mcp" |
| 13 | + "github.com/mark3labs/mcp-go/server" |
| 14 | +) |
| 15 | + |
| 16 | +// GetWorkflowRunLogsResource defines the resource template and handler for getting workflow run logs. |
| 17 | +func GetWorkflowRunLogsResource(getClient GetClientFn, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) { |
| 18 | + return mcp.NewResourceTemplate( |
| 19 | + "actions://{owner}/{repo}/runs/{runId}/logs", // Resource template |
| 20 | + t("RESOURCE_WORKFLOW_RUN_LOGS_DESCRIPTION", "Workflow Run Logs"), |
| 21 | + ), |
| 22 | + WorkflowRunLogsResourceHandler(getClient) |
| 23 | +} |
| 24 | + |
| 25 | +// GetJobLogsResource defines the resource template and handler for getting individual job logs. |
| 26 | +func GetJobLogsResource(getClient GetClientFn, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) { |
| 27 | + return mcp.NewResourceTemplate( |
| 28 | + "actions://{owner}/{repo}/jobs/{jobId}/logs", // Resource template |
| 29 | + t("RESOURCE_JOB_LOGS_DESCRIPTION", "Job Logs"), |
| 30 | + ), |
| 31 | + JobLogsResourceHandler(getClient) |
| 32 | +} |
| 33 | + |
| 34 | +// WorkflowRunLogsResourceHandler returns a handler function for workflow run logs requests. |
| 35 | +func WorkflowRunLogsResourceHandler(getClient GetClientFn) func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) { |
| 36 | + return func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) { |
| 37 | + // Parse parameters from the URI template matcher |
| 38 | + owner, ok := request.Params.Arguments["owner"].([]string) |
| 39 | + if !ok || len(owner) == 0 { |
| 40 | + return nil, errors.New("owner is required") |
| 41 | + } |
| 42 | + |
| 43 | + repo, ok := request.Params.Arguments["repo"].([]string) |
| 44 | + if !ok || len(repo) == 0 { |
| 45 | + return nil, errors.New("repo is required") |
| 46 | + } |
| 47 | + |
| 48 | + runIdStr, ok := request.Params.Arguments["runId"].([]string) |
| 49 | + if !ok || len(runIdStr) == 0 { |
| 50 | + return nil, errors.New("runId is required") |
| 51 | + } |
| 52 | + |
| 53 | + runId, err := strconv.ParseInt(runIdStr[0], 10, 64) |
| 54 | + if err != nil { |
| 55 | + return nil, fmt.Errorf("invalid runId: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + client, err := getClient(ctx) |
| 59 | + if err != nil { |
| 60 | + return nil, fmt.Errorf("failed to get GitHub client: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + // Get the JIT URL for workflow run logs |
| 64 | + url, resp, err := client.Actions.GetWorkflowRunLogs(ctx, owner[0], repo[0], runId, 1) |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf("failed to get workflow run logs URL: %w", err) |
| 67 | + } |
| 68 | + defer func() { _ = resp.Body.Close() }() |
| 69 | + |
| 70 | + // Download the logs content immediately using the JIT URL |
| 71 | + content, err := downloadLogsFromJITURL(ctx, url.String()) |
| 72 | + if err != nil { |
| 73 | + return nil, fmt.Errorf("failed to download workflow run logs: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + return []mcp.ResourceContents{ |
| 77 | + mcp.TextResourceContents{ |
| 78 | + URI: request.Params.URI, |
| 79 | + MIMEType: "application/zip", |
| 80 | + Text: fmt.Sprintf("Workflow run logs for run %d (ZIP archive)\n\nNote: This is a ZIP archive containing all job logs. Download URL was: %s\n\nContent length: %d bytes", runId, url.String(), len(content)), |
| 81 | + }, |
| 82 | + }, nil |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// JobLogsResourceHandler returns a handler function for individual job logs requests. |
| 87 | +func JobLogsResourceHandler(getClient GetClientFn) func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) { |
| 88 | + return func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) { |
| 89 | + // Parse parameters from the URI template matcher |
| 90 | + owner, ok := request.Params.Arguments["owner"].([]string) |
| 91 | + if !ok || len(owner) == 0 { |
| 92 | + return nil, errors.New("owner is required") |
| 93 | + } |
| 94 | + |
| 95 | + repo, ok := request.Params.Arguments["repo"].([]string) |
| 96 | + if !ok || len(repo) == 0 { |
| 97 | + return nil, errors.New("repo is required") |
| 98 | + } |
| 99 | + |
| 100 | + jobIdStr, ok := request.Params.Arguments["jobId"].([]string) |
| 101 | + if !ok || len(jobIdStr) == 0 { |
| 102 | + return nil, errors.New("jobId is required") |
| 103 | + } |
| 104 | + |
| 105 | + jobId, err := strconv.ParseInt(jobIdStr[0], 10, 64) |
| 106 | + if err != nil { |
| 107 | + return nil, fmt.Errorf("invalid jobId: %w", err) |
| 108 | + } |
| 109 | + |
| 110 | + client, err := getClient(ctx) |
| 111 | + if err != nil { |
| 112 | + return nil, fmt.Errorf("failed to get GitHub client: %w", err) |
| 113 | + } |
| 114 | + |
| 115 | + // Get the JIT URL for job logs |
| 116 | + url, resp, err := client.Actions.GetWorkflowJobLogs(ctx, owner[0], repo[0], jobId, 1) |
| 117 | + if err != nil { |
| 118 | + return nil, fmt.Errorf("failed to get job logs URL: %w", err) |
| 119 | + } |
| 120 | + defer func() { _ = resp.Body.Close() }() |
| 121 | + |
| 122 | + // Download the logs content immediately using the JIT URL |
| 123 | + content, err := downloadLogsFromJITURL(ctx, url.String()) |
| 124 | + if err != nil { |
| 125 | + return nil, fmt.Errorf("failed to download job logs: %w", err) |
| 126 | + } |
| 127 | + |
| 128 | + return []mcp.ResourceContents{ |
| 129 | + mcp.TextResourceContents{ |
| 130 | + URI: request.Params.URI, |
| 131 | + MIMEType: "text/plain", |
| 132 | + Text: content, |
| 133 | + }, |
| 134 | + }, nil |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +// downloadLogsFromJITURL downloads content from a GitHub JIT URL |
| 139 | +func downloadLogsFromJITURL(ctx context.Context, jitURL string) (string, error) { |
| 140 | + prof := profiler.New(nil, profiler.IsProfilingEnabled()) |
| 141 | + finish := prof.Start(ctx, "download_jit_logs") |
| 142 | + |
| 143 | + httpResp, err := http.Get(jitURL) //nolint:gosec |
| 144 | + if err != nil { |
| 145 | + _ = finish(0, 0) |
| 146 | + return "", fmt.Errorf("failed to download from JIT URL: %w", err) |
| 147 | + } |
| 148 | + defer httpResp.Body.Close() |
| 149 | + |
| 150 | + if httpResp.StatusCode != http.StatusOK { |
| 151 | + _ = finish(0, 0) |
| 152 | + return "", fmt.Errorf("failed to download logs: HTTP %d", httpResp.StatusCode) |
| 153 | + } |
| 154 | + |
| 155 | + // For large files, we should limit the content size to avoid memory issues |
| 156 | + const maxContentSize = 10 * 1024 * 1024 // 10MB limit |
| 157 | + |
| 158 | + // Read the content with a size limit |
| 159 | + content := make([]byte, 0, 1024*1024) // Start with 1MB capacity |
| 160 | + buffer := make([]byte, 32*1024) // 32KB read buffer |
| 161 | + totalRead := 0 |
| 162 | + |
| 163 | + for { |
| 164 | + n, err := httpResp.Body.Read(buffer) |
| 165 | + if n > 0 { |
| 166 | + if totalRead+n > maxContentSize { |
| 167 | + // Truncate if content is too large |
| 168 | + remaining := maxContentSize - totalRead |
| 169 | + content = append(content, buffer[:remaining]...) |
| 170 | + content = append(content, []byte(fmt.Sprintf("\n\n[Content truncated - original size exceeded %d bytes]", maxContentSize))...) |
| 171 | + break |
| 172 | + } |
| 173 | + content = append(content, buffer[:n]...) |
| 174 | + totalRead += n |
| 175 | + } |
| 176 | + if err != nil { |
| 177 | + if err.Error() == "EOF" { |
| 178 | + break |
| 179 | + } |
| 180 | + _ = finish(0, int64(totalRead)) |
| 181 | + return "", fmt.Errorf("failed to read response body: %w", err) |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + // Count lines for profiler |
| 186 | + lines := 1 |
| 187 | + for _, b := range content { |
| 188 | + if b == '\n' { |
| 189 | + lines++ |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + _ = finish(lines, int64(len(content))) |
| 194 | + return string(content), nil |
| 195 | +} |
0 commit comments