Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
mcp_error.h
1#ifndef SOURCEMETA_CORE_MCP_ERROR_H_
2#define SOURCEMETA_CORE_MCP_ERROR_H_
3
4#ifndef SOURCEMETA_CORE_MCP_EXPORT
5#include <sourcemeta/core/mcp_export.h>
6#endif
7
8#include <sourcemeta/core/error.h>
9#include <sourcemeta/core/http.h>
10#include <sourcemeta/core/json.h>
11#include <sourcemeta/core/jsonrpc.h>
12#include <sourcemeta/core/mcp_protocol.h>
13
14#include <cstdint> // std::int64_t, std::uint8_t
15#include <optional> // std::optional, std::nullopt
16#include <span> // std::span
17
18namespace sourcemeta::core {
19
20SOURCEMETA_CORE_ERROR_WARNINGS_PUSH
21
42class SOURCEMETA_CORE_MCP_EXPORT MCPError : public Error {
43public:
46};
47
48SOURCEMETA_CORE_ERROR_WARNINGS_POP
49
58enum class MCPErrorContext : std::uint8_t {
65};
66
101[[nodiscard]] SOURCEMETA_CORE_MCP_EXPORT auto
102mcp_make_error(MCPProtocolVersion version, const JSON *identifier,
103 std::int64_t code, JSON::StringView message,
104 std::optional<JSON> data = std::nullopt,
106
110constexpr std::int64_t MCP_CODE_RESOURCE_NOT_FOUND{-32002};
111
116constexpr std::int64_t MCP_CODE_URL_ELICITATION_REQUIRED{-32042};
117
121constexpr std::int64_t MCP_CODE_HEADER_MISMATCH{-32020};
122
126constexpr std::int64_t MCP_CODE_MISSING_REQUIRED_CLIENT_CAPABILITY{-32021};
127
131constexpr std::int64_t MCP_CODE_UNSUPPORTED_PROTOCOL_VERSION{-32022};
132
188[[nodiscard]] constexpr auto
190 const std::int64_t code) noexcept
191 -> std::optional<HTTPStatus> {
192 if (version != MCPProtocolVersion::V_2026_07_28) {
193 return std::nullopt;
194 }
195 if (code == JSONRPC_CODE_METHOD_NOT_FOUND) {
197 }
198 if (code == MCP_CODE_HEADER_MISMATCH ||
202 }
203 return std::nullopt;
204}
205
240[[nodiscard]] SOURCEMETA_CORE_MCP_EXPORT auto
242 const MCPProtocolVersion version, const sourcemeta::core::JSON *identifier,
243 const JSON::StringView requested,
244 const std::span<const JSON::StringView> supported)
246
275[[nodiscard]] SOURCEMETA_CORE_MCP_EXPORT auto
277 const MCPProtocolVersion version, const sourcemeta::core::JSON *identifier,
278 sourcemeta::core::JSON required_capabilities,
280
303[[nodiscard]] SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_error_header_mismatch(
304 const MCPProtocolVersion version, const sourcemeta::core::JSON *identifier,
306
337[[nodiscard]] SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_error_header_mismatch(
338 const MCPProtocolVersion version, const sourcemeta::core::JSON *identifier,
339 const JSON::StringView header_name, const JSON::StringView header_value,
340 const JSON::StringView body_value) -> sourcemeta::core::JSON;
341
367[[nodiscard]] SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_error_header_mismatch(
368 const MCPProtocolVersion version, const sourcemeta::core::JSON *identifier,
369 const JSON::StringView header_name, const JSON::StringView header_value)
371
400[[nodiscard]] SOURCEMETA_CORE_MCP_EXPORT auto
402 const sourcemeta::core::JSON *identifier)
404
405} // namespace sourcemeta::core
406
407#endif
auto message() const noexcept -> StaticString
Definition error_base.h:78
Error(const StaticString message) noexcept
Construct the error given its message.
Definition error_base.h:84
constexpr HTTPStatus HTTP_STATUS_BAD_REQUEST
Definition http_status.h:153
constexpr HTTPStatus HTTP_STATUS_NOT_FOUND
Definition http_status.h:173
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
constexpr std::int64_t JSONRPC_CODE_METHOD_NOT_FOUND
Definition jsonrpc.h:35
MCPError(const StaticString message) noexcept
Construct an error with a descriptive message.
Definition mcp_error.h:45
SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_error(MCPProtocolVersion version, const JSON *identifier, std::int64_t code, JSON::StringView message, std::optional< JSON > data=std::nullopt, MCPErrorContext context=MCPErrorContext::Request) -> JSON
SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_error_resource_not_found(const MCPProtocolVersion version, const sourcemeta::core::JSON *identifier) -> sourcemeta::core::JSON
MCPProtocolVersion
Definition mcp_protocol.h:30
constexpr std::int64_t MCP_CODE_MISSING_REQUIRED_CLIENT_CAPABILITY
Definition mcp_error.h:126
SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_error_header_mismatch(const MCPProtocolVersion version, const sourcemeta::core::JSON *identifier, const JSON::StringView message) -> sourcemeta::core::JSON
SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_error_unsupported_protocol_version(const MCPProtocolVersion version, const sourcemeta::core::JSON *identifier, const JSON::StringView requested, const std::span< const JSON::StringView > supported) -> sourcemeta::core::JSON
SOURCEMETA_CORE_MCP_EXPORT auto mcp_make_error_missing_required_capability(const MCPProtocolVersion version, const sourcemeta::core::JSON *identifier, sourcemeta::core::JSON required_capabilities, const JSON::StringView message) -> sourcemeta::core::JSON
constexpr std::int64_t MCP_CODE_RESOURCE_NOT_FOUND
Definition mcp_error.h:110
constexpr auto mcp_error_code_to_http_status(const MCPProtocolVersion version, const std::int64_t code) noexcept -> std::optional< HTTPStatus >
Definition mcp_error.h:189
constexpr std::int64_t MCP_CODE_HEADER_MISMATCH
Definition mcp_error.h:121
constexpr std::int64_t MCP_CODE_UNSUPPORTED_PROTOCOL_VERSION
Definition mcp_error.h:131
MCPErrorContext
Definition mcp_error.h:58
constexpr std::int64_t MCP_CODE_URL_ELICITATION_REQUIRED
Definition mcp_error.h:116
@ V_2026_07_28
The stateless, per-request capability negotiation revision (2026-07-28).
Definition mcp_protocol.h:38
@ Request
A normal request response. Older schemas require a readable identifier.
Definition mcp_error.h:60
@ Transport
Definition mcp_error.h:64
Definition text_static_string.h:20