cxxmcp 1.1.6
C++ MCP SDK
Loading...
Searching...
No Matches
custom_methods.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 [caomengxuan666]
2
3#pragma once
4
11
12#include <cstdint>
13#include <string>
14#include <string_view>
15
19
20namespace mcp::protocol {
21
42template <class Params, class Result>
45 std::string method;
47 Params params;
50
53 template <class Serializer>
54 JsonRpcRequest to_json_rpc(Serializer serializer) const {
55 return make_request(method, id, serializer(params));
56 }
57
61 template <class Deserializer>
63 Deserializer deserializer) const {
64 if (response.has_error()) {
65 return mcp::core::unexpected(
66 core::Error{response.error->code, response.error->message,
67 response.error->data.value_or(Json::object())});
68 }
69 if (!response.has_result()) {
70 return mcp::core::unexpected(
71 core::Error{static_cast<int>(ErrorCode::InternalError),
72 "custom request response has neither result nor error",
73 {}});
74 }
75 return deserializer(*response.result);
76 }
77};
78
90template <class Params>
93 std::string method;
95 Params params;
96
100 template <class Serializer>
101 JsonRpcNotification to_json_rpc(Serializer serializer) const {
102 return make_notification(method, serializer(params));
103 }
104};
105
106} // namespace mcp::protocol
Shared JSON, JSON-RPC, error, cancellation, and progress model types.
std::variant< std::int64_t, std::string > RequestId
JSON-RPC request or response identifier.
Definition types.hpp:56
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
JSON-RPC method names and message construction/parsing helpers.
JsonRpcNotification make_notification(std::string method, Json params=Json::object())
Builds a generic JSON-RPC notification envelope.
JsonRpcRequest make_request(std::string method, RequestId id, Json params=Json::object())
Builds a generic JSON-RPC request envelope.
Structured error returned by fallible SDK operations.
Definition result.hpp:35
Generic typed MCP notification for extension methods.
Definition custom_methods.hpp:91
Params params
Typed notification parameters.
Definition custom_methods.hpp:95
std::string method
Method name on the wire.
Definition custom_methods.hpp:93
JsonRpcNotification to_json_rpc(Serializer serializer) const
Creates a JsonRpcNotification envelope from this typed notification.
Definition custom_methods.hpp:101
Generic typed MCP request for extension methods.
Definition custom_methods.hpp:43
Params params
Typed request parameters.
Definition custom_methods.hpp:47
core::Result< Result > parse_response(const JsonRpcResponse &response, Deserializer deserializer) const
Parses a JsonRpcResponse into a typed Result.
Definition custom_methods.hpp:62
RequestId id
Request id for JSON-RPC correlation.
Definition custom_methods.hpp:49
std::string method
Method name on the wire.
Definition custom_methods.hpp:45
JsonRpcRequest to_json_rpc(Serializer serializer) const
Creates a JsonRpcRequest envelope from this typed request.
Definition custom_methods.hpp:54
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
JSON-RPC response envelope for either success or failure.
Definition types.hpp:115
bool has_result() const noexcept
Returns true when this response contains a successful result.
Definition types.hpp:126
bool has_error() const noexcept
Returns true when this response contains an error object.
Definition types.hpp:131
std::optional< Json > result
Successful method result. Mutually exclusive with error.
Definition types.hpp:119
std::optional< ErrorObject > error
JSON-RPC error object. Mutually exclusive with result.
Definition types.hpp:121