181 lines
5.4 KiB
C++
181 lines
5.4 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Result.hpp>
|
|
#include <Void/MCP/Codec.hpp>
|
|
#include <Void/MCP/Envelope.hpp>
|
|
#include <Void/MCP/Types.hpp>
|
|
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <functional>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <stop_token>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace Void::MCP
|
|
{
|
|
struct HttpHeader {
|
|
std::string name{};
|
|
std::string value{};
|
|
};
|
|
|
|
struct HttpRequest {
|
|
std::string method{};
|
|
std::string target{};
|
|
std::vector<HttpHeader> headers{};
|
|
std::string body{};
|
|
};
|
|
|
|
struct HttpResponse {
|
|
int status{200};
|
|
std::vector<HttpHeader> headers{};
|
|
std::string body{};
|
|
};
|
|
|
|
/// Case-insensitive header lookup. Returns the first match.
|
|
[[nodiscard]] std::optional<std::string_view>
|
|
header_value(const HttpRequest& request, std::string_view name);
|
|
|
|
/// Authorization boundary: validate the bearer token (or any scheme the
|
|
/// application chooses) and return an authenticated principal. MCP does
|
|
/// not implement OAuth, consent, or scope mapping itself.
|
|
using AuthorizationCallback =
|
|
std::function<Void::Result::Result<AuthContext>(const HttpRequest&)>;
|
|
|
|
using RequestHandler =
|
|
std::function<Void::Result::Result<Message>(const Request&)>;
|
|
using NotificationHandler =
|
|
std::function<Void::Result::Result<void>(const Notification&)>;
|
|
|
|
struct StreamableHttpOptions {
|
|
McpLimits limits{};
|
|
/// Called for every POST before the message is dispatched.
|
|
AuthorizationCallback authorize{};
|
|
bool require_authorization{false};
|
|
/// Respond to requests with `text/event-stream` instead of a
|
|
/// single `application/json` object.
|
|
bool prefer_sse{false};
|
|
/// Protocol revisions accepted in `_meta`/headers.
|
|
std::vector<std::string> supported_versions{
|
|
std::string{kProtocolVersion}};
|
|
};
|
|
|
|
/// Streamable HTTP binding for the 2026-07-28 revision: one MCP endpoint
|
|
/// accepting POST only (GET/DELETE answer 405), protocol/method/name
|
|
/// header validation, JSON or request-scoped SSE responses, and no
|
|
/// session ids or resumability.
|
|
class StreamableHttpEndpoint
|
|
{
|
|
public:
|
|
|
|
StreamableHttpEndpoint(RequestHandler requests,
|
|
NotificationHandler notifications,
|
|
StreamableHttpOptions options = {});
|
|
~StreamableHttpEndpoint();
|
|
StreamableHttpEndpoint(const StreamableHttpEndpoint&) = delete;
|
|
StreamableHttpEndpoint&
|
|
operator=(const StreamableHttpEndpoint&) = delete;
|
|
|
|
[[nodiscard]] HttpResponse
|
|
handle(const HttpRequest& request) const;
|
|
|
|
private:
|
|
|
|
struct Impl;
|
|
std::unique_ptr<Impl> m_impl;
|
|
};
|
|
|
|
/// Newline-delimited stdio (or any reliable bidirectional byte stream)
|
|
/// transport. Reads are bounded by `McpLimits::max_line_bytes`; writes
|
|
/// honour cancellation and a bounded timeout on non-blocking fds.
|
|
class StdioTransport
|
|
{
|
|
public:
|
|
|
|
StdioTransport(int read_fd, int write_fd,
|
|
RequestHandler requests,
|
|
NotificationHandler notifications,
|
|
McpLimits limits = {},
|
|
std::chrono::milliseconds io_timeout =
|
|
std::chrono::seconds{30});
|
|
~StdioTransport();
|
|
StdioTransport(const StdioTransport&) = delete;
|
|
StdioTransport& operator=(const StdioTransport&) = delete;
|
|
|
|
/// Read one message. `nullopt` means clean end of stream.
|
|
[[nodiscard]] Void::Result::Result<std::optional<Message>>
|
|
read_message(std::stop_token stop = {});
|
|
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
write_message(const Message& message,
|
|
std::stop_token stop = {});
|
|
|
|
/// Read-dispatch-write until EOF, cancellation, or an I/O error.
|
|
/// Malformed JSON frames are answered with a parse-error
|
|
/// response and do not terminate the loop.
|
|
[[nodiscard]] Void::Result::Result<void>
|
|
run(std::stop_token stop = {});
|
|
|
|
private:
|
|
|
|
struct Impl;
|
|
std::unique_ptr<Impl> m_impl;
|
|
};
|
|
|
|
struct LegacySession {
|
|
std::string id{};
|
|
std::string protocol_version{};
|
|
bool initialized{false};
|
|
};
|
|
|
|
struct LegacySessionOptions {
|
|
/// Revisions of the initialize/session era this adapter accepts,
|
|
/// newest first.
|
|
std::vector<std::string> supported_versions{
|
|
"2025-11-25", "2025-06-18", "2025-03-26", "2024-11-05"};
|
|
std::string server_name{"Void MCP"};
|
|
std::string server_version{"1.0"};
|
|
std::function<std::string()> session_id_factory{};
|
|
};
|
|
|
|
/// Explicit compatibility adapter for the pre-2026-07-28
|
|
/// initialization/session semantics: `initialize` negotiation,
|
|
/// `initialized` notification, and session ids. Kept separate from the
|
|
/// modern transport so the two behaviors cannot bleed into each other.
|
|
class LegacySessionAdapter
|
|
{
|
|
public:
|
|
|
|
explicit LegacySessionAdapter(LegacySessionOptions options = {});
|
|
~LegacySessionAdapter();
|
|
LegacySessionAdapter(const LegacySessionAdapter&) = delete;
|
|
LegacySessionAdapter&
|
|
operator=(const LegacySessionAdapter&) = delete;
|
|
|
|
/// Handle one inbound message. `initialize` creates a session and
|
|
/// returns the negotiated response; `initialized` marks it ready and
|
|
/// returns no reply; everything else is rejected until a session is
|
|
/// initialized.
|
|
[[nodiscard]] Void::Result::Result<std::optional<Message>>
|
|
handle(const Message& inbound,
|
|
std::string_view session_id = {});
|
|
|
|
[[nodiscard]] const LegacySession*
|
|
find(std::string_view session_id) const;
|
|
|
|
bool terminate(std::string_view session_id);
|
|
|
|
[[nodiscard]] std::size_t session_count() const;
|
|
|
|
private:
|
|
|
|
struct Impl;
|
|
std::unique_ptr<Impl> m_impl;
|
|
};
|
|
|
|
} // namespace Void::MCP
|