pub struct AuthorizedServer<S: ServerHandler, A = NoAuth> { /* private fields */ }Expand description
A ServerHandler wrapper that adds authorization-based schema shaping.
Wraps any inner ServerHandler and intercepts list_tools / call_tool to
filter tools and shape schemas based on the request’s
AuthContext.
§Compile-time guarantee
In the spirit of Proof<C> — which makes skipping a
capability check uncompilable — forgetting to wire authentication is a
build error, not a runtime panic. ServerHandler is implemented only for
AuthorizedServer<S, Authorized<P>>, so a server with no auth source chosen
cannot be served:
use mcp_authorization::AuthorizedServer;
use rmcp::handler::server::ServerHandler;
use rmcp::model::{ServerInfo, ServerCapabilities, Implementation};
struct Inner;
impl ServerHandler for Inner {
fn get_info(&self) -> ServerInfo {
ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
.with_server_info(Implementation::new("inner", "0.0.0"))
}
}
fn requires_handler<T: ServerHandler>(_: T) {}
// No auth source chosen → not a ServerHandler → does not compile.
requires_handler(AuthorizedServer::new(Inner));Choosing an auth source makes it servable:
use mcp_authorization::AuthorizedServer;
use rmcp::handler::server::ServerHandler;
use rmcp::model::{ServerInfo, ServerCapabilities, Implementation};
struct Inner;
impl ServerHandler for Inner {
fn get_info(&self) -> ServerInfo {
ServerInfo::new(ServerCapabilities::builder().enable_tools().build())
.with_server_info(Implementation::new("inner", "0.0.0"))
}
}
fn requires_handler<T: ServerHandler>(_: T) {}
// deny_by_default() (or with_auth(..)) yields a real ServerHandler.
requires_handler(AuthorizedServer::new(Inner).deny_by_default());Implementations§
Source§impl<S: ServerHandler> AuthorizedServer<S, NoAuth>
impl<S: ServerHandler> AuthorizedServer<S, NoAuth>
Sourcepub fn new(inner: S) -> Self
pub fn new(inner: S) -> Self
Start building an authorized server. No auth source is chosen yet, so the
result is not yet a ServerHandler — call
with_auth or deny_by_default.
Source§impl<S: ServerHandler, A> AuthorizedServer<S, A>
impl<S: ServerHandler, A> AuthorizedServer<S, A>
Sourcepub fn register<I, O>(
self,
name: impl Into<String>,
description: impl Into<String>,
) -> Selfwhere
I: JsonSchema + AuthSchemaMetadata + DeserializeOwned + 'static,
O: JsonSchema + AuthSchemaMetadata + Serialize + 'static,
pub fn register<I, O>(
self,
name: impl Into<String>,
description: impl Into<String>,
) -> Selfwhere
I: JsonSchema + AuthSchemaMetadata + DeserializeOwned + 'static,
O: JsonSchema + AuthSchemaMetadata + Serialize + 'static,
Register a tool with typed input/output for schema generation and authorization metadata.
Set tool-level authorization for a named tool. The tool is hidden from
list_tools if the request’s AuthContext lacks this capability.
Sourcepub fn with_auth<P: AuthProvider>(
self,
provider: P,
) -> AuthorizedServer<S, Authorized<P>>
pub fn with_auth<P: AuthProvider>( self, provider: P, ) -> AuthorizedServer<S, Authorized<P>>
Choose an explicit auth source. Required before serving any network
transport. Transitions the server into the servable
Authorized state.
provider is anything implementing AuthProvider, including a closure
Fn(&RequestContext<RoleServer>) -> AuthContext.
Sourcepub fn deny_by_default(self) -> AuthorizedServer<S, Authorized<DenyByDefault>>
pub fn deny_by_default(self) -> AuthorizedServer<S, Authorized<DenyByDefault>>
Install DenyByDefault: use an AuthContext injected by middleware if
present, otherwise resolve to AuthContext::empty.
The ergonomic choice for stdio / local / dev: the server is immediately servable and an unauthenticated client sees the least-privileged view (only ungated tools) rather than an error.
Sourcepub fn registry(&self) -> &AuthToolRegistry
pub fn registry(&self) -> &AuthToolRegistry
Get a reference to the authorization registry.