使用 MCP 客户端
Qt Creator MCP 客户端支持模型上下文协议(MCP),可让人工智能助手控制它来调试、构建和管理项目。
要使用 MCP 客户端,请安装一个并激活Qt Creator MCP 服务器扩展,以启动 MCP 服务器。
查看 MCP 服务器状态
Qt Creator 在激活Qt Creator MCP 服务器扩展时启动 MCP 服务器。服务器运行于127.0.0.1 ,接受3001 端口的连接。出于安全考虑,您不能在远程计算机上运行它。
要查看 MCP 服务器状态,请访问Tools >MCP Server >About MCP Server 。

如果服务器未启动,请查看General Messages 是否显示端口正在使用的警告。
确保没有其他进程绑定到该端口。例如,在 Linux 上,在Terminal 中输入lsof -i :3001 。
要使用3001 以外的其他端口,请设置MCP_SERVER_PORT 环境变量,以便在Qt Creator 启动前配置端口。
设置简单的 MCP 客户端
您可以使用任何可以发送 POST 请求并读取 SSE 响应的 HTTP 客户端。本节介绍如何使用pip 安装一个简单的 MCP 客户端以及如何配置它。
安装和配置简单 MCP 客户端:
- 访问Terminal ,然后输入
pip install simple-mcp-client
- 更改
simple_mcp_client\config.json配置文件,使其指向qtcreatorMCP 服务器:"mcpServers": { "qtcreator": { "enable": false, "type": "sse", "url": "http://127.0.0.1:3001/sse", "command": null, "args": [], "env": {} } }
您可以在以下位置找到config.json 文件:
- 在 Linux 上
~/.config/simple_mcp_client/ - 在 Windows 上
C:\Users\<username>\AppData\Roaming\simple_mcp_client
运行Qt Creator 工具
要从 MCP 客户端运行Qt Creator 工具,请启动客户端并连接到Qt Creator MCP 服务器。
要启动 MCP 客户端并查看Qt Creator 工具列表:
- 访问Terminal ,然后输入:
python -m simple_mcp_client.main
- 在Mini MCP Client Network Hub 中,输入
connect qtcreator
- 要查看工具列表,请输入
tools qtcreator

- 要运行工具,请输入
execute qtcreator <tool-name>
例如
execute qtcreator list_sessions
如果 SSE 连接失败,请确保 MCP 客户端支持跨源资源共享 (CORS)。这是一种基于 HTTP 头信息的机制,允许服务器为其他域、方案或端口加载资源。
在 Windows 上,防火墙可能会阻止localhost 的入站连接。请确保Qt Creator 可执行文件可以接受本地流量。
如果调试会话没有在Qt Creator 退出时停止,可以在Debug 模式下手动停止调试,或通过将MCP_QUIT_TIMEOUT 环境变量设置为 60 秒来增加超时时间。
工具汇总
下表总结了服务器注册的Qt Creator 工具。每个工具都是一个 JSON-RPC 方法,都有一个名称、人类可读的标题、描述、输入和输出 JSON 模式,以及表示工具是否只读的标志。
| 名称 | 标题 | 描述 | 参数 | 结果 | 只读 |
|---|---|---|---|---|---|
get_build_status | 获取当前构建状态 | 返回对当前构建活动的简短文字描述。 | 空对象:{} | "result": "string" | 是 |
open_file | 打开文件Qt Creator | 在编辑器中打开给定文件(URI)。文件不会自动保存。 | path:URI 字符串或绝对文件路径 | "success": true|false | 无 |
file_plain_text | 读取纯文本文件 | 返回文件的原始文本内容。 | path:URI | "success": "text": "<plain_text>" | 是 |
set_file_plain_text | 将纯文本写入文件 | 用提供的文本覆盖文件。如果文件在编辑器中打开,则不会自动保存更改。 | path:URI
| "success": true|false | 不保存 |
save_file | 保存文件 | 如果编辑器文件被修改,则保存该文件。 | path:URI | "success": true|false | 无 |
close_file | 关闭文件 | 关闭已打开的文件。 | path:URI | "success": true|false | 无 |
list_projects | 列出所有可用项目 | 返回项目显示名称数组。 | 空 | "projects": ["MyApp", "LibA", ...] | 空 |
list_build_configs | 列出可用的构建配置 | 返回活动项目的构建配置名称数组。 | 空 | "buildConfigs": ["Debug", "Release"] | 是 |
switch_build_config | 切换到特定构建配置 | 为当前项目激活指定的构建配置。 | name:配置名称为字符串 | "success": true|false | 无 |
list_open_files | 列出当前打开的文件 | 返回一个绝对文件路径数组。 | 空 | "openFiles": ["/path/to/file1.cpp", ...] | 空 |
list_sessions | 列出可用的Qt Creator 会话 | 返回会话名称数组。 | 空 | "sessions": ["default", "work", "debug"] | 是 |
load_session | 加载特定会话 | 异步切换到指定的会话。 | sessionName会话名称为字符串 | "success": true|false | 空 |
save_session | 保存当前会话 | 将当前会话保存到磁盘。 | Empty | "success": true|false | 无 |
list_issues | 列出当前问题(警告和错误) | 从Issues 返回问题字符串数组。 | Empty | "issues": ["error: <string>", "warning: <string>"] | 是 |
quit | 退出Qt Creator | 执行优雅关闭(停止调试、保存会话并调用QApplication::quit() )。 | Empty | "success": true|false | 无 |
get_current_project | 获取当前活动项目 | 返回启动项目的显示名称。 | 空 | "project": "MyApp" | 是 |
get_current_build_config | 获取活动构建配置 | 返回当前所选构建配置的名称。 | 空 | "buildConfig": "Debug" | 是 |
get_current_session | 获取活动会话名称 | 返回当前加载的会话名称。 | Empty | "session": "default" | 是 |
除静态工具外,插件还注册了动态工具,用于触发最常见的Qt Creator 操作:
run_project触发Run 操作。build触发Build 操作。clean_project触发Clean 操作。debug触发Debug 操作。
动态工具接收一个空的params 对象,并返回{ "success": true|false } 。
添加工具
MCP 服务器扩展使用Core::ActionManager 创建名称如表所示的工具。该实现通过McpServer::runOnGuiThread() 在用户界面线程上调用QAction::trigger() 。
因此,您可以在McpServer::initializeToolsForCommands() 中添加一个小包装,将通过ActionManager 公开的任何其他命令变成 MCP 工具。
另请参阅 激活扩展。
Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.