Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore/Audacity CLA applies
4 : *
5 : * Copyright (C) 2026 MuseScore/Audacity and others
6 : *
7 : * This program is free software: you can redistribute it and/or modify
8 : * it under the terms of the GNU General Public License version 3 as
9 : * published by the Free Software Foundation.
10 : *
11 : * This program is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 : */
19 :
20 : #include "mcpcontroller.h"
21 :
22 : #include "mcpserver.h"
23 :
24 : #include "log.h"
25 :
26 : using namespace muse::rcontrol;
27 : using namespace muse::rcontrol::mcp;
28 : using namespace muse::rcommand;
29 :
30 0 : static TypeSchema toMcpDataTypeSchema(muse::rcommand::DataType type)
31 : {
32 : using CT = muse::rcommand::DataType;
33 : using MT = muse::rcontrol::mcp::DataType;
34 : using MF = muse::rcontrol::mcp::Format;
35 :
36 0 : switch (type) {
37 0 : case CT::Undefined: return TypeSchema{ MT::Undefined, MF::Undefined };
38 0 : case CT::String: return TypeSchema{ MT::String, MF::Undefined };
39 0 : case CT::Enum: return TypeSchema{ MT::String, MF::Undefined };
40 0 : case CT::Int: return TypeSchema{ MT::Integer, MF::Undefined };
41 0 : case CT::Int64: return TypeSchema{ MT::String, MF::Int64 };
42 0 : case CT::Float: return TypeSchema{ MT::Number, MF::Float };
43 0 : case CT::Double: return TypeSchema{ MT::Number, MF::Double };
44 0 : case CT::Bool: return TypeSchema{ MT::Boolean, MF::Undefined };
45 0 : case CT::List: return TypeSchema{ MT::Array, MF::Undefined };
46 0 : case CT::Map: return TypeSchema{ MT::Object, MF::Undefined };
47 : }
48 0 : return TypeSchema{ MT::Undefined, MF::Undefined };
49 : }
50 :
51 0 : static mcp::Annotations toMcpAnnotations(const muse::rcommand::Annotations& annotations)
52 : {
53 0 : mcp::Annotations result;
54 0 : result.readOnly = annotations.testFlag(muse::rcommand::Annotation::ReadOnly);
55 0 : result.destructive = annotations.testFlag(muse::rcommand::Annotation::Destructive);
56 0 : result.idempotent = annotations.testFlag(muse::rcommand::Annotation::Idempotent);
57 0 : result.openWorld = annotations.testFlag(muse::rcommand::Annotation::OpenWorld);
58 0 : return result;
59 : }
60 :
61 0 : McpController::McpController(const modularity::ContextPtr& iocCtx)
62 0 : : Contextable(iocCtx)
63 : {
64 0 : }
65 :
66 0 : McpController::~McpController()
67 : {
68 0 : deinit();
69 0 : }
70 :
71 : //! NOTE The tool name must be in the format: group_name
72 0 : static std::string commandToToolName(const Command& command)
73 : {
74 0 : std::string path = command.path();
75 0 : std::replace(path.begin(), path.end(), '/', '.');
76 0 : return path;
77 : }
78 :
79 0 : static Command toolNameToCommand(const std::string& name)
80 : {
81 0 : std::string path = name;
82 0 : std::replace(path.begin(), path.end(), '.', '/');
83 0 : return Command(std::string(COMMAND_SCHEME), path);
84 0 : }
85 :
86 0 : void McpController::init()
87 : {
88 0 : m_mcpServer = std::make_unique<McpServer>(application()->version().toStdString());
89 :
90 0 : m_mcpServer->onToolsListRequest([this](const McpServer::ToolsListResultHandler& onResult) {
91 0 : std::vector<Tool> tools = makeToolsList();
92 0 : onResult(tools);
93 0 : });
94 :
95 0 : m_mcpServer->onToolsCallRequest([this](const std::string& name,
96 : const std::map<std::string, Val>& args,
97 : const McpServer::ToolsCallResultHandler& onResult)
98 : {
99 0 : LOGDA() << "Tools call: " << name;
100 :
101 0 : async::Promise<Response> promise = commandsDispatcher()->dispatch(toolNameToCommand(name), args);
102 0 : promise.onResolve(this, [onResult](const Response& response) {
103 0 : ToolResult result;
104 0 : result.ret = response.ret;
105 0 : result.result = response.result;
106 0 : onResult(result);
107 0 : });
108 0 : });
109 :
110 0 : m_mcpServer->init();
111 0 : }
112 :
113 0 : void McpController::deinit()
114 : {
115 0 : if (m_mcpServer) {
116 0 : m_mcpServer->deinit();
117 0 : m_mcpServer = nullptr;
118 : }
119 0 : }
120 :
121 0 : std::vector<Tool> McpController::makeToolsList() const
122 : {
123 0 : std::vector<Tool> tools;
124 0 : auto commandList = commandsRegister()->commandInfoList();
125 0 : tools.reserve(commandList.size());
126 0 : for (const auto& info : commandList) {
127 0 : if (!info.availabilities.testFlag(Availability::MCP)) {
128 0 : continue;
129 : }
130 0 : Tool tool;
131 0 : tool.name = commandToToolName(info.command);
132 0 : tool.title = info.title.raw().translated().toStdString();
133 0 : tool.description = info.description.translated().toStdString();
134 :
135 0 : auto propertiesSchema = [](const ArgsSchema& schema) -> PropertiesSchema {
136 0 : PropertiesSchema result;
137 0 : for (const auto& [name, arg] : schema.args) {
138 0 : Property property;
139 0 : property.type = toMcpDataTypeSchema(arg.dataType);
140 0 : property.description = arg.description.translated().toStdString();
141 0 : property.enumValues = arg.allowedValues;
142 0 : property.minimum = arg.min;
143 0 : property.maximum = arg.max;
144 0 : result.properties[name] = property;
145 :
146 0 : if (arg.presence == Presence::Required) {
147 0 : result.required.push_back(name);
148 : }
149 :
150 : // check if the property is an enum
151 0 : if (arg.dataType == rcommand::DataType::Enum) {
152 0 : DO_ASSERT(arg.allowedValues && arg.allowedValues->size() > 0);
153 : }
154 0 : }
155 0 : return result;
156 0 : };
157 :
158 0 : tool.inputSchema = propertiesSchema(info.inputSchema);
159 0 : tool.outputSchema = propertiesSchema(info.outputSchema);
160 0 : tool.annotations = toMcpAnnotations(info.annotations);
161 :
162 0 : tools.push_back(std::move(tool));
163 0 : }
164 0 : return tools;
165 0 : }
|