SRPC Compiler
The SRPC Compiler (protoc-srpc-plugin) is a protoc plugin that generates Python client stubs and server servicers for SRPC (Slim RPC) from Protocol Buffer service definitions. This plugin enables you to build high-performance RPC services using the SRPC framework.
Features
- Generates Python client stubs for calling SRPC services
- Generates Python server servicers for implementing SRPC services
- Supports all gRPC streaming patterns: unary-unary, unary-stream, stream-unary, and stream-stream
- Compatible with both
protocandbufbuild systems - Automatic import resolution for Protocol Buffer dependencies
Installation
Option 1: Install via Cargo
This will install the protoc-srpc-plugin binary to your Cargo bin directory (usually ~/.cargo/bin).
Option 2: Compile from Source
- Clone the repository:
- Build the plugin:
- The compiled binary will be available at
data-plane/target/release/protoc-srpc-plugin
Usage
Example Protocol Buffer Definition
Create a file called example.proto:
syntax = "proto3";
package example_service;
service Test {
rpc ExampleUnaryUnary(ExampleRequest) returns (ExampleResponse);
rpc ExampleUnaryStream(ExampleRequest) returns (stream ExampleResponse);
rpc ExampleStreamUnary(stream ExampleRequest) returns (ExampleResponse);
rpc ExampleStreamStream(stream ExampleRequest) returns (stream ExampleResponse);
}
message ExampleRequest {
string example_string = 1;
int64 example_integer = 2;
}
message ExampleResponse {
string example_string = 1;
int64 example_integer = 2;
}
Using with protoc
Prerequisites
Make sure you have:
protoc(Protocol Buffer compiler) installed- The
protoc-srpc-pluginbinary in your PATH or specify its full path
Generate Python Files
# Generate both the protobuf Python files and SRPC files
This will generate:
example_pb2.py- Standard protobuf Python bindingsexample_pb2_srpc.py- SRPC client stubs and server servicers
With Custom Types Import
You can specify a custom import for the types module. This allows to import the types from an external package.
For instance, if you don't want to generate the types and you want to import them from a2a.grpc.a2a_pb2`,, you can do:
Using with buf
Prerequisites
bufCLI installedprotoc-srpc-pluginbinary in your PATH
Create buf.gen.yaml
Create a buf.gen.yaml file in your project root:
version: v2
managed:
enabled: true
inputs:
- proto_file: example.proto
plugins:
- local: /path/to/protoc-srpc-plugin
out: .
- remote: buf.build/protocolbuffers/python
out: .
Generate Code
Or generate from a specific file:
Advanced buf Configuration
For more complex setups with custom options:
version: v2
managed:
enabled: true
plugins:
- local: protoc-srpc-plugin
out: generated
opt:
- types_import=from .pb2_types import example_pb2 as pb2
strategy: all
- remote: buf.build/protocolbuffers/python
out: generated
strategy: all
Generated Code Structure
For the example above, the generated example_pb2_srpc.py will contain:
Client Stub
"""Client stub for Test."""
"""Constructor.
Args:
channel: A srpc.Channel.
"""
=
=
# ... other methods
Server Servicer
"""Server servicer for Test. Implement this class to provide your service logic."""
"""Method for ExampleUnaryUnary. Implement your service logic here."""
# ... other methods
Registration Function
# Registers the servicer with the SRPC server
pass
Plugin Parameters
The plugin supports the following parameters:
types_import: Customize how protobuf types are imported- Example:
types_import="from my_package import types_pb2 as pb2" - Default: Uses local import based on the proto file name
- Example:
Example Usage in Python
Client Usage
=
=
# Stubs
=
# Call method
=
= await
=
yield
= await
Server Usage
=
return
# generate async responses stream
yield
=
return
"""Missing associated documentation comment in .proto file."""
"""
Create a new SRPC server instance.
"""
=
return
=
# Create RPCs
await
Troubleshooting
Plugin Not Found
If you get an error that the plugin is not found:
- Ensure
protoc-srpc-pluginis in your PATH - Or specify the full path:
--plugin=protoc-gen-srpc=/full/path/to/protoc-srpc-plugin
Import Errors
If you encounter Python import errors:
- Make sure the generated
*_pb2.pyfiles are in your Python path - Use the
types_importparameter to customize import paths - Ensure all Protocol Buffer dependencies are generated
Build Errors
If the plugin fails to build:
- Ensure you have Rust and Cargo installed
- Check that all dependencies are available
- Try cleaning and rebuilding:
cargo clean && cargo build --release
Contributing
Please see the main repository's contributing guidelines at CONTRIBUTING.md.
License
This project is licensed under the Apache 2.0 License - see the LICENSE.md file for details.