strawberry/mcp/main.cpp
trvon 47bb058fdc
Add MCP server for agent access
A stdio JSON-RPC MCP server exposing status, findings, process search
and YARA path scans. Evidence fields are marked as untrusted data;
protection actions stay read-only from MCP.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:17:04 -06:00

117 lines
4.3 KiB
C++

// strawberry-mcp: Model Context Protocol server (stdio) for the Strawberry
// scanner. Runs as the invoking user; see API_CONTRACT.md for the access model.
#include "mcp_server.hpp"
#include "strawberry/config.hpp"
#include <csignal>
#include <cstdio>
#include <iostream>
#include <string>
#include <unistd.h>
namespace {
constexpr std::size_t kMaxMessageBytes = 4 * 1024 * 1024;
void print_usage(const char* program) {
std::cerr << "Usage: " << program << " [--config PATH] [--socket PATH] [--db PATH] [--read-only]\n"
<< " MCP server over stdio for the Strawberry scanner.\n"
<< " --config PATH daemon config to read control_socket_path from (default /etc/strawberry/strawberry.conf)\n"
<< " --socket PATH control socket (overrides config)\n"
<< " --db PATH evidence database (default: as reported by the daemon)\n"
<< " --read-only expose only status and evidence queries (no scans, reload or rescan)\n"
<< "Register with Claude Code: claude mcp add strawberry -- " << program << "\n";
}
} // namespace
int main(int argc, char* argv[]) {
std::signal(SIGPIPE, SIG_IGN);
std::string config_path = "/etc/strawberry/strawberry.conf";
strawberry::mcp::McpOptions options;
bool socket_set = false;
for (int i = 1; i < argc; ++i) {
const std::string arg = argv[i];
const auto next = [&]() -> std::string {
if (i + 1 >= argc) {
std::cerr << "missing value after " << arg << std::endl;
std::exit(2);
}
return argv[++i];
};
if (arg == "--config") {
config_path = next();
} else if (arg == "--socket") {
options.socket_path = next();
socket_set = true;
} else if (arg == "--db") {
options.db_path_override = next();
} else if (arg == "--read-only") {
options.read_only = true;
} else if (arg == "-h" || arg == "--help") {
print_usage(argv[0]);
return 0;
} else {
std::cerr << "unknown argument: " << arg << std::endl;
print_usage(argv[0]);
return 2;
}
}
// stdout carries protocol messages only. Keep a private handle to it and
// point fd 1 at stderr so library logging can never corrupt the stream.
const int protocol_fd = ::dup(STDOUT_FILENO);
if (protocol_fd < 0 || ::dup2(STDERR_FILENO, STDOUT_FILENO) < 0) {
std::perror("strawberry-mcp: cannot set up stdio");
return 1;
}
std::FILE* protocol_out = ::fdopen(protocol_fd, "w");
if (protocol_out == nullptr) {
std::perror("strawberry-mcp: fdopen");
return 1;
}
if (!socket_set) {
strawberry::Config config;
config.load_from_file(config_path);
options.socket_path = config.control_socket_path;
}
std::cerr << "[strawberry-mcp] socket=" << options.socket_path
<< (options.read_only ? " (read-only)" : "") << std::endl;
strawberry::mcp::McpServer server(options);
std::string line;
while (std::getline(std::cin, line)) {
if (line.empty() || line == "\r") {
continue;
}
strawberry::mcp::json reply;
if (line.size() > kMaxMessageBytes) {
reply = {{"jsonrpc", "2.0"}, {"id", nullptr}, {"error", {{"code", -32600}, {"message", "message too large"}}}};
} else {
const auto message = strawberry::mcp::json::parse(line, nullptr, false);
if (message.is_discarded()) {
reply = {{"jsonrpc", "2.0"}, {"id", nullptr}, {"error", {{"code", -32700}, {"message", "parse error"}}}};
} else if (message.is_array()) {
reply = {{"jsonrpc", "2.0"}, {"id", nullptr}, {"error", {{"code", -32600}, {"message", "batch requests are not supported"}}}};
} else {
const auto response = server.handle(message);
if (!response) {
continue;
}
reply = *response;
}
}
const auto text = strawberry::mcp::McpServer::dump(reply);
std::fwrite(text.data(), 1, text.size(), protocol_out);
std::fputc('\n', protocol_out);
std::fflush(protocol_out);
}
return 0;
}