cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
registry.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
5#include <mutex>
6#include <optional>
7#include <string>
8#include <string_view>
9#include <unordered_map>
10#include <vector>
11
19
22
23namespace mcp::server {
24
37 public:
38 ToolRegistry() = default;
39 ToolRegistry(const ToolRegistry& other);
40 ToolRegistry& operator=(const ToolRegistry& other);
41 ToolRegistry(ToolRegistry&& other) noexcept;
42 ToolRegistry& operator=(ToolRegistry&& other) noexcept;
43
51 ToolHandler handler);
52
55 core::Result<protocol::ToolDefinition> get(std::string_view name) const;
56
62 protocol::Json arguments) const;
63
70
74
81 std::string_view name, protocol::Json arguments,
82 const SessionContext& session_context) const;
83
91 protocol::ToolCall call, const SessionContext& session_context) const;
92
96 const SessionContext& session_context,
97 CancellationToken cancellation) const;
99 protocol::ToolCall call, const SessionContext& session_context,
100 CancellationToken cancellation,
101 const JsonSchemaValidator* schema_validator) const;
102
108 protocol::Json arguments,
109 const std::string& session_id) const;
110
112 std::vector<protocol::ToolDefinition> list() const;
113
114 private:
115 struct Entry {
116 protocol::ToolDefinition definition;
117 ToolHandler handler;
118 };
119
120 mutable std::mutex mutex_;
121 std::unordered_map<std::string, Entry> tools_;
122 mutable std::vector<protocol::ToolDefinition> sorted_tools_cache_;
123 mutable bool sorted_tools_cache_dirty_ = true;
124};
125
132 public:
133 PromptRegistry() = default;
134 PromptRegistry(const PromptRegistry& other);
135 PromptRegistry& operator=(const PromptRegistry& other);
136 PromptRegistry(PromptRegistry&& other) noexcept;
137 PromptRegistry& operator=(PromptRegistry&& other) noexcept;
138
143
146 std::string_view name, protocol::Json arguments,
147 const std::string& session_id) const;
148
153 std::string_view name, protocol::Json arguments,
154 const SessionContext& session_context) const;
156 std::string_view name, protocol::Json arguments,
157 const SessionContext& session_context,
158 CancellationToken cancellation) const;
159
161 std::vector<protocol::Prompt> list() const;
162
163 private:
164 struct Entry {
165 protocol::Prompt prompt;
166 PromptHandler handler;
167 };
168
169 mutable std::mutex mutex_;
170 std::unordered_map<std::string, Entry> prompts_;
171 mutable std::vector<protocol::Prompt> sorted_prompts_cache_;
172 mutable bool sorted_prompts_cache_dirty_ = true;
173};
174
181 public:
182 ResourceRegistry() = default;
184 ResourceRegistry& operator=(const ResourceRegistry& other);
185 ResourceRegistry(ResourceRegistry&& other) noexcept;
186 ResourceRegistry& operator=(ResourceRegistry&& other) noexcept;
187
192 ResourceReadHandler handler);
193
196 std::string_view uri, protocol::Json params,
197 const std::string& session_id) const;
198
202 std::string_view uri, protocol::Json params,
203 const SessionContext& session_context) const;
205 std::string_view uri, protocol::Json params,
206 const SessionContext& session_context,
207 CancellationToken cancellation) const;
208
210 std::vector<protocol::Resource> list() const;
211
212 private:
213 struct Entry {
214 protocol::Resource resource;
215 ResourceReadHandler handler;
216 };
217
218 mutable std::mutex mutex_;
219 std::unordered_map<std::string, Entry> resources_;
220 mutable std::vector<protocol::Resource> sorted_resources_cache_;
221 mutable bool sorted_resources_cache_dirty_ = true;
222};
223
230 public:
231 ResourceTemplateRegistry() = default;
233 ResourceTemplateRegistry& operator=(const ResourceTemplateRegistry& other);
235 ResourceTemplateRegistry& operator=(
236 ResourceTemplateRegistry&& other) noexcept;
237
242
244 std::vector<protocol::ResourceTemplate> list() const;
245
246 private:
247 mutable std::mutex mutex_;
248 std::unordered_map<std::string, protocol::ResourceTemplate>
249 resource_templates_;
250 mutable std::vector<protocol::ResourceTemplate> sorted_templates_cache_;
251 mutable bool sorted_templates_cache_dirty_ = true;
252};
253
254} // namespace mcp::server
Copyable token observed by cancellation-aware SDK operations.
Definition cancellation.hpp:104
Interface for integrating a JSON Schema validator implementation.
Definition schema_validator.hpp:35
Registry of named MCP prompts and their handlers.
Definition registry.hpp:131
std::vector< protocol::Prompt > list() const
Return registered prompt definitions sorted by name.
core::Result< protocol::PromptsGetResult > get(std::string_view name, protocol::Json arguments, const std::string &session_id) const
Render a prompt with only a session id.
core::Result< core::Unit > add(protocol::Prompt prompt, PromptHandler handler)
Register a prompt definition and handler.
core::Result< protocol::PromptsGetResult > get(std::string_view name, protocol::Json arguments, const SessionContext &session_context) const
Render a prompt with full session metadata.
Registry of concrete MCP resources and read handlers.
Definition registry.hpp:180
core::Result< protocol::ResourcesReadResult > read(std::string_view uri, protocol::Json params, const std::string &session_id) const
Read a resource with only a session id.
core::Result< protocol::ResourcesReadResult > read(std::string_view uri, protocol::Json params, const SessionContext &session_context) const
Read a resource with full session metadata.
std::vector< protocol::Resource > list() const
Return registered resources sorted by URI.
core::Result< core::Unit > add(protocol::Resource resource, ResourceReadHandler handler)
Register a concrete resource and read handler.
Registry of advertised resource templates.
Definition registry.hpp:229
std::vector< protocol::ResourceTemplate > list() const
Return registered resource templates sorted by uriTemplate.
core::Result< core::Unit > add(protocol::ResourceTemplate resource_template)
Register a resource template.
Registry of named MCP tools and their handlers.
Definition registry.hpp:36
std::vector< protocol::ToolDefinition > list() const
Return registered tool definitions sorted by name.
core::Result< protocol::ToolResult > call(protocol::ToolCall call, const SessionContext &session_context, CancellationToken cancellation) const
Invoke a parsed protocol call with session metadata and cooperative cancellation.
core::Result< core::Unit > add(protocol::ToolDefinition definition, ToolHandler handler)
Register a tool definition and handler.
core::Result< protocol::ToolResult > call(protocol::ToolCall call, const SessionContext &session_context) const
Invoke a parsed protocol call with full session metadata.
core::Result< protocol::ToolResult > call(std::string_view name, protocol::Json arguments, const SessionContext &session_context) const
Invoke a tool with full session metadata.
core::Result< protocol::ToolResult > call(protocol::ToolCall call) const
Invoke a tool from a parsed protocol call without session metadata.
core::Result< protocol::ToolResult > call(std::string_view name, protocol::Json arguments, const std::string &session_id) const
Invoke a tool with only a session id.
core::Result< protocol::ToolDefinition > get(std::string_view name) const
Look up a registered tool definition by name.
core::Result< core::Unit > validate(const protocol::ToolCall &call) const
Validate that a parsed tool call can target a registered tool.
core::Result< protocol::ToolResult > call(std::string_view name, protocol::Json arguments) const
Invoke a tool without session metadata.
Public server handler callback type aliases.
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
Prompt discovery and rendering payloads.
nlohmann::json Json
JSON value type used by all protocol DTOs.
Definition types.hpp:28
Resource listing, template, subscription, and read payloads.
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.
Server-side transport abstraction for MCP JSON-RPC traffic.
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
Parameters for tools/call.
Definition tool.hpp:335
Metadata describing a callable MCP tool.
Definition tool.hpp:213
Per-message connection metadata supplied to server handlers.
Definition transport.hpp:42
Tool definition, call, and result payloads.