-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmcp_server.cpp
More file actions
204 lines (173 loc) · 6.29 KB
/
mcp_server.cpp
File metadata and controls
204 lines (173 loc) · 6.29 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
#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)
{
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(isError)
result["isError"] = true;
return result;
}
// ── 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 == "initialize")
{
if(m_initialized)
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)
{
// Check for initialize in batch (forbidden by MCP spec)
for(const auto& msg : arr)
{
if(msg.is_object() && msg.value("method", "") == "initialize")
return makeError(nullptr, -32600, "Invalid Request: initialize must not appear in a JSON-RPC batch");
}
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 all were notifications, return nothing
if(responses.empty())
return nullptr;
return responses;
}
// ── MCP method handlers ─────────────────────────────────────────────────────
json McpServer::handleInitialize(const json& msg)
{
json id = msg.value("id", json(nullptr));
// Per MCP spec: always respond with the version we support; the client
// decides whether to continue or disconnect. Strict equality on the
// client's requested version was wrong and breaks newer clients.
static constexpr const char* kSupportedProtocolVersion = "2025-03-26";
json result;
result["protocolVersion"] = kSupportedProtocolVersion;
result["capabilities"]["tools"] = json::object();
result["serverInfo"]["name"] = "renderdoc-mcp";
result["serverInfo"]["version"] = "1.0.0";
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