Skip to content

chore: export repository funcs #166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions pkg/github/repositories.go
Original file line number Diff line number Diff line change
@@ -13,8 +13,8 @@ import (
"github.com/mark3labs/mcp-go/server"
)

// listCommits creates a tool to get commits of a branch in a repository.
func listCommits(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
// ListCommits creates a tool to get commits of a branch in a repository.
func ListCommits(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("list_commits",
mcp.WithDescription(t("TOOL_LIST_COMMITS_DESCRIPTION", "Get list of commits of a branch in a GitHub repository")),
mcp.WithString("owner",
@@ -79,8 +79,8 @@ func listCommits(client *github.Client, t translations.TranslationHelperFunc) (t
}
}

// createOrUpdateFile creates a tool to create or update a file in a GitHub repository.
func createOrUpdateFile(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
// CreateOrUpdateFile creates a tool to create or update a file in a GitHub repository.
func CreateOrUpdateFile(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("create_or_update_file",
mcp.WithDescription(t("TOOL_CREATE_OR_UPDATE_FILE_DESCRIPTION", "Create or update a single file in a GitHub repository")),
mcp.WithString("owner",
@@ -180,8 +180,8 @@ func createOrUpdateFile(client *github.Client, t translations.TranslationHelperF
}
}

// createRepository creates a tool to create a new GitHub repository.
func createRepository(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
// CreateRepository creates a tool to create a new GitHub repository.
func CreateRepository(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("create_repository",
mcp.WithDescription(t("TOOL_CREATE_REPOSITORY_DESCRIPTION", "Create a new GitHub repository in your account")),
mcp.WithString("name",
@@ -246,8 +246,8 @@ func createRepository(client *github.Client, t translations.TranslationHelperFun
}
}

// getFileContents creates a tool to get the contents of a file or directory from a GitHub repository.
func getFileContents(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
// GetFileContents creates a tool to get the contents of a file or directory from a GitHub repository.
func GetFileContents(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("get_file_contents",
mcp.WithDescription(t("TOOL_GET_FILE_CONTENTS_DESCRIPTION", "Get the contents of a file or directory from a GitHub repository")),
mcp.WithString("owner",
@@ -315,8 +315,8 @@ func getFileContents(client *github.Client, t translations.TranslationHelperFunc
}
}

// forkRepository creates a tool to fork a repository.
func forkRepository(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
// ForkRepository creates a tool to fork a repository.
func ForkRepository(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("fork_repository",
mcp.WithDescription(t("TOOL_FORK_REPOSITORY_DESCRIPTION", "Fork a GitHub repository to your account or specified organization")),
mcp.WithString("owner",
@@ -378,8 +378,8 @@ func forkRepository(client *github.Client, t translations.TranslationHelperFunc)
}
}

// createBranch creates a tool to create a new branch.
func createBranch(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
// CreateBranch creates a tool to create a new branch.
func CreateBranch(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("create_branch",
mcp.WithDescription(t("TOOL_CREATE_BRANCH_DESCRIPTION", "Create a new branch in a GitHub repository")),
mcp.WithString("owner",
@@ -458,8 +458,8 @@ func createBranch(client *github.Client, t translations.TranslationHelperFunc) (
}
}

// pushFiles creates a tool to push multiple files in a single commit to a GitHub repository.
func pushFiles(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
// PushFiles creates a tool to push multiple files in a single commit to a GitHub repository.
func PushFiles(client *github.Client, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) {
return mcp.NewTool("push_files",
mcp.WithDescription(t("TOOL_PUSH_FILES_DESCRIPTION", "Push multiple files to a GitHub repository in a single commit")),
mcp.WithString("owner",
28 changes: 14 additions & 14 deletions pkg/github/repositories_test.go
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ import (
func Test_GetFileContents(t *testing.T) {
// Verify tool definition once
mockClient := github.NewClient(nil)
tool, _ := getFileContents(mockClient, translations.NullTranslationHelper)
tool, _ := GetFileContents(mockClient, translations.NullTranslationHelper)

assert.Equal(t, "get_file_contents", tool.Name)
assert.NotEmpty(t, tool.Description)
@@ -132,7 +132,7 @@ func Test_GetFileContents(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
// Setup client with mock
client := github.NewClient(tc.mockedClient)
_, handler := getFileContents(client, translations.NullTranslationHelper)
_, handler := GetFileContents(client, translations.NullTranslationHelper)

// Create call request
request := mcp.CallToolRequest{
@@ -189,7 +189,7 @@ func Test_GetFileContents(t *testing.T) {
func Test_ForkRepository(t *testing.T) {
// Verify tool definition once
mockClient := github.NewClient(nil)
tool, _ := forkRepository(mockClient, translations.NullTranslationHelper)
tool, _ := ForkRepository(mockClient, translations.NullTranslationHelper)

assert.Equal(t, "fork_repository", tool.Name)
assert.NotEmpty(t, tool.Description)
@@ -259,7 +259,7 @@ func Test_ForkRepository(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
// Setup client with mock
client := github.NewClient(tc.mockedClient)
_, handler := forkRepository(client, translations.NullTranslationHelper)
_, handler := ForkRepository(client, translations.NullTranslationHelper)

// Create call request
request := createMCPRequest(tc.requestArgs)
@@ -287,7 +287,7 @@ func Test_ForkRepository(t *testing.T) {
func Test_CreateBranch(t *testing.T) {
// Verify tool definition once
mockClient := github.NewClient(nil)
tool, _ := createBranch(mockClient, translations.NullTranslationHelper)
tool, _ := CreateBranch(mockClient, translations.NullTranslationHelper)

assert.Equal(t, "create_branch", tool.Name)
assert.NotEmpty(t, tool.Description)
@@ -445,7 +445,7 @@ func Test_CreateBranch(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
// Setup client with mock
client := github.NewClient(tc.mockedClient)
_, handler := createBranch(client, translations.NullTranslationHelper)
_, handler := CreateBranch(client, translations.NullTranslationHelper)

// Create call request
request := createMCPRequest(tc.requestArgs)
@@ -478,7 +478,7 @@ func Test_CreateBranch(t *testing.T) {
func Test_ListCommits(t *testing.T) {
// Verify tool definition once
mockClient := github.NewClient(nil)
tool, _ := listCommits(mockClient, translations.NullTranslationHelper)
tool, _ := ListCommits(mockClient, translations.NullTranslationHelper)

assert.Equal(t, "list_commits", tool.Name)
assert.NotEmpty(t, tool.Description)
@@ -614,7 +614,7 @@ func Test_ListCommits(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
// Setup client with mock
client := github.NewClient(tc.mockedClient)
_, handler := listCommits(client, translations.NullTranslationHelper)
_, handler := ListCommits(client, translations.NullTranslationHelper)

// Create call request
request := createMCPRequest(tc.requestArgs)
@@ -652,7 +652,7 @@ func Test_ListCommits(t *testing.T) {
func Test_CreateOrUpdateFile(t *testing.T) {
// Verify tool definition once
mockClient := github.NewClient(nil)
tool, _ := createOrUpdateFile(mockClient, translations.NullTranslationHelper)
tool, _ := CreateOrUpdateFile(mockClient, translations.NullTranslationHelper)

assert.Equal(t, "create_or_update_file", tool.Name)
assert.NotEmpty(t, tool.Description)
@@ -775,7 +775,7 @@ func Test_CreateOrUpdateFile(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
// Setup client with mock
client := github.NewClient(tc.mockedClient)
_, handler := createOrUpdateFile(client, translations.NullTranslationHelper)
_, handler := CreateOrUpdateFile(client, translations.NullTranslationHelper)

// Create call request
request := createMCPRequest(tc.requestArgs)
@@ -815,7 +815,7 @@ func Test_CreateOrUpdateFile(t *testing.T) {
func Test_CreateRepository(t *testing.T) {
// Verify tool definition once
mockClient := github.NewClient(nil)
tool, _ := createRepository(mockClient, translations.NullTranslationHelper)
tool, _ := CreateRepository(mockClient, translations.NullTranslationHelper)

assert.Equal(t, "create_repository", tool.Name)
assert.NotEmpty(t, tool.Description)
@@ -923,7 +923,7 @@ func Test_CreateRepository(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
// Setup client with mock
client := github.NewClient(tc.mockedClient)
_, handler := createRepository(client, translations.NullTranslationHelper)
_, handler := CreateRepository(client, translations.NullTranslationHelper)

// Create call request
request := createMCPRequest(tc.requestArgs)
@@ -961,7 +961,7 @@ func Test_CreateRepository(t *testing.T) {
func Test_PushFiles(t *testing.T) {
// Verify tool definition once
mockClient := github.NewClient(nil)
tool, _ := pushFiles(mockClient, translations.NullTranslationHelper)
tool, _ := PushFiles(mockClient, translations.NullTranslationHelper)

assert.Equal(t, "push_files", tool.Name)
assert.NotEmpty(t, tool.Description)
@@ -1256,7 +1256,7 @@ func Test_PushFiles(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
// Setup client with mock
client := github.NewClient(tc.mockedClient)
_, handler := pushFiles(client, translations.NullTranslationHelper)
_, handler := PushFiles(client, translations.NullTranslationHelper)

// Create call request
request := createMCPRequest(tc.requestArgs)
33 changes: 17 additions & 16 deletions pkg/github/repository_resource.go
Original file line number Diff line number Diff line change
@@ -17,52 +17,53 @@ import (
"github.com/mark3labs/mcp-go/server"
)

// getRepositoryResourceContent defines the resource template and handler for getting repository content.
func getRepositoryResourceContent(client *github.Client, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
// GetRepositoryResourceContent defines the resource template and handler for getting repository content.
func GetRepositoryResourceContent(client *github.Client, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
return mcp.NewResourceTemplate(
"repo://{owner}/{repo}/contents{/path*}", // Resource template
t("RESOURCE_REPOSITORY_CONTENT_DESCRIPTION", "Repository Content"),
),
repositoryResourceContentsHandler(client)
RepositoryResourceContentsHandler(client)
}

// getRepositoryContent defines the resource template and handler for getting repository content for a branch.
func getRepositoryResourceBranchContent(client *github.Client, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
// GetRepositoryResourceBranchContent defines the resource template and handler for getting repository content for a branch.
func GetRepositoryResourceBranchContent(client *github.Client, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
return mcp.NewResourceTemplate(
"repo://{owner}/{repo}/refs/heads/{branch}/contents{/path*}", // Resource template
t("RESOURCE_REPOSITORY_CONTENT_BRANCH_DESCRIPTION", "Repository Content for specific branch"),
),
repositoryResourceContentsHandler(client)
RepositoryResourceContentsHandler(client)
}

// getRepositoryResourceCommitContent defines the resource template and handler for getting repository content for a commit.
func getRepositoryResourceCommitContent(client *github.Client, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
// GetRepositoryResourceCommitContent defines the resource template and handler for getting repository content for a commit.
func GetRepositoryResourceCommitContent(client *github.Client, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
return mcp.NewResourceTemplate(
"repo://{owner}/{repo}/sha/{sha}/contents{/path*}", // Resource template
t("RESOURCE_REPOSITORY_CONTENT_COMMIT_DESCRIPTION", "Repository Content for specific commit"),
),
repositoryResourceContentsHandler(client)
RepositoryResourceContentsHandler(client)
}

// getRepositoryResourceTagContent defines the resource template and handler for getting repository content for a tag.
func getRepositoryResourceTagContent(client *github.Client, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
// GetRepositoryResourceTagContent defines the resource template and handler for getting repository content for a tag.
func GetRepositoryResourceTagContent(client *github.Client, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
return mcp.NewResourceTemplate(
"repo://{owner}/{repo}/refs/tags/{tag}/contents{/path*}", // Resource template
t("RESOURCE_REPOSITORY_CONTENT_TAG_DESCRIPTION", "Repository Content for specific tag"),
),
repositoryResourceContentsHandler(client)
RepositoryResourceContentsHandler(client)
}

// getRepositoryResourcePrContent defines the resource template and handler for getting repository content for a pull request.
func getRepositoryResourcePrContent(client *github.Client, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
// GetRepositoryResourcePrContent defines the resource template and handler for getting repository content for a pull request.
func GetRepositoryResourcePrContent(client *github.Client, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) {
return mcp.NewResourceTemplate(
"repo://{owner}/{repo}/refs/pull/{prNumber}/head/contents{/path*}", // Resource template
t("RESOURCE_REPOSITORY_CONTENT_PR_DESCRIPTION", "Repository Content for specific pull request"),
),
repositoryResourceContentsHandler(client)
RepositoryResourceContentsHandler(client)
}

func repositoryResourceContentsHandler(client *github.Client) func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) {
// RepositoryResourceContentsHandler returns a handler function for repository content requests.
func RepositoryResourceContentsHandler(client *github.Client) func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) {
return func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) {
// the matcher will give []string with one element
// https://github.com/mark3labs/mcp-go/pull/54
22 changes: 11 additions & 11 deletions pkg/github/repository_resource_test.go
Original file line number Diff line number Diff line change
@@ -234,7 +234,7 @@ func Test_repositoryResourceContentsHandler(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
client := github.NewClient(tc.mockedClient)
handler := repositoryResourceContentsHandler(client)
handler := RepositoryResourceContentsHandler(client)

request := mcp.ReadResourceRequest{
Params: struct {
@@ -258,26 +258,26 @@ func Test_repositoryResourceContentsHandler(t *testing.T) {
}
}

func Test_getRepositoryResourceContent(t *testing.T) {
tmpl, _ := getRepositoryResourceContent(nil, translations.NullTranslationHelper)
func Test_GetRepositoryResourceContent(t *testing.T) {
tmpl, _ := GetRepositoryResourceContent(nil, translations.NullTranslationHelper)
require.Equal(t, "repo://{owner}/{repo}/contents{/path*}", tmpl.URITemplate.Raw())
}

func Test_getRepositoryResourceBranchContent(t *testing.T) {
tmpl, _ := getRepositoryResourceBranchContent(nil, translations.NullTranslationHelper)
func Test_GetRepositoryResourceBranchContent(t *testing.T) {
tmpl, _ := GetRepositoryResourceBranchContent(nil, translations.NullTranslationHelper)
require.Equal(t, "repo://{owner}/{repo}/refs/heads/{branch}/contents{/path*}", tmpl.URITemplate.Raw())
}
func Test_getRepositoryResourceCommitContent(t *testing.T) {
tmpl, _ := getRepositoryResourceCommitContent(nil, translations.NullTranslationHelper)
func Test_GetRepositoryResourceCommitContent(t *testing.T) {
tmpl, _ := GetRepositoryResourceCommitContent(nil, translations.NullTranslationHelper)
require.Equal(t, "repo://{owner}/{repo}/sha/{sha}/contents{/path*}", tmpl.URITemplate.Raw())
}

func Test_getRepositoryResourceTagContent(t *testing.T) {
tmpl, _ := getRepositoryResourceTagContent(nil, translations.NullTranslationHelper)
func Test_GetRepositoryResourceTagContent(t *testing.T) {
tmpl, _ := GetRepositoryResourceTagContent(nil, translations.NullTranslationHelper)
require.Equal(t, "repo://{owner}/{repo}/refs/tags/{tag}/contents{/path*}", tmpl.URITemplate.Raw())
}

func Test_getRepositoryResourcePrContent(t *testing.T) {
tmpl, _ := getRepositoryResourcePrContent(nil, translations.NullTranslationHelper)
func Test_GetRepositoryResourcePrContent(t *testing.T) {
tmpl, _ := GetRepositoryResourcePrContent(nil, translations.NullTranslationHelper)
require.Equal(t, "repo://{owner}/{repo}/refs/pull/{prNumber}/head/contents{/path*}", tmpl.URITemplate.Raw())
}
24 changes: 12 additions & 12 deletions pkg/github/server.go
Original file line number Diff line number Diff line change
@@ -24,11 +24,11 @@ func NewServer(client *github.Client, version string, readOnly bool, t translati
server.WithLogging())

// Add GitHub Resources
s.AddResourceTemplate(getRepositoryResourceContent(client, t))
s.AddResourceTemplate(getRepositoryResourceBranchContent(client, t))
s.AddResourceTemplate(getRepositoryResourceCommitContent(client, t))
s.AddResourceTemplate(getRepositoryResourceTagContent(client, t))
s.AddResourceTemplate(getRepositoryResourcePrContent(client, t))
s.AddResourceTemplate(GetRepositoryResourceContent(client, t))
s.AddResourceTemplate(GetRepositoryResourceBranchContent(client, t))
s.AddResourceTemplate(GetRepositoryResourceCommitContent(client, t))
s.AddResourceTemplate(GetRepositoryResourceTagContent(client, t))
s.AddResourceTemplate(GetRepositoryResourcePrContent(client, t))

// Add GitHub tools - Issues
s.AddTool(GetIssue(client, t))
@@ -57,14 +57,14 @@ func NewServer(client *github.Client, version string, readOnly bool, t translati

// Add GitHub tools - Repositories
s.AddTool(searchRepositories(client, t))
s.AddTool(getFileContents(client, t))
s.AddTool(listCommits(client, t))
s.AddTool(GetFileContents(client, t))
s.AddTool(ListCommits(client, t))
if !readOnly {
s.AddTool(createOrUpdateFile(client, t))
s.AddTool(createRepository(client, t))
s.AddTool(forkRepository(client, t))
s.AddTool(createBranch(client, t))
s.AddTool(pushFiles(client, t))
s.AddTool(CreateOrUpdateFile(client, t))
s.AddTool(CreateRepository(client, t))
s.AddTool(ForkRepository(client, t))
s.AddTool(CreateBranch(client, t))
s.AddTool(PushFiles(client, t))
}

// Add GitHub tools - Search
Loading
Oops, something went wrong.