12namespace mcp::client {
14inline core::Error handler_method_not_found(std::string_view message) {
16 static_cast<int>(protocol::ErrorCode::MethodNotFound),
44 virtual void on_initialized()
const {}
46 std::string_view)
const {}
47 virtual void on_logging_message(std::string_view, std::string_view)
const {}
48 virtual void on_tool_list_changed()
const {}
49 virtual void on_prompt_list_changed()
const {}
50 virtual void on_resource_list_changed()
const {}
51 virtual void on_resource_updated(
const std::string&)
const {}
53 virtual void on_elicitation_complete(std::string_view)
const {}
55 virtual void on_roots_list_changed()
const {}
56 virtual std::optional<core::Result<protocol::RootsListResult>>
57 on_list_roots_request()
const {
60 virtual std::optional<core::Result<protocol::RootsListResult>>
63 return on_list_roots_request();
65 virtual std::optional<core::Result<protocol::CreateMessageResult>>
69 virtual std::optional<core::Result<protocol::CreateMessageResult>>
73 return on_create_message_request(params);
75 virtual std::optional<core::Result<protocol::CreateElicitationResult>>
76 on_create_elicitation_request(
80 virtual std::optional<core::Result<protocol::CreateElicitationResult>>
81 on_create_elicitation_request(
85 return on_create_elicitation_request(params);
87 virtual std::optional<core::Result<protocol::Json>> on_custom_request(
91 virtual std::optional<core::Result<protocol::Json>> on_custom_request(
95 return on_custom_request(request);
145 Client::RootsListRequestCancellationHandler
146 on_list_roots_request_with_cancellation;
149 Client::SamplingRequestCancellationHandler
150 on_create_message_request_with_cancellation;
153 Client::ElicitationRequestCancellationHandler
154 on_create_elicitation_request_with_cancellation;
157 Client::CustomRequestCancellationHandler on_custom_request_with_cancellation;
160 Client::RootsListRequestCancellationHandler
161 on_roots_list_request_with_cancellation;
164 Client::SamplingRequestCancellationHandler
165 on_sampling_request_with_cancellation;
168 Client::ElicitationRequestCancellationHandler
169 on_elicitation_request_with_cancellation;
214 if (on_list_roots_request_with_cancellation) {
220 if (on_create_message_request_with_cancellation) {
222 on_create_message_request_with_cancellation);
227 if (on_create_elicitation_request_with_cancellation) {
229 on_create_elicitation_request_with_cancellation);
234 if (on_custom_request_with_cancellation) {
240 if (on_roots_list_request_with_cancellation) {
246 if (on_sampling_request_with_cancellation) {
252 if (on_elicitation_request_with_cancellation) {
275 std::string_view reason) {
276 handler.on_cancelled(request_id, reason);
279 [&handler](std::string_view level, std::string_view message) {
280 handler.on_logging_message(level, message);
285 [&handler]() { handler.on_resource_list_changed(); });
287 [&handler](
const std::string& uri) { handler.on_resource_updated(uri); });
289 handler.on_progress(params);
292 handler.on_elicitation_complete(elicitation_id);
295 [&handler](
const protocol::Task& task) { handler.on_task_status(task); });
299 const auto response = handler.on_list_roots_request(cancellation);
300 if (response.has_value()) {
301 return std::move(*response);
303 return mcp::core::unexpected(
304 handler_method_not_found(
"client handler does not handle list_roots"));
310 const auto response =
311 handler.on_create_message_request(params, cancellation);
312 if (response.has_value()) {
313 return std::move(*response);
315 return mcp::core::unexpected(handler_method_not_found(
316 "client handler does not handle create_message"));
322 const auto response =
323 handler.on_create_elicitation_request(params, cancellation);
324 if (response.has_value()) {
325 return std::move(*response);
327 return mcp::core::unexpected(handler_method_not_found(
328 "client handler does not handle elicitation"));
334 const auto response = handler.on_custom_request(
request, cancellation);
335 if (response.has_value()) {
336 return std::move(*response);
338 return mcp::core::unexpected(handler_method_not_found(
339 "client handler does not handle custom request"));
343 handler.on_raw_notification(notification);
Copyable token observed by cancellation-aware SDK operations.
Definition cancellation.hpp:104
High-level MCP client compatibility API.
Definition client.hpp:132
Client & on_task_status(TaskStatusHandler handler)
Registers a callback for task status notifications.
Client & on_logging_message(LoggingMessageHandler handler)
Registers a callback for logging message notifications.
Client & on_list_roots_request(ListRootsRequestHandler handler)
Registers a handler for server list-roots requests.
std::function< void(std::string_view, std::string_view)> LoggingMessageHandler
Receives logging messages from the server.
Definition client.hpp:184
std::function< void(const protocol::ProgressNotificationParams &)> ProgressHandler
Receives progress notifications associated with a progress token.
Definition client.hpp:205
Client & on_initialized(InitializedHandler handler)
Registers a callback for initialized notifications.
std::function< void(const std::string &)> ResourceUpdatedHandler
Receives resource update notifications.
Definition client.hpp:201
Client & on_roots_list_request(RootsListRequestHandler handler)
Compatibility alias for on_list_roots_request().
std::function< core::Result< protocol::RootsListResult >()> RootsListRequestHandler
Handles a server request for the client's current roots.
Definition client.hpp:217
Client & on_cancelled(CancelledHandler handler)
Registers a callback for cancellation notifications.
Client & on_prompt_list_changed(ListChangedHandler handler)
Registers a callback for prompt list change notifications.
Client & on_elicitation_complete(ElicitationCompleteHandler handler)
Registers a callback for elicitation completion notifications.
Client & on_create_elicitation_request(CreateElicitationRequestHandler handler)
Registers a handler for server elicitation requests.
Client & on_elicitation_request(ElicitationRequestHandler handler)
Compatibility alias for on_create_elicitation_request().
Client & on_sampling_request(SamplingRequestHandler handler)
Compatibility alias for on_create_message_request().
Client & set_handler(const ClientHandler &handler)
Installs every non-empty callback from a ClientHandler aggregate.
Definition handler.hpp:266
core::Result< protocol::Json > request(const protocol::JsonRpcRequest &request)
Sends a raw JSON-RPC request and returns its result JSON.
Client & on_progress(ProgressHandler handler)
Registers a callback for progress notifications.
std::function< void(const protocol::RequestId &, std::string_view)> CancelledHandler
Receives cancellation notifications for in-flight requests.
Definition client.hpp:193
std::function< core::Result< protocol::Json >(const protocol::JsonRpcRequest &)> CustomRequestHandler
Handles non-built-in server requests.
Definition client.hpp:245
Client & on_resource_updated(ResourceUpdatedHandler handler)
Registers a callback for resource update notifications.
Client & on_create_message_request(CreateMessageRequestHandler handler)
Registers a handler for server sampling createMessage requests.
Client & on_custom_request(CustomRequestHandler handler)
Registers a handler for custom server requests not handled by the built-in client dispatcher.
std::function< core::Result< protocol::CreateMessageResult >(const protocol::CreateMessageParams &)> SamplingRequestHandler
Handles a server sampling request.
Definition client.hpp:226
std::function< void(std::string_view)> ElicitationCompleteHandler
Receives completion notifications for elicitation flows.
Definition client.hpp:209
Client & on_tool_list_changed(ListChangedHandler handler)
Registers a callback for tool list change notifications.
std::function< void()> ListChangedHandler
Receives list-change notifications for prompts, resources, tools, or roots.
Definition client.hpp:197
Client & on_custom_notification(RawNotificationHandler handler)
Compatibility alias for on_raw_notification().
std::function< void()> InitializedHandler
Receives notifications that the peer completed initialization.
Definition client.hpp:187
std::function< void(const protocol::JsonRpcNotification &)> RawNotificationHandler
Observes every inbound notification after built-in dispatch.
Definition client.hpp:255
std::function< core::Result< protocol::CreateElicitationResult >(const protocol::CreateElicitationRequestParam &)> ElicitationRequestHandler
Handles a server elicitation request.
Definition client.hpp:236
Client & on_roots_list_changed(ListChangedHandler handler)
Registers a callback for roots list change notifications.
Client & on_resource_list_changed(ListChangedHandler handler)
Registers a callback for resource list change notifications.
std::function< void(const protocol::Task &)> TaskStatusHandler
Receives task status notifications.
Definition client.hpp:212
Client & on_raw_notification(RawNotificationHandler handler)
Registers an observer for raw inbound notifications.
Core client compatibility API and transport interface for MCP clients.
std::variant< std::int64_t, std::string > RequestId
JSON-RPC request or response identifier.
Definition types.hpp:56
tl::expected< T, Error > Result
Alias for the SDK result type.
Definition result.hpp:64
Contract-style client handler interface.
Definition handler.hpp:27
Optional callback bundle for configuring a Client in one call.
Definition handler.hpp:106
ChangedHandler on_prompt_list_changed
Called when the server's prompt list changes.
Definition handler.hpp:130
TaskStatusHandler on_task_status
Called when a task status notification is received.
Definition handler.hpp:140
InitializedHandler on_initialized
Called when the server sends an initialized notification.
Definition handler.hpp:122
SamplingRequestHandler on_create_message_request
Handles server sampling createMessage requests.
Definition handler.hpp:148
void apply_to(Client &client) const
Applies all non-empty callbacks to a client.
Definition handler.hpp:177
RawNotificationHandler on_custom_notification
Compatibility alias for on_raw_notification.
Definition handler.hpp:173
RawNotificationHandler on_raw_notification
Observes raw inbound notifications after built-in dispatch.
Definition handler.hpp:171
SamplingRequestHandler on_sampling_request
Compatibility alias for on_create_message_request.
Definition handler.hpp:163
ResourceUpdatedHandler on_resource_updated
Called when a subscribed resource URI is updated.
Definition handler.hpp:134
ProgressHandler on_progress
Called for progress notifications.
Definition handler.hpp:136
CancelledHandler on_cancelled
Called when the server cancels a request.
Definition handler.hpp:124
ChangedHandler on_resource_list_changed
Called when the server's resource list changes.
Definition handler.hpp:132
LoggingMessageHandler on_logging_message
Called for server logging message notifications.
Definition handler.hpp:126
RootsListRequestHandler on_roots_list_request
Compatibility alias for on_list_roots_request.
Definition handler.hpp:159
ChangedHandler on_tool_list_changed
Called when the server's tool list changes.
Definition handler.hpp:128
RootsListRequestHandler on_list_roots_request
Handles server list-roots requests.
Definition handler.hpp:144
ElicitationRequestHandler on_create_elicitation_request
Handles server elicitation requests.
Definition handler.hpp:152
ElicitationRequestHandler on_elicitation_request
Compatibility alias for on_create_elicitation_request.
Definition handler.hpp:167
CustomRequestHandler on_custom_request
Handles custom server requests.
Definition handler.hpp:156
ChangedHandler on_roots_list_changed
Called when the client's roots list changes.
Definition handler.hpp:142
ElicitationCompleteHandler on_elicitation_complete
Called when an elicitation flow completes.
Definition handler.hpp:138
Parameters for elicitation/create.
Definition elicitation.hpp:335
Parameters for sampling/createMessage.
Definition sampling.hpp:231
JSON-RPC notification envelope for one-way MCP messages.
Definition types.hpp:137
JSON-RPC request envelope carrying an MCP method invocation.
Definition types.hpp:99
Parameters for notifications/progress.
Definition types.hpp:226
Snapshot of an asynchronous task.
Definition task.hpp:144