@consilium108/mcp-module-devops 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/LICENSE +289 -0
- package/README.md +171 -2
- package/bin/consilium-devops-mcp.js +66 -0
- package/bin/consilium-devops-runner.js +120 -0
- package/i18n/en.json +3 -0
- package/i18n/ru.json +3 -0
- package/i18n/th.json +3 -0
- package/i18n/zh.json +3 -0
- package/package.json +48 -4
- package/src/errors.js +12 -0
- package/src/groups/debug.js +44 -0
- package/src/groups/deploy.js +54 -0
- package/src/groups/ops.js +125 -0
- package/src/index.js +36 -0
- package/src/runner/client.js +42 -0
- package/src/runner/config.js +143 -0
- package/src/runner/daemon.js +162 -0
- package/src/runner/doctor.js +111 -0
- package/src/runner/index.js +12 -0
- package/src/runner/install.js +113 -0
- package/src/runner/log.js +33 -0
- package/src/runner/recipes.js +230 -0
- package/src/runner/state.js +84 -0
- package/src/runner/steps.js +196 -0
- package/src/runner/watchdog.js +113 -0
- package/src/standalone.js +227 -0
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
// generated by Claude (Opus 5.5), 2026-11-22; Konsilium #482 (TASK-P37-482): the DevOps MCP on its own — one package, no
|
|
2
|
+
// host. The module's own groups (deploy, debug — the same tools, schemas and texts as inside @consilium108/mcp) plus the
|
|
3
|
+
// read tools of src/groups/ops.js and two session tools, on an MCP SDK server with host-style helpers `h`:
|
|
4
|
+
// • sign-in = the person's Konsilium credential (a scoped key ksk_… or an OAuth access token); the server checks the
|
|
5
|
+
// rights on every call — the module never decides access and never holds a module / service secret;
|
|
6
|
+
// • project = --project / KONSILIUM_PROJECT_ID / select_project / the connection's default project;
|
|
7
|
+
// • every call goes to the Konsilium core API (it proxies /api/deploy*, /api/projects/:id/deploy*, debug reports to the
|
|
8
|
+
// DevOps module) or, with `devopsUrl`, straight to the module API (same paths; the module checks the bearer with the core);
|
|
9
|
+
// • no approval: a deploy is approved only by a person with the deploy right (✅ in the messenger / the app).
|
|
10
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
11
|
+
import { z } from 'zod';
|
|
12
|
+
import mod from './index.js';
|
|
13
|
+
import { OPS_TOOLS, registerOpsTools } from './groups/ops.js';
|
|
14
|
+
import { DevopsMcpError } from './errors.js';
|
|
15
|
+
|
|
16
|
+
export const DEFAULT_API_URL = 'https://konsilium.nativemind.net';
|
|
17
|
+
export const SESSION_TOOLS = ['devops_session', 'select_project'];
|
|
18
|
+
/** Every tool of the standalone server, in registration order. */
|
|
19
|
+
export const STANDALONE_TOOLS = [...SESSION_TOOLS, ...Object.values(mod.groups).flatMap((g) => g.tools), ...OPS_TOOLS];
|
|
20
|
+
|
|
21
|
+
export { DevopsMcpError }; // src/errors.js — shared with the deploy runner
|
|
22
|
+
|
|
23
|
+
const HINTS = {
|
|
24
|
+
auth_required: 'No Konsilium credential: set KONSILIUM_API_KEY (a key from Konsilium → Account → API, with the project and the right tasks:work) or run `npx @consilium108/mcp login` once.',
|
|
25
|
+
auth_invalid: 'Konsilium refused this credential (revoked, expired or mistyped). Create a new key in Konsilium → Account → API, or sign in again.',
|
|
26
|
+
project_not_chosen: 'No project chosen: pass project_id, call select_project, or start the MCP with --project <id> / KONSILIUM_PROJECT_ID.',
|
|
27
|
+
network_unavailable: 'Konsilium is not reachable (network or the server is restarting). Try again in a minute.',
|
|
28
|
+
module_unavailable: 'The DevOps module of Konsilium is not answering right now (the core is up). Try again later; the owner sees it in the server log.',
|
|
29
|
+
module_disabled: 'The DevOps module is turned off for this connection. The person turns it on in Konsilium → Connected apps.',
|
|
30
|
+
key_not_allowed: 'This server does not accept this route by key yet (the Konsilium core is older than this MCP). The other DevOps tools work.',
|
|
31
|
+
no_deploy_right: 'You have no deploy right in this project. The owner grants it in Konsilium (bot or app); an AI cannot.',
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** HTTP answer → a coded error (the server's own code and text when it gives them). */
|
|
35
|
+
export function errorOf(status, body) {
|
|
36
|
+
const code = body?.error?.code ?? body?.code ?? null;
|
|
37
|
+
const msg = body?.error?.message ?? body?.message ?? null;
|
|
38
|
+
if (status === 401) {
|
|
39
|
+
if (code === 'key_not_allowed') return new DevopsMcpError('key_not_allowed', `${msg ?? ''} ${HINTS.key_not_allowed}`.trim(), status);
|
|
40
|
+
if (code === 'session_only') return new DevopsMcpError('session_only', msg ?? 'Only a person in the Konsilium app can do this.', status);
|
|
41
|
+
return new DevopsMcpError('auth_invalid', HINTS.auth_invalid, status);
|
|
42
|
+
}
|
|
43
|
+
if (status === 503 && (code === 'module_unavailable' || !code)) return new DevopsMcpError('module_unavailable', HINTS.module_unavailable, status);
|
|
44
|
+
const c = code ?? (status === 403 ? 'forbidden' : status === 404 ? 'not_found' : status === 429 ? 'rate_limited' : `http_${status}`);
|
|
45
|
+
const hint = HINTS[c] && !msg ? HINTS[c] : null;
|
|
46
|
+
return new DevopsMcpError(c, [msg, hint].filter(Boolean).join(' ') || c, status);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** `/api/projects/{id}/x` + { id: 3, a: 1 } → ['/api/projects/3/x', { a: 1 }]. */
|
|
50
|
+
export function fillPath(path, args = {}) {
|
|
51
|
+
const rest = { ...args };
|
|
52
|
+
const out = path.replace(/\{(\w+)\}/g, (_m, k) => {
|
|
53
|
+
const v = rest[k];
|
|
54
|
+
delete rest[k];
|
|
55
|
+
if (v == null || v === '') throw new DevopsMcpError(k === 'id' ? 'project_not_chosen' : 'bad_request', k === 'id' ? HINTS.project_not_chosen : `${k} is required`);
|
|
56
|
+
return encodeURIComponent(String(v));
|
|
57
|
+
});
|
|
58
|
+
for (const k of Object.keys(rest)) if (rest[k] === undefined) delete rest[k];
|
|
59
|
+
return [out, rest];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The standalone DevOps MCP.
|
|
64
|
+
* @param {object} o
|
|
65
|
+
* @param {string} [o.apiUrl] Konsilium core (session, rights; DevOps routes unless devopsUrl is set)
|
|
66
|
+
* @param {string|null} [o.devopsUrl] the DevOps module API, when it is reachable directly (same paths as the core's)
|
|
67
|
+
* @param {{ bearer: () => Promise<string|null>, refresh?: (failed: string) => Promise<string|null>, source?: () => string } | null} o.credentials
|
|
68
|
+
* @param {number|null} [o.projectId] the project chosen at start (--project / KONSILIUM_PROJECT_ID)
|
|
69
|
+
* @param {string} [o.version] the client version for User-Agent
|
|
70
|
+
* @param {typeof fetch} [o.fetch]
|
|
71
|
+
*/
|
|
72
|
+
export function createDevopsMcp({ apiUrl = DEFAULT_API_URL, devopsUrl = null, credentials = null, projectId = null, version = mod.version, fetch: f = globalThis.fetch, timeoutMs = 60_000 } = {}) {
|
|
73
|
+
const core = String(apiUrl).replace(/\/+$/, '');
|
|
74
|
+
const direct = devopsUrl ? String(devopsUrl).replace(/\/+$/, '') : null;
|
|
75
|
+
let active = Number(projectId) || null;
|
|
76
|
+
let activeSource = active ? 'start' : null;
|
|
77
|
+
let session = null;
|
|
78
|
+
let sessionAt = 0;
|
|
79
|
+
|
|
80
|
+
async function request(base, method, path, args, { retry = true } = {}) {
|
|
81
|
+
const token = credentials ? await credentials.bearer().catch(() => null) : null;
|
|
82
|
+
if (!token) throw new DevopsMcpError('auth_required', HINTS.auth_required, 401);
|
|
83
|
+
const [p, rest] = fillPath(path, args);
|
|
84
|
+
const get = method === 'GET' || method === 'DELETE';
|
|
85
|
+
const qs = get && Object.keys(rest).length ? `?${new URLSearchParams(Object.entries(rest).map(([k, v]) => [k, String(v)]))}` : '';
|
|
86
|
+
let res;
|
|
87
|
+
try {
|
|
88
|
+
res = await f(`${base}${p}${qs}`, {
|
|
89
|
+
method,
|
|
90
|
+
headers: { authorization: `Bearer ${token}`, accept: 'application/json', 'user-agent': `consilium-devops-mcp/${version}`, 'x-nm-client': 'mcp', ...(get ? {} : { 'content-type': 'application/json' }) },
|
|
91
|
+
...(get ? {} : { body: JSON.stringify(rest) }),
|
|
92
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
93
|
+
});
|
|
94
|
+
} catch {
|
|
95
|
+
throw new DevopsMcpError('network_unavailable', HINTS.network_unavailable, 0);
|
|
96
|
+
}
|
|
97
|
+
const text = await res.text();
|
|
98
|
+
let body = null;
|
|
99
|
+
try {
|
|
100
|
+
body = text ? JSON.parse(text) : null;
|
|
101
|
+
} catch {
|
|
102
|
+
body = null;
|
|
103
|
+
}
|
|
104
|
+
if (res.status === 401 && retry && credentials?.refresh) {
|
|
105
|
+
const fresh = await credentials.refresh(token).catch(() => null);
|
|
106
|
+
if (fresh) return request(base, method, path, args, { retry: false });
|
|
107
|
+
}
|
|
108
|
+
if (!res.ok) throw errorOf(res.status, body);
|
|
109
|
+
return body;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** GET /api/mcp/session of the core (cached 60 s): who, projects with rights, modules. */
|
|
113
|
+
async function loadSession({ fresh = false } = {}) {
|
|
114
|
+
if (!fresh && session && Date.now() - sessionAt < 60_000) return session;
|
|
115
|
+
session = await request(core, 'GET', '/api/mcp/session', {});
|
|
116
|
+
sessionAt = Date.now();
|
|
117
|
+
return session;
|
|
118
|
+
}
|
|
119
|
+
const projectIds = (s) => (s?.projects ?? []).flatMap((p) => [p.id, ...(p.subprojects ?? [])]);
|
|
120
|
+
|
|
121
|
+
const P = () => {
|
|
122
|
+
const id = active ?? session?.default_project_id ?? null;
|
|
123
|
+
if (!id) throw new DevopsMcpError('project_not_chosen', HINTS.project_not_chosen);
|
|
124
|
+
return id;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const text = (x) => {
|
|
128
|
+
const obj = x && typeof x === 'object' && !Array.isArray(x) ? x : { result: x };
|
|
129
|
+
return { content: [{ type: 'text', text: typeof x === 'string' ? x : JSON.stringify(x, null, 2) }], structuredContent: obj };
|
|
130
|
+
};
|
|
131
|
+
const fail = (m) => {
|
|
132
|
+
const s = String(m);
|
|
133
|
+
const i = s.indexOf(': ');
|
|
134
|
+
const code = i > 0 && /^[a-z_0-9]+$/.test(s.slice(0, i)) ? s.slice(0, i) : 'error';
|
|
135
|
+
return { content: [{ type: 'text', text: s }], structuredContent: { error: { code, message: i > 0 ? s.slice(i + 2) : s } }, isError: true };
|
|
136
|
+
};
|
|
137
|
+
const safe = (fn) => async (args, ctx) => {
|
|
138
|
+
try {
|
|
139
|
+
if (!active && credentials) await loadSession().catch(() => {}); // the connection's default project
|
|
140
|
+
return await fn(args ?? {}, ctx);
|
|
141
|
+
} catch (e) {
|
|
142
|
+
if (e instanceof DevopsMcpError) return fail(`${e.code}: ${e.message}`);
|
|
143
|
+
return fail(`internal: ${e?.message ?? e}`);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
/** Module paths: the module API directly when configured, else the core (which proxies them). */
|
|
147
|
+
const call = (_ctx, method, path, args = {}) => request(direct ?? core, method, path, args);
|
|
148
|
+
class NeedsInput extends DevopsMcpError {
|
|
149
|
+
constructor({ code = 'needs_input', message = 'More input is needed' } = {}) {
|
|
150
|
+
super(code, message);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const h = {
|
|
154
|
+
z,
|
|
155
|
+
L: 'en',
|
|
156
|
+
M: (k) => k,
|
|
157
|
+
mt: (k) => k,
|
|
158
|
+
text,
|
|
159
|
+
fail,
|
|
160
|
+
safe,
|
|
161
|
+
call,
|
|
162
|
+
P,
|
|
163
|
+
ROOT: P,
|
|
164
|
+
shape: 'project',
|
|
165
|
+
NeedsInput,
|
|
166
|
+
elicitOn: () => false,
|
|
167
|
+
resourcesOn: () => false,
|
|
168
|
+
scopesFor: (id) => (session?.projects ?? []).find((p) => p.id === Number(id))?.scopes ?? [],
|
|
169
|
+
session: {},
|
|
170
|
+
client: () => 'MCP',
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const server = new McpServer({ name: 'consilium-devops', version }, { capabilities: { tools: {} }, instructions: INSTRUCTIONS });
|
|
174
|
+
const ro = { readOnlyHint: true, openWorldHint: false };
|
|
175
|
+
|
|
176
|
+
server.registerTool(
|
|
177
|
+
'devops_session',
|
|
178
|
+
{
|
|
179
|
+
title: 'DevOps session',
|
|
180
|
+
description: 'Who this MCP acts for, the projects of the connection with their rights, the active project, whether the DevOps module is on, and where calls go. Call it first.',
|
|
181
|
+
inputSchema: {},
|
|
182
|
+
annotations: ro,
|
|
183
|
+
},
|
|
184
|
+
safe(async () => {
|
|
185
|
+
const s = await loadSession({ fresh: true });
|
|
186
|
+
const devops = (s.modules ?? []).find((m) => m.id === 'devops') ?? null;
|
|
187
|
+
return text({
|
|
188
|
+
user: s.user ?? null,
|
|
189
|
+
credential: s.kind ?? null,
|
|
190
|
+
active_project_id: active ?? s.default_project_id ?? null,
|
|
191
|
+
active_project_source: active ? activeSource : s.default_project_id ? 'connection_default' : null,
|
|
192
|
+
projects: (s.projects ?? []).map((p) => ({ id: p.id, name: p.name, subprojects: p.subprojects ?? [], scopes: p.scopes ?? [], can_request_deploy: (p.scopes ?? []).includes('tasks:work') })),
|
|
193
|
+
devops_module: devops ? { enabled: !!devops.enabled, ...(devops.reason ? { reason: devops.reason } : {}) } : { enabled: true },
|
|
194
|
+
api: { core: core, devops: direct ?? `${core} (proxied by the core)` },
|
|
195
|
+
mcp: { package: mod.name, version: mod.version },
|
|
196
|
+
approval: 'Deploys are approved only by a person with the deploy right (✅ in the messenger or the app); this MCP can request and watch, never approve.',
|
|
197
|
+
});
|
|
198
|
+
}),
|
|
199
|
+
);
|
|
200
|
+
server.registerTool(
|
|
201
|
+
'select_project',
|
|
202
|
+
{
|
|
203
|
+
title: 'Choose the project',
|
|
204
|
+
description: 'Make a project of this connection (or its subproject) the active one for the DevOps tools. A choice, not a right: the server still checks the rights on every call.',
|
|
205
|
+
inputSchema: { project_id: z.number().int().positive() },
|
|
206
|
+
annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false },
|
|
207
|
+
},
|
|
208
|
+
safe(async ({ project_id }) => {
|
|
209
|
+
const s = await loadSession({ fresh: true });
|
|
210
|
+
if (!projectIds(s).includes(project_id)) throw new DevopsMcpError('forbidden_project', `Project ${project_id} is not in this connection (${projectIds(s).join(', ') || 'none'}). The person adds it in Konsilium → Account → API / Connected apps.`, 403);
|
|
211
|
+
active = project_id;
|
|
212
|
+
activeSource = 'select_project';
|
|
213
|
+
const p = (s.projects ?? []).find((x) => x.id === project_id);
|
|
214
|
+
return text({ active_project_id: project_id, name: p?.name ?? null });
|
|
215
|
+
}),
|
|
216
|
+
);
|
|
217
|
+
for (const g of Object.values(mod.groups)) g.register(server, h);
|
|
218
|
+
registerOpsTools(server, h);
|
|
219
|
+
|
|
220
|
+
return { server, helpers: h, activeProject: () => active, loadSession };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export const INSTRUCTIONS = `Konsilium DevOps MCP (${mod.name} ${mod.version}): deploy requests by button and their status, deploy targets and the runner's heartbeat, Job Copilot debug reports — for the projects of your Konsilium sign-in.
|
|
224
|
+
Start with devops_session (who, projects, rights, active project); select_project switches the project.
|
|
225
|
+
runner_status shows whether the deploy runner of each target is online and what it runs; runner_setup gives a person the steps to install it (text only — the runner is a separate service, bin consilium-devops-runner).
|
|
226
|
+
request_deploy asks the people with the deploy right to approve (✅ in the messenger); you cannot approve, reject or roll back — a person does. Then poll deploy_status(deploy_id): pending → approved → running → done | failed (rejected / expired), exit code, health, log tail.
|
|
227
|
+
Errors come as «code: message» (auth_required, auth_invalid, project_not_chosen, no_deploy_right, module_disabled, module_unavailable, network_unavailable …).`;
|