pub struct McpServerBuilder { /* private fields */ }Expand description
Builder for McpServer — the main entry point for configuring your server.
Implementations§
Source§impl McpServerBuilder
impl McpServerBuilder
pub fn new() -> Self
Sourcepub fn name(self, name: impl Into<String>) -> Self
pub fn name(self, name: impl Into<String>) -> Self
Set the server name (shown to clients during handshake)
Sourcepub fn instructions(self, instructions: impl Into<String>) -> Self
pub fn instructions(self, instructions: impl Into<String>) -> Self
Human-readable instructions for how to use this server
Sourcepub fn auth(self, provider: DynAuthProvider) -> Self
pub fn auth(self, provider: DynAuthProvider) -> Self
Require authentication on all requests using the given provider.
Requests with no or invalid credentials receive HTTP 401 on SSE/HTTP transports. Stdio transport is unaffected (it relies on process-level access control).
§Example
use mcp_kit::prelude::*;
use mcp_kit::auth::BearerTokenProvider;
use std::sync::Arc;
McpServer::builder()
.name("my-server")
.version("1.0")
.auth(Arc::new(BearerTokenProvider::new(["secret"])))
.build();Sourcepub fn optional_auth(self, provider: DynAuthProvider) -> Self
pub fn optional_auth(self, provider: DynAuthProvider) -> Self
Accept an auth provider but allow unauthenticated requests through.
Authenticated requests have an identity available via Auth; unauthenticated
requests have no identity and may reach handlers with None.
Sourcepub fn tool<M>(self, tool: Tool, handler: impl ToolHandler<M>) -> Self
pub fn tool<M>(self, tool: Tool, handler: impl ToolHandler<M>) -> Self
Register a tool with an explicit Tool descriptor and a handler function.
Sourcepub fn tool_def(self, def: ToolDef) -> Self
pub fn tool_def(self, def: ToolDef) -> Self
Register a tool using a pre-built ToolDef (from the #[tool] macro).
Sourcepub fn tool_fn<M>(
self,
name: impl Into<String>,
description: impl Into<String>,
handler: impl ToolHandler<M>,
) -> Self
pub fn tool_fn<M>( self, name: impl Into<String>, description: impl Into<String>, handler: impl ToolHandler<M>, ) -> Self
Convenience: register a no-parameter tool.
Sourcepub fn resource<M>(
self,
resource: Resource,
handler: impl ResourceHandler<M>,
) -> Self
pub fn resource<M>( self, resource: Resource, handler: impl ResourceHandler<M>, ) -> Self
Register a static resource (exact URI match).
Sourcepub fn resource_template<M>(
self,
template: ResourceTemplate,
handler: impl ResourceHandler<M>,
) -> Self
pub fn resource_template<M>( self, template: ResourceTemplate, handler: impl ResourceHandler<M>, ) -> Self
Register a URI-template resource (e.g. "file://{path}").
Sourcepub fn resource_def(self, def: ResourceDef) -> Self
pub fn resource_def(self, def: ResourceDef) -> Self
Register a resource using a pre-built ResourceDef (from the #[resource] macro).
Sourcepub fn prompt<M>(self, prompt: Prompt, handler: impl PromptHandler<M>) -> Self
pub fn prompt<M>(self, prompt: Prompt, handler: impl PromptHandler<M>) -> Self
Register a prompt template.
Sourcepub fn prompt_def(self, def: PromptDef) -> Self
pub fn prompt_def(self, def: PromptDef) -> Self
Register a prompt using a pre-built PromptDef (from the #[prompt] macro).
Sourcepub fn completion<M>(self, handler: impl CompletionHandler<M>) -> Self
pub fn completion<M>(self, handler: impl CompletionHandler<M>) -> Self
Register a global completion handler for auto-completing prompt/resource arguments.
This handler is called for any completion/complete request that doesn’t have
a more specific handler (prompt-specific or resource-specific).
§Example
use mcp_kit::prelude::*;
use mcp_kit::types::messages::{CompleteRequest, CompleteResult};
McpServer::builder()
.name("my-server")
.completion(|req: CompleteRequest| async move {
// Auto-complete based on argument name
let values = match req.argument.name.as_str() {
"language" => vec!["rust", "python", "javascript"],
_ => vec![],
};
Ok(CompleteResult::new(values))
})
.build();Sourcepub fn resource_completion<M>(
self,
uri_pattern: impl Into<String>,
handler: impl CompletionHandler<M>,
) -> Self
pub fn resource_completion<M>( self, uri_pattern: impl Into<String>, handler: impl CompletionHandler<M>, ) -> Self
Register a completion handler for a specific resource URI pattern.
The pattern can be an exact URI or a template like "file://{path}".
Sourcepub fn prompt_with_completion<M1, M2>(
self,
prompt: Prompt,
handler: impl PromptHandler<M1>,
completion: impl CompletionHandler<M2>,
) -> Self
pub fn prompt_with_completion<M1, M2>( self, prompt: Prompt, handler: impl PromptHandler<M1>, completion: impl CompletionHandler<M2>, ) -> Self
Register a prompt with an associated completion handler.
The completion handler provides auto-complete suggestions for the prompt’s arguments.
Sourcepub fn with_plugin_manager(self, plugin_manager: PluginManager) -> Self
pub fn with_plugin_manager(self, plugin_manager: PluginManager) -> Self
Attach a plugin manager with pre-loaded plugins.
All tools, resources, and prompts from loaded plugins will be automatically registered with the server.
§Example
use mcp_kit::prelude::*;
use mcp_kit::plugin::PluginManager;
let mut plugin_manager = PluginManager::new();
plugin_manager.load_from_path("./plugins/weather.so")?;
McpServer::builder()
.name("my-server")
.with_plugin_manager(plugin_manager)
.build();Sourcepub fn load_plugin(self, path: &str) -> McpResult<Self>
pub fn load_plugin(self, path: &str) -> McpResult<Self>
Load a plugin from a file path.
This is a convenience method that creates a PluginManager if needed and loads the plugin.
§Example
use mcp_kit::prelude::*;
McpServer::builder()
.name("my-server")
.load_plugin("./plugins/weather.so")?
.load_plugin("./plugins/database.so")?
.build();Sourcepub fn load_plugin_with_config(
self,
path: &str,
config: PluginConfig,
) -> McpResult<Self>
pub fn load_plugin_with_config( self, path: &str, config: PluginConfig, ) -> McpResult<Self>
Load a plugin with custom configuration.