pub struct ServerBuilder { /* private fields */ }Expand description
Builds a Server.
Implementations§
Source§impl ServerBuilder
impl ServerBuilder
Sourcepub fn new(name: impl Into<String>, version: impl Into<String>) -> Self
pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self
Starts a server with its name and version (reported as serverInfo).
Sourcepub fn title(self, title: impl Into<String>) -> Self
pub fn title(self, title: impl Into<String>) -> Self
Sets the human-readable title in serverInfo.
Sourcepub fn instructions(self, instructions: impl Into<String>) -> Self
pub fn instructions(self, instructions: impl Into<String>) -> Self
Sets the natural-language guidance returned by server/discover.
Sourcepub fn tool<T: Tool>(self) -> Self
pub fn tool<T: Tool>(self) -> Self
Registers a tool declared with #[tool] (or implementing Tool).
Sourcepub fn auto_tools(self) -> Self
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.
Sourcepub fn resource<T: Resource>(self) -> Self
pub fn resource<T: Resource>(self) -> Self
Registers a resource or resource template declared with #[resource]
(or implementing Resource).
Sourcepub fn auto_resources(self) -> Self
pub fn auto_resources(self) -> Self
Registers every #[resource] linked into the binary (feature
auto-register).
Sourcepub fn resource_def(self, def: ResourceDef) -> Self
pub fn resource_def(self, def: ResourceDef) -> Self
Registers a resource definition built by hand.
Sourcepub fn prompt<T: Prompt>(self) -> Self
pub fn prompt<T: Prompt>(self) -> Self
Registers a prompt declared with #[prompt] (or implementing
Prompt).
Sourcepub fn auto_prompts(self) -> Self
pub fn auto_prompts(self) -> Self
Registers every #[prompt] linked into the binary (feature
auto-register).
Sourcepub fn prompt_def(self, def: PromptDef) -> Self
pub fn prompt_def(self, def: PromptDef) -> Self
Registers a prompt definition built by hand.
Sourcepub fn state<T: Send + Sync + 'static>(self, value: T) -> Self
pub fn state<T: Send + Sync + 'static>(self, value: T) -> Self
Registers shared state, available to handlers through State<T>.
Sourcepub fn max_message_bytes(self, bytes: usize) -> Self
pub fn max_message_bytes(self, bytes: usize) -> Self
Maximum size of one message in bytes. Default: 4 MiB.
Sourcepub fn max_json_depth(self, depth: usize) -> Self
pub fn max_json_depth(self, depth: usize) -> Self
Maximum JSON nesting depth. Default: 64 (at most 128).
Sourcepub fn tool_timeout(self, timeout: Duration) -> Self
pub fn tool_timeout(self, timeout: Duration) -> Self
Maximum duration of one tool call, resource read or prompt. Default: 30 s.
Sourcepub fn max_concurrent_calls(self, calls: usize) -> Self
pub fn max_concurrent_calls(self, calls: usize) -> Self
Maximum number of concurrent handler invocations (tools, resources and prompts). Default: 64.
Sourcepub fn list_ttl(self, ttl: Duration) -> Self
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.
Sourcepub fn discover_ttl(self, ttl: Duration) -> Self
pub fn discover_ttl(self, ttl: Duration) -> Self
Freshness hint (ttlMs) for server/discover. Default: 1 hour.
Sourcepub fn cache_scope(self, scope: CacheScope) -> Self
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.
Sourcepub fn request_state_key(self, key: impl Into<Vec<u8>>) -> Self
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.
Sourcepub fn request_state_ttl(self, ttl: Duration) -> Self
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.
Sourcepub fn legacy_clients(self, enabled: bool) -> Self
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.
Sourcepub fn legacy_session_ttl(self, ttl: Duration) -> Self
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.
Sourcepub fn include_server_info(self, include: bool) -> Self
pub fn include_server_info(self, include: bool) -> Self
Whether results carry io.modelcontextprotocol/serverInfo in _meta
(recommended by the specification). Default: true.
Sourcepub fn build(self) -> Result<Server, BuildError>
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.