Skip to main content

ServerBuilder

Struct ServerBuilder 

Source
pub struct ServerBuilder { /* private fields */ }
Expand description

Builds a Server.

Implementations§

Source§

impl ServerBuilder

Source

pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self

Starts a server with its name and version (reported as serverInfo).

Source

pub fn title(self, title: impl Into<String>) -> Self

Sets the human-readable title in serverInfo.

Source

pub fn instructions(self, instructions: impl Into<String>) -> Self

Sets the natural-language guidance returned by server/discover.

Source

pub fn tool<T: Tool>(self) -> Self

Registers a tool declared with #[tool] (or implementing Tool).

Source

pub fn auto_tools(self) -> Self

Registers every #[tool] linked into the binary (feature auto-register). Tools can still be added explicitly; duplicates are reported by build.

Source

pub fn tool_def(self, def: ToolDef) -> Self

Registers a tool definition built by hand.

Source

pub fn resource<T: Resource>(self) -> Self

Registers a resource or resource template declared with #[resource] (or implementing Resource).

Source

pub fn auto_resources(self) -> Self

Registers every #[resource] linked into the binary (feature auto-register).

Source

pub fn resource_def(self, def: ResourceDef) -> Self

Registers a resource definition built by hand.

Source

pub fn prompt<T: Prompt>(self) -> Self

Registers a prompt declared with #[prompt] (or implementing Prompt).

Source

pub fn auto_prompts(self) -> Self

Registers every #[prompt] linked into the binary (feature auto-register).

Source

pub fn prompt_def(self, def: PromptDef) -> Self

Registers a prompt definition built by hand.

Source

pub fn state<T: Send + Sync + 'static>(self, value: T) -> Self

Registers shared state, available to handlers through State<T>.

Source

pub fn extension(self, extension: impl Extension) -> Self

Registers a protocol extension.

Source

pub fn limits(self, limits: Limits) -> Self

Replaces all limits at once.

Source

pub fn max_message_bytes(self, bytes: usize) -> Self

Maximum size of one message in bytes. Default: 4 MiB.

Source

pub fn max_json_depth(self, depth: usize) -> Self

Maximum JSON nesting depth. Default: 64 (at most 128).

Source

pub fn tool_timeout(self, timeout: Duration) -> Self

Maximum duration of one tool call, resource read or prompt. Default: 30 s.

Source

pub fn max_concurrent_calls(self, calls: usize) -> Self

Maximum number of concurrent handler invocations (tools, resources and prompts). Default: 64.

Source

pub fn list_ttl(self, ttl: Duration) -> Self

Freshness hint (ttlMs) for tools/list, resources/list, resources/templates/list and prompts/list. Default: 5 minutes.

Source

pub fn discover_ttl(self, ttl: Duration) -> Self

Freshness hint (ttlMs) for server/discover. Default: 1 hour.

Source

pub fn cache_scope(self, scope: CacheScope) -> Self

cacheScope of list and discovery results. Default: public. Use private when results depend on the caller’s authorization.

Source

pub fn request_state_key(self, key: impl Into<Vec<u8>>) -> Self

Secret key that signs requestState (HMAC-SHA256), at least MIN_REQUEST_STATE_KEY_BYTES bytes. Default: a random key per process, which is enough for a single instance. Set the same key on every instance when requests may be retried on another instance (load balancing), and keep it secret.

Source

pub fn request_state_ttl(self, ttl: Duration) -> Self

How long a client may take to answer an input request (the lifetime of requestState). Default: 10 minutes.

Source

pub fn legacy_clients(self, enabled: bool) -> Self

Whether clients of the older protocol versions (2025-11-25 and 2025-06-18, which open with initialize) are served. Default: true. Handlers do not change: the SDK adapts the protocol, including ctx.elicit, which becomes a request to the client.

Source

pub fn legacy_session_ttl(self, ttl: Duration) -> Self

Lifetime of the signed MCP-Session-Id given to legacy HTTP clients; they reconnect with initialize after it. Default: 24 hours.

Source

pub fn include_server_info(self, include: bool) -> Self

Whether results carry io.modelcontextprotocol/serverInfo in _meta (recommended by the specification). Default: true.

Source

pub fn build(self) -> Result<Server, BuildError>

Validates the configuration and builds the server.

§Errors

Returns a BuildError for duplicate or invalid names and URIs, missing state, invalid completion providers, invalid extensions, out-of-range limits, or serialization failures of the pre-built responses.

Trait Implementations§

Source§

impl Debug for ServerBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more