23namespace mcp::protocol {
36 case PromptMessageRole::User:
38 case PromptMessageRole::Assistant:
47 std::string_view value) {
48 if (value ==
"user") {
49 return PromptMessageRole::User;
51 if (value ==
"assistant") {
52 return PromptMessageRole::Assistant;
108 prompt_.
title = std::move(value);
117 PromptBuilder& argument(std::string name,
bool required =
false,
118 std::string description = {}) {
120 argument.
name = std::move(name);
124 prompt_.
arguments.push_back(std::move(argument));
129 prompt_.
arguments.push_back(std::move(argument));
134 prompt_.
icons.push_back(std::move(value));
144 prompt_.
meta = std::move(value);
149 prompt_.
extensions[std::move(name)] = std::move(value);
153 Prompt build() {
return std::move(prompt_); }
234 static_cast<int>(ErrorCode::InvalidRequest), std::move(message), {}};
239 Json json = Json::object();
240 if (!argument.
title.empty()) {
241 json[
"title"] = argument.
title;
243 json[
"name"] = argument.
name;
248 json[
"required"] = argument.
required;
253 if (argument.
meta.has_value()) {
254 json[
"_meta"] = *argument.
meta;
264 if (!json.is_object()) {
265 return mcp::core::unexpected(
268 if (json.contains(
"title")) {
269 if (!json.at(
"title").is_string()) {
270 return mcp::core::unexpected(
274 if (!json.contains(
"name") || !json.at(
"name").is_string()) {
275 return mcp::core::unexpected(
280 if (json.contains(
"title")) {
281 argument.
title = json.at(
"title").get<std::string>();
283 argument.
name = json.at(
"name").get<std::string>();
284 if (json.contains(
"description")) {
285 if (!json.at(
"description").is_string()) {
286 return mcp::core::unexpected(
289 argument.
description = json.at(
"description").get<std::string>();
291 if (json.contains(
"required")) {
292 if (!json.at(
"required").is_boolean()) {
293 return mcp::core::unexpected(
296 argument.
required = json.at(
"required").get<
bool>();
299 if (json.contains(
"annotations")) {
300 if (!json.at(
"annotations").is_object()) {
301 return mcp::core::unexpected(
306 if (json.contains(
"_meta")) {
307 if (!json.at(
"_meta").is_object()) {
308 return mcp::core::unexpected(
311 argument.
meta = json.at(
"_meta");
315 {
"title",
"name",
"description",
"required",
"annotations",
"_meta"});
321 Json json = Json::object();
322 if (!prompt.title.empty()) {
323 json[
"title"] = prompt.title;
325 json[
"name"] = prompt.name;
326 if (!prompt.description.empty()) {
327 json[
"description"] = prompt.description;
329 if (!prompt.arguments.empty()) {
330 json[
"arguments"] = Json::array();
331 for (
const auto& argument : prompt.arguments) {
335 if (!prompt.icons.empty()) {
336 json[
"icons"] = Json::array();
337 for (
const auto& icon : prompt.icons) {
341 if (!prompt.annotations.empty()) {
342 json[
"annotations"] = prompt.annotations;
344 if (prompt.meta.has_value()) {
345 json[
"_meta"] = *prompt.meta;
354 if (!json.is_object()) {
357 if (json.contains(
"title")) {
358 if (!json.at(
"title").is_string()) {
359 return mcp::core::unexpected(
363 if (!json.contains(
"name") || !json.at(
"name").is_string()) {
364 return mcp::core::unexpected(
369 if (json.contains(
"title")) {
370 prompt.title = json.at(
"title").get<std::string>();
372 prompt.name = json.at(
"name").get<std::string>();
373 if (json.contains(
"description")) {
374 if (!json.at(
"description").is_string()) {
375 return mcp::core::unexpected(
378 prompt.description = json.at(
"description").get<std::string>();
380 if (json.contains(
"arguments")) {
381 if (!json.at(
"arguments").is_array()) {
382 return mcp::core::unexpected(
385 for (
const auto& item : json.at(
"arguments")) {
388 return mcp::core::unexpected(argument.error());
390 prompt.arguments.push_back(*argument);
393 if (json.contains(
"icons")) {
394 if (!json.at(
"icons").is_array()) {
395 return mcp::core::unexpected(
398 for (
const auto& item : json.at(
"icons")) {
400 if (!icon.has_value()) {
401 return mcp::core::unexpected(
404 prompt.icons.push_back(*icon);
407 if (json.contains(
"annotations")) {
408 if (!json.at(
"annotations").is_object()) {
409 return mcp::core::unexpected(
412 prompt.annotations = json.at(
"annotations");
414 if (json.contains(
"_meta")) {
415 if (!json.at(
"_meta").is_object()) {
416 return mcp::core::unexpected(
419 prompt.meta = json.at(
"_meta");
422 json, {
"title",
"name",
"description",
"arguments",
"icons",
423 "annotations",
"_meta"});
429 Json json = Json::object();
430 json[
"prompts"] = Json::array();
431 for (
const auto& prompt : result.
prompts) {
437 if (result.
meta.has_value()) {
438 json[
"_meta"] = *result.
meta;
440 if (result.
ttl_ms.has_value()) {
441 json[
"ttlMs"] = *result.
ttl_ms;
454 if (!json.is_object()) {
455 return mcp::core::unexpected(
458 if (!json.contains(
"prompts") || !json.at(
"prompts").is_array()) {
459 return mcp::core::unexpected(
464 for (
const auto& item : json.at(
"prompts")) {
467 return mcp::core::unexpected(prompt.error());
469 result.
prompts.push_back(*prompt);
471 if (json.contains(
"nextCursor")) {
472 if (!json.at(
"nextCursor").is_string()) {
473 return mcp::core::unexpected(
476 result.
next_cursor = json.at(
"nextCursor").get<std::string>();
478 if (json.contains(
"_meta")) {
479 if (!json.at(
"_meta").is_object()) {
480 return mcp::core::unexpected(
483 result.
meta = json.at(
"_meta");
492 Json json = Json::object();
493 json[
"name"] = params.
name;
497 if (params.
meta.has_value()) {
498 json[
"_meta"] = *params.
meta;
508 if (!json.is_object()) {
509 return mcp::core::unexpected(
512 if (!json.contains(
"name") || !json.at(
"name").is_string()) {
513 return mcp::core::unexpected(
518 params.
name = json.at(
"name").get<std::string>();
519 if (json.contains(
"arguments")) {
520 if (!json.at(
"arguments").is_object()) {
521 return mcp::core::unexpected(
526 if (json.contains(
"_meta")) {
527 if (!json.at(
"_meta").is_object()) {
528 return mcp::core::unexpected(
531 params.
meta = json.at(
"_meta");
540 Json json = Json::object();
541 json[
"role"] = message.
role;
543 if (message.
meta.has_value()) {
544 json[
"_meta"] = *message.
meta;
553 if (!json.is_object()) {
554 return mcp::core::unexpected(
557 if (!json.contains(
"role") || !json.at(
"role").is_string()) {
558 return mcp::core::unexpected(
561 if (!json.contains(
"content")) {
562 return mcp::core::unexpected(
568 return mcp::core::unexpected(content.error());
572 message.
role = json.at(
"role").get<std::string>();
574 if (json.contains(
"_meta")) {
575 if (!json.at(
"_meta").is_object()) {
576 return mcp::core::unexpected(
579 message.
meta = json.at(
"_meta");
588 Json json = Json::object();
596 json[
"messages"] = Json::array();
597 for (
const auto& message : result.
messages) {
601 if (result.
meta.has_value()) {
602 json[
"_meta"] = *result.
meta;
618 if (!json.is_object()) {
619 return mcp::core::unexpected(
622 if (!json.contains(
"messages") && !json.contains(
"resultType") &&
623 !json.contains(
"inputRequests")) {
625 "prompts/get result requires messages, resultType, or inputRequests"));
629 if (json.contains(
"resultType")) {
630 result.
result_type = json.at(
"resultType").get<std::string>();
632 if (json.contains(
"description")) {
633 if (!json.at(
"description").is_string()) {
634 return mcp::core::unexpected(
637 result.
description = json.at(
"description").get<std::string>();
639 if (json.contains(
"messages")) {
640 if (!json.at(
"messages").is_array()) {
641 return mcp::core::unexpected(
644 for (
const auto& item : json.at(
"messages")) {
647 return mcp::core::unexpected(message.error());
649 result.
messages.push_back(*message);
652 if (json.contains(
"_meta")) {
653 if (!json.at(
"_meta").is_object()) {
654 return mcp::core::unexpected(
657 result.
meta = json.at(
"_meta");
659 if (json.contains(
"inputRequests")) {
662 if (json.contains(
"requestState")) {
663 result.
request_state = json.at(
"requestState").get<std::string>();
666 json, {
"description",
"messages",
"_meta",
"resultType",
"inputRequests",
Fluent builder for prompt descriptors.
Definition prompt.hpp:103
core::Result< PromptArgument > prompt_argument_from_json(const Json &json)
Parses a prompt argument descriptor.
Definition prompt.hpp:262
core::Result< PromptsGetResult > prompts_get_result_from_json(const Json &json)
Parses a prompts/get result.
Definition prompt.hpp:616
core::Result< PromptsListResult > prompts_list_result_from_json(const Json &json)
Parses a prompts/list result.
Definition prompt.hpp:452
Json prompts_get_params_to_json(const PromptsGetParams ¶ms)
Serializes prompts/get params.
Definition prompt.hpp:491
std::optional< PromptMessageRole > prompt_message_role_from_string(std::string_view value)
Parses a prompt message role string.
Definition prompt.hpp:46
std::string_view prompt_message_role_to_string(PromptMessageRole role)
Converts a prompt message role to its MCP wire string.
Definition prompt.hpp:34
bool is_valid_prompt_message_role(std::string_view value)
Checks whether a string is a valid prompt message role.
Definition prompt.hpp:58
PromptMessageRole
Roles allowed on a prompt message.
Definition prompt.hpp:26
@ User
Message from the user.
@ Assistant
Message from the assistant (model).
Json prompt_argument_to_json(const PromptArgument &argument)
Serializes a prompt argument descriptor.
Definition prompt.hpp:238
core::Result< PromptMessage > prompt_message_from_json(const Json &json)
Parses one rendered prompt message.
Definition prompt.hpp:552
Json prompts_get_result_to_json(const PromptsGetResult &result)
Serializes a prompts/get result.
Definition prompt.hpp:587
PromptBuilder prompt_definition(std::string name)
Creates a fluent builder for advertised MCP prompt metadata.
Definition prompt.hpp:160
Json prompts_list_result_to_json(const PromptsListResult &result)
Serializes a prompts/list result.
Definition prompt.hpp:428
Json prompt_to_json(const Prompt &prompt)
Serializes a prompt descriptor.
Definition prompt.hpp:320
core::Result< Prompt > prompt_from_json(const Json &json)
Parses a prompt descriptor.
Definition prompt.hpp:353
core::Result< PromptsGetParams > prompts_get_params_from_json(const Json &json)
Parses prompts/get params.
Definition prompt.hpp:506
core::Error prompt_json_error(std::string message)
Builds an InvalidRequest error for prompt JSON validation failures.
Definition prompt.hpp:232
Json prompt_message_to_json(const PromptMessage &message)
Serializes one rendered prompt message.
Definition prompt.hpp:539
Shared JSON, JSON-RPC, error, cancellation, and progress model types.
void append_json_extensions(Json &json, const Json &extensions)
Flattens extension members into a JSON object without overwriting typed fields.
Definition types.hpp:358
nlohmann::json Json
JSON value type used by all protocol DTOs.
Definition types.hpp:28
Json collect_json_extensions(const Json &json, std::initializer_list< std::string_view > known_keys)
Collects unknown object members so typed DTOs can preserve future protocol fields and vendor extensio...
Definition types.hpp:320
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
Structured error returned by fallible SDK operations.
Definition result.hpp:35
A single content item returned by a tool or embedded in prompts.
Definition tool.hpp:93
static ContentBlock text_content(std::string value)
Creates a text content block.
Definition tool.hpp:115
Icon descriptor used by tools, resources, resource templates, and prompts.
Definition types.hpp:160
Argument accepted by a prompt template.
Definition prompt.hpp:63
std::string name
Stable argument name used as a key in prompts/get arguments.
Definition prompt.hpp:67
Json extensions
Unknown JSON members preserved for forward-compatible round trips.
Definition prompt.hpp:79
std::optional< Json > meta
Optional _meta extension object preserved on the wire.
Definition prompt.hpp:77
Json annotations
Optional annotations for model or client presentation.
Definition prompt.hpp:75
bool required
Whether the caller must provide this argument.
Definition prompt.hpp:71
bool required_present
Whether required was explicitly present on the wire or configured.
Definition prompt.hpp:73
std::string title
Optional human-readable display title.
Definition prompt.hpp:65
std::string description
Optional human-readable description.
Definition prompt.hpp:69
One rendered prompt message.
Definition prompt.hpp:193
Json extensions
Unknown JSON members preserved for forward-compatible round trips.
Definition prompt.hpp:201
static PromptMessage text(std::string role, std::string value)
Creates a text-only prompt message for the given role.
Definition prompt.hpp:204
std::optional< Json > meta
Optional _meta extension object preserved on the wire.
Definition prompt.hpp:199
ContentBlock content
Message content block.
Definition prompt.hpp:197
std::string role
Message role understood by the receiving model or peer.
Definition prompt.hpp:195
Prompt descriptor returned by prompts/list.
Definition prompt.hpp:83
std::optional< Json > meta
Optional _meta extension object preserved on the wire.
Definition prompt.hpp:97
std::string name
Stable prompt name used by prompts/get.
Definition prompt.hpp:87
std::string description
Optional human-readable description.
Definition prompt.hpp:89
std::vector< Icon > icons
Optional icon descriptors for client presentation.
Definition prompt.hpp:93
std::vector< PromptArgument > arguments
Prompt arguments accepted by this prompt.
Definition prompt.hpp:91
std::string title
Optional human-readable display title.
Definition prompt.hpp:85
Json extensions
Unknown JSON members preserved for forward-compatible round trips.
Definition prompt.hpp:99
Json annotations
Optional annotations for model or client presentation.
Definition prompt.hpp:95
Parameters for prompts/get.
Definition prompt.hpp:181
std::string name
Prompt name matching a Prompt descriptor.
Definition prompt.hpp:183
Json arguments
JSON object keyed by prompt argument name.
Definition prompt.hpp:185
Json extensions
Unknown JSON members preserved for forward-compatible round trips.
Definition prompt.hpp:189
std::optional< Json > meta
Optional _meta extension object preserved on the wire.
Definition prompt.hpp:187
Result object for prompts/get.
Definition prompt.hpp:213
std::optional< Json > input_requests
Server input requests for MRTR (SEP-2322).
Definition prompt.hpp:224
std::string description
Optional rendered prompt description.
Definition prompt.hpp:215
std::vector< PromptMessage > messages
Ordered messages produced by the prompt.
Definition prompt.hpp:217
std::optional< std::string > request_state
Opaque request state for MRTR retries (SEP-2322).
Definition prompt.hpp:226
Json extensions
Unknown JSON members preserved for forward-compatible round trips.
Definition prompt.hpp:228
std::optional< Json > meta
Optional _meta extension object preserved on the wire.
Definition prompt.hpp:219
std::optional< std::string > result_type
Result type discriminator.
Definition prompt.hpp:222
Result object for prompts/list.
Definition prompt.hpp:165
std::vector< Prompt > prompts
Prompts available to the caller.
Definition prompt.hpp:167
std::optional< std::string > next_cursor
Optional cursor for retrieving the next page.
Definition prompt.hpp:169
std::optional< std::string > cache_scope
Cache scope hint: "public" or "private" (SEP-2549).
Definition prompt.hpp:177
std::optional< std::int64_t > ttl_ms
Cache time-to-live hint in milliseconds (SEP-2549).
Definition prompt.hpp:175
Json extensions
Unknown JSON members preserved for forward-compatible round trips.
Definition prompt.hpp:173
std::optional< Json > meta
Optional _meta extension object preserved on the wire.
Definition prompt.hpp:171
Reflection specializations for DTOs defined in types.hpp.
Json icon_to_json(const Icon &icon)
Serializes a shared icon descriptor.
Definition types_reflect.hpp:109
std::optional< Icon > icon_from_json(const Json &json)
Parses a shared icon descriptor.
Definition types_reflect.hpp:114