Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
mcp.h
1#ifndef SOURCEMETA_CORE_MCP_H_
2#define SOURCEMETA_CORE_MCP_H_
3
4#ifndef SOURCEMETA_CORE_MCP_EXPORT
5#include <sourcemeta/core/mcp_export.h>
6#endif
7
8#include <sourcemeta/core/json.h>
9#include <sourcemeta/core/jsonrpc.h>
10
11#include <cstddef> // std::size_t
12#include <cstdint> // std::int64_t, std::uint8_t
13#include <optional> // std::optional, std::nullopt
14#include <utility> // std::unreachable
15
24
25namespace sourcemeta::core {
26
37
50constexpr auto
53 switch (version) {
55 return "2025-03-26";
57 return "2025-06-18";
59 return "2025-11-25";
60 }
61 std::unreachable();
62}
63
67
71
75
79
82constexpr JSON::StringView MCP_METHOD_RESOURCES_LIST{"resources/list"};
83
86constexpr JSON::StringView MCP_METHOD_RESOURCES_READ{"resources/read"};
87
91 "resources/templates/list"};
92
96 "notifications/initialized"};
97
100constexpr std::int64_t MCP_CODE_RESOURCE_NOT_FOUND{-32002};
101
105constexpr std::int64_t MCP_CODE_URL_ELICITATION_REQUIRED{-32042};
106
118constexpr auto mcp_is_request_method(const JSON::StringView method) noexcept
119 -> bool {
120 return method == MCP_METHOD_INITIALIZE || method == MCP_METHOD_PING ||
121 method == MCP_METHOD_TOOLS_LIST || method == MCP_METHOD_TOOLS_CALL ||
122 method == MCP_METHOD_RESOURCES_LIST ||
123 method == MCP_METHOD_RESOURCES_READ ||
125}
126
143constexpr auto
145 -> std::optional<MCPProtocolVersion> {
146 if (header.empty()) {
147 // Per the MCP Streamable HTTP transport spec: if the server does not
148 // receive an MCP-Protocol-Version header, and has no other way to identify
149 // the version, the server SHOULD assume protocol version 2025-03-26.
150 // https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#protocol-version-header
152 }
153 if (header == "2025-11-25") {
155 }
156 if (header == "2025-06-18") {
158 }
159 if (header == "2025-03-26") {
161 }
162 return std::nullopt;
163}
164
167constexpr auto
168mcp_supports_output_schema(const MCPProtocolVersion version) noexcept -> bool {
169 return version != MCPProtocolVersion::V_2025_03_26;
170}
171
175constexpr auto
177 -> bool {
178 return version != MCPProtocolVersion::V_2025_03_26;
179}
180
183constexpr auto
185 -> bool {
186 return version != MCPProtocolVersion::V_2025_03_26;
187}
188
192constexpr auto
194 -> bool {
195 return version != MCPProtocolVersion::V_2025_03_26;
196}
197
202 const MCPProtocolVersion version) noexcept -> bool {
203 return version == MCPProtocolVersion::V_2025_11_25;
204}
205
210 const MCPProtocolVersion version) noexcept -> bool {
211 return version == MCPProtocolVersion::V_2025_11_25;
212}
213
216constexpr auto
218 -> bool {
219 return version == MCPProtocolVersion::V_2025_03_26;
220}
221
234SOURCEMETA_CORE_MCP_EXPORT
236
249SOURCEMETA_CORE_MCP_EXPORT
251 const JSON::StringView uri,
252 const JSON::StringView mime_type,
253 const JSON::StringView name = {},
254 const JSON::StringView description = {})
256
275SOURCEMETA_CORE_MCP_EXPORT
277 const sourcemeta::core::JSON &identifier,
280
301SOURCEMETA_CORE_MCP_EXPORT
303 const sourcemeta::core::JSON &identifier,
304 sourcemeta::core::JSON structured,
305 sourcemeta::core::JSON content_blocks)
307
322SOURCEMETA_CORE_MCP_EXPORT
324 const JSON::StringView message)
326
341SOURCEMETA_CORE_MCP_EXPORT
344
357SOURCEMETA_CORE_MCP_EXPORT
359 const JSON::StringView mime_type,
360 const JSON::StringView description = {},
361 const std::optional<std::size_t> size = std::nullopt,
362 const std::optional<double> priority = std::nullopt)
364
376SOURCEMETA_CORE_MCP_EXPORT
378 const JSON::StringView mime_type,
379 const JSON::StringView text)
381
399SOURCEMETA_CORE_MCP_EXPORT
402
415SOURCEMETA_CORE_MCP_EXPORT
417 const JSON::StringView name,
418 const JSON::StringView description,
419 const JSON::StringView mime_type)
421
428 bool read_only = false;
430 bool destructive = true;
433 bool idempotent = false;
435 bool open_world = true;
436};
437
450SOURCEMETA_CORE_MCP_EXPORT
452 const MCPProtocolVersion version, const JSON::StringView name,
453 const JSON::StringView description, sourcemeta::core::JSON input_schema,
454 std::optional<sourcemeta::core::JSON> output_schema = std::nullopt,
455 const MCPToolAnnotations &annotations = {}) -> sourcemeta::core::JSON;
456
472
478 bool prompts = false;
480 bool resources = false;
482 bool tools = false;
484 bool logging = false;
486 bool completions = false;
487};
488
511SOURCEMETA_CORE_MCP_EXPORT
513 const MCPServerCapabilities &capabilities,
514 const MCPImplementation &server,
515 const JSON::StringView instructions = {})
517
538SOURCEMETA_CORE_MCP_EXPORT
540 -> const sourcemeta::core::JSON *;
541
542} // namespace sourcemeta::core
543
544#endif
std::basic_string_view< Char, CharTraits > StringView
The string view type used by the JSON document.
Definition json_value.h:54
Definition json_value.h:39
JSON::StringView title
Optional human-readable title for the tool.
Definition mcp.h:426
JSON::StringView description
Optional human-readable description.
Definition mcp.h:468
bool logging
Whether the server advertises logging.
Definition mcp.h:484
bool completions
Whether the server advertises completions.
Definition mcp.h:486
bool tools
Whether the server advertises tools.
Definition mcp.h:482
bool destructive
true when the tool may mutate or delete state.
Definition mcp.h:430
bool idempotent
Definition mcp.h:433
JSON::StringView version
Semver-compatible server version.
Definition mcp.h:464
JSON::StringView title
Optional human-readable title.
Definition mcp.h:466
bool open_world
true when the tool interacts with state outside the server's control.
Definition mcp.h:435
JSON::StringView website_url
Optional public website URL.
Definition mcp.h:470
bool resources
Whether the server advertises resources.
Definition mcp.h:480
bool read_only
true when the tool guarantees no side effects.
Definition mcp.h:428
bool prompts
Whether the server advertises prompts.
Definition mcp.h:478
JSON::StringView name
Short machine-readable server name.
Definition mcp.h:462
constexpr auto mcp_supports_implementation_website_url(const MCPProtocolVersion version) noexcept -> bool
Definition mcp.h:209
constexpr JSON::StringView MCP_METHOD_TOOLS_LIST
Definition mcp.h:74
MCPProtocolVersion
Definition mcp.h:29
constexpr auto mcp_supports_jsonrpc_batching(const MCPProtocolVersion version) noexcept -> bool
Definition mcp.h:217
SOURCEMETA_CORE_MCP_EXPORT auto mcp_tool_call_arguments(const sourcemeta::core::JSON &envelope) -> const sourcemeta::core::JSON *
SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_resources_read_result(sourcemeta::core::JSON contents) -> sourcemeta::core::JSON
constexpr JSON::StringView MCP_METHOD_TOOLS_CALL
Definition mcp.h:78
SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_resource_link(const MCPProtocolVersion version, const JSON::StringView uri, const JSON::StringView mime_type, const JSON::StringView name={}, const JSON::StringView description={}) -> sourcemeta::core::JSON
constexpr JSON::StringView MCP_METHOD_NOTIFICATIONS_INITIALIZED
Definition mcp.h:95
constexpr auto mcp_supports_output_schema(const MCPProtocolVersion version) noexcept -> bool
Definition mcp.h:168
constexpr auto mcp_supports_structured_content(const MCPProtocolVersion version) noexcept -> bool
Definition mcp.h:176
constexpr auto mcp_supports_resource_link_content(const MCPProtocolVersion version) noexcept -> bool
Definition mcp.h:184
SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_text_block(const JSON::StringView text) -> sourcemeta::core::JSON
SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_tool_error(const sourcemeta::core::JSON &identifier, const JSON::StringView message) -> sourcemeta::core::JSON
constexpr auto mcp_resolve_protocol_version(const JSON::StringView header) noexcept -> std::optional< MCPProtocolVersion >
Definition mcp.h:144
constexpr JSON::StringView MCP_METHOD_PING
Definition mcp.h:70
SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_resource_text_content(const JSON::StringView uri, const JSON::StringView mime_type, const JSON::StringView text) -> sourcemeta::core::JSON
SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_tool_descriptor(const MCPProtocolVersion version, const JSON::StringView name, const JSON::StringView description, sourcemeta::core::JSON input_schema, std::optional< sourcemeta::core::JSON > output_schema=std::nullopt, const MCPToolAnnotations &annotations={}) -> sourcemeta::core::JSON
constexpr JSON::StringView MCP_METHOD_RESOURCES_LIST
Definition mcp.h:82
constexpr auto mcp_is_request_method(const JSON::StringView method) noexcept -> bool
Definition mcp.h:118
constexpr std::int64_t MCP_CODE_RESOURCE_NOT_FOUND
Definition mcp.h:100
SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_tool_success(const MCPProtocolVersion version, const sourcemeta::core::JSON &identifier, sourcemeta::core::JSON result) -> sourcemeta::core::JSON
constexpr auto mcp_supports_implementation_description(const MCPProtocolVersion version) noexcept -> bool
Definition mcp.h:201
SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_error_resource_not_found(const sourcemeta::core::JSON &identifier) -> sourcemeta::core::JSON
constexpr JSON::StringView MCP_METHOD_INITIALIZE
Definition mcp.h:66
constexpr auto mcp_protocol_version_string(const MCPProtocolVersion version) noexcept -> JSON::StringView
Definition mcp.h:51
SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_resource(const JSON::StringView uri, const JSON::StringView name, const JSON::StringView mime_type, const JSON::StringView description={}, const std::optional< std::size_t > size=std::nullopt, const std::optional< double > priority=std::nullopt) -> sourcemeta::core::JSON
constexpr JSON::StringView MCP_METHOD_RESOURCES_TEMPLATES_LIST
Definition mcp.h:90
constexpr std::int64_t MCP_CODE_URL_ELICITATION_REQUIRED
Definition mcp.h:105
constexpr auto mcp_supports_implementation_title(const MCPProtocolVersion version) noexcept -> bool
Definition mcp.h:193
SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_initialize_result(const sourcemeta::core::JSON &request, const MCPServerCapabilities &capabilities, const MCPImplementation &server, const JSON::StringView instructions={}) -> sourcemeta::core::JSON
SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_resource_template(const JSON::StringView uri_template, const JSON::StringView name, const JSON::StringView description, const JSON::StringView mime_type) -> sourcemeta::core::JSON
constexpr JSON::StringView MCP_METHOD_RESOURCES_READ
Definition mcp.h:86
@ V_2025_03_26
The MCP 2025-03-26 protocol revision.
Definition mcp.h:31
@ V_2025_11_25
The MCP 2025-11-25 protocol revision.
Definition mcp.h:35
@ V_2025_06_18
The MCP 2025-06-18 protocol revision.
Definition mcp.h:33