cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
handler.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
7
8#include <utility>
9
11
12namespace mcp::client {
13
14inline core::Error handler_method_not_found(std::string_view message) {
15 return core::Error{
16 static_cast<int>(protocol::ErrorCode::MethodNotFound),
17 std::string(message),
18 {},
19 };
20}
21
28 using InitializedHandler = Client::InitializedHandler;
29 using CancelledHandler = Client::CancelledHandler;
30 using LoggingMessageHandler = Client::LoggingMessageHandler;
31 using ChangedHandler = Client::ListChangedHandler;
32 using ResourceUpdatedHandler = Client::ResourceUpdatedHandler;
33 using ProgressHandler = Client::ProgressHandler;
34 using ElicitationCompleteHandler = Client::ElicitationCompleteHandler;
35 using TaskStatusHandler = Client::TaskStatusHandler;
36 using RootsListRequestHandler = Client::RootsListRequestHandler;
37 using SamplingRequestHandler = Client::SamplingRequestHandler;
38 using ElicitationRequestHandler = Client::ElicitationRequestHandler;
39 using CustomRequestHandler = Client::CustomRequestHandler;
40 using RawNotificationHandler = Client::RawNotificationHandler;
41
42 virtual ~ClientHandlerInterface() = default;
43
44 virtual void on_initialized() const {}
45 virtual void on_cancelled(const protocol::RequestId&,
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 {}
52 virtual void on_progress(const protocol::ProgressNotificationParams&) const {}
53 virtual void on_elicitation_complete(std::string_view) const {}
54 virtual void on_task_status(const protocol::Task&) const {}
55 virtual void on_roots_list_changed() const {}
56 virtual std::optional<core::Result<protocol::RootsListResult>>
57 on_list_roots_request() const {
58 return std::nullopt;
59 }
60 virtual std::optional<core::Result<protocol::RootsListResult>>
61 on_list_roots_request(CancellationToken cancellation) const {
62 (void)cancellation;
63 return on_list_roots_request();
64 }
65 virtual std::optional<core::Result<protocol::CreateMessageResult>>
66 on_create_message_request(const protocol::CreateMessageParams&) const {
67 return std::nullopt;
68 }
69 virtual std::optional<core::Result<protocol::CreateMessageResult>>
70 on_create_message_request(const protocol::CreateMessageParams& params,
71 CancellationToken cancellation) const {
72 (void)cancellation;
73 return on_create_message_request(params);
74 }
75 virtual std::optional<core::Result<protocol::CreateElicitationResult>>
76 on_create_elicitation_request(
78 return std::nullopt;
79 }
80 virtual std::optional<core::Result<protocol::CreateElicitationResult>>
81 on_create_elicitation_request(
83 CancellationToken cancellation) const {
84 (void)cancellation;
85 return on_create_elicitation_request(params);
86 }
87 virtual std::optional<core::Result<protocol::Json>> on_custom_request(
88 const protocol::JsonRpcRequest&) const {
89 return std::nullopt;
90 }
91 virtual std::optional<core::Result<protocol::Json>> on_custom_request(
92 const protocol::JsonRpcRequest& request,
93 CancellationToken cancellation) const {
94 (void)cancellation;
95 return on_custom_request(request);
96 }
97 virtual void on_raw_notification(const protocol::JsonRpcNotification&) const {
98 }
99};
100
107 using InitializedHandler = Client::InitializedHandler;
108 using CancelledHandler = Client::CancelledHandler;
109 using LoggingMessageHandler = Client::LoggingMessageHandler;
110 using ChangedHandler = Client::ListChangedHandler;
111 using ResourceUpdatedHandler = Client::ResourceUpdatedHandler;
112 using ProgressHandler = Client::ProgressHandler;
113 using ElicitationCompleteHandler = Client::ElicitationCompleteHandler;
114 using TaskStatusHandler = Client::TaskStatusHandler;
115 using RootsListRequestHandler = Client::RootsListRequestHandler;
116 using SamplingRequestHandler = Client::SamplingRequestHandler;
117 using ElicitationRequestHandler = Client::ElicitationRequestHandler;
118 using CustomRequestHandler = Client::CustomRequestHandler;
119 using RawNotificationHandler = Client::RawNotificationHandler;
120
122 InitializedHandler on_initialized;
124 CancelledHandler on_cancelled;
126 LoggingMessageHandler on_logging_message;
128 ChangedHandler on_tool_list_changed;
134 ResourceUpdatedHandler on_resource_updated;
136 ProgressHandler on_progress;
138 ElicitationCompleteHandler on_elicitation_complete;
140 TaskStatusHandler on_task_status;
142 ChangedHandler on_roots_list_changed;
144 RootsListRequestHandler on_list_roots_request;
145 Client::RootsListRequestCancellationHandler
146 on_list_roots_request_with_cancellation;
148 SamplingRequestHandler on_create_message_request;
149 Client::SamplingRequestCancellationHandler
150 on_create_message_request_with_cancellation;
152 ElicitationRequestHandler on_create_elicitation_request;
153 Client::ElicitationRequestCancellationHandler
154 on_create_elicitation_request_with_cancellation;
156 CustomRequestHandler on_custom_request;
157 Client::CustomRequestCancellationHandler on_custom_request_with_cancellation;
159 RootsListRequestHandler on_roots_list_request;
160 Client::RootsListRequestCancellationHandler
161 on_roots_list_request_with_cancellation;
163 SamplingRequestHandler on_sampling_request;
164 Client::SamplingRequestCancellationHandler
165 on_sampling_request_with_cancellation;
167 ElicitationRequestHandler on_elicitation_request;
168 Client::ElicitationRequestCancellationHandler
169 on_elicitation_request_with_cancellation;
171 RawNotificationHandler on_raw_notification;
173 RawNotificationHandler on_custom_notification;
174
177 void apply_to(Client& client) const {
178 if (on_initialized) {
180 }
181 if (on_cancelled) {
183 }
184 if (on_logging_message) {
186 }
189 }
192 }
195 }
198 }
199 if (on_progress) {
200 client.on_progress(on_progress);
201 }
204 }
205 if (on_task_status) {
207 }
210 }
213 }
214 if (on_list_roots_request_with_cancellation) {
215 client.on_list_roots_request(on_list_roots_request_with_cancellation);
216 }
219 }
220 if (on_create_message_request_with_cancellation) {
222 on_create_message_request_with_cancellation);
223 }
226 }
227 if (on_create_elicitation_request_with_cancellation) {
229 on_create_elicitation_request_with_cancellation);
230 }
231 if (on_custom_request) {
233 }
234 if (on_custom_request_with_cancellation) {
235 client.on_custom_request(on_custom_request_with_cancellation);
236 }
239 }
240 if (on_roots_list_request_with_cancellation) {
241 client.on_roots_list_request(on_roots_list_request_with_cancellation);
242 }
245 }
246 if (on_sampling_request_with_cancellation) {
247 client.on_sampling_request(on_sampling_request_with_cancellation);
248 }
251 }
252 if (on_elicitation_request_with_cancellation) {
253 client.on_elicitation_request(on_elicitation_request_with_cancellation);
254 }
257 }
260 }
261 }
262};
263
266inline Client& Client::set_handler(const ClientHandler& handler) {
267 handler.apply_to(*this);
268 return *this;
269}
270
273 on_initialized([&handler]() { handler.on_initialized(); });
274 on_cancelled([&handler](const protocol::RequestId& request_id,
275 std::string_view reason) {
276 handler.on_cancelled(request_id, reason);
277 });
279 [&handler](std::string_view level, std::string_view message) {
280 handler.on_logging_message(level, message);
281 });
282 on_tool_list_changed([&handler]() { handler.on_tool_list_changed(); });
283 on_prompt_list_changed([&handler]() { handler.on_prompt_list_changed(); });
285 [&handler]() { handler.on_resource_list_changed(); });
287 [&handler](const std::string& uri) { handler.on_resource_updated(uri); });
288 on_progress([&handler](const protocol::ProgressNotificationParams& params) {
289 handler.on_progress(params);
290 });
291 on_elicitation_complete([&handler](std::string_view elicitation_id) {
292 handler.on_elicitation_complete(elicitation_id);
293 });
295 [&handler](const protocol::Task& task) { handler.on_task_status(task); });
296 on_roots_list_changed([&handler]() { handler.on_roots_list_changed(); });
297 on_list_roots_request([&handler](CancellationToken cancellation)
299 const auto response = handler.on_list_roots_request(cancellation);
300 if (response.has_value()) {
301 return std::move(*response);
302 }
303 return mcp::core::unexpected(
304 handler_method_not_found("client handler does not handle list_roots"));
305 });
307 [&handler](const protocol::CreateMessageParams& params,
308 CancellationToken cancellation)
310 const auto response =
311 handler.on_create_message_request(params, cancellation);
312 if (response.has_value()) {
313 return std::move(*response);
314 }
315 return mcp::core::unexpected(handler_method_not_found(
316 "client handler does not handle create_message"));
317 });
319 [&handler](const protocol::CreateElicitationRequestParam& params,
320 CancellationToken cancellation)
322 const auto response =
323 handler.on_create_elicitation_request(params, cancellation);
324 if (response.has_value()) {
325 return std::move(*response);
326 }
327 return mcp::core::unexpected(handler_method_not_found(
328 "client handler does not handle elicitation"));
329 });
331 [&handler](
334 const auto response = handler.on_custom_request(request, cancellation);
335 if (response.has_value()) {
336 return std::move(*response);
337 }
338 return mcp::core::unexpected(handler_method_not_found(
339 "client handler does not handle custom request"));
340 });
342 [&handler](const protocol::JsonRpcNotification& notification) {
343 handler.on_raw_notification(notification);
344 });
345 return *this;
346}
347
348} // namespace mcp::client
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