// In-process tests for the MCP server: protocol, every tool, argument // validation, read-only mode, scan_paths permission handling, and the // CLI/MCP JSON contract. argv[1] is the path to the `strawberry` binary. #include "../mcp_server.hpp" #include "strawberry/control_server.hpp" #include #include #include #include #include #include #include #include #include #include #include namespace fs = std::filesystem; using strawberry::mcp::json; using strawberry::mcp::McpServer; using strawberry::mcp::McpOptions; namespace { int next_id = 1; json call(McpServer& server, const std::string& method, const json& params = json::object()) { auto reply = server.handle({{"jsonrpc", "2.0"}, {"id", next_id++}, {"method", method}, {"params", params}}); assert(reply); return *reply; } json tool(McpServer& server, const std::string& name, const json& args = json::object()) { return call(server, "tools/call", {{"name", name}, {"arguments", args}}); } std::string run_capture(const std::string& command) { std::string output; std::FILE* pipe = ::popen(command.c_str(), "r"); assert(pipe != nullptr); char buffer[4096]; std::size_t n = 0; while ((n = std::fread(buffer, 1, sizeof(buffer), pipe)) > 0) { output.append(buffer, n); } ::pclose(pipe); return output; } std::set tool_names(const json& list_reply) { std::set names; for (const auto& t : list_reply["result"]["tools"]) { names.insert(t["name"].get()); } return names; } } // namespace int main(int argc, char* argv[]) { assert(argc >= 2 && "pass the strawberry binary path"); const std::string strawberry_bin = argv[1]; std::cout << "Running MCP server tests..." << std::endl << std::endl; const auto root = fs::temp_directory_path() / ("strawberry_mcp_test_" + std::to_string(::getpid())); std::error_code ec; fs::remove_all(root, ec); fs::create_directories(root / "scan" / "sub"); const auto db_path = (root / "evidence.db").string(); const auto config_path = (root / "demo.conf").string(); const auto socket_path = (root / "c.sock").string(); { std::ofstream config(config_path); config << "db_path=" << db_path << "\nenable_bpf_monitoring=false\n"; } // Seed evidence through the real CLI. assert(std::system(("'" + strawberry_bin + "' --seed-demo-trace '" + config_path + "' >/dev/null 2>&1").c_str()) == 0); // Fake daemon: real ControlServer, fake handlers. std::atomic scans{0}; std::atomic reloads{0}; strawberry::ControlServer daemon; strawberry::ControlServerOptions daemon_options; daemon_options.socket_path = socket_path; strawberry::ControlHandlers handlers; handlers.status = [&]() { return std::string(R"({"db_path":")") + db_path + R"(","monitor":{"enabled":true,"backend":"bpf","execveat":true},"rules":{"loaded":true,"last_error":""},"pipeline":{}})"; }; handlers.reload_rules = [&]() { ++reloads; return std::string(R"({"reloaded":true,"error":""})"); }; handlers.rescan = []() { return std::string(R"({"queued":true})"); }; handlers.policy = []() { return std::string(R"({"policy":{"feeds":[{"id":"yara-forge-core","enabled":true}],"allowlist":[{"id":"allow-1","kind":"sha256","value":")") + std::string(64, 'a') + R"("}],"history":[]},"feed_state":{},"feeds_dir":"/x","policy_error":""})"; }; handlers.scan_fd = [&](int fd, unsigned, int) { ++scans; char buffer[256] = {}; const ssize_t n = ::pread(fd, buffer, sizeof(buffer) - 1, 0); const bool evil = n > 0 && std::string(buffer, static_cast(n)).find("EVIL") != std::string::npos; return std::string(R"({"scanned":true,"error":"","size":)") + std::to_string(n) + R"(,"sha256":"x","matches":)" + (evil ? R"([{"rule":"Evil","severity":"high","namespace":"default","tags":[],"meta":""}])" : "[]") + "}"; }; assert(daemon.start(daemon_options, handlers)); McpOptions options; options.socket_path = socket_path; McpServer server(options); std::cout << "Test protocol basics:" << std::endl; auto init = call(server, "initialize", {{"protocolVersion", "2025-06-18"}, {"capabilities", json::object()}}); assert(init["result"]["protocolVersion"] == "2025-06-18"); assert(init["result"]["capabilities"].contains("tools")); // The server must warn agents that evidence may be attacker-controlled. assert(init["result"]["instructions"].get().find("attacker") != std::string::npos); auto unknown_version = call(server, "initialize", {{"protocolVersion", "1999-01-01"}}); assert(unknown_version["result"]["protocolVersion"] == "2025-11-25"); assert(!server.handle({{"jsonrpc", "2.0"}, {"method", "notifications/initialized"}})); assert(call(server, "ping")["result"].is_object()); assert(call(server, "nope/method")["error"]["code"] == -32601); assert((*server.handle({{"id", 1}, {"method", "ping"}}))["error"]["code"] == -32600); // Wrong-typed fields are invalid requests, never a crash (found by fuzzing). assert((*server.handle({{"jsonrpc", 1}, {"id", 1}, {"method", "ping"}}))["error"]["code"] == -32600); assert((*server.handle({{"jsonrpc", "2.0"}, {"id", 1}, {"method", 7}}))["error"]["code"] == -32600); std::cout << " PASSED" << std::endl; std::cout << "Test tools/list and read-only mode:" << std::endl; const auto full = tool_names(call(server, "tools/list")); assert(full.size() == 9 && full.contains("strawberry_scan_paths") && full.contains("strawberry_rescan")); assert(full.contains("strawberry_get_policy")); // Agents may read policy but never change it. for (const auto& name : full) { assert(name.find("allowlist") == std::string::npos && name.find("feed") == std::string::npos); } McpOptions ro_options = options; ro_options.read_only = true; McpServer read_only(ro_options); const auto limited = tool_names(call(read_only, "tools/list")); assert(limited.size() == 6 && !limited.contains("strawberry_scan_paths") && !limited.contains("strawberry_reload_rules")); assert(limited.contains("strawberry_get_policy")); assert(tool(read_only, "strawberry_reload_rules")["error"]["code"] == -32602); assert(reloads == 0); std::cout << " PASSED" << std::endl; std::cout << "Test evidence tools:" << std::endl; auto status = tool(server, "strawberry_status"); assert(status["result"]["isError"] == false); assert(status["result"]["structuredContent"]["healthy"] == true); assert(status["result"]["structuredContent"]["evidence"]["readable"] == true); auto findings = tool(server, "strawberry_list_findings", {{"limit", 10}}); const auto& finding_list = findings["result"]["structuredContent"]["findings"]; assert(!finding_list.empty()); const auto finding_id = finding_list[0]["id"].get(); auto high_only = tool(server, "strawberry_list_findings", {{"min_severity", "critical"}}); for (const auto& f : high_only["result"]["structuredContent"]["findings"]) { assert(f["severity"] == "critical"); } auto finding = tool(server, "strawberry_get_finding", {{"id", finding_id}}); assert(finding["result"]["structuredContent"]["finding"]["id"] == finding_id); assert(!finding["result"]["structuredContent"]["processes"].empty()); auto missing = tool(server, "strawberry_get_finding", {{"id", "finding_does_not_exist"}}); assert(missing["result"]["isError"] == true); auto search = tool(server, "strawberry_search_processes", {{"query", "654"}}); const auto& processes = search["result"]["structuredContent"]["processes"]; assert(!processes.empty()); const auto node_id = processes[0]["node_id"].get(); auto process = tool(server, "strawberry_get_process", {{"node_id", node_id}, {"trace_depth", 2}}); assert(process["result"]["structuredContent"]["process"]["id"] == node_id); assert(process["result"]["structuredContent"]["trace"].get().find("Incident trace") != std::string::npos); std::cout << " PASSED" << std::endl; std::cout << "Test argument validation:" << std::endl; assert(tool(server, "strawberry_list_findings", {{"limit", 0}})["error"]["code"] == -32602); assert(tool(server, "strawberry_list_findings", {{"limit", "5"}})["error"]["code"] == -32602); assert(tool(server, "strawberry_list_findings", {{"min_severity", "extreme"}})["error"]["code"] == -32602); assert(tool(server, "strawberry_list_findings", {{"bogus", 1}})["error"]["code"] == -32602); assert(tool(server, "strawberry_get_process", {})["error"]["code"] == -32602); assert(tool(server, "strawberry_scan_paths", {{"paths", {"relative/path"}}})["error"]["code"] == -32602); assert(tool(server, "strawberry_scan_paths", {{"paths", json::array()}})["error"]["code"] == -32602); assert(tool(server, "no_such_tool")["error"]["code"] == -32602); std::cout << " PASSED" << std::endl; std::cout << "Test scan_paths permissions, symlinks and limits:" << std::endl; { std::ofstream(root / "scan" / "sub" / "bad.bin") << "xx EVIL xx"; } { std::ofstream(root / "scan" / "ok.txt") << "fine"; } { std::ofstream(root / "scan" / "locked") << "EVIL but unreadable"; } ::chmod((root / "scan" / "locked").c_str(), 0); fs::create_symlink(root / "scan" / "sub" / "bad.bin", root / "scan" / "link"); const bool running_as_root = ::geteuid() == 0; scans = 0; auto scan = tool(server, "strawberry_scan_paths", {{"paths", {(root / "scan").string(), "/proc/self/environ"}}, {"recursive", true}}); const auto& result = scan["result"]["structuredContent"]; assert(scan["result"]["isError"] == false); // Root can read the mode-000 file, so it legitimately matches too. assert(result["matched_files"] == (running_as_root ? 2 : 1)); bool saw_bad = false; for (const auto& m : result["matches"]) { saw_bad |= m["path"].get().ends_with("bad.bin"); } assert(saw_bad); bool saw_symlink = false; bool saw_proc = false; for (const auto& s : result["skipped"]) { saw_symlink |= s["reason"] == "symlink not followed"; saw_proc |= s["path"] == "/proc/self/environ"; } assert(saw_symlink && saw_proc); if (!running_as_root) { // The unreadable file never reaches the daemon. assert(result["errors"].size() == 1); assert(scans == 2); } auto non_recursive = tool(server, "strawberry_scan_paths", {{"paths", {(root / "scan").string()}}}); // Top level only: bad.bin is in sub/, and "locked" is readable only by root. assert(non_recursive["result"]["structuredContent"]["matched_files"] == (running_as_root ? 1 : 0)); auto limited_scan = tool(server, "strawberry_scan_paths", {{"paths", {(root / "scan").string()}}, {"recursive", true}, {"max_files", 1}}); assert(limited_scan["result"]["structuredContent"]["truncated"] == true); std::cout << " PASSED" << std::endl; std::cout << "Test policy (read-only):" << std::endl; auto policy = tool(server, "strawberry_get_policy"); assert(policy["result"]["isError"] == false); assert(policy["result"]["structuredContent"]["policy"]["allowlist"].size() == 1); assert(policy["result"]["content"][0]["text"].get().find("1 of 1 rule feed") != std::string::npos); std::cout << " PASSED" << std::endl; std::cout << "Test controls:" << std::endl; auto reload = tool(server, "strawberry_reload_rules"); assert(reload["result"]["isError"] == false && reloads == 1); auto rescan = tool(server, "strawberry_rescan"); assert(rescan["result"]["isError"] == false); auto rescan_again = tool(server, "strawberry_rescan"); assert(rescan_again["result"]["isError"] == true); // rate limited std::cout << " PASSED" << std::endl; std::cout << "Test CLI and MCP findings agree:" << std::endl; const auto cli = json::parse(run_capture("'" + strawberry_bin + "' --list-findings --json '" + config_path + "' 2>/dev/null")); std::map cli_by_id; for (const auto& f : cli["findings"]) { cli_by_id[f["id"].get()] = f; } const auto all = tool(server, "strawberry_list_findings", {{"limit", 200}})["result"]["structuredContent"]["findings"]; assert(all.size() == cli_by_id.size()); for (const auto& f : all) { assert(cli_by_id.at(f["id"].get()) == f); } std::cout << " PASSED" << std::endl; std::cout << "Test daemon down:" << std::endl; daemon.stop(); auto down = tool(server, "strawberry_status"); assert(down["result"]["structuredContent"]["reachable"] == false); assert(tool(server, "strawberry_rescan")["result"]["isError"] == true); auto scan_down = tool(server, "strawberry_scan_paths", {{"paths", {(root / "scan" / "ok.txt").string()}}}); assert(scan_down["result"]["isError"] == true); // Evidence stays queryable without the daemon. assert(!tool(server, "strawberry_list_findings")["result"]["structuredContent"]["findings"].empty()); std::cout << " PASSED" << std::endl; ::chmod((root / "scan" / "locked").c_str(), 0600); fs::remove_all(root, ec); std::cout << std::endl << "All tests passed!" << std::endl; return 0; }