Skip to main content

McpServerBuilder

Struct McpServerBuilder 

Source
pub struct McpServerBuilder<Counterpart: Role, Responder>
where Responder: RunWithConnectionTo<Counterpart>,
{ /* private fields */ }
Expand description

Builder for creating MCP servers with tools.

Use McpServer::builder to create a new builder, then chain methods to configure the server and call build to create the server.

§Example

let server = McpServer::builder("my-server".to_string())
    .instructions("A helpful assistant")
    .tool(EchoTool)
    .tool_fn(
        "greet",
        "Greet someone by name",
        async |input: GreetInput, _cx| Ok(format!("Hello, {}!", input.name)),
        agent_client_protocol::tool_fn!(),
    )
    .build();

Implementations§

Source§

impl<Counterpart: Role, Responder> McpServerBuilder<Counterpart, Responder>
where Responder: RunWithConnectionTo<Counterpart>,

Source

pub fn instructions(self, instructions: impl ToString) -> Self

Set the server instructions that are provided to the client.

Source

pub fn tool(self, tool: impl McpTool<Counterpart> + 'static) -> Self

Add a tool to the server.

Source

pub fn disable_all_tools(self) -> Self

Disable all tools. After calling this, only tools explicitly enabled with enable_tool will be available.

Source

pub fn enable_all_tools(self) -> Self

Enable all tools. After calling this, all tools will be available except those explicitly disabled with disable_tool.

Source

pub fn disable_tool(self, name: &str) -> Result<Self, Error>

Disable a specific tool by name.

Returns an error if the tool is not registered.

Source

pub fn enable_tool(self, name: &str) -> Result<Self, Error>

Enable a specific tool by name.

Returns an error if the tool is not registered.

Source

pub fn tool_fn_mut<P, Ret, F>( self, name: impl ToString, description: impl ToString, func: F, tool_future_hack: impl for<'a> Fn(&'a mut F, P, McpConnectionTo<Counterpart>) -> BoxFuture<'a, Result<Ret, Error>> + Send + 'static, ) -> McpServerBuilder<Counterpart, impl RunWithConnectionTo<Counterpart>>
where P: JsonSchema + DeserializeOwned + 'static + Send, Ret: JsonSchema + Serialize + 'static + Send, F: AsyncFnMut(P, McpConnectionTo<Counterpart>) -> Result<Ret, Error> + Send,

Convenience wrapper for defining a “single-threaded” tool without having to create a struct. By “single-threaded”, we mean that only one invocation of the tool can be running at a time. Typically agents invoke a tool once per session and then block waiting for the result, so this is fine, but they could attempt to run multiple invocations concurrently, in which case those invocations would be serialized.

§Parameters
  • name: The name of the tool.
  • description: The description of the tool.
  • func: The function that implements the tool. Use an async closure like async |args, cx| { .. }.
§Examples
McpServer::builder("my-server")
    .tool_fn_mut(
        "greet",
        "Greet someone by name",
        async |input: GreetInput, _cx| Ok(format!("Hello, {}!", input.name)),
    )
Source

pub fn tool_fn<P, Ret, F>( self, name: impl ToString, description: impl ToString, func: F, tool_future_hack: impl for<'a> Fn(&'a F, P, McpConnectionTo<Counterpart>) -> BoxFuture<'a, Result<Ret, Error>> + Send + Sync + 'static, ) -> McpServerBuilder<Counterpart, impl RunWithConnectionTo<Counterpart>>
where P: JsonSchema + DeserializeOwned + 'static + Send, Ret: JsonSchema + Serialize + 'static + Send, F: AsyncFn(P, McpConnectionTo<Counterpart>) -> Result<Ret, Error> + Send + Sync + 'static,

Convenience wrapper for defining a stateless tool that can run concurrently. Unlike tool_fn_mut, multiple invocations of this tool can run at the same time since the function is Fn rather than FnMut.

§Parameters
  • name: The name of the tool.
  • description: The description of the tool.
  • func: The function that implements the tool. Use an async closure like async |args, cx| { .. }.
§Examples
McpServer::builder("my-server")
    .tool_fn(
        "greet",
        "Greet someone by name",
        async |input: GreetInput, _cx| Ok(format!("Hello, {}!", input.name)),
    )
Source

pub fn build(self) -> McpServer<Counterpart, Responder>

Create an MCP server from this builder.

This builder can be attached to new sessions (see SessionBuilder::with_mcp_server) or served up as part of a proxy (see Builder::with_mcp_server).

Trait Implementations§

Source§

impl<Counterpart: Debug + Role, Responder> Debug for McpServerBuilder<Counterpart, Responder>
where Responder: RunWithConnectionTo<Counterpart> + Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Counterpart, Responder> Freeze for McpServerBuilder<Counterpart, Responder>
where Responder: Freeze,

§

impl<Counterpart, Responder> !RefUnwindSafe for McpServerBuilder<Counterpart, Responder>

§

impl<Counterpart, Responder> Send for McpServerBuilder<Counterpart, Responder>

§

impl<Counterpart, Responder> Sync for McpServerBuilder<Counterpart, Responder>
where Responder: Sync,

§

impl<Counterpart, Responder> Unpin for McpServerBuilder<Counterpart, Responder>
where Responder: Unpin, Counterpart: Unpin,

§

impl<Counterpart, Responder> UnsafeUnpin for McpServerBuilder<Counterpart, Responder>
where Responder: UnsafeUnpin,

§

impl<Counterpart, Responder> !UnwindSafe for McpServerBuilder<Counterpart, Responder>

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

Source§

impl<T> IntoOption<T> for T

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