-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSharedMemoryTransport.hpp
More file actions
97 lines (82 loc) · 5.11 KB
/
SharedMemoryTransport.hpp
File metadata and controls
97 lines (82 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//==========================================================================================================
// SPDX-License-Identifier: MIT
// Copyright (c) 2025 Vinny Parla
// File: SharedMemoryTransport.hpp
// Purpose: Cross-process shared-memory JSON-RPC transport built on Boost.Interprocess
//==========================================================================================================
#pragma once
#include <future>
#include <memory>
#include <string>
#include "mcp/Transport.h"
namespace mcp {
class SharedMemoryTransport : public ITransport {
public:
//==========================================================================================================
// Options
// Purpose: Configuration for channel identity and queue sizing.
// Fields:
// channelName: Base channel name. Two queues are derived: "<channelName>_c2s" and "<channelName>_s2c".
// create: When true, create queues (server-side). When false, open existing queues (client-side).
// maxMessageSize: Maximum size per message in bytes when creating queues (default: 1 MiB).
// maxMessageCount: Maximum number of messages buffered per queue when creating (default: 256).
//==========================================================================================================
struct Options {
std::string channelName;
bool create{false};
std::size_t maxMessageSize{1024ull * 1024ull};
unsigned int maxMessageCount{256u};
};
explicit SharedMemoryTransport(const Options& opts);
~SharedMemoryTransport() override;
////////////////////////////////////////// ITransport //////////////////////////////////////////
//==========================================================================================================
// Starts the transport I/O loop. Returns when worker is ready to accept requests.
//==========================================================================================================
std::future<void> Start() override;
//==========================================================================================================
// Closes the transport and stops the I/O loop.
//==========================================================================================================
std::future<void> Close() override;
//==========================================================================================================
// Indicates whether the transport session is currently connected.
//==========================================================================================================
bool IsConnected() const override;
//==========================================================================================================
// Returns a transport session identifier for diagnostics.
//==========================================================================================================
std::string GetSessionId() const override;
//==========================================================================================================
// Sends a JSON-RPC request and returns a future for the response.
//==========================================================================================================
std::future<std::unique_ptr<JSONRPCResponse>> SendRequest(
std::unique_ptr<JSONRPCRequest> request) override;
//==========================================================================================================
// Sends a JSON-RPC notification (no response expected).
//==========================================================================================================
std::future<void> SendNotification(
std::unique_ptr<JSONRPCNotification> notification) override;
//==========================================================================================================
// Registers handlers for incoming notifications and errors. RequestHandler is used when this
// transport instance is wired on the server side.
//==========================================================================================================
void SetNotificationHandler(NotificationHandler handler) override;
void SetRequestHandler(RequestHandler handler) override;
void SetErrorHandler(ErrorHandler handler) override;
private:
class Impl;
std::unique_ptr<Impl> pImpl;
};
//==========================================================================================================
// SharedMemoryTransportFactory
// Purpose: Factory for creating shared-memory transports from a configuration string.
// Supported formats:
// - "shm://<channelName>?create=true&maxSize=<bytes>&maxCount=<n>"
// - "<channelName>" (defaults to create=false)
// Unknown parameters are ignored.
//==========================================================================================================
class SharedMemoryTransportFactory : public ITransportFactory {
public:
std::unique_ptr<ITransport> CreateTransport(const std::string& config) override;
};
} // namespace mcp