Skip to main content

ServerBuilder

Struct ServerBuilder 

Source
pub struct ServerBuilder<H: McpHandler> { /* private fields */ }
Expand description

Server builder for configuring and running MCP servers.

This builder provides a fluent API for:

  • Selecting transport at runtime
  • Configuring rate limits and connection limits
  • Setting up graceful shutdown
  • Integrating with existing server infrastructure

§Example

use turbomcp::prelude::*;

MyServer.builder()
    .transport(Transport::http("0.0.0.0:8080"))
    .with_rate_limit(100, Duration::from_secs(1))
    .serve()
    .await?;

Implementations§

Source§

impl<H: McpHandler> ServerBuilder<H>

Source

pub fn new(handler: H) -> Self

Create a new server builder wrapping the given handler.

Source

pub fn transport(self, transport: Transport) -> Self

Set the transport for this server.

§Example
builder.transport(Transport::http("0.0.0.0:8080"))
Source

pub fn with_rate_limit(self, max_requests: u32, window: Duration) -> Self

Configure rate limiting.

§Arguments
  • requests - Maximum requests allowed
  • per - Time window for the limit
§Example
// Allow 100 requests per second
builder.with_rate_limit(100, Duration::from_secs(1))
Source

pub fn with_connection_limit(self, max: usize) -> Self

Configure maximum concurrent connections.

This limit applies to TCP, HTTP, WebSocket, and Unix transports. STDIO transport always has exactly one connection.

§Example
builder.with_connection_limit(1000)
Source

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

Configure graceful shutdown timeout.

When the server receives a shutdown signal, it will wait up to this duration for in-flight requests to complete.

§Example
builder.with_graceful_shutdown(Duration::from_secs(30))
Source

pub fn with_max_message_size(self, size: usize) -> Self

Configure maximum message size.

Messages exceeding this size will be rejected. Default: 10MB.

§Example
// Limit messages to 1MB
builder.with_max_message_size(1024 * 1024)
Source

pub fn with_config(self, config: ServerConfig) -> Self

Apply a custom server configuration.

This replaces any previously set configuration options.

§Example
let config = ServerConfig::builder()
    .rate_limit(rate_config)
    .connection_limits(limits)
    .build();

builder.with_config(config)
Source

pub async fn serve(self) -> McpResult<()>

Run the server with the configured transport.

This is the main entry point that starts the server and blocks until shutdown.

§Example
MyServer.builder()
    .transport(Transport::http("0.0.0.0:8080"))
    .serve()
    .await?;
Source

pub fn handler(&self) -> &H

Get the underlying handler.

Useful for testing or custom integrations.

Source

pub fn into_handler(self) -> H

Consume the builder and return the handler.

Useful for custom integrations where you need ownership.

Trait Implementations§

Source§

impl<H: Debug + McpHandler> Debug for ServerBuilder<H>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<H> Freeze for ServerBuilder<H>
where H: Freeze,

§

impl<H> RefUnwindSafe for ServerBuilder<H>
where H: RefUnwindSafe,

§

impl<H> Send for ServerBuilder<H>

§

impl<H> Sync for ServerBuilder<H>

§

impl<H> Unpin for ServerBuilder<H>
where H: Unpin,

§

impl<H> UnsafeUnpin for ServerBuilder<H>
where H: UnsafeUnpin,

§

impl<H> UnwindSafe for ServerBuilder<H>
where H: UnwindSafe,

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<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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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
Source§

impl<T> MaybeSend for T
where T: Send + ?Sized,

Source§

impl<T> MaybeSync for T
where T: Sync + ?Sized,