norc-mcp 0.2.2 → 0.3.1
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/README.md +4 -0
- package/dist/handler.js +56 -0
- package/dist/server.js +28 -38
- package/dist/tools/gitops.js +61 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -46,6 +46,10 @@ claude mcp add norc --env NORC_API_KEY=norc_sk_... -- npx -y norc-mcp
|
|
|
46
46
|
|
|
47
47
|
Use the same `command` / `args` / `env` block as the Claude Desktop config above. Without `NORC_API_KEY` (or `--stdio`) the server starts in HTTP mode instead, and a missing or malformed key in stdio mode exits with an error.
|
|
48
48
|
|
|
49
|
+
### Hosted (remote clients)
|
|
50
|
+
|
|
51
|
+
For clients that only support remote servers, use `https://mcp.norc.app/mcp` (Streamable HTTP, `POST`) with the header `Authorization: Bearer norc_sk_...`. It is the same code as this repo, deployed on Vercel; your key is forwarded to `vault.norc.app` and not stored.
|
|
52
|
+
|
|
49
53
|
## Tools
|
|
50
54
|
|
|
51
55
|
- `list_jobs`, `create_job`, `update_job`, `delete_job`
|
package/dist/handler.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
2
|
+
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
|
3
|
+
import { VaultClient } from './vault-client.js';
|
|
4
|
+
import { registerJobTools } from './tools/jobs.js';
|
|
5
|
+
import { registerGitopsTools } from './tools/gitops.js';
|
|
6
|
+
import { registerMachineTools } from './tools/machines.js';
|
|
7
|
+
import { registerRunTools } from './tools/runs.js';
|
|
8
|
+
const VAULT_BASE_URL = process.env.NORC_VAULT_URL ?? 'https://vault.norc.app';
|
|
9
|
+
export function extractApiKey(headers) {
|
|
10
|
+
const auth = headers.get('authorization') ?? '';
|
|
11
|
+
if (!auth.startsWith('Bearer '))
|
|
12
|
+
return null;
|
|
13
|
+
const raw = auth.slice('Bearer '.length).trim();
|
|
14
|
+
return raw.startsWith('norc_sk_') ? raw : null;
|
|
15
|
+
}
|
|
16
|
+
export function buildServer(apiKey) {
|
|
17
|
+
const server = new McpServer({ name: 'norc-mcp', version: '0.3.1' });
|
|
18
|
+
const client = new VaultClient(apiKey, VAULT_BASE_URL);
|
|
19
|
+
registerJobTools(server, client);
|
|
20
|
+
registerMachineTools(server, client);
|
|
21
|
+
registerRunTools(server, client);
|
|
22
|
+
registerGitopsTools(server, client);
|
|
23
|
+
return server;
|
|
24
|
+
}
|
|
25
|
+
// One stateless MCP exchange per request. Shared by the standalone listener (server.ts) and
|
|
26
|
+
// the Vercel function (api/mcp.js), which have different routing but identical behaviour.
|
|
27
|
+
export async function handleMcp(req, res) {
|
|
28
|
+
if (req.method !== 'POST') {
|
|
29
|
+
res.writeHead(405, { allow: 'POST' }).end();
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const headers = new Headers(req.headers);
|
|
33
|
+
const apiKey = extractApiKey(headers);
|
|
34
|
+
if (!apiKey) {
|
|
35
|
+
res.writeHead(401, { 'content-type': 'application/json' }).end(JSON.stringify({ error: 'missing or malformed norc API key' }));
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
// Vercel parses lazily on first access and throws on malformed JSON.
|
|
39
|
+
let body;
|
|
40
|
+
try {
|
|
41
|
+
body = req.body;
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
res.writeHead(400, { 'content-type': 'application/json' }).end(JSON.stringify({ error: 'invalid JSON body' }));
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const server = buildServer(apiKey);
|
|
48
|
+
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined });
|
|
49
|
+
res.on('close', () => {
|
|
50
|
+
transport.close();
|
|
51
|
+
server.close();
|
|
52
|
+
});
|
|
53
|
+
await server.connect(transport);
|
|
54
|
+
// Vercel pre-parses the body onto req.body; the standalone listener leaves the stream unread.
|
|
55
|
+
await transport.handleRequest(req, res, body);
|
|
56
|
+
}
|
package/dist/server.js
CHANGED
|
@@ -1,51 +1,41 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { realpathSync } from 'node:fs';
|
|
2
3
|
import { createServer } from 'node:http';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const auth = headers.get('authorization') ?? '';
|
|
12
|
-
if (!auth.startsWith('Bearer '))
|
|
13
|
-
return null;
|
|
14
|
-
const raw = auth.slice('Bearer '.length).trim();
|
|
15
|
-
return raw.startsWith('norc_sk_') ? raw : null;
|
|
4
|
+
import { pathToFileURL } from 'node:url';
|
|
5
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
6
|
+
import { buildServer, extractApiKey, handleMcp } from './handler.js';
|
|
7
|
+
export { extractApiKey };
|
|
8
|
+
// stdio mode (a local MCP client spawns us, key comes from the environment) when
|
|
9
|
+
// NORC_API_KEY is set or --stdio is passed; otherwise the HTTP listener (Docker, self-hosting).
|
|
10
|
+
export function wantsStdio(env, argv) {
|
|
11
|
+
return argv.includes('--stdio') || Boolean(env.NORC_API_KEY);
|
|
16
12
|
}
|
|
17
|
-
function
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
13
|
+
async function runStdio() {
|
|
14
|
+
const apiKey = process.env.NORC_API_KEY ?? '';
|
|
15
|
+
if (!apiKey.startsWith('norc_sk_')) {
|
|
16
|
+
// stdout carries the protocol, so diagnostics go to stderr
|
|
17
|
+
console.error('norc-mcp: set NORC_API_KEY to a norc API key (norc_sk_...)');
|
|
18
|
+
process.exit(1);
|
|
19
|
+
}
|
|
20
|
+
await buildServer(apiKey).connect(new StdioServerTransport());
|
|
24
21
|
}
|
|
25
|
-
|
|
26
|
-
// extractApiKey/buildServer without binding a port.
|
|
27
|
-
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
22
|
+
function runHttp() {
|
|
28
23
|
const port = Number(process.env.PORT ?? 3000);
|
|
29
24
|
createServer(async (req, res) => {
|
|
30
|
-
if (req.url !== '/mcp'
|
|
25
|
+
if (req.url !== '/mcp') {
|
|
31
26
|
res.writeHead(404).end();
|
|
32
27
|
return;
|
|
33
28
|
}
|
|
34
|
-
|
|
35
|
-
const apiKey = extractApiKey(headers);
|
|
36
|
-
if (!apiKey) {
|
|
37
|
-
res.writeHead(401, { 'content-type': 'application/json' }).end(JSON.stringify({ error: 'missing or malformed norc API key' }));
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
const server = buildServer(apiKey);
|
|
41
|
-
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined });
|
|
42
|
-
res.on('close', () => {
|
|
43
|
-
transport.close();
|
|
44
|
-
server.close();
|
|
45
|
-
});
|
|
46
|
-
await server.connect(transport);
|
|
47
|
-
await transport.handleRequest(req, res);
|
|
29
|
+
await handleMcp(req, res);
|
|
48
30
|
}).listen(port, () => {
|
|
49
31
|
console.log(`norc-mcp listening on :${port}`);
|
|
50
32
|
});
|
|
51
33
|
}
|
|
34
|
+
// Only start when executed directly, so tests can import extractApiKey/buildServer without
|
|
35
|
+
// binding a port. realpath: an npm bin is a symlink, so argv[1] differs from import.meta.url.
|
|
36
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href) {
|
|
37
|
+
if (wantsStdio(process.env, process.argv))
|
|
38
|
+
await runStdio();
|
|
39
|
+
else
|
|
40
|
+
runHttp();
|
|
41
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
// JSON key, not "a/b": names may contain "/" and would collide.
|
|
3
|
+
const key = (machine, name) => JSON.stringify([machine, name]);
|
|
4
|
+
export function computeDiff(desired, machines, jobs) {
|
|
5
|
+
const machineName = new Map(machines.map((m) => [m.id, m.name]));
|
|
6
|
+
const machineId = new Map(machines.map((m) => [m.name, m.id]));
|
|
7
|
+
const live = new Map(jobs.map((j) => [key(machineName.get(j.machine_id) ?? '', j.name), j]));
|
|
8
|
+
const want = new Set(desired.map((d) => key(d.machine, d.name)));
|
|
9
|
+
const errors = [];
|
|
10
|
+
const creates = [];
|
|
11
|
+
const updates = [];
|
|
12
|
+
for (const d of desired) {
|
|
13
|
+
const k = `${d.machine}/${d.name}`;
|
|
14
|
+
const mid = machineId.get(d.machine);
|
|
15
|
+
if (!mid) {
|
|
16
|
+
errors.push(`${k}: unknown machine "${d.machine}"`);
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
const existing = live.get(key(d.machine, d.name));
|
|
20
|
+
if (!existing)
|
|
21
|
+
creates.push({ key: k, machineId: mid, name: d.name, schedule: d.schedule, command: d.command, enabled: d.enabled });
|
|
22
|
+
else if (existing.schedule !== d.schedule || existing.command !== d.command || existing.enabled !== d.enabled) {
|
|
23
|
+
updates.push({ key: k, id: existing.id, patch: { schedule: d.schedule, command: d.command, enabled: d.enabled } });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const deletes = jobs
|
|
27
|
+
.filter((j) => !want.has(key(machineName.get(j.machine_id) ?? '', j.name)))
|
|
28
|
+
.map((j) => ({ key: `${machineName.get(j.machine_id) ?? j.machine_id}/${j.name}`, id: j.id }));
|
|
29
|
+
return { creates, updates, deletes, errors };
|
|
30
|
+
}
|
|
31
|
+
export function registerGitopsTools(server, client) {
|
|
32
|
+
server.registerTool('sync_jobs', {
|
|
33
|
+
description: "Make the account's jobs match a declared set (declarative, like `norc gitops`). Jobs not listed are DELETED. " +
|
|
34
|
+
'Defaults to a dry run that only returns the diff; set apply=true to execute it. Always show the user the dry-run diff, ' +
|
|
35
|
+
'especially the deletes, and get their confirmation before applying. Requires the gitops sync feature on the account.',
|
|
36
|
+
inputSchema: {
|
|
37
|
+
jobs: z
|
|
38
|
+
.array(z.object({
|
|
39
|
+
machine: z.string().describe('Machine name (not id)'),
|
|
40
|
+
name: z.string(),
|
|
41
|
+
schedule: z.string().describe('Cron expression, e.g. "0 * * * *"'),
|
|
42
|
+
command: z.string(),
|
|
43
|
+
enabled: z.boolean().optional().describe('Defaults to true'),
|
|
44
|
+
}))
|
|
45
|
+
.describe('The complete desired job set across all machines'),
|
|
46
|
+
apply: z.boolean().optional().describe('Execute the diff. Default false (dry run).'),
|
|
47
|
+
},
|
|
48
|
+
}, async ({ jobs, apply }) => {
|
|
49
|
+
const desired = jobs.map((j) => ({ ...j, enabled: j.enabled ?? true }));
|
|
50
|
+
const [machines, live] = await Promise.all([client.get('/api/v1/machines'), client.get('/api/v1/jobs')]);
|
|
51
|
+
const { errors, ...diff } = computeDiff(desired, machines, live);
|
|
52
|
+
if (errors.length > 0) {
|
|
53
|
+
return { isError: true, content: [{ type: 'text', text: `Not applied. ${errors.join('; ')}` }] };
|
|
54
|
+
}
|
|
55
|
+
if (!apply) {
|
|
56
|
+
return { content: [{ type: 'text', text: JSON.stringify({ applied: false, diff }, null, 2) }] };
|
|
57
|
+
}
|
|
58
|
+
const result = await client.post('/api/v1/jobs/sync', diff);
|
|
59
|
+
return { content: [{ type: 'text', text: JSON.stringify({ applied: true, ...result }, null, 2) }] };
|
|
60
|
+
});
|
|
61
|
+
}
|