Sandbox
@ziglana/gRPC-zig

Zig gRPC and MCP client-server library

gRPC-zig gives Zig projects client and server implementations for gRPC, with HTTP/2, streaming, compression, TLS, JWT auth, and health checks. The package is split into client, server, transport, protocol, and HTTP/2 modules, with examples and tests showing how the pieces fit together.

147 starsโ€ข31 forksโ€ขZigโ€ขUpdated 7mo ago
Who it's for

Builders who want to add gRPC or MCP networking to Zig projects.

What it delivers

You can build and ship Zig services and clients without wiring gRPC plumbing from scratch.

What it does

Client and server APIs

Provides `GrpcClient` and `GrpcServer` entry points for making calls and serving handlers.

HTTP/2 transport

Implements HTTP/2 support with flow control through the transport layer.

Streaming support

Handles bi-directional streaming for request and response flows.

Authentication and security

Includes JWT authentication and TLS support.

Compression support

Supports gzip and deflate compression for messages.

Health checks

Includes a built-in health checking system.

Benchmarking and tests

Ships a benchmark tool, benchmark script, unit tests, and integration tests.

How to get it

  1. 1Add the dependency to your project
    zig fetch --save git+https://github.com/ziglana/gRPC-zig#main

README

๐Ÿš€ gRPC-zig

A blazingly fast gRPC client & server implementation in Zig, designed for maximum performance and minimal overhead.

License: Unlicense Zig HTTP/2

โšก๏ธ Features

  • ๐Ÿ”ฅ Blazingly Fast: Built from ground up in Zig for maximum performance
  • ๐Ÿ” Full Security: Built-in JWT authentication and TLS support
  • ๐Ÿ—œ๏ธ Compression: Support for gzip and deflate compression
  • ๐ŸŒŠ Streaming: Efficient bi-directional streaming
  • ๐Ÿ’ช HTTP/2: Full HTTP/2 support with proper flow control
  • ๐Ÿฅ Health Checks: Built-in health checking system
  • ๐ŸŽฏ Zero Dependencies: Pure Zig implementation
  • ๐Ÿ” Type Safety: Leverages Zig's comptime for compile-time checks

๐Ÿš€ Quick Start

const std = @import("std");
const GrpcServer = @import("grpc-server").GrpcServer;

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    // Create and configure server
    var server = try GrpcServer.init(allocator, 50051, "secret-key");
    defer server.deinit();

    // Register handlers
    try server.handlers.append(allocator, .{
        .name = "SayHello",
        .handler_fn = sayHello,
    });

    // Start server
    try server.start();
}

fn sayHello(request: []const u8, allocator: std.mem.Allocator) ![]u8 {
    _ = request;
    return allocator.dupe(u8, "Hello from gRPC-zig!");
}

๐Ÿ“š Examples

Basic Server

See examples/basic_server.zig for a complete example.

const std = @import("std");
const GrpcServer = @import("grpc-server").GrpcServer;

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    var server = try GrpcServer.init(allocator, 50051, "secret-key");
    defer server.deinit();

    try server.start();
}

Basic Client

See examples/basic_client.zig for a complete example.

const std = @import("std");
const GrpcClient = @import("grpc-client").GrpcClient;

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();
    const allocator = gpa.allocator();

    var client = try GrpcClient.init(allocator, "localhost", 50051);
    defer client.deinit();

    const response = try client.call("SayHello", "World", .none);
    defer allocator.free(response);

    std.debug.print("Response: {s}\n", .{response});
}

Features

All features are demonstrated in the examples/ directory:

๐Ÿ”ง Installation

Option 1: Using zig fetch (Recommended)

  1. Add the dependency to your project:
zig fetch --save git+https://github.com/ziglana/gRPC-zig#main
  1. Add to your build.zig:
const grpc_zig_dep = b.dependency("grpc_zig", .{
    .target = target,
    .optimize = optimize,
});

// For server development
exe.root_module.addImport("grpc-server", grpc_zig_dep.module("grpc-server"));

// For client development
exe.root_module.addImport("grpc-client", grpc_zig_dep.module("grpc-client"));
  1. Import in your code:
const GrpcServer = @import("grpc-server").GrpcServer;
const GrpcClient = @import("grpc-client").GrpcClient;

Option 2: Manual setup

Clone the repository and add it to your build.zig.zon:

.{
    .name = "my-project",
    .version = "0.1.0",
    .dependencies = .{
        .grpc_zig = .{
            .url = "https://github.com/ziglana/gRPC-zig/archive/refs/heads/main.tar.gz",
            // Replace with actual hash after first fetch
            .hash = "...",
        },
    },
}

๐Ÿƒ Performance

Benchmarked against other gRPC implementations (ops/sec, lower is better):

gRPC-zig    โ”‚โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ–‘โ”‚  2.1ms
gRPC Go     โ”‚โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘โ–‘โ–‘โ–‘โ”‚  3.8ms
gRPC C++    โ”‚โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–‘โ–‘โ”‚  4.2ms

Running Benchmarks

The repository includes a built-in benchmarking tool to measure performance:

# Build the benchmark tool
zig build

# Run benchmarks with default settings
zig build benchmark

# Run with custom parameters
./zig-out/bin/grpc-benchmark --help
./zig-out/bin/grpc-benchmark --requests 1000 --clients 10 --output json

# Or use the convenient script
./scripts/run_benchmark.sh

Benchmark Options:

  • --host <host>: Server host (default: localhost)
  • --port <port>: Server port (default: 50051)
  • --requests <n>: Number of requests per client (default: 1000)
  • --clients <n>: Number of concurrent clients (default: 10)
  • --size <bytes>: Request payload size (default: 1024)
  • --output <format>: Output format: text|json (default: text)

Benchmark Metrics:

  • Latency statistics (min, max, average, P95, P99)
  • Throughput (requests per second)
  • Error rates and success rates
  • Total execution time

The benchmarks automatically run in CI/CD on every pull request and provide performance feedback.

๐Ÿ“– Detailed Benchmarking Guide

๐Ÿงช Testing

Unit Tests

Run the unit test suite:

zig build test

The test suite covers:

  • Compression algorithms (gzip, deflate, none)
  • Benchmark handler functionality
  • Core protocol functionality

Integration Tests

Run integration tests with a Python client validating the Zig server:

cd integration_test
./run_tests.sh

Or manually:

# Build and start the test server
zig build integration_test
./zig-out/bin/grpc-test-server

# In another terminal, run Python tests
cd integration_test
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python3 test_client.py

The integration tests validate:

  • HTTP/2 protocol compliance
  • gRPC request/response flow
  • Compression functionality
  • Health checking
  • Authentication integration

๐Ÿ“– Integration Test Documentation

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

๐Ÿ“œ License

This project is licensed under the Unlicense - see the LICENSE file for details.

โญ๏ธ Support

If you find this project useful, please consider giving it a star on GitHub to show your support!

๐Ÿ™ Acknowledgments

  • Spice - For the amazing Protocol Buffers implementation
  • Tonic - For inspiration on API design
  • The Zig community for their invaluable feedback and support

Made with โค๏ธ in Zig

Files in the repo

Repository payloadโ€ข11 top-level entries
  • .github
  • docs
  • examples
  • integration_test
  • scripts
  • src
  • .gitignore
  • build.zig
  • build.zig.zon
  • LICENSE
  • README.md

Discussion (0)

Ask about usage, or say what you built with it

Sign in to join the discussion.

No comments yet. Be the first to say what this is good for.

More frameworks & sdks

HKUDS/nanobotFrameworks & SDKs

Ultra-lightweight, open-source, self-hosted personal AI agent framework in Python with WebUI, tools, memory, MCP, multi-agent workflows, automation, and chat apps

48k
microsoft/
SkillOpt
microsoft/SkillOptFrameworks & SDKs

SkillOpt is a text-space optimizer that trains reusable natural-language skills for frozen LLM agents through trajectory-driven edits, validation-gated updates, and deployable best_skill.md artifacts.

17k
omnigent-ai/omnigentFrameworks & SDKs

Omnigent is an open-source AI agent framework and meta-harness: orchestrate Claude Code, Codex, Cursor, Pi, and custom agents โ€” swap harnesses without rewriting, enforce policies and sandboxing, and collaborate in real time from any device.

9.8k
kyegomez/
OpenMythos
kyegomez/OpenMythosFrameworks & SDKs

A theoretical reconstruction of the Claude Mythos architecture, built from first principles using the available research literature.

15k
D4Vinci/ScraplingFrameworks & SDKs

๐Ÿ•ท๏ธ An adaptive Web Scraping framework that handles everything from a single request to a full-scale crawl!

80k