24#include <unordered_map>
25#include <unordered_set>
41namespace mcp::server {
114 const protocol::PaginatedRequestParams& params,
118 core::Result<protocol::ToolDefinition>
get_tool(std::string_view name) const;
127 std::string_view name,
128 protocol::Json arguments = protocol::Json::
object(),
129 const std::
string& session_id = {})
const;
155 const protocol::PaginatedRequestParams& params,
164 std::string_view name,
165 protocol::Json arguments = protocol::Json::
object(),
166 const std::
string& session_id = {})
const;
183 const protocol::PaginatedRequestParams& params,
192 std::string_view uri, protocol::Json params = protocol::Json::
object(),
193 const std::
string& session_id = {})
const;
210 const protocol::PaginatedRequestParams& params,
259 const SessionContext& context, std::string_view uri,
bool subscribed);
272 std::string elicitation_id);
306 std::shared_ptr<const JsonSchemaValidator> validator);
310 return schema_validator_;
322 std::function<core::Result<protocol::Json>(const protocol::Json&)>;
323 using JsonContextHandler = std::function<core::Result<protocol::Json>(
325 using JsonRequestContextHandler = std::function<core::Result<protocol::Json>(
330 std::function<
void(std::string_view, std::string_view)>;
336 std::function<std::optional<protocol::JsonRpcResponse>(
338 using RawRequestContextHandler =
339 std::function<std::optional<protocol::JsonRpcResponse>(
353 std::function<core::Result<protocol::ToolsListResult>(
358 std::function<core::Result<protocol::PromptsListResult>(
363 std::function<core::Result<protocol::ResourcesListResult>(
368 std::function<core::Result<protocol::ResourceTemplatesListResult>(
389 const protocol::ProgressNotificationParams&, const
SessionContext&)>;
418 return raw_request_handler_;
423 return raw_request_context_handler_;
500 template <
typename Handler>
501 Handler copy_handler(Handler
Server::* slot) const {
502 std::lock_guard<std::mutex> lock(*handlers_mutex_);
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);
512 ServerOptions options_;
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>();
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_;
590 std::unique_ptr<AuthProvider> auth_provider);
597 std::shared_ptr<const JsonSchemaValidator> validator);
604 std::shared_ptr<TaskOperationProcessor> processor);
633 template <
class Router>
635 return router.apply_to(*
this);
640 ServerBuilder& on_completion(Server::JsonContextHandler handler);
641 ServerBuilder& on_completion(Server::JsonRequestContextHandler handler);
645 ServerBuilder& on_sampling(Server::JsonContextHandler handler);
646 ServerBuilder& on_sampling(Server::JsonRequestContextHandler handler);
719 std::shared_ptr<const ServerHandlerInterface> handler);
727 using ServerRegistration = std::function<core::Result<core::Unit>(
Server&)>;
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_;
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 ¶ms)
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 ¶ms)
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 ¬ification, 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.
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
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.