MCPcopy
hub / github.com/github/github-mcp-server / matchFiles

Function matchFiles

pkg/github/repositories_helper.go:95–125  ·  view source on GitHub ↗

matchFiles searches for files in the Git tree that match the given path. It's used when GetContents fails or returns unexpected results.

(ctx context.Context, client *github.Client, owner, repo, ref, path string, rawOpts *raw.ContentOpts, rawAPIResponseCode int)

Source from the content-addressed store, hash-verified

93// matchFiles searches for files in the Git tree that match the given path.
94// It's used when GetContents fails or returns unexpected results.
95func matchFiles(ctx context.Context, client *github.Client, owner, repo, ref, path string, rawOpts *raw.ContentOpts, rawAPIResponseCode int) (*mcp.CallToolResult, any, error) {
96 // Step 1: Get Git Tree recursively
97 tree, response, err := client.Git.GetTree(ctx, owner, repo, ref, true)
98 if err != nil {
99 return ghErrors.NewGitHubAPIErrorResponse(ctx,
100 "failed to get git tree",
101 response,
102 err,
103 ), nil, nil
104 }
105 defer func() { _ = response.Body.Close() }()
106
107 // Step 2: Filter tree for matching paths
108 const maxMatchingFiles = 3
109 matchingFiles := filterPaths(tree.Entries, path, maxMatchingFiles)
110 if len(matchingFiles) > 0 {
111 matchingFilesJSON, err := json.Marshal(matchingFiles)
112 if err != nil {
113 return utils.NewToolResultError(fmt.Sprintf("failed to marshal matching files: %s", err)), nil, nil
114 }
115 resolvedRefs, err := json.Marshal(rawOpts)
116 if err != nil {
117 return utils.NewToolResultError(fmt.Sprintf("failed to marshal resolved refs: %s", err)), nil, nil
118 }
119 if rawAPIResponseCode > 0 {
120 return utils.NewToolResultText(fmt.Sprintf("Resolved potential matches in the repository tree (resolved refs: %s, matching files: %s), but the content API returned an unexpected status code %d.", string(resolvedRefs), string(matchingFilesJSON), rawAPIResponseCode)), nil, nil
121 }
122 return utils.NewToolResultText(fmt.Sprintf("Resolved potential matches in the repository tree (resolved refs: %s, matching files: %s).", string(resolvedRefs), string(matchingFilesJSON))), nil, nil
123 }
124 return utils.NewToolResultError("Failed to get file contents. The path does not point to a file or directory, or the file does not exist in the repository."), nil, nil
125}
126
127// filterPaths filters the entries in a GitHub tree to find paths that
128// match the given suffix.

Callers 1

GetFileContentsFunction · 0.85

Calls 4

NewToolResultErrorFunction · 0.92
NewToolResultTextFunction · 0.92
filterPathsFunction · 0.85
CloseMethod · 0.80

Tested by

no test coverage detected