git.cpp¶
src/mcp/servers/git.cpp
@file git.cpp @brief GitServer implementation — version control operations.
Owners: @tvanfossen
GitServer
entropic::GitServer::GitServer(const std::filesystem::path &repo_dir, const std::string &data_dir)
Construct with repo directory and data dir.
src/mcp/servers/git.cpp:563
Source · 12 lines
GitServer::GitServer(
const std::filesystem::path& repo_dir,
const std::string& data_dir)
: MCPServerBase("git")
, repo_dir_(repo_dir) {
create_git_tools(data_dir + "/tools");
register_git_tools();
logger->info("GitServer initialized: repo='{}'",
repo_dir_.string());
}Activity · 3 steps
Requirements
create_git_tools
void entropic::GitServer::create_git_tools(const std::string &tools_dir)
Construct the eight git tool instances (ctor step 1).
src/mcp/servers/git.cpp:586
Called by GitServer
Calls GitAddTool, GitBranchTool, GitCheckoutTool, GitCommitTool, GitDiffTool, GitLogTool, GitResetTool, GitStatusTool, load_tool_definition
Source · 18 lines
void GitServer::create_git_tools(const std::string& tools_dir) {
status_ = std::make_unique<GitStatusTool>(
load_tool_definition("status", "git", tools_dir), *this);
diff_ = std::make_unique<GitDiffTool>(
load_tool_definition("diff", "git", tools_dir), *this);
log_ = std::make_unique<GitLogTool>(
load_tool_definition("log", "git", tools_dir), *this);
commit_ = std::make_unique<GitCommitTool>(
load_tool_definition("commit", "git", tools_dir), *this);
branch_ = std::make_unique<GitBranchTool>(
load_tool_definition("branch", "git", tools_dir), *this);
checkout_ = std::make_unique<GitCheckoutTool>(
load_tool_definition("checkout", "git", tools_dir), *this);
add_ = std::make_unique<GitAddTool>(
load_tool_definition("add", "git", tools_dir), *this);
reset_ = std::make_unique<GitResetTool>(
load_tool_definition("reset", "git", tools_dir), *this);
}Activity · 8 steps
Requirements
drain_and_close
static std::string entropic::drain_and_close(int fd)
Read a pipe to EOF and close it.
src/mcp/servers/git.cpp:67
Source · 9 lines
static std::string drain_and_close(int fd) {
std::string output;
std::array<char, 4096> buf{};
for (ssize_t n; fd >= 0 && (n = ::read(fd, buf.data(), buf.size())) > 0;) {
output.append(buf.data(), static_cast<size_t>(n));
}
if (fd >= 0) { ::close(fd); }
return output;
}Activity · 7 steps
Requirements
make_git_response
static ServerResponse entropic::make_git_response(const std::string &output, int exit_code)
Format a git result as JSON ServerResponse .
src/mcp/servers/git.cpp:158
Source · 8 lines
static ServerResponse make_git_response(
const std::string& output, int exit_code) {
nlohmann::json result;
result["exit_code"] = exit_code;
result["output"] = output;
return {result.dump(), {}};
}Activity · 4 steps
Requirements
register_git_tools
void entropic::GitServer::register_git_tools()
Register the eight git tools with the base server (step 2).
src/mcp/servers/git.cpp:614
Source · 10 lines
void GitServer::register_git_tools() {
register_tool(status_.get());
register_tool(diff_.get());
register_tool(log_.get());
register_tool(commit_.get());
register_tool(branch_.get());
register_tool(checkout_.get());
register_tool(add_.get());
register_tool(reset_.get());
}Activity · 8 steps
Requirements
repo_dir
const std::filesystem::path & entropic::GitServer::repo_dir() const
Get repo directory.
src/mcp/servers/git.cpp:655
Source · 3 lines
const std::filesystem::path& GitServer::repo_dir() const {
return repo_dir_;
}Activity · 1 steps
Requirements
required_access_level
MCPAccessLevel entropic::GitStatusTool::required_access_level() const override
Read-only tool — requires READ access.
src/mcp/servers/git.cpp:196
Source · 3 lines
MCPAccessLevel required_access_level() const override {
return MCPAccessLevel::READ;
}Activity · 1 steps
MCPAccessLevel entropic::GitDiffTool::required_access_level() const override
Read-only tool — requires READ access.
src/mcp/servers/git.cpp:245
Source · 3 lines
MCPAccessLevel required_access_level() const override {
return MCPAccessLevel::READ;
}Activity · 1 steps
MCPAccessLevel entropic::GitLogTool::required_access_level() const override
Read-only tool — requires READ access.
src/mcp/servers/git.cpp:301
Source · 3 lines
MCPAccessLevel required_access_level() const override {
return MCPAccessLevel::READ;
}Activity · 1 steps
Requirements
run_git
static std::pair< std::string, int > entropic::run_git(const std::string &repo_dir, const std::vector< std::string > &args)
Run git -C with NO shell (gh#211, v2.14.4).
src/mcp/servers/git.cpp:96
Source · 15 lines
static std::pair<std::string, int> run_git(
const std::string& repo_dir,
const std::vector<std::string>& args) {
int fd = -1;
const pid_t pid = spawn_git(repo_dir, args, fd);
const std::string output = drain_and_close(fd);
int status = 0;
const bool reaped = pid > 0 && ::waitpid(pid, &status, 0) == pid;
const int exit_code = (reaped && WIFEXITED(status)) ? WEXITSTATUS(status) : -1;
logger->info("Git: {} arg(s) [{}] exit={}, {} chars", args.size(),
args.empty() ? std::string{} : args.front(), exit_code,
output.size());
return reaped ? std::make_pair(output, exit_code)
: std::make_pair(std::string("Failed to open git process"), -1);
}Activity · 8 steps
Requirements
set_working_dir
bool entropic::GitServer::set_working_dir(const std::string &path) override
Set working directory (repo root).
src/mcp/servers/git.cpp:643
Source · 5 lines
bool GitServer::set_working_dir(const std::string& path) {
repo_dir_ = path;
logger->info("Repo directory set to: {}", path);
return true;
}Activity · 3 steps
Requirements
spawn_git
static pid_t entropic::spawn_git(const std::string &repo_dir, const std::vector< std::string > &args, int &read_fd)
Start git -C with stdout+stderr on a pipe.
src/mcp/servers/git.cpp:38
Source · 21 lines
static pid_t spawn_git(const std::string& repo_dir,
const std::vector<std::string>& args, int& read_fd) {
std::vector<std::string> owned = {"git", "-C", repo_dir};
owned.insert(owned.end(), args.begin(), args.end());
std::vector<char*> argv;
for (auto& a : owned) { argv.push_back(a.data()); }
argv.push_back(nullptr);
int fds[2] = {-1, -1};
const pid_t pid = (::pipe(fds) == 0) ? ::fork() : -1;
if (pid == 0) {
::dup2(fds[1], STDOUT_FILENO);
::dup2(fds[1], STDERR_FILENO);
::close(fds[0]);
::close(fds[1]);
::execvp("git", argv.data());
::_exit(127);
}
if (fds[1] >= 0) { ::close(fds[1]); }
read_fd = fds[0];
return pid;
}