McpProtocol.h header
#include <ew/app/McpProtocol.h>
Namespace ew::app
McpRequest struct
struct ew::app::McpRequest
One JSON-RPC 2.0 message parsed from a single stdio line (MCP stdio transport: newline-delimited UTF-8 JSON objects). A message without an "id" key is a notification and must never be answered.
Members
bool ew::app::McpRequest::valid = false
True when the line parsed to a JSON-RPC message shape; false leaves parseError set.
QString ew::app::McpRequest::parseError
Why the line failed to parse (when not valid).
bool ew::app::McpRequest::isNotification = false
True when the message carries no "id" (a notification – fire-and-forget).
QJsonValue ew::app::McpRequest::id
The request id to echo in the response (number or string, as the client sent it).
QString ew::app::McpRequest::method
The JSON-RPC method ("initialize", "tools/list", "tools/call", "ping", ...).
QJsonObject ew::app::McpRequest::params
The "params" object (empty when the request carries none).
McpServer class
class ew::app::McpServer
The MCP request router: owns the tool registry and answers the protocol's methods (initialize / tools/list / tools/call / ping), echoing ids and emitting JSON-RPC errors per spec. It performs no I/O of its own – the caller feeds it one line and writes back the returned response line, which is what makes the whole protocol unit-testable (docs/MCP_SERVER.md §2). Registration order is the tools/list order.
Members
ew::app::McpServer::McpServer(QString name, QString version, QString instructions)
A server presenting itself as name / version; instructions (may be empty) is surfaced to the client on initialize to teach the assistant the project idiom.
void ew::app::McpServer::registerTool(McpToolSpec spec, McpToolHandler handler)
Adds spec to the advertised catalogue with handler as its implementation. A re-registered name replaces its handler and keeps the original list position.
std::optional< QString > ew::app::McpServer::handleLine(const QString &line)
Handles one stdio line: returns the single-line response to write back, or nothing when the line must not be answered (a notification, or a blank line).
void ew::app::McpServer::setBeforeToolCall(std::function< void(const QString &toolName)> hook)
Registers a hook run immediately before each dispatched tool call, given the tool's name – after the tool is resolved but before it executes, and for every tool (read or write). The server uses it to reload the project from disk when another writer (the desktop app) changed it, so a read reflects current data and a write never clobbers it (docs/MCP_SERVER.md §4). The router does no I/O itself, which is what keeps it unit-testable; any freshness side effect belongs in this hook. A single hook – re-setting replaces it; passing an empty function clears it.
const std::vector< McpToolSpec > & ew::app::McpServer::tools() const
The advertised tool catalogue, in registration order.
McpToolResult struct
struct ew::app::McpToolResult
The outcome of one tool execution. The text is returned to the client as the tool result's content; ok == false marks it "isError" so the model can correct itself – an honest refusal/failure is a RESULT, never a protocol error (docs/MCP_SERVER.md §2).
Members
bool ew::app::McpToolResult::ok = false
True when the tool did its work (or was a valid no-op).
QString ew::app::McpToolResult::text
The human-readable outcome (compact Markdown, names-first).
McpToolSpec struct
struct ew::app::McpToolSpec
One tool the server advertises via tools/list.
Members
QString ew::app::McpToolSpec::name
The tool's wire name (snake_case, e.g. "create_entity").
QString ew::app::McpToolSpec::description
What the tool does, written for the model that will decide to call it.
QJsonObject ew::app::McpToolSpec::inputSchema
The JSON-Schema object describing the tool's arguments.
Types
using ew::app::McpToolHandler = std::function<McpToolResult(const QJsonObject& arguments)>
Executes one call of a registered tool with its raw "arguments" object.
Functions
QString ew::app::mcpErrorLine(const QJsonValue &id, int code, const QString &message)
One compact single-line JSON-RPC error response with code and message.
QString ew::app::mcpResultLine(const QJsonValue &id, const QJsonObject &result)
One compact single-line JSON-RPC success response: {"jsonrpc":"2.0","id":...,"result":...}.
McpRequest ew::app::parseMcpRequestLine(const QString &line)
Parses one stdio line into a JSON-RPC message. Never throws; a malformed line returns valid == false with the reason in parseError.
Constants
int ew::app::kMcpInvalidParams = -32602
JSON-RPC "invalid params": the method's params were missing or malformed.
int ew::app::kMcpMethodNotFound = -32601
JSON-RPC "method not found": the requested method name is not one the router handles.
int ew::app::kMcpParseError = -32700
JSON-RPC 2.0 error codes the router emits (the protocol's own, not tool failures – a tool that runs and fails reports through McpToolResult::ok so the model can react).
QStringView ew::app::kMcpProtocolVersion = u"2025-06-18"
The MCP protocol revision this server pins when a client requests one it does not know (docs/MCP_SERVER.md §2). A known requested revision is echoed back instead – our tools-only usage is identical across the revisions in kMcpKnownProtocolVersions.