// 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 #include #include #include #include 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 (as a user in the strawberry group):\n" << " claude mcp add strawberry -- " << program << "\n" << " codex mcp add strawberry -- " << program << "\n" << " pi 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; }