MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / handleResourcesRead

Method handleResourcesRead

app/src/API/MCPHandler.cpp:388–424  ·  view source on GitHub ↗

* @brief Handle resources/read request */

Source from the content-addressed store, hash-verified

386 * @brief Handle resources/read request
387 */
388API::MCP::MCPResponse API::MCPHandler::handleResourcesRead(const MCP::MCPRequest& request)
389{
390 const auto uri = request.params.value(QStringLiteral("uri")).toString();
391
392 if (uri.isEmpty()) {
393 return MCP::MCPResponse::makeError(
394 request.id, MCP::ErrorCode::InvalidParams, QStringLiteral("Missing resource URI"));
395 }
396
397 QMutexLocker locker(&m_frameMutex);
398
399 QJsonObject content;
400 content[QStringLiteral("mimeType")] = QStringLiteral("application/json");
401
402 if (uri == QStringLiteral("serialstudio://frame/current")) {
403 content[QStringLiteral("uri")] = uri;
404 const auto frameJson = DataModel::serialize(m_currentFrame);
405 content[QStringLiteral("text")] =
406 QString::fromUtf8(QJsonDocument(frameJson).toJson(QJsonDocument::Indented));
407 } else if (uri == QStringLiteral("serialstudio://frame/history")) {
408 QJsonArray history;
409 for (const auto& frame : std::as_const(m_frameHistory))
410 history.append(DataModel::serialize(frame));
411
412 content[QStringLiteral("uri")] = uri;
413 content[QStringLiteral("text")] =
414 QString::fromUtf8(QJsonDocument(history).toJson(QJsonDocument::Indented));
415 } else {
416 return MCP::MCPResponse::makeError(
417 request.id, MCP::ErrorCode::InvalidParams, QStringLiteral("Unknown resource: %1").arg(uri));
418 }
419
420 QJsonObject result;
421 result[QStringLiteral("contents")] = QJsonArray{content};
422
423 return MCP::MCPResponse::makeSuccess(request.id, result);
424}
425
426/**
427 * @brief Handle resources/subscribe request

Callers

nothing calls this directly

Calls 5

serializeFunction · 0.85
isEmptyMethod · 0.80
valueMethod · 0.45
toJsonMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected