ZAP Protocol
Language Bindings

C++

ZAP C++ SDK - High-performance native binding

C++ Binding

The C++ binding provides high-performance native access to ZAP with async I/O support.

Installation

Using CMake FetchContent:

include(FetchContent)
FetchContent_Declare(
  zap-cpp
  GIT_REPOSITORY https://github.com/zap-protocol/zap-cpp
  GIT_TAG v0.1.0
)
FetchContent_MakeAvailable(zap-cpp)

target_link_libraries(your_target PRIVATE zap::zap)

Quick Start

#include <zap/zap.hpp>
#include <iostream>

int main() {
    auto client = zap::connect("zap://localhost:9000").wait();

    auto server = client->init({
        .name = "my-agent",
        .version = "1.0.0"
    }).wait();

    std::cout << "Connected to: " << server.name << " v" << server.version << "\n";

    auto tools = client->listTools().wait();
    for (const auto& tool : tools) {
        std::cout << "  " << tool.name << " - " << tool.description << "\n";
    }

    auto result = client->callTool({
        .id = "call-1",
        .name = "read_file",
        .args = R"({"path": "/etc/hosts"})"
    }).wait();

    if (result.error.empty()) {
        std::cout << "Result: " << result.content << "\n";
    }

    return 0;
}

Async API

#include <zap/zap.hpp>

zap::Promise<void> runAgent() {
    auto client = co_await zap::connect("zap://localhost:9000");

    auto server = co_await client->init({
        .name = "my-agent",
        .version = "1.0.0"
    });

    auto tools = co_await client->listTools();

    for (const auto& tool : tools) {
        auto result = co_await client->callTool({
            .id = tool.name,
            .name = tool.name,
            .args = "{}"
        });
        // Process result
    }
}

int main() {
    zap::EventLoop loop;
    runAgent().wait();
    return 0;
}

Gateway

auto gateway = zap::Gateway::create();

auto fsId = co_await gateway->addServer(
    "filesystem",
    "stdio://npx -y @modelcontextprotocol/server-filesystem /tmp",
    {.transport = zap::Transport::Stdio}
);

auto tools = co_await gateway->listTools();

Documentation

Full documentation at zap-protocol/zap-cpp.

Last updated on

On this page