Define a service
An RPC service is the interface a peer offers to callers: named methods with request and response schemas. For example, a task worker can accept an assignment, stream progress, and receive artifact chunks through one service. This guide shows Python and TypeScript definitions; Python decorator reference gives the detailed Python options.
Start with a working first service. TypeScript also needs the metadata build. Most blocks below are definition fragments: names such as WatchRequest and application helpers must come from your application. They do not start a server or connect a client.
The language tabs illustrate each language independently. Their method and field naming conventions, and default numeric types, differ; matching wire names alone does not make the two examples a compatible pair. Use the executed cross-language example when testing one language against another.
Wire types
Section titled “Wire types”Every request and response type needs a stable wire identity for cross-language serialization. Define your types as dataclasses and tag them with @wire_type:
from dataclasses import dataclassfrom aster import wire_type
@wire_type("myapp/TaskRequest")@dataclassclass TaskRequest: task_id: str = "" priority: int = 0
@wire_type("myapp/TaskResult")@dataclassclass TaskResult: task_id: str = "" status: str = "" output: str = ""import { WireType } from '@aster-rpc/aster';
@WireType("myapp/TaskRequest")class TaskRequest { taskId = ""; priority = 0; constructor(init?: Partial<TaskRequest>) { if (init) Object.assign(this, init); }}
@WireType("myapp/TaskResult")class TaskResult { taskId = ""; status = ""; output = ""; constructor(init?: Partial<TaskResult>) { if (init) Object.assign(this, init); }}The tag string (e.g. "myapp/TaskRequest") is the canonical wire identity. It must be unique within your deployment and stable across releases. The format is namespace/TypeName, where the namespace is typically your package or domain.
Auto-tagging vs explicit @wire_type
Section titled “Auto-tagging vs explicit @wire_type”If you omit @wire_type, the @service decorator auto-derives a tag from the module and class name at decoration time (e.g. myapp.tasks.TaskRequest). This works for development, but auto-tags are fragile: renaming a module or moving a class changes the wire identity and breaks existing consumers.
Production recommendation: Always use explicit @wire_type tags. The @service decorator emits a warning when it auto-tags a type.
The @service decorator
Section titled “The @service decorator”The @service decorator marks a class as an Aster RPC service. It scans the class for decorated methods, extracts type information from signatures, and validates wire types.
Calling forms
Section titled “Calling forms”from aster import service
# Bare -- service name defaults to class name@serviceclass TaskService: ...
# Explicit name@service("TaskEngine")class TaskService: ...
# With options@service(name="TaskEngine", version=2)class TaskService: ...import { Service } from '@aster-rpc/aster';
// With options (name is always explicit in TypeScript)@Service({ name: "TaskEngine", version: 2 })class TaskService { // ...methods}
// Minimal — version defaults to 1@Service({ name: "TaskEngine" })class TaskService { // ...methods}Service options
Section titled “Service options”| Option | Python | TypeScript | Default | Description |
|---|---|---|---|---|
name |
str |
string |
Class name | Service name used in the wire protocol and discovery. |
version |
int |
number |
1 |
Service version. Distinct versions are separate services. |
serialization |
SerializationMode or list |
SerializationMode[] |
[XLANG] |
Supported serialization modes. |
scoped |
str |
'shared' | 'session' |
"shared" |
"shared" for singleton, "session" for instances keyed by connection, session ID, and service (Python __init__ receives peer). |
requires |
CapabilityRequirement |
CapabilityRequirement |
None / undefined |
Capability required to call any method on this service. |
RPC methods
Section titled “RPC methods”Unary (@rpc / @Rpc)
Section titled “Unary (@rpc / @Rpc)”A unary method takes one request and returns one response:
from aster import service, rpc
@serviceclass TaskService:
@rpc async def assign_task(self, req: TaskRequest) -> TaskResult: return TaskResult(task_id=req.task_id, status="assigned")@rpc supports both bare (@rpc) and parameterized (@rpc(...)) syntax.
import { Service, Rpc } from '@aster-rpc/aster';
@Service({ name: "TaskService" })class TaskService { @Rpc() async assignTask(req: TaskRequest): Promise<TaskResult> { return new TaskResult({ taskId: req.taskId, status: "assigned" }); }}@Rpc() accepts optional { timeout, idempotent, requires } options. The scanner reads request/response types from the AST — run npx aster-gen before starting your server. See TypeScript Build Setup.
Server streaming (@server_stream / @ServerStream)
Section titled “Server streaming (@server_stream / @ServerStream)”The server sends a stream of responses for a single request. The method must be an async generator:
from typing import AsyncIterator
@serviceclass TaskService:
@server_stream async def watch_tasks(self, req: WatchRequest) -> AsyncIterator[TaskUpdate]: for task in get_pending_tasks(): yield TaskUpdate(task_id=task.id, status=task.status)@Service({ name: "TaskService" })class TaskService { @ServerStream() async *watchTasks(req: WatchRequest): AsyncGenerator<TaskUpdate> { for (const task of getPendingTasks()) { yield new TaskUpdate({ taskId: task.id, status: task.status }); } }}Client streaming (@client_stream / @ClientStream)
Section titled “Client streaming (@client_stream / @ClientStream)”The client sends a stream of requests; the server returns a single response:
@serviceclass TaskService:
@client_stream async def upload_artifacts(self, stream: AsyncIterator[ArtifactChunk]) -> UploadResult: total_bytes = 0 async for chunk in stream: total_bytes += len(chunk.data) return UploadResult(total_bytes=total_bytes)@Service({ name: "TaskService" })class TaskService { @ClientStream() async uploadArtifacts(stream: AsyncIterable<ArtifactChunk>): Promise<UploadResult> { let totalBytes = 0; for await (const chunk of stream) { totalBytes += chunk.data.length; } return new UploadResult({ totalBytes }); }}Bidirectional streaming (@bidi_stream / @BidiStream)
Section titled “Bidirectional streaming (@bidi_stream / @BidiStream)”Both sides stream concurrently. The method receives an async iterator and yields responses:
@serviceclass TaskService:
@bidi_stream async def interactive_review( self, requests: AsyncIterator[ReviewItem] ) -> AsyncIterator[ReviewResult]: async for item in requests: yield ReviewResult(item_id=item.id, approved=True)@Service({ name: "TaskService" })class TaskService { @BidiStream() async *interactiveReview( requests: AsyncIterable<ReviewItem> ): AsyncGenerator<ReviewResult> { for await (const item of requests) { yield new ReviewResult({ itemId: item.id, approved: true }); } }}Method options
Section titled “Method options”All method decorators accept these keyword arguments:
| Option | Type | Default | Description |
|---|---|---|---|
timeout |
float or None |
None |
Default timeout in seconds for this method. |
idempotent |
bool |
False |
Marks the method as safe to retry; automatic retries require an installed retry policy. |
serialization |
SerializationMode or None |
None |
Override the service-level serialization for this method. |
requires |
CapabilityRequirement or None |
None |
Capability required to call this specific method. |
@rpc(timeout=30.0, idempotent=True)async def get_task(self, req: TaskRequest) -> TaskResult: ...Serialization modes
Section titled “Serialization modes”Use SerializationMode.XLANG, the default, for cross-language calls. Python also exposes NATIVE and ROW; those modes are Python-only. See serialization for their limits.
Dynamic proxies accept plain objects and construct XLANG payloads from the producer’s published manifest. They require usable type metadata. JSON wire mode has been retired.
Session-scoped services
Section titled “Session-scoped services”By default, one shared service instance handles calls from all connections. A session-scoped instance belongs to a connection, session ID, and service. Two sessions on one connection can therefore hold separate chat state. Each method call uses its own stream; ending a call stream does not close the session.
@service("ChatRoom", scoped="session")class ChatRoomService:
def __init__(self, peer: str): self.peer = peer self.nickname = f"anon-{peer[:8]}" self.messages: list[str] = []
@rpc async def set_nickname(self, req: SetNicknameRequest) -> SetNicknameResponse: self.nickname = req.nickname return SetNicknameResponse(greeting=f"Welcome, {req.nickname}!")
@rpc async def send_message(self, req: SendMessageRequest) -> SendMessageResponse: self.messages.append(req.text) return SendMessageResponse(echo=f"[{self.nickname}] {req.text}")
async def close(self) -> None: """Scheduled when the session closes.""" print(f"Session closed for {self.nickname}: {len(self.messages)} messages")Requirements for session-scoped services:
__init__must accept the namedpeerargument (the remote peer’s endpoint ID as a hex string). The factory callscls(peer=...)for a new session instance.- Instance attributes persist across all RPC calls within the session.
- The optional
close()hook runs when the session is released. An async hook is scheduled rather than awaited by session cleanup; own and await critical durable cleanup separately. - Session-scoped services associate method calls with a connection-scoped session ID. Each call uses its own stream; the service instance is shared for the session lifetime.
Complete service definition
Section titled “Complete service definition”This block defines payloads and handlers. Register the service with the matching server API to expose it; it is not a standalone application.
from dataclasses import dataclassfrom typing import AsyncIterator
from aster import ( service, rpc, server_stream, client_stream, bidi_stream, wire_type, SerializationMode,)
# --- Wire types ---
@wire_type("example.tasks/TaskRequest")@dataclassclass TaskRequest: task_id: str = "" priority: int = 0
@wire_type("example.tasks/TaskResult")@dataclassclass TaskResult: task_id: str = "" status: str = ""
@wire_type("example.tasks/WatchRequest")@dataclassclass WatchRequest: filter: str = ""
@wire_type("example.tasks/TaskUpdate")@dataclassclass TaskUpdate: task_id: str = "" status: str = ""
@wire_type("example.tasks/LogEntry")@dataclassclass LogEntry: line: str = ""
@wire_type("example.tasks/IngestResult")@dataclassclass IngestResult: count: int = 0
@wire_type("example.tasks/Command")@dataclassclass Command: action: str = ""
@wire_type("example.tasks/CommandResult")@dataclassclass CommandResult: output: str = ""
# --- Service ---
@service("TaskEngine", version=1)class TaskService:
@rpc(timeout=10.0, idempotent=True) async def get_task(self, req: TaskRequest) -> TaskResult: return TaskResult(task_id=req.task_id, status="running")
@server_stream async def watch_tasks(self, req: WatchRequest) -> AsyncIterator[TaskUpdate]: for i in range(5): yield TaskUpdate(task_id=f"task-{i}", status="active")
@client_stream async def ingest_logs(self, stream: AsyncIterator[LogEntry]) -> IngestResult: count = 0 async for entry in stream: count += 1 return IngestResult(count=count)
@bidi_stream async def execute( self, commands: AsyncIterator[Command] ) -> AsyncIterator[CommandResult]: async for cmd in commands: yield CommandResult(output=f"executed: {cmd.action}")This service can be hosted with AsterServer(services=[TaskService()]) and consumed via AsterClient. See Hello Service for a runnable end-to-end example.
Verify the four patterns
Section titled “Verify the four patterns”Register the complete TaskService definition above with the Python server, then connect its typed client. A get_task request for task-7 returns that ID with status running; watch_tasks yields five updates; ingest_logs returns the number of supplied entries; execute echoes each action as executed: <action>.
For a session service, open two sessions on the same connection, set a different nickname in each, and verify calls retain the matching state. Close one session and confirm the other remains usable. Do not use a completed method stream as evidence that session cleanup ran.
Apply deadlines and cancel a stream while it is active. The application must release its own work and resources according to the binding’s cancellation behavior. This definition does not implement durable task storage or deduplicate mutations.