Custom gRPC services¶
Host your application's generated gRPC servicers on the NodeService endpoint. Clients can discover their APIs through reflection and call them with ordinary generated stubs. A service can run without graph nodes, or alongside them.
Python and C++ can register custom services before startup. Existing node-only startup programs keep their existing behavior.
Generate Python and C++ bindings¶
Write your service and message definitions in an application-owned .proto.
The SDK includes neuraverse-generate-protos in its Python wheel and Docker
image. Run it during your application build:
poetry run neuraverse-generate-protos \
--proto-dir proto \
--python-out src/generated
For C++, use the SDK builder environment. To generate both languages in an environment with the Python SDK dependencies and native C++ tools installed:
neuraverse-generate-protos \
--proto-dir proto \
--python-out generated/python \
--cpp-out generated/cpp
Use either output flag alone when you need one language. Output directories
are created automatically. All .proto files below --proto-dir are processed,
preserving their relative paths; optional trailing filenames select individual
files relative to that root. Repeat --include DIR to resolve additional
imports. Imported application messages need their own generated bindings;
include directories are not automatically selected for generation.
| Output | Generated files for records.proto |
|---|---|
| Python | records_pb2.py messages/descriptors and records_pb2_grpc.py client/server bindings |
| C++ | records.pb.h/.cc messages/descriptors and records.grpc.pb.h/.cc client/server bindings |
Put the Python output root on PYTHONPATH or include it in your package. Compile
and link the C++ outputs into your application. Python uses the installed
grpcio-tools; C++ uses native protoc and grpc_cpp_plugin. Select matching
native tools explicitly with --protoc PATH --grpc-cpp-plugin PATH when needed.
The C++ example's CMake build supplies its toolchain's paths automatically.
Missing tools, missing schemas, and compiler errors fail the command. The
generator supplies messages and bindings; you implement the service methods.
NodeService startup loads the generated code and advertises its descriptors
through reflection. It does not compile .proto files at runtime.
Register a Python service¶
Define the API in an application-owned proto and generate its Python messages
and gRPC bindings. Import GrpcServiceRegistration from
neuraverse_sdk.grpc_service_registration and set_grpc_services from
neuraverse_sdk.node_manager_service. Each registration contains:
| Member | Supply |
|---|---|
descriptor |
The generated service descriptor, such as records_pb2.DESCRIPTOR.services_by_name["Records"]. |
add_to_server |
The matching generated add_RecordsServicer_to_server function. |
factory |
A callable receiving the completed NodeServiceConfig and returning your servicer. |
The CRUD example registers its servicer as follows:
def register_services():
set_node_service_class(ApplicationService)
set_grpc_services(
[
GrpcServiceRegistration(
descriptor=records_pb2.DESCRIPTOR.services_by_name["Records"],
add_to_server=records_pb2_grpc.add_RecordsServicer_to_server,
factory=lambda config: RecordsServicer(InMemoryStore()),
)
]
)
Call run() after registration. You may omit set_node_classes() or pass an
empty list when custom services are present. To host graph nodes too, call
set_node_classes([YourNode]) as usual. Custom services require no NodeBase
inheritance, node schema, node ID, graph ID, or graph initialization call.
Factories run once after host configuration and dependency injection are ready. Use the configuration argument or capture your application's dependencies in the factory. The host retains each instance until serving stops; client connections share its state. Handlers can run concurrently, so protect shared state.
The interface accepts normal generated servicers; no convenience base class or NodeGraph message envelope is required. Service-level node dependency hooks remain separate; see Service-level hooks.
Alongside graph nodes¶
Register node classes and custom services before the same run() call. They
share one process and one gRPC endpoint, with separate lifetimes:
flowchart LR
E["Node engine"] --> A
C["Application client"] --> S
C -.->|Discover APIs| R
subgraph H["NodeService: one gRPC endpoint"]
A["Built-in node API"] --> N["Your NodeBase instances"]
S["Custom gRPC servicer"] --> D[("Application state")]
R["Reflection: built-in and custom API schemas"]
end
The engine initializes and manages node instances for graph execution. Custom servicers are created at host startup and remain callable when nodes stop execution or are cleaned up. Both share process resources; separate lifetimes do not provide process isolation.
Run the supplied Python example with --mixed to host IdleNode alongside
the CRUD API. Its ordinary node needs a reachable MQTT broker. Existing node
capability discovery is unchanged; reflection describes RPC services rather
than adding custom APIs to the available-node list.
Discover and call¶
Clients must already know a reachable NodeService address. For a local plaintext listener:
grpcurl -plaintext 127.0.0.1:50051 list
grpcurl -plaintext 127.0.0.1:50051 describe examples.records.v1.Records
Reflection lists registered service names and supplies their methods, message
descriptors, and imported types. Service identity is the fully qualified
protobuf name: company.records.v1.Records and other.records.v1.Records may
share an endpoint. Two registrations of company.records.v1.Records may not.
Loading a proto does not advertise every service declared in it.
Use your generated client stub to call the service. Package the application proto and generated bindings with the server and clients. There is no required central contract package update. The first delivery supports descriptors from the normal generated protobuf pool.
Endpoint discovery does not locate hosts across the platform or add custom services to the graph editor's available-node list.
Authentication and transport¶
Custom methods inherit the host's listener settings and interceptors. Python
requires valid JWT credentials by default. An author may explicitly mark an RPC
with the existing unauthenticated decorator from
neuraverse_common.grpc.auth.jwt_decorators; the exception is scoped to the
fully qualified service and method. Another service's same-named method remains
protected. Application authorization remains the handler's responsibility.
Since SDK 2.51.0
A handler that calls get_frames(), get_robots() or get_paths() with project_service_v2_endpoint set does not forward its caller's token. Project-v2 sees the token the engine sent last, so the call fails until the engine has initialized the node, and the handler must authorize its caller itself. See Scene from project-v2.
Python retains its existing unauthenticated v1alpha reflection policy, including
custom descriptors. C++ retains its configured TLS/mTLS or external transport policy and validates
the same bearer JWT, but per service: the registration declares
Authentication::InHandlers or Authentication::None, and each handler calls
neuraverse::sdk::authenticate(context) first. See
C++ SDK mapping for the registration API and
the jwt__* configuration.
Since SDK 2.47.0
C++ custom services require the Authentication argument at registration. Before
2.47.0 the C++ host had no JWT check for them.
Lifetime, errors, and startup¶
All four generated RPC shapes are supported: unary, server streaming, client
streaming, and bidirectional streaming. Normal gRPC statuses, trailers, deadlines,
and cancellation remain available. Write streaming handlers that cooperate with
cancellation. Long-lived Python streams occupy workers in the existing shared
thread pool; size grpc_server_max_workers for your workload.
Stopping execution, stopping and cleaning up, or using the deprecated Stop leaves custom services available while NodeService remains running. None resets the example's database substitute. Whole-host shutdown allows a five-second gRPC drain, then cancels outstanding calls before releasing servicer ownership. Arbitrary application code cannot be forcibly interrupted; authors must release their own business resources and cooperate with cancellation.
Python service owners can override NodeServiceBase.on_shutdown() to close
shared resources after the servers stop and before forced process exit.
Register the service class before run(); cleanup after that call does not run.
See Service shutdown
for the tested resource-stack example and failure behavior.
The complete service set is registered before accepting requests. Duplicate fully qualified names, names reserved by the framework, missing generated descriptors, factory failures, and invalid generated bindings fail startup with a registration diagnostic. Services are never silently skipped or replaced. Changing registrations after startup is rejected.
Python still rejects a process with neither nodes nor custom application services. C++ keeps its existing empty-host behavior. These rules do not change the graph lifecycle or the built-in NodeService API.
CRUD behavior¶
The runnable example uses one record with string id and value, and an
injected in-memory store. Python and C++ use the same proto and behavior:
| Method | Result |
|---|---|
CreateRecord |
Creates a caller-supplied ID; duplicate IDs return ALREADY_EXISTS. |
GetRecord |
Reads the current value; absent IDs return NOT_FOUND. |
UpdateRecord |
Replaces an existing value; absent IDs return NOT_FOUND. |
DeleteRecord |
Deletes an existing record; absent IDs return NOT_FOUND. |
An empty ID returns INVALID_ARGUMENT; an empty value is valid. Changes are
visible to other clients of the same process. The store resets on process
restart. No real database, persistence, migrations, queries, or transactions
are included.