Qt MCP is a Qt-based implementation of the Model Context Protocol, providing a robust framework for communication between clients and servers in model-driven applications. This library is part of the Qt framework, offering seamless integration with Qt applications.
- Overview
- Requirements
- Project Structure
- Features
- Building from Source
- Getting Started
- Development
- Testing
- Protocol Specification
- License
- Contributing
The Model Context Protocol (MCP) defines a standardized way for applications to communicate with language models and other AI services. This Qt implementation provides:
- Client-server communication framework
- Resource management
- Tool execution capabilities
- Prompt template handling
- Real-time notifications
- Progress tracking
- Logging facilities
- Experimental features support
- Qt 6.8.1 or later
- CMake 3.16.0 or later
- C++20 compatible compiler
- Qt Core
- Qt BuildInternals
- Qt Gui (for Image support)
- Qt Widgets (for GUI examples)
- Qt Network (for HTTP-based communication)
.
├── src/
│ ├── mcpclient/ # Client implementation
│ ├── mcpcommon/ # Shared components and protocol definitions
│ ├── mcpserver/ # Server implementation
│ └── plugins/ # Plugin implementations
│ ├── mcpclientbackend/ # Client backend plugins
│ └── mcpserverbackend/ # Server backend plugins
├── examples/ # Example implementations
│ ├── mcpclient/ # Client examples
│ │ └── inspector/ # MCP Inspector GUI
│ └── mcpserver/ # Server examples
│ ├── echo/ # Echo server example
│ └── window/ # Window server example
├── tests/ # Test suite
└── spec/ # Protocol specification
└── schema.json # JSON Schema definition
- Resource access and management
- Tool execution with schema validation
- Progress tracking and cancellation
- Prompt template handling
- Real-time notifications
- Customizable logging levels
- Experimental features support
- Sampling capabilities
- Resource provision and template support
- Tool hosting with input validation
- Prompt template management
- Progress reporting and cancellation
- Logging facilities with severity levels
- Subscription management
- HTTP server capabilities
- Experimental features
- JSON-RPC based communication
- URI-based resource identification
- Template-based resource access
- Tool execution framework with schema validation
- Progress notification system with cancellation support
- Logging with configurable severity levels
- Experimental features framework
- Sampling and model hints
- Clone the repository:
git clone https://gitlab.com/signal-slot/qtmcp.git
cd qtmcp
- Create a build directory:
mkdir build && cd build
- Configure with CMake:
cmake -DCMAKE_PREFIX_PATH=/path/to/qt \
-DBUILD_EXAMPLES=ON \
-DBUILD_TESTING=ON ..
- Build the project:
cmake --build . --parallel
- Optionally run tests:
ctest --output-on-failure
The project includes example applications that demonstrate both client and server implementations of the MCP protocol:
Located in examples/mcpclient/inspector/
, the MCP Inspector provides a comprehensive GUI for:
- Connection management with MCP servers
- Tool execution and monitoring
- Resource browsing and management
- Prompt template listing and execution
- Real-time protocol inspection
- Experimental features testing
Example usage:
#include <QMcpClient>
// Create and configure the client
QMcpClient client;
client.setServerUrl(QUrl("mcp://localhost:8080"));
// Connect to server
client.connectToServer();
// Execute a tool
QJsonObject args;
args["input"] = "Hello, World!";
client.callTool("echo", args);
Located in examples/mcpserver/echo/
, this example demonstrates:
- Basic MCP server implementation
- Standard input/output communication
- Simple request-response pattern
- Resource template usage
Example implementation:
#include <QMcpServer>
class EchoServer : public QMcpServer
{
Q_OBJECT
public:
void handleToolCall(const QString &tool, const QJsonObject &args) override
{
if (tool == "echo") {
emit toolResult(args["input"].toString());
}
}
};
Located in examples/mcpserver/window/
, this example shows:
- GUI-based MCP server implementation
- Window management capabilities
- Complex resource handling
- Tool implementation patterns
mkdir build && cd build
cmake -DBUILD_EXAMPLES=ON ..
cmake --build . --parallel
The example executables will be available in the build directory under their respective paths.
Qt MCP supports a plugin-based architecture for both client and server backends:
-
Client Plugins (
src/plugins/mcpclientbackend/
):- Implement
QMcpClientBackendInterface
- Handle client-side communication
- Support different transport mechanisms
- Experimental features support
- Implement
-
Server Plugins (
src/plugins/mcpserverbackend/
):- Implement
QMcpServerBackendInterface
- Handle server-side communication
- Support custom resource and tool implementations
- HTTP server capabilities
- Implement
The project uses a modular CMake build system:
.cmake.conf
: Global configuration variablesconfigure.cmake
: Build system configurationqt_cmdline.cmake
: Command-line build options- Component-specific configuration in
src/*/configure.cmake
Tools can be implemented by creating a new tool definition that follows the MCP schema. The tool definition includes:
- Name and description
- Input schema specification (JSON Schema)
- Implementation of the tool's functionality
- Optional experimental features
Example tool definition:
QMcpTool tool;
tool.setName("calculator");
tool.setDescription("Performs basic arithmetic operations");
tool.setInputSchema(R"({
"type": "object",
"properties": {
"operation": {
"type": "string",
"enum": ["add", "subtract", "multiply", "divide"]
},
"operands": {
"type": "array",
"items": { "type": "number" },
"minItems": 2
}
},
"required": ["operation", "operands"]
})");
Resources are identified by URIs and can be:
- Static resources with direct URIs
- Dynamic resources using URI templates
- Blob and text content types
- Subscribed to for real-time updates
The protocol provides comprehensive error handling through:
- Standard error codes
- Detailed error messages
- Progress notifications with cancellation
- Logging with configurable severity levels
- JSON-RPC error responses
The project includes a comprehensive test suite:
- Unit Tests: Located in
tests/auto/
- Client tests (
tests/auto/mcpclient/
)- QMcpClient functionality
- Backend interface testing
- Plugin system verification
- Common component tests (
tests/auto/mcpcommon/
) - Server tests (
tests/auto/mcpserver/
)
- Client tests (
Run the tests using:
cd build
ctest --output-on-failure
The Model Context Protocol is defined using JSON Schema (see spec/schema.json
). Key components include:
-
Message Types:
- Requests (client to server and vice versa)
- Notifications (asynchronous events)
- Results (responses to requests)
-
Core Concepts:
- Resources: Identified by URIs, can be static or template-based
- Tools: Server-provided functions with defined input schemas
- Prompts: Template-based message generation
- Roles: User and assistant roles in communication
- Experimental Features: Optional protocol extensions
Schema Structure:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Request": {
"type": "object",
"properties": {
"jsonrpc": { "const": "2.0" },
"method": { "type": "string" },
"params": { "type": "object" },
"id": { "type": ["string", "number"] }
},
"required": ["jsonrpc", "method", "id"]
},
"Response": {
"type": "object",
"properties": {
"jsonrpc": { "const": "2.0" },
"result": { "type": "object" },
"error": { "$ref": "#/definitions/Error" },
"id": { "type": ["string", "number"] }
},
"required": ["jsonrpc", "id"]
}
}
}
Copyright (C) 2025 Signal Slot Inc. Licensed under:
- LGPL-3.0-only OR
- GPL-2.0-only OR
- GPL-3.0-only
For more information about contributing to Qt MCP, please visit Signal Slot's website.