|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/github/github-mcp-server/pkg/raw" |
| 11 | + "github.com/github/github-mcp-server/pkg/translations" |
| 12 | + "github.com/google/go-github/v89/github" |
| 13 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | +) |
| 17 | + |
| 18 | +// Regression test for https://github.com/github/github-mcp-server/issues/3189. |
| 19 | +// |
| 20 | +// get_file_contents returns text-file bodies ONLY inside an EmbeddedResource. |
| 21 | +// Client surfaces that drop embedded resources (e.g. Claude voice mode) then |
| 22 | +// show just "[non-text content: resource]" plus the short "successfully |
| 23 | +// downloaded text file (SHA: ...)" TextContent, so the model never sees the |
| 24 | +// file. The TextContent must also carry the file body. |
| 25 | +func Test_GetFileContents_TextContentIncludesFileBody(t *testing.T) { |
| 26 | + t.Parallel() |
| 27 | + |
| 28 | + fileBody := "# Test Repository\n\nThis is a test repository." |
| 29 | + serverTool := GetFileContents(translations.NullTranslationHelper) |
| 30 | + |
| 31 | + mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 32 | + GetReposGitRefByOwnerByRepoByRef: mockResponse(t, http.StatusOK, "{\"ref\": \"refs/heads/main\", \"object\": {\"sha\": \"\"}}"), |
| 33 | + GetReposByOwnerByRepo: mockResponse(t, http.StatusOK, "{\"name\": \"repo\", \"default_branch\": \"main\"}"), |
| 34 | + GetReposContentsByOwnerByRepoByPath: func(w http.ResponseWriter, _ *http.Request) { |
| 35 | + w.WriteHeader(http.StatusOK) |
| 36 | + fileContent := &github.RepositoryContent{ |
| 37 | + Name: github.Ptr("README.md"), |
| 38 | + Path: github.Ptr("README.md"), |
| 39 | + SHA: github.Ptr(gitBlobSHA([]byte(fileBody))), |
| 40 | + Type: github.Ptr("file"), |
| 41 | + Content: github.Ptr(fileBody), |
| 42 | + Size: github.Ptr(len(fileBody)), |
| 43 | + } |
| 44 | + contentBytes, _ := json.Marshal(fileContent) |
| 45 | + _, _ = w.Write(contentBytes) |
| 46 | + }, |
| 47 | + }) |
| 48 | + |
| 49 | + client := mustNewGHClient(t, mockedClient) |
| 50 | + mockRawClient, err := raw.NewClient(client, &url.URL{Scheme: "https", Host: "raw.example.com", Path: "/"}) |
| 51 | + require.NoError(t, err) |
| 52 | + deps := BaseDeps{Client: client, RawClient: mockRawClient} |
| 53 | + handler := serverTool.Handler(deps) |
| 54 | + request := createMCPRequest(map[string]any{ |
| 55 | + "owner": "owner", |
| 56 | + "repo": "repo", |
| 57 | + "path": "README.md", |
| 58 | + "ref": "refs/heads/main", |
| 59 | + }) |
| 60 | + |
| 61 | + result, err := handler(ContextWithDeps(context.Background(), deps), &request) |
| 62 | + require.NoError(t, err) |
| 63 | + require.False(t, result.IsError) |
| 64 | + require.Len(t, result.Content, 2) |
| 65 | + |
| 66 | + // The embedded resource keeps carrying the full body (existing contract). |
| 67 | + resource, ok := result.Content[1].(*mcp.EmbeddedResource) |
| 68 | + require.True(t, ok, "expected Content[1] to be EmbeddedResource") |
| 69 | + require.NotNil(t, resource.Resource) |
| 70 | + assert.Equal(t, fileBody, resource.Resource.Text) |
| 71 | + |
| 72 | + // The text content must ALSO carry the body so resource-dropping clients |
| 73 | + // still expose the file to the model (issue #3189). |
| 74 | + text, ok := result.Content[0].(*mcp.TextContent) |
| 75 | + require.True(t, ok, "expected Content[0] to be TextContent") |
| 76 | + assert.Contains(t, text.Text, fileBody) |
| 77 | +} |
0 commit comments