cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
client.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
13
14#include <atomic>
15#include <chrono>
16#include <condition_variable>
17#include <cstdint>
18#include <functional>
19#include <memory>
20#include <mutex>
21#include <optional>
22#include <string>
23#include <string_view>
24#include <unordered_map>
25#include <utility>
26#include <vector>
27
40#include "cxxmcp/request.hpp"
41
42namespace mcp::client {
43
44struct HttpAuthChallenge;
45using HttpAuthRefreshHandler =
46 std::function<std::optional<std::string>(const HttpAuthChallenge&)>;
47
51 std::string endpoint;
52
54 std::chrono::milliseconds timeout{30000};
55};
56
59struct ClientHandler;
60struct ClientHandlerInterface;
61
67 std::function<core::Result<protocol::JsonRpcResponse>(
69
74using TransportNotificationHandler = std::function<core::Result<core::Unit>(
76
83class Transport {
84 public:
85 virtual ~Transport() = default;
86
91 const protocol::JsonRpcRequest& request) = 0;
92
97 const protocol::JsonRpcNotification& notification);
98
109 TransportRequestHandler request_handler,
110 TransportNotificationHandler notification_handler = {}) {
111 (void)request_handler;
112 (void)notification_handler;
113 return core::Unit{};
114 }
115
120 virtual void stop() noexcept {}
121};
122
132class Client {
133 public:
134#if defined(CXXMCP_ENABLE_HTTP)
137 struct StreamableHttpEndpoint {
141 std::string uri;
142
144 std::string host;
146 std::uint16_t port = 80;
148 std::string path = "/mcp";
150 std::unordered_map<std::string, std::string> headers;
151
155 std::optional<std::string> auth_header;
156
159 HttpAuthRefreshHandler auth_refresh_handler;
160
162 std::chrono::milliseconds timeout{30000};
163 };
164#endif
165
169 std::string command;
171 std::vector<std::string> args;
173 std::string cwd;
175 std::unordered_map<std::string, std::string> env;
176 };
177
178 using Root = protocol::Root;
179
184 std::function<void(std::string_view, std::string_view)>;
185
187 using InitializedHandler = std::function<void()>;
188
193 std::function<void(const protocol::RequestId&, std::string_view)>;
194
197 using ListChangedHandler = std::function<void()>;
198
201 using ResourceUpdatedHandler = std::function<void(const std::string&)>;
202
205 std::function<void(const protocol::ProgressNotificationParams&)>;
206
209 using ElicitationCompleteHandler = std::function<void(std::string_view)>;
210
212 using TaskStatusHandler = std::function<void(const protocol::Task&)>;
213
217 std::function<core::Result<protocol::RootsListResult>()>;
218 using RootsListRequestCancellationHandler =
219 std::function<core::Result<protocol::RootsListResult>(CancellationToken)>;
220
225 std::function<core::Result<protocol::CreateMessageResult>(
227 using SamplingRequestCancellationHandler =
228 std::function<core::Result<protocol::CreateMessageResult>(
230
235 std::function<core::Result<protocol::CreateElicitationResult>(
237 using ElicitationRequestCancellationHandler =
238 std::function<core::Result<protocol::CreateElicitationResult>(
240
244 using CustomRequestHandler = std::function<core::Result<protocol::Json>(
246 using CustomRequestCancellationHandler =
247 std::function<core::Result<protocol::Json>(
249 using ListRootsRequestHandler = RootsListRequestHandler;
250 using CreateMessageRequestHandler = SamplingRequestHandler;
251 using CreateElicitationRequestHandler = ElicitationRequestHandler;
252
255 std::function<void(const protocol::JsonRpcNotification&)>;
256
257#if defined(CXXMCP_ENABLE_HTTP)
261 static Client connect_streamable_http(StreamableHttpEndpoint endpoint);
262
266 static Client connect_streamable_http(std::string uri);
267
271 static Client connect_legacy_sse(StreamableHttpEndpoint endpoint);
272
276 static Client connect_legacy_sse(std::string uri);
277#endif
278
284
287 explicit Client(std::unique_ptr<Transport> transport);
288 Client(const Client&) = delete;
289 Client& operator=(const Client&) = delete;
290 Client(Client&& other) noexcept;
291 Client& operator=(Client&& other) noexcept;
292
297 core::Result<protocol::Json> initialize(std::string client_name = "cxxmcp",
298 std::string client_version = "0");
299
306 std::string client_version,
307 RequestOptions options);
308
310 std::optional<protocol::ServerCapabilities> server_capabilities() const;
311
315
320 std::string reason = {});
321
328 protocol::ProgressToken progress_token, double progress,
329 std::optional<double> total = std::nullopt, std::string message = {});
330
333
336
339
342 const protocol::PaginatedRequestParams& params = {});
343
346
349 const protocol::PromptsGetParams& params);
350
353 std::string_view name,
354 const protocol::Json& arguments = protocol::Json::object());
355
358
361 const protocol::PaginatedRequestParams& params = {});
362
365
368 const protocol::ResourcesReadParams& params);
369
372 std::string_view uri);
373
377
381 const protocol::PaginatedRequestParams& params = {});
382
387
390
393 const protocol::PaginatedRequestParams& params = {});
394
398
401
404 const protocol::ToolCall& call);
405
408 std::string_view name,
409 const protocol::Json& arguments = protocol::Json::object());
410
414
417
420 std::string_view prompt_name, std::string_view argument_name,
421 std::string current_value,
422 protocol::Json context = protocol::Json::object());
423
427 std::string_view uri_template, std::string_view argument_name,
428 std::string current_value,
429 protocol::Json context = protocol::Json::object());
430
433 std::string_view prompt_name, std::string_view argument_name,
434 std::string current_value,
435 protocol::Json context = protocol::Json::object());
436
439 std::string_view uri_template, std::string_view argument_name,
440 std::string current_value,
441 protocol::Json context = protocol::Json::object());
442
447
450
454
457 const protocol::Json& request);
458
461
465
468
471
473 core::Result<protocol::Task> get_task(std::string_view task_id);
474
478
480 core::Result<protocol::Task> cancel_task(std::string_view task_id);
481
485
487 core::Result<protocol::Json> task_result(std::string_view task_id);
488
491 const protocol::LoggingSetLevelParams& params);
492
494 core::Result<core::Unit> set_level(std::string_view level);
495
497 core::Result<core::Unit> subscribe(std::string_view uri);
498
501
504
507 std::string method, protocol::Json params = protocol::Json::object(),
508 RequestOptions options = {});
509
513 template <class T, class Parser>
515 Parser parser, RequestOptions options = {}) {
517 request.method = std::move(method);
518 request.params = std::move(params);
519 request.id = next_request_id();
520 if (options.meta.has_value()) {
521 request.meta = std::move(options.meta);
522 }
523 request.transport_headers = std::move(options.headers);
524 request.protocol_version_override = std::move(options.protocol_version);
525
526 const auto request_id = request.id;
527 ++in_flight_requests_;
528 auto in_flight_mutex = &in_flight_mutex_;
529 auto in_flight_cv = in_flight_cv_;
530 auto in_flight_count = &in_flight_requests_;
532 request_id, options.timeout, options.cancellation_token,
533 [this, request_id](std::string reason) mutable {
534 return notify_cancelled(std::move(request_id), std::move(reason));
535 },
536 [this, request = std::move(request), parser = std::move(parser),
537 in_flight_mutex, in_flight_cv,
538 in_flight_count]() mutable -> core::Result<T> {
539 auto payload = raw_request(request);
540 core::Result<T> result;
541 if (!payload) {
542 result = mcp::core::unexpected(payload.error());
543 } else {
544 result = parser(*payload);
545 }
546 {
547 std::lock_guard lock(*in_flight_mutex);
548 --(*in_flight_count);
549 in_flight_cv->notify_one();
550 }
551 return result;
552 });
553 }
554
557 RequestOptions options = {});
558
561 RequestOptions options = {});
562
565 RequestOptions options = {});
566
571
574 const protocol::ToolCall& call, RequestOptions options = {});
575
578 std::string_view name,
579 const protocol::Json& arguments = protocol::Json::object(),
580 RequestOptions options = {});
581
584 const protocol::ToolCall& call, RequestOptions options = {});
585
588 const protocol::PromptsGetParams& params, RequestOptions options = {});
589
592 std::string_view name,
593 const protocol::Json& arguments = protocol::Json::object(),
594 RequestOptions options = {});
595
598 const protocol::ResourcesReadParams& params, RequestOptions options = {});
599
602 std::string_view uri, RequestOptions options = {});
603
606 const protocol::CompleteParams& request, RequestOptions options = {});
607
610 RequestOptions options = {});
611
614 const protocol::CreateMessageParams& request,
615 RequestOptions options = {});
616
619 const protocol::Json& request, RequestOptions options = {});
620
624 RequestOptions options = {});
625
628 const protocol::Json& request, RequestOptions options = {});
629
632 RequestOptions options = {});
633
636 const protocol::TaskGetParams& request, RequestOptions options = {});
637
640 RequestOptions options = {});
641
644 const protocol::TaskCancelParams& request, RequestOptions options = {});
645
648 RequestOptions options = {});
649
652 const protocol::TaskResultParams& request, RequestOptions options = {});
653
656 RequestOptions options = {});
657
660 const protocol::JsonRpcNotification& notification);
661
663 std::vector<protocol::Root> list_roots() const;
664
670 Client& set_roots(std::vector<protocol::Root> roots);
671
676
679
682
685
688
691
694
697
700
703
706
709
711 Client& on_list_roots_request(ListRootsRequestHandler handler);
712 Client& on_list_roots_request(RootsListRequestCancellationHandler handler);
713
715 Client& on_create_message_request(CreateMessageRequestHandler handler);
716 Client& on_create_message_request(SamplingRequestCancellationHandler handler);
717
720 CreateElicitationRequestHandler handler);
721 Client& on_create_elicitation_request(
722 ElicitationRequestCancellationHandler handler);
723
727 Client& on_custom_request(CustomRequestCancellationHandler handler);
728
731 Client& on_roots_list_request(RootsListRequestCancellationHandler handler);
732
735 Client& on_sampling_request(SamplingRequestCancellationHandler handler);
736
739 Client& on_elicitation_request(ElicitationRequestCancellationHandler handler);
740
743
746
751 Client& set_handler(const ClientHandler& handler);
752
756 Client& set_handler(const ClientHandlerInterface& handler);
757
761
765 const protocol::JsonRpcNotification& notification);
766
770 const protocol::JsonRpcRequest& request);
771
774 const protocol::JsonRpcRequest& request);
775
778 const protocol::JsonRpcNotification& notification);
779
781 void stop() noexcept;
782
786
787 private:
788 template <typename Handler>
789 Handler copy_handler(Handler Client::* slot) const {
790 std::lock_guard<std::mutex> lock(*handlers_mutex_);
791 return this->*slot;
792 }
793
794 template <typename Handler>
795 void store_handler(Handler Client::* slot, Handler handler) {
796 std::lock_guard<std::mutex> lock(*handlers_mutex_);
797 this->*slot = std::move(handler);
798 }
799
800 core::Result<protocol::Json> send_request(std::string method,
801 protocol::Json params);
804 core::Result<core::Unit> ensure_transport_started();
805 core::Result<core::Unit> record_server_capabilities(
806 const protocol::Json& initialize_payload);
807 bool server_capabilities_known() const noexcept;
808 bool supports_server_completion() const noexcept;
809 bool supports_server_logging() const noexcept;
810 bool supports_server_resource_subscribe() const noexcept;
811 bool supports_server_task_list() const noexcept;
812 bool supports_server_task_cancel() const noexcept;
813 bool supports_server_tasks() const noexcept;
814 bool supports_server_task_tool_call() const noexcept;
815 std::optional<protocol::ClientCapabilities> capabilities_snapshot() const;
816 std::optional<protocol::ServerCapabilities> server_capabilities_snapshot()
817 const;
818 CancellationToken begin_request_cancellation(
819 const protocol::RequestId& request_id);
820 void end_request_cancellation(const protocol::RequestId& request_id) noexcept;
821 void cancel_request(const protocol::RequestId& request_id) noexcept;
822 std::int64_t next_request_id() noexcept {
823 return next_request_id_.fetch_add(1, std::memory_order_relaxed);
824 }
825
826 std::unique_ptr<Transport> transport_;
827 std::atomic<std::int64_t> next_request_id_{1};
828 bool transport_started_ = false;
829 std::vector<protocol::Root> roots_;
830 std::optional<protocol::Json> last_initialize_params_;
831 std::optional<protocol::ClientCapabilities> capabilities_;
832 std::optional<protocol::ServerCapabilities> server_capabilities_;
833 InitializedHandler initialized_handler_;
834 CancelledHandler cancelled_handler_;
835 LoggingMessageHandler logging_message_handler_;
836 ListChangedHandler tool_list_changed_handler_;
837 ListChangedHandler prompt_list_changed_handler_;
838 ListChangedHandler resource_list_changed_handler_;
839 ResourceUpdatedHandler resource_updated_handler_;
840 ProgressHandler progress_handler_;
841 ElicitationCompleteHandler elicitation_complete_handler_;
842 TaskStatusHandler task_status_handler_;
843 ListChangedHandler roots_list_changed_handler_;
844 RootsListRequestHandler roots_list_request_handler_;
845 RootsListRequestCancellationHandler roots_list_request_cancellation_handler_;
846 SamplingRequestHandler sampling_request_handler_;
847 SamplingRequestCancellationHandler sampling_request_cancellation_handler_;
848 ElicitationRequestHandler elicitation_request_handler_;
849 ElicitationRequestCancellationHandler
850 elicitation_request_cancellation_handler_;
851 CustomRequestHandler custom_request_handler_;
852 CustomRequestCancellationHandler custom_request_cancellation_handler_;
853 RawNotificationHandler raw_notification_handler_;
854 mutable std::shared_ptr<std::mutex> state_mutex_ =
855 std::make_shared<std::mutex>();
856 mutable std::shared_ptr<std::mutex> handlers_mutex_ =
857 std::make_shared<std::mutex>();
858 std::shared_ptr<std::mutex> active_request_cancellations_mutex_ =
859 std::make_shared<std::mutex>();
860 std::shared_ptr<std::unordered_map<std::string, CancellationSource>>
861 active_request_cancellations_ = std::make_shared<
862 std::unordered_map<std::string, CancellationSource>>();
863
864 std::atomic<int> in_flight_requests_{0};
865 mutable std::mutex in_flight_mutex_;
866 std::shared_ptr<std::condition_variable> in_flight_cv_ =
867 std::make_shared<std::condition_variable>();
868};
869
870} // namespace mcp::client
871
MCP client and server capability declarations.
Copyable token observed by cancellation-aware SDK operations.
Definition cancellation.hpp:104
Definition request.hpp:123
High-level MCP client compatibility API.
Definition client.hpp:132
Client & on_task_status(TaskStatusHandler handler)
Registers a callback for task status notifications.
core::Result< std::vector< protocol::ToolDefinition > > list_tools()
Lists one page of tool definitions advertised by the server.
core::Result< core::Unit > unsubscribe(std::string_view uri)
Unsubscribes from resource update notifications for a URI.
Client & on_logging_message(LoggingMessageHandler handler)
Registers a callback for logging message notifications.
core::Result< protocol::Json > raw_request(const protocol::JsonRpcRequest &request)
Sends a raw JSON-RPC request and returns the raw result JSON.
Client & on_list_roots_request(ListRootsRequestHandler handler)
Registers a handler for server list-roots requests.
RequestHandle< protocol::ToolResult > call_tool_async(std::string_view name, const protocol::Json &arguments=protocol::Json::object(), RequestOptions options={})
Asynchronously calls a tool by name.
core::Result< std::vector< protocol::Resource > > list_all_resources()
Lists all resources, following pagination cursors until exhausted.
core::Result< protocol::PromptsListResult > list_prompts_page(const protocol::PaginatedRequestParams &params={})
Lists one typed page of prompts, preserving pagination metadata.
RequestHandle< std::vector< protocol::Prompt > > list_prompts_async(RequestOptions options={})
Asynchronously lists prompts and parses the typed response.
core::Result< protocol::Task > get_task(std::string_view task_id)
Gets a task by task identifier.
core::Result< protocol::PromptsGetResult > get_prompt(const protocol::PromptsGetParams &params)
Gets a prompt by protocol parameter object.
std::function< void(std::string_view, std::string_view)> LoggingMessageHandler
Receives logging messages from the server.
Definition client.hpp:184
core::Result< std::vector< protocol::ToolDefinition > > list_all_tools()
Lists all tool definitions, following pagination cursors until exhausted.
RequestHandle< T > request_async(std::string method, protocol::Json params, Parser parser, RequestOptions options={})
Sends a cancellable typed request and parses its result payload.
Definition client.hpp:514
std::function< void(const protocol::ProgressNotificationParams &)> ProgressHandler
Receives progress notifications associated with a progress token.
Definition client.hpp:205
core::Result< core::Unit > subscribe(std::string_view uri)
Subscribes to resource update notifications for a URI.
Client & on_initialized(InitializedHandler handler)
Registers a callback for initialized notifications.
std::optional< protocol::ServerCapabilities > server_capabilities() const
Returns server capabilities learned from initialize(), if known.
core::Result< protocol::Json > initialize(std::string client_name="cxxmcp", std::string client_version="0")
Sends the MCP initialize request.
core::Result< core::Unit > notify_roots_list_changed()
Notifies the server that the client's roots list changed.
core::Result< core::Unit > notify(const protocol::JsonRpcNotification &notification)
Sends a raw JSON-RPC notification.
core::Result< protocol::CreateElicitationResult > create_elicitation(const protocol::CreateElicitationRequestParam &request)
Sends an elicitation request using typed protocol parameters.
core::Result< core::Unit > notify_progress(protocol::ProgressToken progress_token, double progress, std::optional< double > total=std::nullopt, std::string message={})
Sends a progress notification.
std::function< void(const std::string &)> ResourceUpdatedHandler
Receives resource update notifications.
Definition client.hpp:201
core::Result< protocol::Json > create_elicitation(const protocol::Json &request)
Sends an elicitation request using raw JSON parameters.
core::Result< core::Unit > ping()
Sends an MCP ping request.
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
core::Result< std::vector< protocol::Prompt > > list_all_prompts()
Lists all prompts, following pagination cursors until exhausted.
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.
RequestHandle< protocol::Json > task_result_async(const protocol::TaskResultParams &request, RequestOptions options={})
Asynchronously gets a task result using typed protocol parameters.
Client & set_roots(std::vector< protocol::Root > roots)
Replaces the roots advertised by this client.
Client & on_elicitation_request(ElicitationRequestHandler handler)
Compatibility alias for on_create_elicitation_request().
RequestHandle< protocol::Task > cancel_task_async(std::string_view task_id, RequestOptions options={})
Asynchronously cancels a task by identifier.
core::Result< protocol::Json > complete(const protocol::Json &request)
Requests completion using raw JSON parameters.
Client & on_sampling_request(SamplingRequestHandler handler)
Compatibility alias for on_create_message_request().
RequestHandle< protocol::ToolResult > call_tool_async(const protocol::ToolCall &call, RequestOptions options={})
Asynchronously calls a tool and parses the typed response.
core::Result< std::vector< protocol::Task > > list_all_tasks()
Lists all tasks, following pagination cursors until exhausted.
core::Result< std::vector< std::string > > complete_resource_simple(std::string_view uri_template, std::string_view argument_name, std::string current_value, protocol::Json context=protocol::Json::object())
Returns resource completion values only.
Client(std::unique_ptr< Transport > transport)
Constructs a client from a custom transport.
core::Result< core::Unit > start()
Starts the underlying transport receive side if the transport needs explicit startup.
RequestHandle< protocol::Task > cancel_task_async(const protocol::TaskCancelParams &request, RequestOptions options={})
Asynchronously cancels a task using typed protocol parameters.
core::Result< protocol::CompleteResult > complete(const protocol::CompleteParams &request)
Requests completion using typed protocol parameters.
RequestHandle< std::vector< protocol::Resource > > list_resources_async(RequestOptions options={})
Asynchronously lists resources and parses the typed response.
void stop() noexcept
Stops the underlying transport and clears receive-side state.
core::Result< protocol::Json > request(const protocol::JsonRpcRequest &request)
Sends a raw JSON-RPC request and returns its result JSON.
core::Result< core::Unit > notify_initialized()
Sends the initialized notification after a successful initialize exchange.
core::Result< protocol::Task > get_task(const protocol::TaskGetParams &request)
Gets a task using typed protocol parameters.
RequestHandle< std::vector< protocol::ToolDefinition > > list_tools_async(RequestOptions options={})
Asynchronously lists tools and parses the typed response.
RequestHandle< protocol::Json > create_message_async(const protocol::Json &request, RequestOptions options={})
Asynchronously sends a raw sampling request.
Client & on_progress(ProgressHandler handler)
Registers a callback for progress notifications.
core::Result< protocol::ToolResult > call_raw(std::string_view name, const protocol::Json &arguments=protocol::Json::object())
Calls a tool by name with optional JSON arguments.
static Client connect_stdio(StdioEndpoint endpoint)
Creates a client connected to a child process over stdio.
core::Result< protocol::PromptsGetResult > get_prompt(std::string_view name, const protocol::Json &arguments=protocol::Json::object())
Gets a prompt by name and optional JSON arguments.
core::Result< protocol::ResourcesReadResult > read_resource(std::string_view uri)
Reads a resource by URI.
core::Result< std::vector< protocol::ResourceTemplate > > list_resource_templates()
Lists one page of resource templates advertised by the server.
core::Result< protocol::Task > cancel_task(std::string_view task_id)
Cancels a task by task identifier.
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
core::Result< std::vector< std::string > > complete_prompt_simple(std::string_view prompt_name, std::string_view argument_name, std::string current_value, protocol::Json context=protocol::Json::object())
Returns prompt completion values only.
core::Result< protocol::Json > initialize(std::string client_name, std::string client_version, RequestOptions options)
Sends the MCP initialize request with per-request options.
RequestHandle< protocol::PromptsGetResult > get_prompt_async(const protocol::PromptsGetParams &params, RequestOptions options={})
Asynchronously gets a prompt and parses the typed response.
Client & set_capabilities(protocol::ClientCapabilities capabilities)
Sets client capabilities used during initialization.
RequestHandle< protocol::Json > request_async(std::string method, protocol::Json params=protocol::Json::object(), RequestOptions options={})
Sends a cancellable raw request with an auto-generated id.
core::Result< protocol::CreateMessageResult > create_message(const protocol::CreateMessageParams &request)
Sends a sampling createMessage request using typed protocol parameters.
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.
core::Result< protocol::CompletionResult > complete_resource_argument(std::string_view uri_template, std::string_view argument_name, std::string current_value, protocol::Json context=protocol::Json::object())
Completes a resource template argument using RMCP-style helper parameters.
Client & on_custom_request(CustomRequestHandler handler)
Registers a handler for custom server requests not handled by the built-in client dispatcher.
core::Result< protocol::CompletionResult > complete_prompt_argument(std::string_view prompt_name, std::string_view argument_name, std::string current_value, protocol::Json context=protocol::Json::object())
Completes a prompt argument using RMCP-style helper parameters.
RequestHandle< protocol::ResourcesReadResult > read_resource_async(const protocol::ResourcesReadParams &params, RequestOptions options={})
Asynchronously reads a resource and parses the typed response.
std::vector< protocol::Root > list_roots() const
Returns the roots currently advertised by this client.
core::Result< core::Unit > set_level(std::string_view level)
Sets the server logging level by level name.
RequestHandle< protocol::Json > task_result_async(std::string_view task_id, RequestOptions options={})
Asynchronously gets a task result by identifier.
core::Result< protocol::ResourceTemplatesListResult > list_resource_templates_page(const protocol::PaginatedRequestParams &params={})
Lists one typed page of resource templates.
std::function< core::Result< protocol::CreateMessageResult >(const protocol::CreateMessageParams &)> SamplingRequestHandler
Handles a server sampling request.
Definition client.hpp:226
RequestHandle< protocol::CreateTaskResult > call_tool_task_async(const protocol::ToolCall &call, RequestOptions options={})
Asynchronously starts a task-aware tool call.
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.
RequestHandle< protocol::PromptsGetResult > get_prompt_async(std::string_view name, const protocol::Json &arguments=protocol::Json::object(), RequestOptions options={})
Asynchronously gets a prompt by name.
RequestHandle< protocol::ResourcesReadResult > read_resource_async(std::string_view uri, RequestOptions options={})
Asynchronously reads a resource by URI.
core::Result< core::Unit > handle_notification(const protocol::JsonRpcNotification &notification)
Dispatches an inbound notification through built-in and registered handlers.
core::Result< protocol::ResourcesListResult > list_resources_page(const protocol::PaginatedRequestParams &params={})
Lists one typed page of resources, preserving pagination metadata.
RequestHandle< protocol::CreateElicitationResult > create_elicitation_async(const protocol::CreateElicitationRequestParam &request, RequestOptions options={})
Asynchronously creates an elicitation request.
RequestHandle< protocol::Json > create_elicitation_async(const protocol::Json &request, RequestOptions options={})
Asynchronously sends a raw elicitation request.
std::function< void()> ListChangedHandler
Receives list-change notifications for prompts, resources, tools, or roots.
Definition client.hpp:197
core::Result< std::vector< protocol::Task > > list_tasks()
Lists one page of tasks advertised by the server.
core::Result< protocol::CreateTaskResult > call_tool_task(const protocol::ToolCall &call)
Calls a task-aware tool and returns the created task handle.
core::Result< protocol::ResourcesReadResult > read_resource(const protocol::ResourcesReadParams &params)
Reads a resource by protocol parameter object.
RequestHandle< protocol::Task > get_task_async(const protocol::TaskGetParams &request, RequestOptions options={})
Asynchronously gets a task using typed protocol parameters.
core::Result< protocol::Json > task_result(const protocol::TaskResultParams &request)
Gets a task result using typed protocol parameters.
core::Result< std::vector< protocol::Resource > > list_resources()
Lists one page of resources advertised by the server.
RequestHandle< std::vector< protocol::Task > > list_tasks_async(RequestOptions options={})
Asynchronously lists tasks and parses the typed response.
core::Result< protocol::ToolsListResult > list_tools_page(const protocol::PaginatedRequestParams &params={})
Lists one typed page of tool definitions.
RequestHandle< protocol::CreateMessageResult > create_message_async(const protocol::CreateMessageParams &request, RequestOptions options={})
Asynchronously creates a sampling message.
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
core::Result< core::Unit > raw_notification(const protocol::JsonRpcNotification &notification)
Sends a raw JSON-RPC notification.
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
core::Result< protocol::ToolResult > call_tool(const protocol::ToolCall &call)
Calls a tool using a protocol ToolCall object.
core::Result< core::Unit > notify_cancelled(protocol::RequestId request_id, std::string reason={})
Sends a cancellation notification for a request.
RequestHandle< protocol::Json > complete_async(const protocol::Json &request, RequestOptions options={})
Asynchronously sends a raw completion request.
Client & on_roots_list_changed(ListChangedHandler handler)
Registers a callback for roots list change notifications.
RequestHandle< protocol::CompleteResult > complete_async(const protocol::CompleteParams &request, RequestOptions options={})
Asynchronously completes a prompt/resource argument.
core::Result< protocol::Task > cancel_task(const protocol::TaskCancelParams &request)
Cancels a task using typed protocol parameters.
RequestHandle< protocol::Task > get_task_async(std::string_view task_id, RequestOptions options={})
Asynchronously gets a task by identifier.
core::Result< protocol::Json > create_message(const protocol::Json &request)
Sends a sampling createMessage request using raw JSON parameters.
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
core::Result< protocol::JsonRpcResponse > handle_request(const protocol::JsonRpcRequest &request)
Dispatches an inbound server request through built-in and custom handlers.
core::Result< protocol::TaskListResult > list_tasks_page(const protocol::TaskListParams &request={})
Lists one typed task page, preserving pagination metadata.
RequestHandle< std::vector< protocol::ResourceTemplate > > list_resource_templates_async(RequestOptions options={})
Asynchronously lists resource templates and parses the typed response.
core::Result< core::Unit > set_level(const protocol::LoggingSetLevelParams &params)
Sets the server logging level using typed protocol parameters.
core::Result< std::vector< protocol::ResourceTemplate > > list_all_resource_templates()
Lists all resource templates, following pagination cursors until exhausted.
core::Result< std::vector< protocol::Prompt > > list_prompts()
Lists one page of prompts advertised by the server.
Client & on_raw_notification(RawNotificationHandler handler)
Registers an observer for raw inbound notifications.
core::Result< protocol::Json > task_result(std::string_view task_id)
Gets a task result by task identifier.
Abstract client transport used by Client to exchange JSON-RPC messages.
Definition client.hpp:83
virtual core::Result< core::Unit > start(TransportRequestHandler request_handler, TransportNotificationHandler notification_handler={})
Starts receiving inbound messages for transports that need an active loop.
Definition client.hpp:108
virtual core::Result< core::Unit > send_notification(const protocol::JsonRpcNotification &notification)
Sends a JSON-RPC notification without waiting for a response.
virtual core::Result< protocol::JsonRpcResponse > send(const protocol::JsonRpcRequest &request)=0
Sends a JSON-RPC request and waits for the corresponding response.
virtual void stop() noexcept
Requests transport shutdown.
Definition client.hpp:120
std::function< core::Result< core::Unit >(const protocol::JsonRpcNotification &)> TransportNotificationHandler
Handles JSON-RPC notifications sent by the server to this client.
Definition client.hpp:75
std::function< core::Result< protocol::JsonRpcResponse >(const protocol::JsonRpcRequest &)> TransportRequestHandler
Handles JSON-RPC requests sent by the server to this client.
Definition client.hpp:68
Aggregate callback configuration for mcp::client::Client.
Completion request and result payloads.
Elicitation request, result, and schema payloads.
Logging level and notification payloads.
Prompt discovery and rendering payloads.
Shared JSON, JSON-RPC, error, cancellation, and progress model types.
std::variant< std::int64_t, std::string > ProgressToken
Identifier used to associate progress notifications with a request.
Definition types.hpp:215
nlohmann::json Json
JSON value type used by all protocol DTOs.
Definition types.hpp:28
std::variant< std::int64_t, std::string > RequestId
JSON-RPC request or response identifier.
Definition types.hpp:56
Request lifecycle helpers for cancellable SDK calls.
Resource listing, template, subscription, and read payloads.
Shared result and error primitives used by the public cxxmcp SDK.
std::monostate Unit
Success value for operations that only need to report failure.
Definition result.hpp:55
tl::expected< T, Error > Result
Alias for the SDK result type.
Definition result.hpp:64
Client root discovery payloads.
Client-side model sampling request and response payloads.
JSON-RPC method names and message construction/parsing helpers.
Options for an outbound SDK request.
Definition request.hpp:103
Contract-style client handler interface.
Definition handler.hpp:27
Optional callback bundle for configuring a Client in one call.
Definition handler.hpp:106
Basic options for endpoint-oriented client construction.
Definition client.hpp:49
std::chrono::milliseconds timeout
Per-operation timeout used by transports that support request deadlines.
Definition client.hpp:54
std::string endpoint
Remote endpoint URI or implementation-defined endpoint string.
Definition client.hpp:51
Endpoint options for launching a child process over stdio.
Definition client.hpp:167
std::unordered_map< std::string, std::string > env
Extra environment variables for the child process.
Definition client.hpp:175
std::string cwd
Optional working directory for the child process.
Definition client.hpp:173
std::vector< std::string > args
Command-line arguments passed to the executable.
Definition client.hpp:171
std::string command
Executable to launch.
Definition client.hpp:169
Capabilities advertised by an MCP client during initialization.
Definition capabilities.hpp:296
Parameters for completion/complete.
Definition completion.hpp:98
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
std::string method
JSON-RPC method name such as tools/call or initialize.
Definition types.hpp:101
Parameters for logging/setLevel.
Definition logging.hpp:42
Shared pagination params for MCP list requests that use cursor.
Definition types.hpp:43
Parameters for notifications/progress.
Definition types.hpp:226
Parameters for prompts/get.
Definition prompt.hpp:181
Parameters for resources/read.
Definition resource.hpp:227
One client root entry.
Definition roots.hpp:24
Parameters for tasks/cancel.
Definition task.hpp:234
Parameters for tasks/get.
Definition task.hpp:209
Parameters for tasks/list.
Definition task.hpp:187
Snapshot of an asynchronous task.
Definition task.hpp:144
Parameters for tools/call.
Definition tool.hpp:335
Tool definition, call, and result payloads.