pub struct Service { /* private fields */ }Expand description
Service builder. Holds name, bind address, the tonic router being assembled, the optional auth layer, and the optional in-process MCP listener config + custom spawner.
Implementations§
Source§impl Service
impl Service
Sourcepub fn new(name: impl Into<String>) -> Self
pub fn new(name: impl Into<String>) -> Self
Construct a new service. Initializes OTLP tracing as a side effect
(idempotent; disabled if TONIN_TELEMETRY=off).
pub fn addr(self, addr: SocketAddr) -> Self
Sourcepub fn enable_mcp(self) -> Self
pub fn enable_mcp(self) -> Self
Enable the in-process MCP listener on the default port
(0.0.0.0:50052). Same process, second port — MCP tool calls
see the same AuthCtx task-local, share DB/cache/storage
connections, and live or die with the gRPC server. To override
the port use Service::mcp_addr.
What this currently does: spawns a hyper listener that answers
GET /health with 200. The real MCP wire protocol (rmcp +
#[tool] macros bridging each gRPC method) lands in a
follow-up. This call only commits the framework to the
“in-process, second port” lifecycle.
Sourcepub fn mcp_addr(self, addr: SocketAddr) -> Self
pub fn mcp_addr(self, addr: SocketAddr) -> Self
Enable the in-process MCP listener on a specific address.
Useful for tests (bind :0 to grab a random free port) or
for non-default deployments.
Sourcepub fn enable_mcp_with<H, F>(self, factory: F) -> Self
pub fn enable_mcp_with<H, F>(self, factory: F) -> Self
Enable the in-process MCP listener with a custom handler.
Use this to wire a #[tonin::mcp_expose]-generated adapter
into the framework so MCP tools/list exposes one tool per
gRPC method on the user’s impl.
factory is invoked once per MCP session by rmcp’s
StreamableHttpService — returning a fresh handler instance
each time (cheap if the handler is Arc-backed, which the
macro-generated adapter is).
Example:
let svc = Service::new("greeter")
.with_auth(auth::verifier())
.enable_mcp_with(move || {
Ok(GreeterImplMcpAdapter::new(GreeterImpl::new(state.clone())))
});Sourcepub fn with_auth<V: TokenVerifier>(self, verifier: V) -> Self
pub fn with_auth<V: TokenVerifier>(self, verifier: V) -> Self
Install an auth verifier with the default
auth::default::BearerHeaderExtractor.
Every inbound request runs the extractor → verifier chain. The
resulting auth::AuthCtx is placed in request extensions AND
in the auth::CURRENT_AUTH task-local for the handler’s
execution.
For custom token extraction (cookies, custom headers, etc.) build
an auth::AuthLayer directly and pass via
Service::with_auth_layer.
Sourcepub fn with_auth_layer(self, layer: AuthLayer) -> Self
pub fn with_auth_layer(self, layer: AuthLayer) -> Self
Install a fully-customized auth layer. Use this when you need a non-default token extractor (e.g. cookie-based auth).
Sourcepub fn without_auth(self) -> Self
pub fn without_auth(self) -> Self
Run all requests as anonymous. Handlers can still read
AuthCtx::from(&req) — they’ll get
AuthCtx { kind: PrincipalKind::Anonymous, .. }.
For services that genuinely don’t authenticate (read-only public APIs, internal-mesh-only with mTLS as the only check).
Sourcepub fn handler<S>(self, svc: S) -> Self
pub fn handler<S>(self, svc: S) -> Self
Attach a tonic-generated service. Codegen will call this for the user.
Server-side trace-context extraction is installed once, the first time a handler is added; every subsequent handler shares the layer.
The auth layer (if configured via Service::with_auth) wraps
each individual service via tonic’s per-service layering at
run() time, so it applies uniformly to every handler.
pub async fn run(self) -> Result<()>
Auto Trait Implementations§
impl Freeze for Service
impl !RefUnwindSafe for Service
impl Send for Service
impl !Sync for Service
impl Unpin for Service
impl UnsafeUnpin for Service
impl !UnwindSafe for Service
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request