test(chat): MCP 工具循环端到端测试

- tests/api/fake_openai.py:假 OpenAI 流式接口,先返回 tool_call 再返回文本
- tests/chattest.cpp:真实 ChatClient + McpManager 走完 请求→工具→回填→回答
- fix(mcp): 回调应收到 JSON-RPC 的 result,而不是整个响应包(否则工具结果为空)
- 两个无头测试均通过
This commit is contained in:
2026-10-30 20:25:37 +08:00
parent ba7f57c7d5
commit 46263e70d7
4 changed files with 254 additions and 6 deletions
+11 -6
View File
@@ -72,10 +72,7 @@ void McpServer::start()
const int toolsId = m_nextId++;
m_callbacks.insert(toolsId, [this](const QJsonObject &response) {
m_tools = response.value(QStringLiteral("result"))
.toObject()
.value(QStringLiteral("tools"))
.toArray();
m_tools = response.value(QStringLiteral("tools")).toArray();
m_ready = true;
Q_EMIT readyChanged();
});
@@ -119,8 +116,16 @@ void McpServer::handleLine(const QByteArray &line)
if (object.contains(QStringLiteral("id"))) {
const int id = object.value(QStringLiteral("id")).toInt(-1);
auto callback = m_callbacks.take(id);
if (callback) {
callback(object);
if (!callback) {
return;
}
if (object.contains(QStringLiteral("error"))) {
const QJsonObject error = object.value(QStringLiteral("error")).toObject();
callback(QJsonObject{ { QStringLiteral("error"),
error.value(QStringLiteral("message"))
.toString(QStringLiteral("MCP 调用失败")) } });
} else {
callback(object.value(QStringLiteral("result")).toObject());
}
}
}