cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
mcp::server::ToolRegistry Class Reference

Registry of named MCP tools and their handlers. More...

#include <registry.hpp>

Public Member Functions

 ToolRegistry (const ToolRegistry &other)
 
ToolRegistryoperator= (const ToolRegistry &other)
 
 ToolRegistry (ToolRegistry &&other) noexcept
 
ToolRegistryoperator= (ToolRegistry &&other) noexcept
 
core::Result< core::Unitadd (protocol::ToolDefinition definition, ToolHandler handler)
 Register a tool definition and handler.
 
core::Result< protocol::ToolDefinitionget (std::string_view name) const
 Look up a registered tool definition by name.
 
core::Result< protocol::ToolResultcall (std::string_view name, protocol::Json arguments) const
 Invoke a tool without session metadata.
 
core::Result< protocol::ToolResultcall (protocol::ToolCall call) const
 Invoke a tool from a parsed protocol call without session metadata.
 
core::Result< core::Unitvalidate (const protocol::ToolCall &call) const
 Validate that a parsed tool call can target a registered tool.
 
core::Result< protocol::ToolResultcall (std::string_view name, protocol::Json arguments, const SessionContext &session_context) const
 Invoke a tool with full session metadata.
 
core::Result< protocol::ToolResultcall (protocol::ToolCall call, const SessionContext &session_context) const
 Invoke a parsed protocol call with full session metadata.
 
core::Result< protocol::ToolResultcall (protocol::ToolCall call, const SessionContext &session_context, CancellationToken cancellation) const
 Invoke a parsed protocol call with session metadata and cooperative cancellation.
 
core::Result< protocol::ToolResultcall (protocol::ToolCall call, const SessionContext &session_context, CancellationToken cancellation, const JsonSchemaValidator *schema_validator) const
 
core::Result< protocol::ToolResultcall (std::string_view name, protocol::Json arguments, const std::string &session_id) const
 Invoke a tool with only a session id.
 
std::vector< protocol::ToolDefinitionlist () const
 Return registered tool definitions sorted by name.
 

Detailed Description

Registry of named MCP tools and their handlers.

The registry owns copies of tool definitions and std::function handlers. It performs duplicate and bounded-name validation on add(): names must be non-empty, fit the SDK length limit, and contain no control characters. Calls are synchronous: the selected handler is copied under the registry lock, invoked on the caller's thread outside the lock, and any handler error is returned unchanged.

The registry synchronizes add/get/list/call access internally. Handler callbacks may reenter the registry without deadlocking.

Member Function Documentation

◆ add()

core::Result< core::Unit > mcp::server::ToolRegistry::add ( protocol::ToolDefinition  definition,
ToolHandler  handler 
)

Register a tool definition and handler.

Parameters
definitionTool metadata. The name must be non-empty, bounded, control-character-free, and unique.
handlerCallable used to execute the tool.
Returns
core::Unit on success, or InvalidRequest for invalid name, duplicate name, or empty handler.

◆ call() [1/5]

core::Result< protocol::ToolResult > mcp::server::ToolRegistry::call ( protocol::ToolCall  call) const

Invoke a tool from a parsed protocol call without session metadata.

Parameters
callParsed protocol call including arguments and optional task request metadata.
Returns
Handler result, ToolNotFound, task-support validation failure, or the handler's own error.

◆ call() [2/5]

core::Result< protocol::ToolResult > mcp::server::ToolRegistry::call ( protocol::ToolCall  call,
const SessionContext session_context 
) const

Invoke a parsed protocol call with full session metadata.

Parameters
callParsed protocol call including arguments and optional task request metadata.
session_contextMetadata copied into ToolContext.
Returns
Handler result, ToolNotFound, task-support validation failure, or the handler's own error.

◆ call() [3/5]

core::Result< protocol::ToolResult > mcp::server::ToolRegistry::call ( std::string_view  name,
protocol::Json  arguments 
) const

Invoke a tool without session metadata.

Parameters
nameRegistered tool name.
argumentsJSON arguments passed to the handler.
Returns
Handler result, ToolNotFound, or the handler's own error.

◆ call() [4/5]

core::Result< protocol::ToolResult > mcp::server::ToolRegistry::call ( std::string_view  name,
protocol::Json  arguments,
const SessionContext session_context 
) const

Invoke a tool with full session metadata.

Parameters
nameRegistered tool name.
argumentsJSON arguments passed to the handler.
session_contextMetadata copied into ToolContext.
Returns
Handler result, ToolNotFound, or the handler's own error.

◆ call() [5/5]

core::Result< protocol::ToolResult > mcp::server::ToolRegistry::call ( std::string_view  name,
protocol::Json  arguments,
const std::string &  session_id 
) const

Invoke a tool with only a session id.

Parameters
nameRegistered tool name.
argumentsJSON arguments passed to the handler.
session_idSession id copied into ToolContext.

◆ get()

core::Result< protocol::ToolDefinition > mcp::server::ToolRegistry::get ( std::string_view  name) const

Look up a registered tool definition by name.

Returns
The definition, or ToolNotFound when absent.

◆ validate()

core::Result< core::Unit > mcp::server::ToolRegistry::validate ( const protocol::ToolCall call) const

Validate that a parsed tool call can target a registered tool.

Returns
Unit on success, ToolNotFound, or task-support validation failure.

The documentation for this class was generated from the following file: