-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmcp_server.cpp
More file actions
243 lines (207 loc) · 7.34 KB
/
mcp_server.cpp
File metadata and controls
243 lines (207 loc) · 7.34 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "mcp/mcp_server.h"
#include "mcp/tool_registry.h"
#include "core/session.h"
#include "core/diff_session.h"
#include "core/errors.h"
#include <stdexcept>
using json = nlohmann::json;
namespace renderdoc::mcp {
// ── Injection constructor ──────────────────────────────────────────────────
McpServer::McpServer(core::Session& session, core::DiffSession& diffSession, ToolRegistry& registry)
: m_session(&session)
, m_diffSession(&diffSession)
, m_registry(®istry)
, m_initialized(false)
{
}
McpServer::~McpServer() = default;
void McpServer::shutdown()
{
if(m_session)
m_session->close();
if(m_diffSession)
m_diffSession->close();
}
// ── JSON-RPC helpers ────────────────────────────────────────────────────────
json McpServer::makeResponse(const json& id, const json& result)
{
json resp;
resp["jsonrpc"] = "2.0";
resp["id"] = id;
resp["result"] = result;
return resp;
}
json McpServer::makeError(const json& id, int code, const std::string& message)
{
json resp;
resp["jsonrpc"] = "2.0";
resp["id"] = id;
resp["error"]["code"] = code;
resp["error"]["message"] = message;
return resp;
}
json McpServer::makeToolResult(const json& data, bool isError) const
{
json result;
json content;
content["type"] = "text";
if(data.is_string())
content["text"] = data.get<std::string>();
else
content["text"] = data.dump();
result["content"] = json::array({content});
if(m_protocolVersion == ProtocolVersion::V2025_06_18 && !isError && data.is_object())
result["structuredContent"] = data;
if(isError)
result["isError"] = true;
return result;
}
ProtocolVersion McpServer::negotiateProtocolVersion(const json& params)
{
if(params.contains("protocolVersion"))
{
const auto clientVersion = params["protocolVersion"].get<std::string>();
if(clientVersion == kProtocolVersion2025_03_26)
return ProtocolVersion::V2025_03_26;
if(clientVersion == kProtocolVersion2025_06_18)
return ProtocolVersion::V2025_06_18;
}
return ProtocolVersion::V2025_06_18;
}
const char* McpServer::protocolVersionString(ProtocolVersion version)
{
switch(version)
{
case ProtocolVersion::V2025_03_26:
return kProtocolVersion2025_03_26;
case ProtocolVersion::V2025_06_18:
return kProtocolVersion2025_06_18;
}
return kProtocolVersion;
}
// ── Message dispatch ────────────────────────────────────────────────────────
json McpServer::handleMessage(const json& msg)
{
// Check for valid JSON-RPC 2.0
if(!msg.contains("jsonrpc") || msg["jsonrpc"] != "2.0")
return makeError(msg.value("id", json(nullptr)), -32600, "Invalid Request: missing jsonrpc 2.0");
std::string method = msg.value("method", "");
bool isNotification = !msg.contains("id");
json id = msg.value("id", json(nullptr));
// Route methods
if(method == "ping")
{
return makeResponse(id, json::object());
}
else if(method == "initialize")
{
if(m_initializeSeen)
return makeError(id, -32600, "Server already initialized");
return handleInitialize(msg);
}
else if(method == "notifications/initialized")
{
m_initialized = true;
return nullptr; // No response for notifications
}
else if(method == "shutdown")
{
shutdown();
return makeResponse(id, json::object());
}
else if(method == "tools/list" || method == "tools/call")
{
if(!m_initialized && !isNotification)
return makeError(id, -32002, "Server not initialized");
if(method == "tools/list")
return handleToolsList(msg);
return handleToolsCall(msg);
}
else if(isNotification)
return nullptr; // Unknown notifications are silently ignored
else
return makeError(id, -32601, "Method not found: " + method);
}
json McpServer::handleBatch(const json& arr)
{
if(!m_initializeSeen || m_protocolVersion == ProtocolVersion::V2025_06_18)
{
return makeError(nullptr, -32600,
"Invalid Request: JSON-RPC batching is not supported by the negotiated MCP protocol");
}
json responses = json::array();
for(const auto& msg : arr)
{
if(!msg.is_object())
{
responses.push_back(makeError(nullptr, -32600, "Invalid Request: batch element is not an object"));
continue;
}
json resp = handleMessage(msg);
if(!resp.is_null())
responses.push_back(resp);
}
if(responses.empty())
return nullptr;
return responses;
}
// ── MCP method handlers ─────────────────────────────────────────────────────
json McpServer::handleInitialize(const json& msg)
{
json id = msg.value("id", json(nullptr));
json params = msg.value("params", json::object());
if(!params.is_object())
{
return makeError(id, -32602, "Invalid params: initialize params must be an object");
}
if(params.contains("protocolVersion") && !params["protocolVersion"].is_string())
{
return makeError(id, -32602, "Invalid params: protocolVersion must be a string");
}
m_protocolVersion = negotiateProtocolVersion(params);
json result;
result["protocolVersion"] = protocolVersionString(m_protocolVersion);
result["capabilities"]["tools"] = json::object();
result["serverInfo"]["name"] = "renderdoc-mcp";
result["serverInfo"]["version"] = "1.0.0";
m_initializeSeen = true;
return makeResponse(id, result);
}
json McpServer::handleToolsList(const json& msg)
{
json id = msg.value("id", json(nullptr));
json result;
result["tools"] = m_registry->getToolDefinitions();
return makeResponse(id, result);
}
json McpServer::handleToolsCall(const json& msg)
{
json id = msg.value("id", json(nullptr));
json params = msg.value("params", json::object());
std::string toolName = params.value("name", "");
json arguments = params.value("arguments", json::object());
if(toolName.empty())
return makeError(id, -32602, "Invalid params: missing tool name");
try
{
ToolContext ctx{*m_session, *m_diffSession};
json rawResult = m_registry->callTool(toolName, ctx, arguments);
return makeResponse(id, makeToolResult(rawResult));
}
catch(const InvalidParamsError& e)
{
// Protocol-level error: unknown tool, missing required, type mismatch, bad enum
return makeError(id, -32602, std::string("Invalid params: ") + e.what());
}
catch(const core::CoreError& e)
{
// Core-level error: no capture open, invalid event id, etc.
return makeResponse(id, makeToolResult(std::string(e.what()), true));
}
catch(const std::exception& e)
{
// Tool-level error: renderdoc API failure, etc.
return makeResponse(id, makeToolResult(std::string(e.what()), true));
}
}
} // namespace renderdoc::mcp