PegBoard

Struct PegBoard 

Source
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

Source

pub fn new() -> Self

Creates a new empty PegBoard

Source

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:

  1. Calls list_tools() on the service to discover all available tools
  2. Converts tools from rmcp format to PegBoard’s Tool format
  3. If namespace is provided, prefixes each tool name (e.g., “namespace-tool_name”)
  4. If namespace is None or empty, uses original tool names (no prefixing)
  5. Adds the service to the registry
  6. Registers all tools for use with LLM
§Arguments
  • namespace - Optional namespace prefix. Use None or empty string if no conflicts expected
  • service - 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"
Source

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.

Source

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

Source

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 exist
  • None if 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
}
Source

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

Source

pub fn list_tools_in_namespace(&self, namespace: &str) -> Vec<String>

Gets all tool names in a namespace (prefixed if namespace was used)

Source

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)

Source

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

Source

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

Source

pub fn list_namespaces(&self) -> Vec<String>

Gets all registered namespaces

Source

pub fn unregister_tool(&self, prefixed_name: &str) -> Result<(), PegBoardError>

Removes a tool by its prefixed name

Source

pub fn unregister_namespace( &self, namespace: &str, ) -> Result<usize, PegBoardError>

Removes all tools in a namespace (when removing a service)

Source

pub fn tool_count(&self) -> usize

Returns the number of registered tools

Source

pub fn service_count(&self) -> usize

Returns the number of registered services

Source

pub fn namespace_count(&self) -> usize

Returns the number of registered namespaces

Source

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:

  1. Looks up the routing information using the tool name
  2. Finds the service that provides this tool
  3. Calls the service’s call_tool method 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
  • CallToolResult containing the tool’s response
§Errors
  • PegBoardError::ToolNotFound - If the tool name is not registered
  • PegBoardError::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?;

Trait Implementations§

Source§

impl Default for PegBoard

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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