Context
Context
Section titled “Context”Context propagation for metadata across service calls
Overview
Section titled “Overview”The context object carries metadata that propagates through the entire call chain. When Service A calls Service B, and B calls Service C, context flows A→B→C.
┌─────────────┐ ┌─────────────┐ ┌─────────────┐│ Service A │────▶│ Service B │────▶│ Service C ││ │ │ │ │ ││ tenant: X │ │ tenant: X │ │ tenant: X ││ caller: A │ │ caller: B │ │ caller: C │└─────────────┘ └─────────────┘ └─────────────┘Context Object
Section titled “Context Object”{ "context": { "caller": "checkout-service", "tenant_id": "tenant_acme", "user_id": "user_42" }}Standard Fields
Section titled “Standard Fields”| Field | Type | Required | Description |
|---|---|---|---|
caller | string | No | Identifier of the calling service |
Custom Fields
Section titled “Custom Fields”Applications MAY add custom fields to context for domain-specific propagation:
{ "context": { "caller": "checkout-service", "tenant_id": "tenant_acme", "user_id": "user_42", "feature_flags": ["new_checkout", "beta_pricing"], "correlation_id": "order_12345" }}Common custom fields:
tenant_id— Multi-tenant isolationuser_id— Acting user (for audit)feature_flags— Active feature flagscorrelation_id— Business correlationenvironment— Environment hint (staging, production)
Propagation Rules
Section titled “Propagation Rules”Incoming Requests
Section titled “Incoming Requests”When receiving a request, servers MUST:
- Extract
contextfrom the request - Preserve custom fields unchanged
- Update
callerto current service name for downstream calls
Outgoing Requests
Section titled “Outgoing Requests”When making downstream calls, clients MUST:
- Copy custom fields from current context
- Set
callerto current service name - Propagate custom fields as appropriate
Example Chain
Section titled “Example Chain”Request A→B:
{ "context": { "caller": "api-gateway", "tenant_id": "tenant_acme" }}Request B→C:
{ "context": { "caller": "order-service", "tenant_id": "tenant_acme" }}Context vs Extensions
Section titled “Context vs Extensions”Context and extensions serve different purposes:
| Aspect | Context | Extensions |
|---|---|---|
| Propagates | Yes, through call chain | Per-request (may influence downstream) |
| Purpose | Business metadata | Optional capabilities |
| Examples | tenant_id, user_id, caller | deadline, idempotency, tracing |
Rule of thumb:
- If downstream services need it automatically → Context
- If it’s an optional capability → Extensions
Distributed Tracing
Section titled “Distributed Tracing”For distributed tracing (trace_id, span_id, parent_span_id), use the Tracing Extension. This keeps observability concerns separate from business context.
Missing Context
Section titled “Missing Context”Servers MUST handle requests without context gracefully:
// Request without context{ "protocol": { "name": "mesh", "version": "0.1.0" }, "id": "req_123", "call": { "function": "orders.get", "version": "1", "arguments": { "order_id": 42 } }}When context is missing:
- Servers SHOULD log a warning
- Servers MUST process the request normally
Examples
Section titled “Examples”Basic Context
Section titled “Basic Context”{ "protocol": { "name": "mesh", "version": "0.1.0" }, "id": "req_basic", "call": { "function": "orders.create", "version": "1", "arguments": { ... } }, "context": { "caller": "web-frontend" }}Multi-Tenant Context
Section titled “Multi-Tenant Context”{ "protocol": { "name": "mesh", "version": "0.1.0" }, "id": "req_multi_tenant", "call": { "function": "reports.generate", "version": "1", "arguments": { ... } }, "context": { "caller": "dashboard-service", "tenant_id": "tenant_acme_corp", "user_id": "user_42", "feature_flags": ["new_report_engine"] }}With Tracing Extension
Section titled “With Tracing Extension”For full distributed tracing, combine context with the tracing extension:
{ "protocol": { "name": "mesh", "version": "0.1.0" }, "id": "req_traced", "call": { "function": "orders.create", "version": "1", "arguments": { ... } }, "context": { "caller": "checkout-service", "tenant_id": "tenant_acme" }, "extensions": [ { "urn": "urn:mesh:ext:tracing", "options": { "trace_id": "tr_abc123def456", "span_id": "sp_001" } } ]}