pub struct PegBoard { /* private fields */ }Expand description
PegBoard manages tool registration and their associated MCP services with namespace support.
§Namespace and Tool Name Prefixing
When MCP services are registered with a namespace, their tool names are automatically prefixed to avoid conflicts. For example:
- Service “web_search” with tool “search” becomes “web_search-search”
- Service “file_search” with tool “search” becomes “file_search-search”
The Tool’s name field is modified to include the prefix, so when tools are sent
to the LLM, they have unique names. The PegBoard maintains the mapping between
prefixed names and original names for routing tool calls back to the correct service.
§Thread Safety
PegBoard is designed for concurrent access. All read methods use &self and can be
called concurrently from multiple threads/tasks. For tokio usage, wrap in Arc<PegBoard>.
See TOKIO_USAGE.md for detailed examples.
Implementations§
Source§impl PegBoard
impl PegBoard
Sourcepub async fn add_service(
&mut self,
namespace: Option<String>,
service: RunningService<RoleClient, Box<dyn DynService<RoleClient>>>,
) -> Result<usize, PegBoardError>
pub async fn add_service( &mut self, namespace: Option<String>, service: RunningService<RoleClient, Box<dyn DynService<RoleClient>>>, ) -> Result<usize, PegBoardError>
Registers a service and automatically discovers all its tools.
This method:
- Calls
list_tools()on the service to discover all available tools - Converts tools from rmcp format to PegBoard’s Tool format
- If namespace is provided, prefixes each tool name (e.g., “namespace-tool_name”)
- If namespace is None or empty, uses original tool names (no prefixing)
- Adds the service to the registry
- Registers all tools for use with LLM
§Arguments
namespace- Optional namespace prefix. Use None or empty string if no conflicts expectedservice- The MCP service to register
§Returns
- Number of tools discovered and registered
§Example
// With namespace (prefixing enabled)
pegboard.add_service(Some("web".to_string()), service).await?;
// Tool "search" is now available as "web-search"
// Without namespace (no prefixing)
pegboard.add_service(None, service).await?;
// Tool "search" keeps its original name "search"Sourcepub fn register_tool(
&self,
namespace: Option<&str>,
tool: Tool,
service_idx: usize,
) -> Result<(), PegBoardError>
pub fn register_tool( &self, namespace: Option<&str>, tool: Tool, service_idx: usize, ) -> Result<(), PegBoardError>
Manually registers a tool with an optional namespace and service index.
If namespace is provided, the tool name will be prefixed.
If namespace is None or empty, the original tool name is used.
Prefer add_service() for automatic tool discovery.
Sourcepub fn get_tool(&self, tool_name: &str) -> Option<Tool>
pub fn get_tool(&self, tool_name: &str) -> Option<Tool>
Gets a tool by its name (prefixed if registered with namespace, original otherwise) This is the name that the LLM sees and uses
Sourcepub fn select_tools(&self, tool_names: &[&str]) -> Option<Vec<Tool>>
pub fn select_tools(&self, tool_names: &[&str]) -> Option<Vec<Tool>>
Selects multiple tools by their names.
Returns Some(Vec<Tool>) if ALL requested tools are found.
Returns None if ANY tool is missing.
§Arguments
tool_names- A slice of tool names (prefixed if registered with namespace)
§Returns
Some(Vec<Tool>)if all tools existNoneif any tool is missing
§Example
let tools = pegboard.select_tools(&["web-search", "file-read"]);
if let Some(tools) = tools {
// All tools found, can use them
} else {
// One or more tools not found
}Sourcepub fn get_tool_route(&self, tool_name: &str) -> Option<(usize, String)>
pub fn get_tool_route(&self, tool_name: &str) -> Option<(usize, String)>
Gets routing information for a tool by its name Returns (service_index, original_tool_name) for routing the call
Sourcepub fn list_tools_in_namespace(&self, namespace: &str) -> Vec<String>
pub fn list_tools_in_namespace(&self, namespace: &str) -> Vec<String>
Gets all tool names in a namespace (prefixed if namespace was used)
Sourcepub fn get_tools_in_namespace(&self, namespace: &str) -> Vec<Tool>
pub fn get_tools_in_namespace(&self, namespace: &str) -> Vec<Tool>
Gets all Tool objects in a namespace (with names as they appear to LLM)
Sourcepub fn list_all_tools(&self) -> Vec<String>
pub fn list_all_tools(&self) -> Vec<String>
Gets all registered tool names across all namespaces These are the names that should be sent to the LLM
Sourcepub fn get_all_tools(&self) -> Vec<Tool>
pub fn get_all_tools(&self) -> Vec<Tool>
Gets all tools as a Vec These are the tools that should be sent to the LLM
Sourcepub fn list_namespaces(&self) -> Vec<String>
pub fn list_namespaces(&self) -> Vec<String>
Gets all registered namespaces
Sourcepub fn unregister_tool(&self, prefixed_name: &str) -> Result<(), PegBoardError>
pub fn unregister_tool(&self, prefixed_name: &str) -> Result<(), PegBoardError>
Removes a tool by its prefixed name
Sourcepub fn unregister_namespace(
&self,
namespace: &str,
) -> Result<usize, PegBoardError>
pub fn unregister_namespace( &self, namespace: &str, ) -> Result<usize, PegBoardError>
Removes all tools in a namespace (when removing a service)
Sourcepub fn tool_count(&self) -> usize
pub fn tool_count(&self) -> usize
Returns the number of registered tools
Sourcepub fn service_count(&self) -> usize
pub fn service_count(&self) -> usize
Returns the number of registered services
Sourcepub fn namespace_count(&self) -> usize
pub fn namespace_count(&self) -> usize
Returns the number of registered namespaces
Sourcepub async fn call_tool(
&self,
tool_name: &str,
arguments: Value,
) -> Result<CallToolResult, PegBoardError>
pub async fn call_tool( &self, tool_name: &str, arguments: Value, ) -> Result<CallToolResult, PegBoardError>
Calls a tool by its name (as seen by the LLM) with the given arguments.
This method:
- Looks up the routing information using the tool name
- Finds the service that provides this tool
- Calls the service’s
call_toolmethod with the original tool name
§Arguments
tool_name- The tool name as seen by the LLM (prefixed if namespace was used)arguments- The arguments to pass to the tool as a JSON value
§Returns
CallToolResultcontaining the tool’s response
§Errors
PegBoardError::ToolNotFound- If the tool name is not registeredPegBoardError::ServiceError- If the service call fails
§Example
// LLM calls "web-search"
let result = pegboard.call_tool(
"web-search",
serde_json::json!({"query": "rust programming"}),
).await?;