cxxmcp 1.3.0
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
230 const protocol::JsonRpcRequest& request, const SessionContext& context);
231
239 const SessionContext& context);
240
243 const protocol::JsonRpcNotification& notification,
244 const SessionContext& context);
245
248
251
254
257
261
264 const SessionContext& context, std::string_view uri, bool subscribed);
265
269
273
277 std::string elicitation_id);
278
281
288 const protocol::Task& task);
289
293 core::Result<core::Unit> add_transport(std::unique_ptr<Transport> transport);
294
302
304 void set_auth_provider(std::unique_ptr<AuthProvider> auth_provider);
305
307 void set_rate_limiter(std::unique_ptr<RateLimiter> rate_limiter);
308
311 std::shared_ptr<const JsonSchemaValidator> validator);
312
314 std::shared_ptr<const JsonSchemaValidator> schema_validator() const noexcept {
315 return schema_validator_;
316 }
317
320
322 void stop() noexcept;
323
327 std::function<core::Result<protocol::Json>(const protocol::Json&)>;
328 using JsonContextHandler = std::function<core::Result<protocol::Json>(
329 const protocol::Json&, const SessionContext&)>;
330 using JsonRequestContextHandler = std::function<core::Result<protocol::Json>(
331 const protocol::Json&, const SessionContext&, CancellationToken)>;
332
335 std::function<void(std::string_view, std::string_view)>;
336
341 std::function<std::optional<protocol::JsonRpcResponse>(
342 const protocol::JsonRpcRequest&, const SessionContext&)>;
343 using RawRequestContextHandler =
344 std::function<std::optional<protocol::JsonRpcResponse>(
345 const protocol::JsonRpcRequest&, const SessionContext&,
347
349 using RawNotificationHandler = std::function<core::Result<core::Unit>(
350 const protocol::JsonRpcNotification&, const SessionContext&)>;
351
353 using TaskListHandler = std::function<core::Result<protocol::TaskListResult>(
354 const protocol::TaskListParams&, const SessionContext&)>;
355
358 std::function<core::Result<protocol::ToolsListResult>(
359 const protocol::PaginatedRequestParams&, const SessionContext&)>;
360
363 std::function<core::Result<protocol::PromptsListResult>(
364 const protocol::PaginatedRequestParams&, const SessionContext&)>;
365
368 std::function<core::Result<protocol::ResourcesListResult>(
369 const protocol::PaginatedRequestParams&, const SessionContext&)>;
370
373 std::function<core::Result<protocol::ResourceTemplatesListResult>(
374 const protocol::PaginatedRequestParams&, const SessionContext&)>;
375
377 using TaskGetHandler = std::function<core::Result<protocol::Task>(
378 const protocol::TaskGetParams&, const SessionContext&)>;
379
381 using TaskCancelHandler = std::function<core::Result<protocol::Task>(
382 const protocol::TaskCancelParams&, const SessionContext&)>;
383
385 using TaskResultHandler = std::function<core::Result<protocol::Json>(
386 const protocol::TaskResultParams&, const SessionContext&)>;
387
390 std::function<core::Result<core::Unit>(const SessionContext&)>;
391
393 using ProgressHandler = std::function<core::Result<core::Unit>(
394 const protocol::ProgressNotificationParams&, const SessionContext&)>;
395
398 std::function<core::Result<core::Unit>(const SessionContext&)>;
399
401 using ResourceUpdatedHandler = std::function<core::Result<core::Unit>(
402 const std::string& uri, const SessionContext&)>;
403
406 void set_completion_handler(JsonContextHandler handler);
407 void set_completion_handler(JsonRequestContextHandler handler);
408
411 void set_sampling_handler(JsonContextHandler handler);
412 void set_sampling_handler(JsonRequestContextHandler handler);
413
416
419 void set_raw_request_handler(RawRequestContextHandler handler);
420
422 const RawRequestHandler& raw_request_handler() const noexcept {
423 return raw_request_handler_;
424 }
425
427 const RawRequestContextHandler& raw_request_context_handler() const noexcept {
428 return raw_request_context_handler_;
429 }
430
433
436 void set_custom_request_handler(RawRequestContextHandler handler);
437
440
443
446
449
452
456
459
462
465
468
471
474
477
480
483
488 Server& set_handler(const ServerHandler& handler);
489
496
502 Server& set_handler(std::shared_ptr<const ServerHandlerInterface> handler);
503
504 private:
505 template <typename Handler>
506 Handler copy_handler(Handler Server::* slot) const {
507 std::lock_guard<std::mutex> lock(*handlers_mutex_);
508 return this->*slot;
509 }
510
511 template <typename Handler>
512 void store_handler(Handler Server::* slot, Handler handler) {
513 std::lock_guard<std::mutex> lock(*handlers_mutex_);
514 this->*slot = std::move(handler);
515 }
516
517 ServerOptions options_;
518 ToolRegistry tools_;
519 PromptRegistry prompts_;
520 ResourceRegistry resources_;
521 ResourceTemplateRegistry resource_templates_;
522 std::shared_ptr<TaskOperationProcessor> task_processor_;
523 std::unique_ptr<AuthProvider> auth_provider_;
524 std::unique_ptr<RateLimiter> rate_limiter_;
525 std::shared_ptr<const JsonSchemaValidator> schema_validator_;
526 std::vector<std::unique_ptr<Transport>> transports_;
527 JsonRequestContextHandler completion_handler_;
528 JsonRequestContextHandler sampling_handler_;
529 LoggingHandler logging_handler_;
530 RawRequestHandler raw_request_handler_;
531 RawRequestContextHandler raw_request_context_handler_;
532 RawNotificationHandler raw_notification_handler_;
533 ToolsListHandler tools_list_handler_;
534 PromptsListHandler prompts_list_handler_;
535 ResourcesListHandler resources_list_handler_;
536 ResourceTemplatesListHandler resource_templates_list_handler_;
537 TaskListHandler task_list_handler_;
538 TaskGetHandler task_get_handler_;
539 TaskCancelHandler task_cancel_handler_;
540 TaskResultHandler task_result_handler_;
541 ProgressHandler progress_handler_;
542 RootsListChangedHandler roots_list_changed_handler_;
543 ListChangedHandler tool_list_changed_handler_;
544 ListChangedHandler prompt_list_changed_handler_;
545 ListChangedHandler resource_list_changed_handler_;
546 ResourceUpdatedHandler resource_updated_handler_;
547 mutable std::shared_ptr<std::mutex> handlers_mutex_ =
548 std::make_shared<std::mutex>();
549 std::unordered_map<Transport*, std::unordered_set<std::string>>
550 resource_subscriptions_;
551 std::vector<Transport*> session_transports_;
552 std::unordered_set<Transport*> session_transport_set_;
553 std::shared_ptr<std::mutex> subscriptions_mutex_ =
554 std::make_shared<std::mutex>();
555
556 core::Result<core::Unit> broadcast_notification(
557 const protocol::JsonRpcNotification& notification);
558 core::Result<core::Unit> notify_resource_subscribers(
559 std::string_view uri, const protocol::JsonRpcNotification& notification);
560 CancellationToken begin_request_cancellation(
561 const protocol::RequestId& request_id);
562 void end_request_cancellation(const protocol::RequestId& request_id) noexcept;
563 void cancel_request(const protocol::RequestId& request_id) noexcept;
564
565 std::shared_ptr<std::mutex> active_request_cancellations_mutex_ =
566 std::make_shared<std::mutex>();
567 std::unordered_map<std::string, CancellationSource>
568 active_request_cancellations_;
569};
570
577 public:
579 ServerBuilder& name(std::string value);
580
582 ServerBuilder& version(std::string value);
583
585 ServerBuilder& instructions(std::string value);
586
589
591 ServerBuilder& with_transport(std::unique_ptr<Transport> transport);
592
595 std::unique_ptr<AuthProvider> auth_provider);
596
598 ServerBuilder& with_rate_limiter(std::unique_ptr<RateLimiter> rate_limiter);
599
602 std::shared_ptr<const JsonSchemaValidator> validator);
603
606
609 std::shared_ptr<TaskOperationProcessor> processor);
610
618 ToolHandler handler);
619
624
629 ResourceReadHandler handler);
630
635 protocol::ResourceTemplate resource_template);
636
638 template <class Router>
639 ServerBuilder& with_router(const Router& router) {
640 return router.apply_to(*this);
641 }
642
645 ServerBuilder& on_completion(Server::JsonContextHandler handler);
646 ServerBuilder& on_completion(Server::JsonRequestContextHandler handler);
647
650 ServerBuilder& on_sampling(Server::JsonContextHandler handler);
651 ServerBuilder& on_sampling(Server::JsonRequestContextHandler handler);
652
655
658
661
664
667
670
673
676
679
683
686
689
692
695
698
701
704
707
710
713
719
724 std::shared_ptr<const ServerHandlerInterface> handler);
725
730
731 private:
732 using ServerRegistration = std::function<core::Result<core::Unit>(Server&)>;
733
734 ServerOptions options_;
735 std::unique_ptr<AuthProvider> auth_provider_;
736 std::unique_ptr<RateLimiter> rate_limiter_;
737 std::shared_ptr<const JsonSchemaValidator> schema_validator_;
738 std::shared_ptr<TaskOperationProcessor> task_processor_;
739 std::vector<std::unique_ptr<Transport>> transports_;
740 std::vector<ServerRegistration> registrations_;
741 Server::JsonRequestContextHandler completion_handler_;
742 Server::JsonRequestContextHandler sampling_handler_;
743 Server::LoggingHandler logging_handler_;
744 Server::RawRequestHandler raw_request_handler_;
745 Server::RawNotificationHandler raw_notification_handler_;
746 Server::ToolsListHandler tools_list_handler_;
747 Server::PromptsListHandler prompts_list_handler_;
748 Server::ResourcesListHandler resources_list_handler_;
749 Server::ResourceTemplatesListHandler resource_templates_list_handler_;
750 Server::TaskListHandler task_list_handler_;
751 Server::TaskGetHandler task_get_handler_;
752 Server::TaskCancelHandler task_cancel_handler_;
753 Server::TaskResultHandler task_result_handler_;
754 Server::ProgressHandler progress_handler_;
755 Server::RootsListChangedHandler roots_list_changed_handler_;
756 Server::ListChangedHandler tool_list_changed_handler_;
757 Server::ListChangedHandler prompt_list_changed_handler_;
758 Server::ListChangedHandler resource_list_changed_handler_;
759 Server::ResourceUpdatedHandler resource_updated_handler_;
760};
761
762} // 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:576
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:639
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:422
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:402
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:364
std::function< core::Result< protocol::ResourceTemplatesListResult >(const protocol::PaginatedRequestParams &, const SessionContext &)> ResourceTemplatesListHandler
Handles resources/templates/list requests.
Definition server.hpp:374
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:314
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:378
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:386
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:382
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.
core::Result< protocol::JsonRpcResponse > handle_request_impl(const protocol::JsonRpcRequest &request, const SessionContext &context)
Version-stamping-free dispatch used by handle_request; callers should invoke handle_request() so wire...
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:342
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:327
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:350
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:335
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:390
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:369
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:398
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:394
std::function< core::Result< protocol::TaskListResult >(const protocol::TaskListParams &, const SessionContext &)> TaskListHandler
Handles task list requests.
Definition server.hpp:354
std::function< core::Result< protocol::ToolsListResult >(const protocol::PaginatedRequestParams &, const SessionContext &)> ToolsListHandler
Handles tools/list requests.
Definition server.hpp:359
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:427
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:154
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:43
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.