ToolRegistry

class ToolRegistry(val toolTimeoutMs: Long = DEFAULT_TOOL_TIMEOUT_MS, confirmer: ToolCallConfirmer? = null, requiresConfirmation: (McpTool) -> Boolean = { it.annotations.destructiveHint })

Thread-safe collection of registered McpTools, keyed by McpTool.name. Backed by a ConcurrentHashMap so tools can be registered and invoked concurrently. Also tracks a host-controlled runtime gate (see setDisabledTools) that hides/blocks tools without unregistering them.

This is the single source of truth the transports and McpProtocol read from for tools/list and tools/call. Every change to the client-visible set (register, unregister, gate edits) is reported to addChangeListener subscribers; the HTTP transport turns that into notifications/tools/list_changed.

Constructors

Link copied to clipboard
constructor(toolTimeoutMs: Long = DEFAULT_TOOL_TIMEOUT_MS, confirmer: ToolCallConfirmer? = null, requiresConfirmation: (McpTool) -> Boolean = { it.annotations.destructiveHint })

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Upper bound on a single executeTool call; a tool still running after this is cancelled and reported as a tool_timeout error. Must be positive. The same bound applies to waiting for confirmer.

Functions

Link copied to clipboard
fun addChangeListener(listener: () -> Unit)

Call listener whenever the client-visible tool set may have changed.

Link copied to clipboard

Names currently gated off.

Link copied to clipboard
suspend fun executeTool(name: String, params: Map<String, Any>, clientLabel: String? = null): ToolResult

Execute the named tool with params on Dispatchers.IO, bounded by toolTimeoutMs. Returns a tool_disabled error if the tool is gated off, an Unknown tool error if it isn't registered, a tool_timeout error if it overran, and converts any exception thrown by McpTool.execute into a failed ToolResult. Coroutine cancellation of the caller is rethrown rather than swallowed, so a cancelled request stops its tool.

Link copied to clipboard
fun getTool(name: String): McpTool?

The registered tool with this name, or null if none is registered.

Link copied to clipboard

Whether the named tool is currently enabled (registered tools are enabled by default).

Link copied to clipboard

Tools currently visible to clients — registered and not gated off.

Link copied to clipboard

All registered tools, regardless of gate state. See listEnabledTools for the client-visible set.

Link copied to clipboard
fun register(tool: McpTool)

Register tool, replacing any existing tool with the same McpTool.name.

Link copied to clipboard
fun registerAll(toolList: List<McpTool>)

Register every tool in toolList (e.g. the output of a module provider's all(context)), notifying listeners once.

Link copied to clipboard
fun removeChangeListener(listener: () -> Unit)
Link copied to clipboard

Replace the entire disabled set in one atomic swap (e.g. from a checkbox grid). @Synchronized on the same monitor as setToolEnabled so a full replace can't interleave with a single-tool toggle and lose its update.

Link copied to clipboard

Enable or disable a single tool by name. @Synchronized so concurrent single-tool toggles don't lose updates in the copy-on-write swap.

Link copied to clipboard

Remove the named tool (e.g. after the user revokes the permission it needs).