14#include <Poco/Dynamic/Var.h>
15#include <Poco/JSON/Array.h>
16#include <Poco/JSON/Object.h>
17#include <Poco/JSON/Parser.h>
29std::string textPayload(Poco::JSON::Object::Ptr
object,
bool isError =
false) {
33std::string errorPayload(
const std::string&
message,
const std::string& hint = {},
34 const std::vector<std::string>& nextTools = {}) {
38std::string unavailablePayload(
const char* capability) {
40 std::string(capability) +
" is not provided by the loaded modules",
41 "Load the platform_event (and graphics for capture) modules, or start a live eve run session",
false,
43 out->set(
"unavailable", capability);
44 return textPayload(
out);
57std::int64_t g_nextId = 1;
58std::vector<PendingBatch> g_pending;
59std::vector<Poco::JSON::Object::Ptr> g_receipts;
61Poco::JSON::Object::Ptr findReceipt(std::int64_t
id) {
62 for (
auto& receipt : g_receipts) {
63 if (receipt && receipt->has(
"id") && receipt->getValue<Poco::Int64>(
"id") ==
id)
return receipt;
69 const auto* diag =
status.primaryDiagnostic();
70 if (diag && !diag->message().empty())
return diag->message();
74std::string applyOneAction(Poco::JSON::Object::Ptr action, Poco::JSON::Object::Ptr receipt) {
75 if (!action || !
action->has(
"op"))
return "action.op is required";
78 op =
action->get(
"op").convert<std::string>();
80 return "action.op must be a string";
83 auto*
input = eve::cap::query<eve::IInputInjection>();
84 auto* ui = eve::cap::query<eve::IUIAutomation>();
87 const int frames = std::max(0,
getArgInt(action,
"frames", 1));
88 receipt->set(
"waitFrames", receipt->has(
"waitFrames")
89 ? receipt->getValue<
int>(
"waitFrames") + frames
97 const bool down =
getArgBool(action,
"down",
true);
98 const bool repeat =
getArgBool(action,
"repeat",
false);
103 if (op ==
"mouse_move") {
110 if (op ==
"mouse_button") {
129 if (op ==
"ui_click") {
133 if (
widget.empty())
return "ui_click requires widget";
135 if (
out.rfind(
"error:", 0) == 0)
return out.substr(6);
138 return "unknown action.op '" + op +
"' (key|mouse_move|mouse_button|wheel|text|ui_click|wait)";
141void finishCapture(PendingBatch& batch) {
142 Poco::JSON::Object::Ptr
out = batch.result ? batch.result : Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
143 out->set(
"id",
static_cast<Poco::Int64
>(batch.id));
144 out->set(
"status",
"ready");
145 if (!batch.wantCapture) {
146 out->set(
"ok",
true);
151 auto* capture = eve::cap::query<eve::IRenderCapture>();
153 out->set(
"ok",
false);
155 out->set(
"hint",
"Wait until graphics has presented a frame, then eve_screenshot / eve_input_poll");
156 Poco::JSON::Array::Ptr
next = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
157 next->add(
"eve_render_status");
158 next->add(
"eve_screenshot");
159 out->set(
"nextTools", next);
164 std::string
path = batch.capturePath.empty() ?
"mcp_input_capture.png" : batch.capturePath;
165 auto saved = capture->savePng(
path);
167 const auto&
status = saved.status();
168 out->set(
"ok",
false);
169 out->set(
"retryable",
true);
170 out->set(
"reason",
"no-presented-frame");
171 out->set(
"error", diagnosticMessage(
status));
172 out->set(
"hint",
"Call eve_input_poll again after one rendered frame, or use eve_screenshot");
173 Poco::JSON::Array::Ptr
next = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
174 next->add(
"eve_input_poll");
175 next->add(
"eve_screenshot");
176 out->set(
"nextTools", next);
181 out->set(
"ok",
true);
182 out->set(
"captured",
true);
184 out->set(
"width", saved.value().width);
185 out->set(
"height", saved.value().height);
190std::string inputStatus() {
191 Poco::JSON::Object::Ptr
out = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
192 out->set(
"ok",
true);
193 out->set(
"injection", eve::cap::query<eve::IInputInjection>() !=
nullptr);
194 out->set(
"ui", eve::cap::query<eve::IUIAutomation>() !=
nullptr);
195 out->set(
"capture", eve::cap::query<eve::IRenderCapture>() !=
nullptr);
196 std::lock_guard lock(g_mu);
197 out->set(
"pending",
static_cast<int>(g_pending.size()));
198 out->set(
"completed",
static_cast<int>(g_receipts.size()));
199 return textPayload(
out);
202std::string inputPoll(Poco::JSON::Object::Ptr args) {
203 const std::int64_t
id =
getArgInt64(args,
"id", 0);
204 std::lock_guard lock(g_mu);
206 if (
auto receipt = findReceipt(
id))
return textPayload(receipt);
207 for (
auto& batch : g_pending) {
208 if (batch.id ==
id) {
209 Poco::JSON::Object::Ptr
out = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
210 out->set(
"ok",
true);
211 out->set(
"status",
"pending");
212 out->set(
"id",
static_cast<Poco::Int64
>(
id));
213 out->set(
"framesLeft", batch.framesLeft);
214 out->set(
"hint",
"Keep calling eve_input_poll each frame until status=ready");
215 Poco::JSON::Array::Ptr
next = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
216 next->add(
"eve_input_poll");
217 out->set(
"nextTools", next);
218 return textPayload(
out);
221 return errorPayload(
"unknown input batch id " + std::to_string(
id),
222 "Call eve_input_batch first and reuse the returned id", {
"eve_input_batch"});
224 if (!g_receipts.empty())
return textPayload(g_receipts.back());
225 if (!g_pending.empty()) {
226 Poco::JSON::Object::Ptr
out = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
227 out->set(
"ok",
true);
228 out->set(
"status",
"pending");
229 out->set(
"id",
static_cast<Poco::Int64
>(g_pending.back().id));
230 out->set(
"framesLeft", g_pending.back().framesLeft);
231 out->set(
"hint",
"Keep calling eve_input_poll each frame until status=ready");
232 return textPayload(
out);
234 return errorPayload(
"no input batch to poll",
"Submit actions with eve_input_batch", {
"eve_input_batch"});
237std::string inputBatch(Poco::JSON::Object::Ptr args) {
238 if (!args || !args->has(
"actions")) {
239 return errorPayload(
"actions array is required",
240 "Pass actions:[{op:key|mouse_move|mouse_button|wheel|text|ui_click|wait},...]",
241 {
"eve_input_status"});
243 Poco::JSON::Array::Ptr
actions;
245 actions = args->getArray(
"actions");
247 return errorPayload(
"actions must be a JSON array");
249 if (!
actions ||
actions->size() == 0)
return errorPayload(
"actions must not be empty");
251 if (!eve::cap::query<eve::IInputInjection>() && !eve::cap::query<eve::IUIAutomation>()) {
255 Poco::JSON::Object::Ptr receipt = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
256 receipt->set(
"injected", 0);
257 Poco::JSON::Array::Ptr
steps = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
260 for (std::size_t i = 0; i <
actions->size(); ++i) {
261 Poco::JSON::Object::Ptr
action;
265 return errorPayload(
"actions[" + std::to_string(i) +
"] must be an object");
267 const int beforeWait = receipt->has(
"waitFrames") ? receipt->getValue<
int>(
"waitFrames") : 0;
268 const std::string err = applyOneAction(action, receipt);
269 Poco::JSON::Object::Ptr
step = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
270 step->set(
"index",
static_cast<int>(i));
273 step->set(
"ok",
false);
274 step->set(
"error", err);
276 receipt->set(
"ok",
false);
277 receipt->set(
"error", err);
278 receipt->set(
"steps",
steps);
279 receipt->set(
"hint",
"Fix the failing action; key names use SDL_GetKeyName spelling");
280 Poco::JSON::Array::Ptr
next = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
281 next->add(
"eve_input_status");
282 receipt->set(
"nextTools", next);
283 return textPayload(receipt,
true);
285 step->set(
"ok",
true);
287 const int afterWait = receipt->has(
"waitFrames") ? receipt->getValue<
int>(
"waitFrames") : 0;
288 if (afterWait == beforeWait) receipt->set(
"injected", receipt->getValue<
int>(
"injected") + 1);
289 waitFrames = afterWait;
295 waitFrames += std::max(0,
getArgInt(args,
"waitFrames", 0));
297 receipt->set(
"steps",
steps);
298 receipt->set(
"waitFrames", waitFrames);
302 receipt->set(
"ok",
true);
303 receipt->set(
"status",
"ready");
304 receipt->set(
"hint",
"Events are queued for the next PlatformEvent::pump; compose with eve_play step "
305 "or waitFrames/capture for a closed loop");
306 Poco::JSON::Array::Ptr
next = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
307 next->add(
"eve_play");
308 next->add(
"eve_screenshot");
309 receipt->set(
"nextTools", next);
310 return textPayload(receipt);
315 std::lock_guard lock(g_mu);
316 batch.id = g_nextId++;
317 batch.framesLeft = waitFrames;
320 batch.result = receipt;
321 if (batch.framesLeft <= 0) {
322 finishCapture(batch);
323 g_receipts.push_back(batch.result);
324 return textPayload(batch.result);
326 g_pending.push_back(batch);
329 receipt->set(
"ok",
true);
330 receipt->set(
"status",
"pending");
331 receipt->set(
"id",
static_cast<Poco::Int64
>(batch.id));
332 receipt->set(
"hint",
"Call eve_input_poll after each game frame until status=ready");
333 Poco::JSON::Array::Ptr
next = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
334 next->add(
"eve_input_poll");
335 receipt->set(
"nextTools", next);
336 return textPayload(receipt);
342 return name ==
"eve_input_batch" ||
name ==
"eve_input_poll" ||
name ==
"eve_input_status";
346 if (
name ==
"eve_input_status")
return inputStatus();
347 if (
name ==
"eve_input_poll")
return inputPoll(args);
348 if (
name ==
"eve_input_batch")
return inputBatch(args);
349 return errorPayload(
"unknown input tool '" + std::string(
name) +
"'");
353 return R
"json({"name":"eve_input_status","description":"Whether IInputInjection / IUIAutomation / IRenderCapture are available and how many input batches are pending.","inputSchema":{"type":"object","properties":{}}},{"name":"eve_input_batch","description":"Inject a playtest action sequence (key/mouse_move/mouse_button/wheel/text/ui_click/wait) through IInputInjection + IUIAutomation. Optional waitFrames/capture completes across subsequent McpServer::poll ticks — call eve_input_poll until status=ready. Does not block the game loop.","inputSchema":{"type":"object","properties":{"actions":{"type":"array","items":{"type":"object","properties":{"op":{"type":"string","enum":["key","mouse_move","mouse_button","wheel","text","ui_click","wait"]},"key":{"type":"string"},"down":{"type":"boolean"},"repeat":{"type":"boolean"},"button":{"type":"integer"},"x":{"type":"number"},"y":{"type":"number"},"dx":{"type":"number"},"dy":{"type":"number"},"text":{"type":"string"},"host":{"type":"string"},"widget":{"type":"string"},"frames":{"type":"integer"}},"required":["op"]}},"waitFrames":{"type":"integer","minimum":0},"capture":{"type":"boolean"},"capturePath":{"type":"string"}},"required":["actions"]}},{"name":"eve_input_poll","description":"Poll a pending eve_input_batch wait/capture. Pass id from the batch response (omit to use the latest). Returns status=pending|ready with optional PNG path.","inputSchema":{"type":"object","properties":{"id":{"type":"integer","minimum":1}}}})json";
357 std::vector<PendingBatch> finished;
359 std::lock_guard lock(g_mu);
360 for (
auto& batch : g_pending) {
361 if (batch.framesLeft > 0) --batch.framesLeft;
362 if (batch.framesLeft <= 0) {
363 finishCapture(batch);
364 finished.push_back(batch);
367 g_pending.erase(std::remove_if(g_pending.begin(), g_pending.end(),
368 [](
const PendingBatch&
b) { return b.framesLeft <= 0 && b.done; }),
370 for (
auto& batch : finished) g_receipts.push_back(batch.result);
371 constexpr std::size_t kMaxReceipts = 32;
372 if (g_receipts.size() > kMaxReceipts) {
373 g_receipts.erase(g_receipts.begin(), g_receipts.end() -
static_cast<std::ptrdiff_t
>(kMaxReceipts));
379 std::lock_guard lock(g_mu);
wgpu::PopErrorScopeStatus status
std::vector< ActionSpec > actions
Move-only, checked operation results for the common layer.
Structured operation status used by the common Result foundation.
static constexpr const char * capabilityName
static constexpr const char * capabilityName
Structured status and zero or more diagnostics for an operation.
bool isMcpInputTool(std::string_view name)
Whether name belongs to the input playtest tool family.
bool getArgBool(Poco::JSON::Object::Ptr args, const char *key, bool def=false)
Boolean argument, or def when absent or not convertible.
float getArgFloat(Poco::JSON::Object::Ptr args, const char *key, float def=0.f)
Floating-point argument, or def when absent or not convertible.
std::string callMcpInputTool(std::string_view name, Poco::JSON::Object::Ptr args)
Execute one input tool.
std::string mcpStringify(const Poco::Dynamic::Var &value)
Compact single-line JSON text for a newline-framed MCP payload.
std::string textContentResult(const std::string &text, bool isError=false)
MCP tools/call result object carrying one text content item.
void resetMcpInputBatchesForTest()
Drop pending batches (tests / server stop).
std::string_view mcpInputToolSchemas()
Comma-free MCP tool schema fragments for the input family.
Poco::JSON::Object::Ptr mcpFailureObject(const std::string &error, const std::string &hint={}, bool retryable=false, const std::vector< std::string > &nextTools={})
Structured tool failure object for agent self-correction.
void pollMcpInputBatches()
Advance pending wait/capture batches once per game-loop poll. @thread MCP poll / game-loop thread onl...
long long getArgInt64(Poco::JSON::Object::Ptr args, const char *key, long long def=0)
64-bit integer argument (sequence numbers, handles), or def.
int getArgInt(Poco::JSON::Object::Ptr args, const char *key, int def=0)
Integer argument, or def when absent or not convertible.
std::string getArgString(Poco::JSON::Object::Ptr args, const char *key, const std::string &def={})
String argument, or def when absent or not convertible.
constexpr HexDirection next(HexDirection d) noexcept
The next direction clockwise (NW wraps to NE).
constexpr std::string_view statusCodeName(StatusCode code) noexcept
Return the stable protocol spelling of a status code.