This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
mgclient is a C library implementing the Bolt protocol client for Memgraph (also compatible with Neo4j Bolt). The core library is C11. A header-only C++17 wrapper (mgclient_cpp) sits on top, and it can also be compiled to WebAssembly via Emscripten.
Standard build (produces libmgclient.a + libmgclient.so/.dylib):
mkdir build && cd build
cmake ..
make
With tests enabled (this also forces BUILD_CPP_BINDINGS=ON):
cmake -DBUILD_TESTING=ON -DBUILD_TESTING_INTEGRATION=ON ..
make
ctest
- Run a single test:
ctest -R encoder(test names:value,encoder,decoder,client,transport,allocator,unit_mgclient_value, plusintegration_basic_c,integration_basic_cpp,example_*). - Unit tests only (no running Memgraph):
ctest -E "example|integration". Theintegration_*andexample_*tests require a live Memgraph on127.0.0.1:7687. - OpenSSL not found: pass
-DOPENSSL_ROOT_DIR=...(see README for macOS/Windows specifics). - WASM build (Linux only):
cmake .. -DWASM=ON && make→ emitsmgclient.js+mgclient.wasm. WASM uses WebSocket transport and has no OpenSSL dependency.
./tool/format.sh runs clang-format (Google style, 80-col, right-aligned pointers) over all *.c/*.h/*.cpp/*.hpp files in place and fails if anything changed. CI runs this on every push/PR as the clang_check job — formatting failures break CI, so run it before committing.
Coverage report: ./tool/coverage.sh (requires a build with -DENABLE_COVERAGE=ON; uses llvm-profdata/llvm-cov).
The library is layered. Public symbols are exported via the MGCLIENT_EXPORT macro (generated mgclient-export.h); everything in src/*.h is internal.
-
include/mgclient.h— the entire public C API and its Doxygen documentation. The big comment block at the top is the authoritative spec for the ownership model (read it before touching value/container code): non-const pointer returns transfer ownership to the caller; const pointer returns are read-only views valid only while the owner lives; insert functions steal ownership of inserted values. Getting this wrong causes double-frees. -
Session layer (
mgsession.c,mgsession.h) —mg_sessionis the connection object and is single-command-at-a-time: youmg_session_runa query, thenmg_session_pullrows until it returns 0 before running anything else.mg_connectperforms the Bolt handshake and HELLO. The session struct holds the in/out buffers, the negotiated Boltversion, transaction state (explicit_transaction), and two allocators (one general, one scoped to decoding). -
Encoder / decoder (
mgsession-encoder.c,mgsession-decoder.c) — serialize/deserialize Bolt messages and values over the chunked Bolt framing. All Bolt markers, struct signatures, and message signatures live insrc/mgconstants.h— this is the reference when adding a new value type or Bolt message. Note that Bolt has multiple protocol versions and some value types (temporal types, ZonedDateTime) are version-gated; check howsession->versionis consulted. -
Transport layer (
mgtransport.c,mgtransport.h) — polymorphicmg_transportstruct of function pointers (send/recv/destroy/suspend hooks). Three implementations:mg_raw_transport(plain socket),mg_secure_transport(OpenSSL/SSL, supports peer pubkey fingerprint verification via trust callback), and the WASM WebSocket path. The session talks only to themg_transportinterface and is agnostic to which one is in use. -
Socket layer — OS-specific, selected at CMake configure time:
src/{linux,apple,windows}/mgsocket.c(matchingmgcommon.hper platform). The build picks exactly one based onMGCLIENT_ON_{LINUX,APPLE,WINDOWS}. -
Values (
mgvalue.c,mgvalue.h) — implementation of all Bolt data types (mg_value,mg_string,mg_list,mg_map,mg_node,mg_relationship,mg_path, temporal types, points). This is the largest file and where the ownership rules frommgclient.hare enforced. -
Allocator (
mgallocator.c,mgallocator.h) — pluggablemg_allocatorinterface; the library allocates through it rather than callingmallocdirectly. -
C++ wrapper (
mgclient_cpp/include/, header-only) —mg::Client(RAII connection withClient::Connect(params)),mg::Value, and an exception hierarchy (MgException→ClientException/TransientException/DatabaseException). Pure wrapper over the C API; no separate compiled library.
tests/*.cpp— unit tests (GTest, fetched viaFetchContentatrelease-1.8.1). They link againstmgclient-staticand the C++ bindings.tests/integration/— require a running Memgraph instance; gated behindBUILD_TESTING_INTEGRATION.client.cppmocksmg_secure_transport_initusing the linker--wrapmechanism (-Wl,--wrap=on Linux,-Wl,-alias,on Apple) — seetests/CMakeLists.txt. If you rename that function, update the wrap flags too.examples/(basic.c,basic.cpp,advanced.cpp) are also compiled and registered as ctest tests; they double as API usage references.
CMakeLists.txt carries two independent version numbers: project(... VERSION x.y.z) and mgclient_SOVERSION. A minor version bump can mean ABI incompatibility — the SOVERSION must be bumped manually when the ABI changes (it is not derived from the project version).