跳到主要内容

Qubix tools from code: QubApp.mcp

QubApp.mcp lets code do inside Qubix what you do by hand in the panel: read the dashboard and the lists, save a tracker campaign, pause an ad, edit a Britva rule. These are the same tools an external program reaches over MCP/API. Code calls them right on the server and with no key: the call runs with the rights of the code's owner.

The shortest example​

JavaScript
function main() {
const who = QubApp.mcp.mcp_whoami();
console.log('running as', who.email, '· administrator:', who.is_admin);
}

QubApp.mcp has one method for every tool the owner may use. The method is named after the tool — get_dashboard, get_offers_list, campaign_save, ad_pause and so on — takes an object with the same arguments as over MCP and returns the tool's answer as a ready object: there is nothing to parse. The call is synchronous: the answer is there on the next line.

A tracker campaign, for example, is saved like this — the rules are passed as a JSON string, as in the campaign_save tool:

JavaScript
const saved = QubApp.mcp.campaign_save({ id: campaignId, rules: JSON.stringify(rules) });
console.log('saved:', saved.success);

A complete script built this way — Tracker campaign auto-optimization.

Whose rights a call has​

There is no key to enter: the call runs as the owner of the code, and a tool that person cannot use in the panel is not available to the code either — it does not even appear in the list.

Where the code runsWhose rights the calls have
Scripts: on a schedule and with the 立即运行 buttonthe script's owner
Britva rulesthe rule's owner, reading only — a tool that changes data is refused; pause and resume are the native pauseAd/activateAd, and other writes belong in a script. A team or a global rule has no person to act for: its calls are refused
Landing page handlersthe landing page's owner — for every visitor of the page
A page handler acts for every visitor

A website page handler calls the tools with its owner's rights whoever opens the page. Do not pass what came from the visitor — the address, the form fields, the headers — into a tool that changes data without checking it first.

Only code saved by the owner (scripts)​

In a script QubApp.mcp works only in code that was last changed and saved by the script's owner or by an administrator — by the same rules as for submitting spend: Only code saved by the owner. An unsaved edit run with the 立即运行 button gets a refusal too: save the script first. A script an administrator handed over to another owner keeps this confirmation: its code works with the rights of the new owner.

Test runs only read​

The 测试 tab of a Britva rule and the 运行 button of a website page handler change nothing. In these runs QubApp.mcp calls the tools with the rights of whoever started the test, and only the tools that read work: a tool that changes data is refused with the words "… changes data, and a test run changes nothing".

The 立即运行 button of a script is not a test: it runs the code for real, and a tool that changes data does change it.

Finding out which tools are available​

JavaScript
QubApp.mcp.tools().forEach(function (t) {
console.log(t.name, '·', t.write ? 'changes data' : 'reads', '·', t.description);
});

tools() answers with the tools the owner of the code may use, already narrowed by that person's rights: the name, the description and whether the tool changes data. Use QubApp.mcp.call(name, args) when the name of the tool is computed while the code runs; it does exactly what the named method does.

When a call fails​

A failed call throws an McpError, and its delivery field tells the kind of failure:

JavaScript
try {
QubApp.mcp.ad_pause({ ad_id: adId });
} catch (e) {
if (e.delivery) {
// the call never reached the tool: see the table below
console.log('not delivered:', e.message);
} else {
// the tool answered with a refusal: no permission, wrong arguments
console.log(e.tool, 'refused:', e.message);
}
}
RefusaldeliveryWhat happened
"QubApp.mcp works only in code a person owns: …"truea system script, a team or a global Britva rule, code without an owner
"QubApp.mcp: the owner of this code is not among the users, …"truethe owner of the code was removed
"QubApp.mcp: the account of this code's owner is deactivated."truethe owner's account is deactivated
"QubApp.mcp works only in code the script's owner or an administrator changed and saved. …"truethe script's code is not confirmed — see above
"QubApp.mcp: the plan of this installation does not include the MCP/API interface."truethe plan does not include MCP/API
"Changes are paused: …"truethe license or the subscription of the installation is not active: the subscription has expired, the license is frozen, revoked or bound to another server
"QubApp.mcp: there is no tool …"truea mistake in the tool name
"QubApp.mcp: at most N tool calls in one run of the code …"truethe limit of calls is used up — see below
"QubApp.mcp: … changes data, and a test run changes nothing. …"truea test run asked for a tool that changes data
"QubApp.mcp: … changes data, and in a Britva rule only the tools that read work. …"truea Britva rule asked for a tool that changes data (writes belong in a script)
"QubApp.mcp: the arguments of … are too large or refer to themselves. …"truethe argument object is a cycle or too large to pass
"You do not have the right to do this. …" and other answers of the toolfalsethe tool itself refused

The arguments are plain objects, lists, dates and plain values. A BigInt, Symbol, Map, Set, Proxy, regular expression, typed array or function, and arguments holding more than 1,048,576 values, more than 16 MiB of text or nested deeper than 10,000 levels, throw a TypeError before the call reaches the tool.

A refusal that is not caught stops the run with an error, like any other exception. In a script both kinds of refusal are also written into the run console as warnings — even when the code catches them, so a script whose calls quietly stopped working still shows it in its run history (Writing a script).

Refusal texts come in the language of the code owner's profile — in a test run, of whoever started it — Russian or English; with any other profile language, and when the code has no owner, in English.

Limit of calls​

One run of code may make as many tool calls as the 每次运行的工具调用数 field allows — the Qubix 工具 group on the JavaScript tab (see System); 20 by default. A run of a script and a request to a page handler each get the whole limit; a Britva rule gets it once per check cycle, for all the ads the rule checks in that cycle. A call that reached the tool counts even if the tool then refused; tools() and calls refused before reaching a tool do not count.

What's next​