GitHub
仓库管理、文件操作和GitHub API集成
概述
GitHub MCP 服务器提供全面的 GitHub API 功能,支持仓库管理、文件操作、搜索等功能,具有自动分支创建、完善的错误处理、保留 Git 历史记录和批量操作能力。
常用功能
文件操作
// 创建或更新单个文件
const result = await create_or_update_file({
owner: "username",
repo: "repo-name",
path: "path/to/file.txt",
content: "Hello, GitHub!",
message: "Add new file",
branch: "main"
});
// 批量推送多个文件
const pushResult = await push_files({
owner: "username",
repo: "repo-name",
branch: "feature-branch",
files: [
{ path: "file1.txt", content: "Content 1" },
{ path: "file2.txt", content: "Content 2" }
],
message: "Add multiple files"
});
// 获取文件内容
const fileContent = await get_file_contents({
owner: "username",
repo: "repo-name",
path: "README.md",
branch: "main"
});
仓库管理
// 创建仓库
const newRepo = await create_repository({
name: "new-project",
description: "My awesome project",
private: false,
autoInit: true
});
// 创建分支
const branch = await create_branch({
owner: "username",
repo: "repo-name",
branch: "feature-branch",
from_branch: "main"
});
// Fork 仓库
const fork = await fork_repository({
owner: "original-owner",
repo: "original-repo"
});
问题和拉取请求
// 创建问题
const issue = await create_issue({
owner: "username",
repo: "repo-name",
title: "Bug: Application crashes on startup",
body: "When starting the application, it crashes immediately.",
labels: ["bug", "priority-high"]
});
// 创建拉取请求
const pr = await create_pull_request({
owner: "username",
repo: "repo-name",
title: "Add new feature",
body: "This PR implements the new feature X",
head: "feature-branch",
base: "main"
});
// 添加评论
const comment = await add_issue_comment({
owner: "username",
repo: "repo-name",
issue_number: 42,
body: "I can reproduce this issue."
});
搜索功能
// 搜索仓库
const repos = await search_repositories({
query: "topic:javascript stars:>1000",
page: 1,
perPage: 10
});
// 搜索代码
const code = await search_code({
q: "addClass in:file language:js repo:jquery/jquery",
per_page: 20
});
// 搜索问题
const issues = await search_issues({
q: "is:issue is:open label:bug",
sort: "created",
order: "desc"
});
// 搜索用户
const users = await search_users({
q: "location:berlin language:javascript",
sort: "followers"
});
提示
- 操作时如果指定的分支不存在,服务器会自动创建该分支
- 所有操作都会保留正确的 Git 历史记录,不会强制推送
- API 提供详细的错误信息,方便调试
- 搜索 API 支持 GitHub 高级搜索语法
- 请求时可以指定分页参数来控制返回结果数量