Skip to main content

ProxyBuilder

Struct ProxyBuilder 

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

Fluent builder for constructing an MCP proxy without TOML config files.

Call build() to connect backends and produce a ready-to-serve Proxy.

Implementations§

Source§

impl ProxyBuilder

Source

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

Create a new proxy builder with the given name.

Defaults: version “0.1.0”, separator “/”, listen 127.0.0.1:8080.

Source

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

Set the proxy version (default: “0.1.0”).

Source

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

Set the namespace separator (default: “/”).

Source

pub fn listen(self, host: impl Into<String>, port: u16) -> Self

Set the listen address and port (default: 127.0.0.1:8080).

Source

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

Set instructions text sent to MCP clients.

Source

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

Set the graceful shutdown timeout (default: 30s).

Source

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

Enable hot reload for watching config file changes.

Source

pub fn global_rate_limit(self, requests: usize, period: Duration) -> Self

Set a global rate limit across all backends.

Source

pub fn stdio_backend( self, name: impl Into<String>, command: impl Into<String>, args: &[&str], ) -> Self

Add a stdio backend (subprocess).

Source

pub fn stdio_backend_with_env( self, name: impl Into<String>, command: impl Into<String>, args: &[&str], env: HashMap<String, String>, ) -> Self

Add a stdio backend with environment variables.

Source

pub fn http_backend( self, name: impl Into<String>, url: impl Into<String>, ) -> Self

Add an HTTP backend.

Source

pub fn http_backend_with_token( self, name: impl Into<String>, url: impl Into<String>, token: impl Into<String>, ) -> Self

Add an HTTP backend with a bearer token.

Source

pub fn configure_backend(self, f: impl FnOnce(&mut BackendConfig)) -> Self

Configure the last added backend with a per-backend modifier.

§Panics

Panics if no backends have been added.

Source

pub fn bearer_auth(self, tokens: Vec<String>) -> Self

Enable bearer token authentication. Enable bearer token authentication.

All tokens in this list have unrestricted access to all tools. For per-token tool scoping, use scoped_bearer_auth.

Source

pub fn scoped_bearer_auth(self, scoped_tokens: Vec<BearerTokenConfig>) -> Self

Enable bearer token authentication with per-token tool scoping.

Each BearerTokenConfig can specify allow_tools or deny_tools to restrict which tools that token can access.

Source

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

Enable request coalescing.

Source

pub fn max_argument_size(self, max_bytes: usize) -> Self

Set the maximum argument size for validation.

Source

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

Enable audit logging.

Source

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

Enable access logging.

Source

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

Set the log level (default: “info”).

Source

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

Enable structured JSON logging.

Source

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

Enable Prometheus metrics.

Source

pub fn timeout(self, seconds: u64) -> Self

Set the timeout for the last added backend.

§Panics

Panics if no backends have been added.

§Example
use mcp_proxy::builder::ProxyBuilder;

let config = ProxyBuilder::new("my-proxy")
    .http_backend("api", "http://api:8080")
    .timeout(30)
    .into_config();

assert_eq!(config.backends[0].timeout.as_ref().unwrap().seconds, 30);
Source

pub fn rate_limit(self, requests: usize, period_seconds: u64) -> Self

Set the rate limit for the last added backend.

§Panics

Panics if no backends have been added.

§Example
use mcp_proxy::builder::ProxyBuilder;

let config = ProxyBuilder::new("my-proxy")
    .http_backend("api", "http://api:8080")
    .rate_limit(100, 1)
    .into_config();

let rl = config.backends[0].rate_limit.as_ref().unwrap();
assert_eq!(rl.requests, 100);
assert_eq!(rl.period_seconds, 1);
Source

pub fn circuit_breaker(self, failure_rate: f64) -> Self

Set the circuit breaker for the last added backend.

Uses sensible defaults for other fields: minimum 5 calls, 30-second wait duration, and 3 half-open calls.

§Panics

Panics if no backends have been added.

§Example
use mcp_proxy::builder::ProxyBuilder;

let config = ProxyBuilder::new("my-proxy")
    .http_backend("api", "http://api:8080")
    .circuit_breaker(0.5)
    .into_config();

let cb = config.backends[0].circuit_breaker.as_ref().unwrap();
assert!((cb.failure_rate_threshold - 0.5).abs() < f64::EPSILON);
Source

pub fn expose_tools(self, tools: &[&str]) -> Self

Set the tool allowlist for the last added backend.

Only the listed tools will be exposed through the proxy.

§Panics

Panics if no backends have been added.

§Example
use mcp_proxy::builder::ProxyBuilder;

let config = ProxyBuilder::new("my-proxy")
    .http_backend("api", "http://api:8080")
    .expose_tools(&["read_file", "list_dir"])
    .into_config();

assert_eq!(config.backends[0].expose_tools, vec!["read_file", "list_dir"]);
Source

pub fn hide_tools(self, tools: &[&str]) -> Self

Set the tool denylist for the last added backend.

The listed tools will be hidden from clients.

§Panics

Panics if no backends have been added.

§Example
use mcp_proxy::builder::ProxyBuilder;

let config = ProxyBuilder::new("my-proxy")
    .http_backend("api", "http://api:8080")
    .hide_tools(&["dangerous_op"])
    .into_config();

assert_eq!(config.backends[0].hide_tools, vec!["dangerous_op"]);
Source

pub fn retry(self, max_retries: u32) -> Self

Set the retry policy for the last added backend.

Uses sensible defaults: 100ms initial backoff, 5000ms max backoff, no budget limit.

§Panics

Panics if no backends have been added.

§Example
use mcp_proxy::builder::ProxyBuilder;

let config = ProxyBuilder::new("my-proxy")
    .http_backend("api", "http://api:8080")
    .retry(3)
    .into_config();

let retry = config.backends[0].retry.as_ref().unwrap();
assert_eq!(retry.max_retries, 3);
Source

pub fn into_config(self) -> ProxyConfig

Extract the built ProxyConfig without connecting to backends.

Useful for inspection, serialization, or passing to Proxy::from_config() manually.

Source

pub async fn build(self) -> Result<Proxy>

Build the proxy: validate config, connect to all backends, and construct the middleware stack.

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

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

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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 = 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