From 69a937d36f178d9aeb8c1b49c5c3aa060ae1b60c Mon Sep 17 00:00:00 2001 From: f4exb Date: Sat, 24 Oct 2026 12:17:56 +0200 Subject: [PATCH 1/3] Fixed MCP version negotiation --- .../feature/mcpserver/mcprequesthandler.cpp | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/plugins/feature/mcpserver/mcprequesthandler.cpp b/plugins/feature/mcpserver/mcprequesthandler.cpp index 075e3e554..ebc9c5f5b 100644 --- a/plugins/feature/mcpserver/mcprequesthandler.cpp +++ b/plugins/feature/mcpserver/mcprequesthandler.cpp @@ -82,7 +82,10 @@ void MCPRequestHandler::service(qtwebapp::HttpRequest& request, qtwebapp::HttpRe return; } - if (!checkProtocolVersion(request, response)) { + // Check protocol version for non-POST requests + // POST requests check version in handlePost after parsing JSON + // to allow initialize requests to negotiate version + if (method != "POST" && !checkProtocolVersion(request, response)) { return; } @@ -113,6 +116,8 @@ void MCPRequestHandler::service(qtwebapp::HttpRequest& request, qtwebapp::HttpRe // Clients send MCP-Protocol-Version on every request after initialize. A version this server // does not implement is refused with 400 rather than served on a guess, as the Streamable HTTP // transport requires. A missing header means the oldest version, which is supported. +// Note: POST requests are checked in handlePost after JSON parsing to allow initialize +// requests to negotiate version in the JSON body. bool MCPRequestHandler::checkProtocolVersion(qtwebapp::HttpRequest& request, qtwebapp::HttpResponse& response) { QString version = QString::fromUtf8(request.getHeader("MCP-Protocol-Version")).trimmed(); @@ -312,6 +317,46 @@ void MCPRequestHandler::handlePost(qtwebapp::HttpRequest& request, qtwebapp::Htt return; } + // Check protocol version for all requests except initialize + // Initialize negotiates version in the JSON body + QString version = QString::fromUtf8(request.getHeader("MCP-Protocol-Version")).trimmed(); + if (!version.isEmpty() && !MCPProtocol::isSupportedVersion(version)) + { + // Check if this request contains an initialize message + // (allowing initialize to negotiate version even with unsupported version in header) + bool containsInitialize = false; + + if (doc.isArray()) { + // Batch request - check all messages + QJsonArray array = doc.array(); + for (const auto& value : array) { + if (value.isObject()) { + QJsonObject msg = value.toObject(); + if (msg["method"].toString() == "initialize") { + containsInitialize = true; + break; + } + } + } + } else if (doc.isObject()) { + // Single message + QJsonObject obj = doc.object(); + if (obj["method"].toString() == "initialize") { + containsInitialize = true; + } + } + + // Reject requests without initialize that have unsupported protocol version + if (!containsInitialize) { + sendHttpError(response, 400, "Bad Request", + QString("Unsupported MCP protocol version %1. This server supports %2") + .arg(version).arg(MCPProtocol::supportedVersions().join(", "))); + return; + } + // Requests containing initialize with unsupported version in header are allowed + // The version will be negotiated in the initialize method + } + // A batch is an array of messages. Batches were removed in protocol version 2025-06-18 // but older clients may still send them so both forms are handled. QList messages; From 3c56e6f883342a22b6199d2374c38bf0cad633af Mon Sep 17 00:00:00 2001 From: f4exb Date: Sat, 24 Oct 2026 13:25:01 +0200 Subject: [PATCH 2/3] MCP server fix: applied Copilot suggestion --- .../feature/mcpserver/mcprequesthandler.cpp | 33 +++++++------------ 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/plugins/feature/mcpserver/mcprequesthandler.cpp b/plugins/feature/mcpserver/mcprequesthandler.cpp index ebc9c5f5b..24aff20d8 100644 --- a/plugins/feature/mcpserver/mcprequesthandler.cpp +++ b/plugins/feature/mcpserver/mcprequesthandler.cpp @@ -317,43 +317,34 @@ void MCPRequestHandler::handlePost(qtwebapp::HttpRequest& request, qtwebapp::Htt return; } - // Check protocol version for all requests except initialize +// Check protocol version for all requests except initialize // Initialize negotiates version in the JSON body QString version = QString::fromUtf8(request.getHeader("MCP-Protocol-Version")).trimmed(); if (!version.isEmpty() && !MCPProtocol::isSupportedVersion(version)) { - // Check if this request contains an initialize message - // (allowing initialize to negotiate version even with unsupported version in header) - bool containsInitialize = false; + // Check if this is a single initialize request (not a batch) + // Allow initialize to negotiate version even with unsupported version in header, + // but reject batches entirely since they could contain other messages like tools/call + bool isSingleInitialize = false; - if (doc.isArray()) { - // Batch request - check all messages - QJsonArray array = doc.array(); - for (const auto& value : array) { - if (value.isObject()) { - QJsonObject msg = value.toObject(); - if (msg["method"].toString() == "initialize") { - containsInitialize = true; - break; - } - } - } - } else if (doc.isObject()) { + if (doc.isObject()) { // Single message QJsonObject obj = doc.object(); if (obj["method"].toString() == "initialize") { - containsInitialize = true; + isSingleInitialize = true; } } + // Batch requests with unsupported version are always rejected, + // even if they contain only initialize messages - // Reject requests without initialize that have unsupported protocol version - if (!containsInitialize) { + // Reject requests that are not single initialize with unsupported protocol version + if (!isSingleInitialize) { sendHttpError(response, 400, "Bad Request", QString("Unsupported MCP protocol version %1. This server supports %2") .arg(version).arg(MCPProtocol::supportedVersions().join(", "))); return; } - // Requests containing initialize with unsupported version in header are allowed + // Single initialize requests with unsupported version in header are allowed // The version will be negotiated in the initialize method } From 5b7cdf64f4601b36eea0a5a05cf726c6857e08ab Mon Sep 17 00:00:00 2001 From: f4exb Date: Sat, 24 Oct 2026 13:26:24 +0200 Subject: [PATCH 3/3] Do not build server version in Appveyor to save time --- .appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.appveyor.yml b/.appveyor.yml index 8377b1176..28a5698de 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -141,4 +141,4 @@ for: build_script: - cd sdrangel - ./build_vanilla.sh -T ${APPVEYOR_PULL_REQUEST_HEAD_COMMIT:-${APPVEYOR_REPO_COMMIT}} - - ./build_server.sh -T ${APPVEYOR_PULL_REQUEST_HEAD_COMMIT:-${APPVEYOR_REPO_COMMIT}} +# - ./build_server.sh -T ${APPVEYOR_PULL_REQUEST_HEAD_COMMIT:-${APPVEYOR_REPO_COMMIT}}