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>,
impl<Counterpart: Role, Responder> McpServerBuilder<Counterpart, Responder>where
Responder: RunWithConnectionTo<Counterpart>,
Sourcepub fn instructions(self, instructions: impl ToString) -> Self
pub fn instructions(self, instructions: impl ToString) -> Self
Set the server instructions that are provided to the client.
Sourcepub fn disable_all_tools(self) -> Self
pub fn disable_all_tools(self) -> Self
Disable all tools. After calling this, only tools explicitly enabled
with enable_tool will be available.
Sourcepub fn enable_all_tools(self) -> Self
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.
Sourcepub fn disable_tool(self, name: &str) -> Result<Self, Error>
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.
Sourcepub fn enable_tool(self, name: &str) -> Result<Self, Error>
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.
Sourcepub 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,
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 likeasync |args, cx| { .. }.
§Examples
McpServer::builder("my-server")
.tool_fn_mut(
"greet",
"Greet someone by name",
async |input: GreetInput, _cx| Ok(format!("Hello, {}!", input.name)),
)Sourcepub 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,
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 likeasync |args, cx| { .. }.
§Examples
McpServer::builder("my-server")
.tool_fn(
"greet",
"Greet someone by name",
async |input: GreetInput, _cx| Ok(format!("Hello, {}!", input.name)),
)Sourcepub fn build(self) -> McpServer<Counterpart, Responder>
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).