cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
server.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
13
14#include <cstdint>
15#include <functional>
16#include <memory>
17#include <mutex>
18#include <optional>
19#include <stdexcept>
20#include <string>
21#include <string_view>
22#include <tuple>
23#include <type_traits>
24#include <unordered_map>
25#include <unordered_set>
26#include <utility>
27#include <vector>
28
40
41namespace mcp::server {
42
47
49 std::string server_name = "cxxmcp";
50
52 std::string server_version = "2.0.0";
53
55 std::string instructions;
56};
57
59struct ServerInfo {
61 std::string name;
62
64 std::string version;
65
67 std::string instructions;
68};
69
72struct ServerHandler;
74
83class Server {
84 public:
87 explicit Server(ServerOptions options);
88
91
92 Server(const Server&) = delete;
93 Server& operator=(const Server&) = delete;
94 Server(Server&&) noexcept;
95 Server& operator=(Server&&) noexcept;
96
99
101 const protocol::ServerCapabilities& capabilities() const noexcept;
102
104 ToolRegistry& tools() noexcept;
105
107 const ToolRegistry& tools() const noexcept;
108
110 std::vector<protocol::ToolDefinition> list_tools() const;
111
113 core::Result<protocol::ToolsListResult> list_tools(
114 const protocol::PaginatedRequestParams& params,
115 const SessionContext& context) const;
116
118 core::Result<protocol::ToolDefinition> get_tool(std::string_view name) const;
119
126 core::Result<protocol::ToolResult> call_tool(
127 std::string_view name,
128 protocol::Json arguments = protocol::Json::object(),
129 const std::string& session_id = {}) const;
131 std::string_view name, protocol::Json arguments,
132 const SessionContext& context,
133 CancellationToken cancellation = CancellationToken::none()) const;
134
137
139 Server& use_task_manager(std::shared_ptr<TaskOperationProcessor> processor);
140
142 std::shared_ptr<TaskOperationProcessor> task_manager() const noexcept;
143
146
148 const PromptRegistry& prompts() const noexcept;
149
151 std::vector<protocol::Prompt> list_prompts() const;
152
154 core::Result<protocol::PromptsListResult> list_prompts(
155 const protocol::PaginatedRequestParams& params,
156 const SessionContext& context) const;
157
163 core::Result<protocol::PromptsGetResult> get_prompt(
164 std::string_view name,
165 protocol::Json arguments = protocol::Json::object(),
166 const std::string& session_id = {}) const;
168 std::string_view name, protocol::Json arguments,
169 const SessionContext& context,
170 CancellationToken cancellation = CancellationToken::none()) const;
171
174
176 const ResourceRegistry& resources() const noexcept;
177
179 std::vector<protocol::Resource> list_resources() const;
180
182 core::Result<protocol::ResourcesListResult> list_resources(
183 const protocol::PaginatedRequestParams& params,
184 const SessionContext& context) const;
185
191 core::Result<protocol::ResourcesReadResult> read_resource(
192 std::string_view uri, protocol::Json params = protocol::Json::object(),
193 const std::string& session_id = {}) const;
195 std::string_view uri, protocol::Json params,
196 const SessionContext& context,
197 CancellationToken cancellation = CancellationToken::none()) const;
198
201
204
206 std::vector<protocol::ResourceTemplate> list_resource_templates() const;
207
209 core::Result<protocol::ResourceTemplatesListResult> list_resource_templates(
210 const protocol::PaginatedRequestParams& params,
211 const SessionContext& context) const;
212
214 core::Result<protocol::Json> initialize();
215
218 core::Result<protocol::Json> ping(const SessionContext& context = {});
219
225 const protocol::JsonRpcRequest& request, const SessionContext& context);
226
234 const SessionContext& context);
235
238 const protocol::JsonRpcNotification& notification,
239 const SessionContext& context);
240
243
246
249
252
256
259 const SessionContext& context, std::string_view uri, bool subscribed);
260
264
268
272 std::string elicitation_id);
273
276
283 const protocol::Task& task);
284
288 core::Result<core::Unit> add_transport(std::unique_ptr<Transport> transport);
289
297
299 void set_auth_provider(std::unique_ptr<AuthProvider> auth_provider);
300
302 void set_rate_limiter(std::unique_ptr<RateLimiter> rate_limiter);
303
306 std::shared_ptr<const JsonSchemaValidator> validator);
307
309 std::shared_ptr<const JsonSchemaValidator> schema_validator() const noexcept {
310 return schema_validator_;
311 }
312
315
317 void stop() noexcept;
318
322 std::function<core::Result<protocol::Json>(const protocol::Json&)>;
323 using JsonContextHandler = std::function<core::Result<protocol::Json>(
324 const protocol::Json&, const SessionContext&)>;
325 using JsonRequestContextHandler = std::function<core::Result<protocol::Json>(
326 const protocol::Json&, const SessionContext&, CancellationToken)>;
327
330 std::function<void(std::string_view, std::string_view)>;
331
336 std::function<std::optional<protocol::JsonRpcResponse>(
337 const protocol::JsonRpcRequest&, const SessionContext&)>;
338 using RawRequestContextHandler =
339 std::function<std::optional<protocol::JsonRpcResponse>(
340 const protocol::JsonRpcRequest&, const SessionContext&,
342
344 using RawNotificationHandler = std::function<core::Result<core::Unit>(
345 const protocol::JsonRpcNotification&, const SessionContext&)>;
346
348 using TaskListHandler = std::function<core::Result<protocol::TaskListResult>(
349 const protocol::TaskListParams&, const SessionContext&)>;
350
353 std::function<core::Result<protocol::ToolsListResult>(
354 const protocol::PaginatedRequestParams&, const SessionContext&)>;
355
358 std::function<core::Result<protocol::PromptsListResult>(
359 const protocol::PaginatedRequestParams&, const SessionContext&)>;
360
363 std::function<core::Result<protocol::ResourcesListResult>(
364 const protocol::PaginatedRequestParams&, const SessionContext&)>;
365
368 std::function<core::Result<protocol::ResourceTemplatesListResult>(
369 const protocol::PaginatedRequestParams&, const SessionContext&)>;
370
372 using TaskGetHandler = std::function<core::Result<protocol::Task>(
373 const protocol::TaskGetParams&, const SessionContext&)>;
374
376 using TaskCancelHandler = std::function<core::Result<protocol::Task>(
377 const protocol::TaskCancelParams&, const SessionContext&)>;
378
380 using TaskResultHandler = std::function<core::Result<protocol::Json>(
381 const protocol::TaskResultParams&, const SessionContext&)>;
382
385 std::function<core::Result<core::Unit>(const SessionContext&)>;
386
388 using ProgressHandler = std::function<core::Result<core::Unit>(
389 const protocol::ProgressNotificationParams&, const SessionContext&)>;
390
393 std::function<core::Result<core::Unit>(const SessionContext&)>;
394
396 using ResourceUpdatedHandler = std::function<core::Result<core::Unit>(
397 const std::string& uri, const SessionContext&)>;
398
401 void set_completion_handler(JsonContextHandler handler);
402 void set_completion_handler(JsonRequestContextHandler handler);
403
406 void set_sampling_handler(JsonContextHandler handler);
407 void set_sampling_handler(JsonRequestContextHandler handler);
408
411
414 void set_raw_request_handler(RawRequestContextHandler handler);
415
417 const RawRequestHandler& raw_request_handler() const noexcept {
418 return raw_request_handler_;
419 }
420
422 const RawRequestContextHandler& raw_request_context_handler() const noexcept {
423 return raw_request_context_handler_;
424 }
425
428
431 void set_custom_request_handler(RawRequestContextHandler handler);
432
435
438
441
444
447
451
454
457
460
463
466
469
472
475
478
483 Server& set_handler(const ServerHandler& handler);
484
491
497 Server& set_handler(std::shared_ptr<const ServerHandlerInterface> handler);
498
499 private:
500 template <typename Handler>
501 Handler copy_handler(Handler Server::* slot) const {
502 std::lock_guard<std::mutex> lock(*handlers_mutex_);
503 return this->*slot;
504 }
505
506 template <typename Handler>
507 void store_handler(Handler Server::* slot, Handler handler) {
508 std::lock_guard<std::mutex> lock(*handlers_mutex_);
509 this->*slot = std::move(handler);
510 }
511
512 ServerOptions options_;
513 ToolRegistry tools_;
514 PromptRegistry prompts_;
515 ResourceRegistry resources_;
516 ResourceTemplateRegistry resource_templates_;
517 std::shared_ptr<TaskOperationProcessor> task_processor_;
518 std::unique_ptr<AuthProvider> auth_provider_;
519 std::unique_ptr<RateLimiter> rate_limiter_;
520 std::shared_ptr<const JsonSchemaValidator> schema_validator_;
521 std::vector<std::unique_ptr<Transport>> transports_;
522 JsonRequestContextHandler completion_handler_;
523 JsonRequestContextHandler sampling_handler_;
524 LoggingHandler logging_handler_;
525 RawRequestHandler raw_request_handler_;
526 RawRequestContextHandler raw_request_context_handler_;
527 RawNotificationHandler raw_notification_handler_;
528 ToolsListHandler tools_list_handler_;
529 PromptsListHandler prompts_list_handler_;
530 ResourcesListHandler resources_list_handler_;
531 ResourceTemplatesListHandler resource_templates_list_handler_;
532 TaskListHandler task_list_handler_;
533 TaskGetHandler task_get_handler_;
534 TaskCancelHandler task_cancel_handler_;
535 TaskResultHandler task_result_handler_;
536 ProgressHandler progress_handler_;
537 RootsListChangedHandler roots_list_changed_handler_;
538 ListChangedHandler tool_list_changed_handler_;
539 ListChangedHandler prompt_list_changed_handler_;
540 ListChangedHandler resource_list_changed_handler_;
541 ResourceUpdatedHandler resource_updated_handler_;
542 mutable std::shared_ptr<std::mutex> handlers_mutex_ =
543 std::make_shared<std::mutex>();
544 std::unordered_map<Transport*, std::unordered_set<std::string>>
545 resource_subscriptions_;
546 std::vector<Transport*> session_transports_;
547 std::unordered_set<Transport*> session_transport_set_;
548 std::shared_ptr<std::mutex> subscriptions_mutex_ =
549 std::make_shared<std::mutex>();
550
551 core::Result<core::Unit> broadcast_notification(
552 const protocol::JsonRpcNotification& notification);
553 core::Result<core::Unit> notify_resource_subscribers(
554 std::string_view uri, const protocol::JsonRpcNotification& notification);
555 CancellationToken begin_request_cancellation(
556 const protocol::RequestId& request_id);
557 void end_request_cancellation(const protocol::RequestId& request_id) noexcept;
558 void cancel_request(const protocol::RequestId& request_id) noexcept;
559
560 std::shared_ptr<std::mutex> active_request_cancellations_mutex_ =
561 std::make_shared<std::mutex>();
562 std::unordered_map<std::string, CancellationSource>
563 active_request_cancellations_;
564};
565
572 public:
574 ServerBuilder& name(std::string value);
575
577 ServerBuilder& version(std::string value);
578
580 ServerBuilder& instructions(std::string value);
581
584
586 ServerBuilder& with_transport(std::unique_ptr<Transport> transport);
587
590 std::unique_ptr<AuthProvider> auth_provider);
591
593 ServerBuilder& with_rate_limiter(std::unique_ptr<RateLimiter> rate_limiter);
594
597 std::shared_ptr<const JsonSchemaValidator> validator);
598
601
604 std::shared_ptr<TaskOperationProcessor> processor);
605
613 ToolHandler handler);
614
619
624 ResourceReadHandler handler);
625
630 protocol::ResourceTemplate resource_template);
631
633 template <class Router>
634 ServerBuilder& with_router(const Router& router) {
635 return router.apply_to(*this);
636 }
637
640 ServerBuilder& on_completion(Server::JsonContextHandler handler);
641 ServerBuilder& on_completion(Server::JsonRequestContextHandler handler);
642
645 ServerBuilder& on_sampling(Server::JsonContextHandler handler);
646 ServerBuilder& on_sampling(Server::JsonRequestContextHandler handler);
647
650
653
656
659
662
665
668
671
674
678
681
684
687
690
693
696
699
702
705
708
714
719 std::shared_ptr<const ServerHandlerInterface> handler);
720
725
726 private:
727 using ServerRegistration = std::function<core::Result<core::Unit>(Server&)>;
728
729 ServerOptions options_;
730 std::unique_ptr<AuthProvider> auth_provider_;
731 std::unique_ptr<RateLimiter> rate_limiter_;
732 std::shared_ptr<const JsonSchemaValidator> schema_validator_;
733 std::shared_ptr<TaskOperationProcessor> task_processor_;
734 std::vector<std::unique_ptr<Transport>> transports_;
735 std::vector<ServerRegistration> registrations_;
736 Server::JsonRequestContextHandler completion_handler_;
737 Server::JsonRequestContextHandler sampling_handler_;
738 Server::LoggingHandler logging_handler_;
739 Server::RawRequestHandler raw_request_handler_;
740 Server::RawNotificationHandler raw_notification_handler_;
741 Server::ToolsListHandler tools_list_handler_;
742 Server::PromptsListHandler prompts_list_handler_;
743 Server::ResourcesListHandler resources_list_handler_;
744 Server::ResourceTemplatesListHandler resource_templates_list_handler_;
745 Server::TaskListHandler task_list_handler_;
746 Server::TaskGetHandler task_get_handler_;
747 Server::TaskCancelHandler task_cancel_handler_;
748 Server::TaskResultHandler task_result_handler_;
749 Server::ProgressHandler progress_handler_;
750 Server::RootsListChangedHandler roots_list_changed_handler_;
751 Server::ListChangedHandler tool_list_changed_handler_;
752 Server::ListChangedHandler prompt_list_changed_handler_;
753 Server::ListChangedHandler resource_list_changed_handler_;
754 Server::ResourceUpdatedHandler resource_updated_handler_;
755};
756
757} // namespace mcp::server
MCP client and server capability declarations.
Copyable token observed by cancellation-aware SDK operations.
Definition cancellation.hpp:104
static CancellationToken none()
Constructs a detached token that is never cancelled.
Definition cancellation.hpp:110
Registry of named MCP prompts and their handlers.
Definition registry.hpp:131
Registry of concrete MCP resources and read handlers.
Definition registry.hpp:180
Registry of advertised resource templates.
Definition registry.hpp:229
Fluent builder for constructing a configured Server.
Definition server.hpp:571
ServerBuilder & on_resource_list_changed(Server::ListChangedHandler handler)
Sets the resource-list-changed notification handler.
ServerBuilder & with_transport(std::unique_ptr< Transport > transport)
Adds a transport to be owned by the built server.
ServerBuilder & with_task_manager(TaskOperationProcessorOptions options={})
Enables the built-in SDK task processor on the built server.
ServerBuilder & on_tool_list_changed(Server::ListChangedHandler handler)
Sets the tool-list-changed notification handler.
ServerBuilder & on_task_get(Server::TaskGetHandler handler)
Sets the task get handler.
ServerBuilder & with_capabilities(protocol::ServerCapabilities capabilities)
Replaces the capabilities advertised during initialization.
ServerBuilder & on_progress(Server::ProgressHandler handler)
Sets the progress notification handler.
ServerBuilder & on_completion(Server::JsonHandler handler)
Sets the completion request handler.
ServerBuilder & on_task_list(Server::TaskListHandler handler)
Sets the task list handler.
ServerBuilder & add_tool(protocol::ToolDefinition definition, ToolHandler handler)
Registers a tool definition and handler.
ServerBuilder & name(std::string value)
Sets the advertised server name.
ServerBuilder & with_auth_provider(std::unique_ptr< AuthProvider > auth_provider)
Sets the authentication provider owned by the built server.
ServerBuilder & with_schema_validator(std::shared_ptr< const JsonSchemaValidator > validator)
Sets the optional JSON Schema validator used by the built server.
ServerBuilder & add_prompt(protocol::Prompt prompt, PromptHandler handler)
Registers a prompt and handler.
core::Result< std::unique_ptr< Server > > build()
Builds a configured server.
ServerBuilder & on_resource_templates_list(Server::ResourceTemplatesListHandler handler)
Sets the resources/templates/list handler.
ServerBuilder & version(std::string value)
Sets the advertised server version.
ServerBuilder & on_roots_list_changed(Server::RootsListChangedHandler handler)
Sets the roots-list-changed notification handler.
ServerBuilder & with_rate_limiter(std::unique_ptr< RateLimiter > rate_limiter)
Sets the rate limiter owned by the built server.
ServerBuilder & with_task_manager(std::shared_ptr< TaskOperationProcessor > processor)
Uses an explicit task processor on the built server.
ServerBuilder & with_handler(ServerHandler handler)
Installs every non-empty callback from a handler aggregate.
ServerBuilder & on_task_result(Server::TaskResultHandler handler)
Sets the task result handler.
ServerBuilder & on_logging(Server::LoggingHandler handler)
Sets the logging notification handler.
ServerBuilder & on_raw_notification(Server::RawNotificationHandler handler)
Sets the raw notification hook.
ServerBuilder & on_sampling(Server::JsonHandler handler)
Sets the sampling request handler.
ServerBuilder & with_handler(std::shared_ptr< const ServerHandlerInterface > handler)
Installs callbacks from an owned contract-style handler.
ServerBuilder & instructions(std::string value)
Sets the advertised server instructions.
ServerBuilder & on_prompts_list(Server::PromptsListHandler handler)
Sets the prompts/list handler.
ServerBuilder & on_custom_request(Server::RawRequestHandler handler)
Sets the custom request handler.
ServerBuilder & on_prompt_list_changed(Server::ListChangedHandler handler)
Sets the prompt-list-changed notification handler.
ServerBuilder & add_resource(protocol::Resource resource, ResourceReadHandler handler)
Registers a resource and read handler.
ServerBuilder & on_tools_list(Server::ToolsListHandler handler)
Sets the tools/list handler.
ServerBuilder & on_resource_updated(Server::ResourceUpdatedHandler handler)
Sets the resource-updated notification handler.
ServerBuilder & on_custom_notification(Server::RawNotificationHandler handler)
Sets the custom notification handler.
ServerBuilder & on_resources_list(Server::ResourcesListHandler handler)
Sets the resources/list handler.
ServerBuilder & add_resource_template(protocol::ResourceTemplate resource_template)
Registers a resource template.
ServerBuilder & on_task_cancel(Server::TaskCancelHandler handler)
Sets the task cancel handler.
ServerBuilder & with_router(const Router &router)
Applies a composable router object to this builder.
Definition server.hpp:634
ServerBuilder & on_raw_request(Server::RawRequestHandler handler)
Sets the raw request hook.
ServerBuilder & with_handler(const ServerHandlerInterface &handler)
Installs callbacks from a borrowed contract-style handler.
High-level MCP server compatibility API.
Definition server.hpp:83
const RawRequestHandler & raw_request_handler() const noexcept
Returns the registered raw request handler, if any.
Definition server.hpp:417
Server & use_task_manager(TaskOperationProcessorOptions options={})
Enables the built-in SDK task processor.
void set_resource_templates_list_handler(ResourceTemplatesListHandler handler)
Registers a resources/templates/list request handler.
core::Result< core::Unit > notify_tool_list_changed()
Notifies connected clients that the tool list changed.
const protocol::ServerCapabilities & capabilities() const noexcept
Returns the capabilities advertised to clients.
void set_custom_notification_handler(RawNotificationHandler handler)
Registers a custom notification handler.
void set_prompt_list_changed_handler(ListChangedHandler handler)
Registers a prompt-list-changed notification handler.
~Server()
Stops transports and task workers before owned registries vanish.
core::Result< core::Unit > add_transport(std::unique_ptr< Transport > transport)
Adds a transport owned by the server.
core::Result< core::Unit > notify_elicitation_complete(std::string elicitation_id)
Sends an elicitation-complete notification.
void set_schema_validator(std::shared_ptr< const JsonSchemaValidator > validator)
Installs an optional JSON Schema validator.
std::vector< protocol::ResourceTemplate > list_resource_templates() const
Lists registered resource templates.
std::function< core::Result< core::Unit >(const std::string &uri, const SessionContext &)> ResourceUpdatedHandler
Handles client resource-updated notifications.
Definition server.hpp:397
void set_custom_request_handler(RawRequestHandler handler)
Registers a custom request handler.
std::function< core::Result< protocol::PromptsListResult >(const protocol::PaginatedRequestParams &, const SessionContext &)> PromptsListHandler
Handles prompts/list requests.
Definition server.hpp:359
std::function< core::Result< protocol::ResourceTemplatesListResult >(const protocol::PaginatedRequestParams &, const SessionContext &)> ResourceTemplatesListHandler
Handles resources/templates/list requests.
Definition server.hpp:369
std::vector< protocol::ToolDefinition > list_tools() const
Lists registered tool definitions.
PromptRegistry & prompts() noexcept
Returns the mutable prompt registry.
std::shared_ptr< const JsonSchemaValidator > schema_validator() const noexcept
Returns the configured JSON Schema validator, if any.
Definition server.hpp:309
core::Result< core::Unit > set_resource_subscription(const SessionContext &context, std::string_view uri, bool subscribed)
Updates resource subscription routing for a transport session.
void set_prompts_list_handler(PromptsListHandler handler)
Registers a prompts/list request handler.
core::Result< SessionContext > authenticate_context(const SessionContext &context)
Applies the configured AuthProvider to a session context.
void set_auth_provider(std::unique_ptr< AuthProvider > auth_provider)
Installs an authentication provider used by supported transports.
void set_progress_handler(ProgressHandler handler)
Registers a progress notification handler.
core::Result< protocol::Json > initialize()
Builds the JSON result for an initialize request.
core::Result< core::Unit > start()
Starts every registered transport.
void set_resource_updated_handler(ResourceUpdatedHandler handler)
Registers a resource-updated notification handler.
void set_logging_handler(LoggingHandler handler)
Registers the logging notification handler.
void set_tools_list_handler(ToolsListHandler handler)
Registers a tools/list request handler.
core::Result< core::Unit > add_session_transport(Transport &transport)
Registers a borrowed session transport for outbound server events.
void set_resource_list_changed_handler(ListChangedHandler handler)
Registers a resource-list-changed notification handler.
core::Result< core::Unit > notify_resource_updated(std::string_view uri)
Notifies subscribed clients that a resource URI was updated.
std::function< core::Result< protocol::Task >(const protocol::TaskGetParams &, const SessionContext &)> TaskGetHandler
Handles task get requests.
Definition server.hpp:373
void set_resources_list_handler(ResourcesListHandler handler)
Registers a resources/list request handler.
void set_task_list_handler(TaskListHandler handler)
Registers a task list request handler.
core::Result< core::Unit > notify_roots_list_changed()
Notifies connected clients that roots changed.
std::function< core::Result< protocol::Json >(const protocol::TaskResultParams &, const SessionContext &)> TaskResultHandler
Handles task result requests.
Definition server.hpp:381
ResourceRegistry & resources() noexcept
Returns the mutable resource registry.
core::Result< protocol::Json > ping(const SessionContext &context={})
Handles a ping request.
core::Result< core::Unit > notify_progress(const protocol::ProgressNotificationParams &params)
Sends a progress notification to connected clients.
void set_raw_request_handler(RawRequestHandler handler)
Registers a raw request hook.
void set_task_cancel_handler(TaskCancelHandler handler)
Registers a task cancel request handler.
void set_raw_notification_handler(RawNotificationHandler handler)
Registers a raw notification hook.
core::Result< protocol::ToolResult > call_tool(std::string_view name, protocol::Json arguments=protocol::Json::object(), const std::string &session_id={}) const
Invokes a registered tool.
void set_rate_limiter(std::unique_ptr< RateLimiter > rate_limiter)
Installs a rate limiter used by supported transports.
std::function< core::Result< protocol::Task >(const protocol::TaskCancelParams &, const SessionContext &)> TaskCancelHandler
Handles task cancel requests.
Definition server.hpp:377
void set_task_get_handler(TaskGetHandler handler)
Registers a task get request handler.
core::Result< core::Unit > notify_task_status(const protocol::Task &task)
Sends a task status notification.
std::shared_ptr< TaskOperationProcessor > task_manager() const noexcept
Returns the installed task processor, if any.
ServerInfo get_info() const
Returns server metadata advertised during initialization.
std::function< std::optional< protocol::JsonRpcResponse >(const protocol::JsonRpcRequest &, const SessionContext &)> RawRequestHandler
Optionally handles raw or custom requests before built-in fallback.
Definition server.hpp:337
void stop() noexcept
Stops all transports.
std::vector< protocol::Resource > list_resources() const
Lists registered resources.
core::Result< core::Unit > notify_logging_message(const protocol::LoggingMessageNotificationParams &params)
Sends a logging message notification to connected clients.
core::Result< protocol::JsonRpcResponse > handle_request(const protocol::JsonRpcRequest &request, const SessionContext &context)
Dispatches an inbound JSON-RPC request from a transport.
std::function< core::Result< protocol::Json >(const protocol::Json &)> JsonHandler
Handles JSON-shaped extension requests such as completion or sampling.
Definition server.hpp:322
core::Result< protocol::ResourcesReadResult > read_resource(std::string_view uri, protocol::Json params=protocol::Json::object(), const std::string &session_id={}) const
Reads a registered resource.
std::function< core::Result< core::Unit >(const protocol::JsonRpcNotification &, const SessionContext &)> RawNotificationHandler
Handles raw or custom notifications.
Definition server.hpp:345
void set_completion_handler(JsonHandler handler)
Registers the completion request handler.
Server & use_task_manager(std::shared_ptr< TaskOperationProcessor > processor)
Installs a caller-owned task processor shared with this server.
std::function< void(std::string_view, std::string_view)> LoggingHandler
Handles logging messages from clients.
Definition server.hpp:330
void set_tool_list_changed_handler(ListChangedHandler handler)
Registers a tool-list-changed notification handler.
void set_task_result_handler(TaskResultHandler handler)
Registers a task result request handler.
std::function< core::Result< core::Unit >(const SessionContext &)> RootsListChangedHandler
Handles client roots-list-changed notifications.
Definition server.hpp:385
core::Result< protocol::CreateTaskResult > enqueue_task(const protocol::Task &task)
Sends a server-to-client request to create a new task.
void set_sampling_handler(JsonHandler handler)
Registers the sampling request handler.
std::function< core::Result< protocol::ResourcesListResult >(const protocol::PaginatedRequestParams &, const SessionContext &)> ResourcesListHandler
Handles resources/list requests.
Definition server.hpp:364
core::Result< core::Unit > notify_resource_list_changed()
Notifies connected clients that the resource list changed.
Server(ServerOptions options)
Constructs a server from explicit options.
std::function< core::Result< core::Unit >(const SessionContext &)> ListChangedHandler
Handles client list-changed notifications.
Definition server.hpp:393
ToolRegistry & tools() noexcept
Returns the mutable tool registry.
std::function< core::Result< core::Unit >(const protocol::ProgressNotificationParams &, const SessionContext &)> ProgressHandler
Handles client progress notifications.
Definition server.hpp:389
std::function< core::Result< protocol::TaskListResult >(const protocol::TaskListParams &, const SessionContext &)> TaskListHandler
Handles task list requests.
Definition server.hpp:349
std::function< core::Result< protocol::ToolsListResult >(const protocol::PaginatedRequestParams &, const SessionContext &)> ToolsListHandler
Handles tools/list requests.
Definition server.hpp:354
Server & set_handler(const ServerHandler &handler)
Installs every non-empty callback from a ServerHandler aggregate.
Definition handler.hpp:718
core::Result< protocol::ToolDefinition > get_tool(std::string_view name) const
Gets a registered tool definition by name.
core::Result< core::Unit > notify_prompt_list_changed()
Notifies connected clients that the prompt list changed.
const RawRequestContextHandler & raw_request_context_handler() const noexcept
Returns the registered raw request context handler, if any.
Definition server.hpp:422
core::Result< protocol::PromptsGetResult > get_prompt(std::string_view name, protocol::Json arguments=protocol::Json::object(), const std::string &session_id={}) const
Gets a registered prompt result.
ResourceTemplateRegistry & resource_templates() noexcept
Returns the mutable resource-template registry.
core::Result< core::Unit > handle_notification(const protocol::JsonRpcNotification &notification, const SessionContext &context)
Dispatches an inbound JSON-RPC notification from a transport.
void set_roots_list_changed_handler(RootsListChangedHandler handler)
Registers a roots-list-changed notification handler.
std::vector< protocol::Prompt > list_prompts() const
Lists registered prompts.
Registry of named MCP tools and their handlers.
Definition registry.hpp:36
Abstract server transport for receiving client JSON-RPC messages.
Definition transport.hpp:99
Server-side handler invocation contexts.
std::function< core::Result< protocol::ToolResult >(const ToolContext &)> ToolHandler
Application callback that executes a tool.
Definition handler_types.hpp:21
std::function< core::Result< protocol::ResourcesReadResult >(const ResourceContext &)> ResourceReadHandler
Application callback that reads a resource.
Definition handler_types.hpp:32
std::function< core::Result< protocol::PromptsGetResult >(const PromptContext &)> PromptHandler
Application callback that renders a prompt.
Definition handler_types.hpp:26
Logging level and notification payloads.
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
Rate limiting extension points for server request admission.
In-memory registries and handler contracts for server capabilities.
Shared result and error primitives used by the public cxxmcp SDK.
tl::expected< T, Error > Result
Alias for the SDK result type.
Definition result.hpp:64
Pluggable JSON Schema validation contract for server-side SDK paths.
Authentication extension points for server transports.
Server-side transport abstraction for MCP JSON-RPC traffic.
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/message.
Definition logging.hpp:52
Parameters for notifications/progress.
Definition types.hpp:226
Prompt descriptor returned by prompts/list.
Definition prompt.hpp:83
URI template advertised by resources/templates/list.
Definition resource.hpp:127
Concrete resource advertised by resources/list.
Definition resource.hpp:28
Capabilities advertised by an MCP server in the initialize result.
Definition capabilities.hpp:458
Snapshot of an asynchronous task.
Definition task.hpp:144
Metadata describing a callable MCP tool.
Definition tool.hpp:213
Contract-style server handler interface.
Definition handler.hpp:26
Optional callback bundle for configuring a Server in one call.
Definition handler.hpp:564
Public server metadata returned by Server::get_info().
Definition server.hpp:59
std::string name
Advertised server name.
Definition server.hpp:61
std::string version
Advertised server version.
Definition server.hpp:64
std::string instructions
Advertised server instructions.
Definition server.hpp:67
Configuration used to construct a Server.
Definition server.hpp:44
std::string instructions
Optional human-readable instructions advertised to clients.
Definition server.hpp:55
std::string server_name
Server name advertised to clients.
Definition server.hpp:49
std::string server_version
Server version advertised to clients.
Definition server.hpp:52
protocol::ServerCapabilities capabilities
Capabilities advertised in the initialize response.
Definition server.hpp:46
Per-message connection metadata supplied to server handlers.
Definition transport.hpp:42
Options for the SDK server task processor.
Definition task_manager.hpp:35
Asynchronous task status and task-management payloads.
Server-side task operation processor for asynchronous MCP requests.