hcloud-mcp 0.2.0 → 0.3.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.
@@ -1,39 +0,0 @@
1
- /**
2
- * Test list_* API calls using the same Hetzner SDK as the MCP server.
3
- * Run from project root: node scripts/test-list-api.mjs
4
- * Uses HCLOUD_TOKEN from environment or .env.
5
- */
6
- import "dotenv/config";
7
- import {
8
- LocationsService,
9
- ServerTypesService,
10
- OpenAPI,
11
- } from "hetzner-sdk-ts";
12
-
13
- const token = process.env.HCLOUD_TOKEN;
14
- if (!token) {
15
- console.error("Error: HCLOUD_TOKEN environment variable is required");
16
- process.exit(1);
17
- }
18
- OpenAPI.TOKEN = token;
19
-
20
- async function run() {
21
- console.log("Testing list_locations...");
22
- try {
23
- const r = await LocationsService.getLocations({});
24
- console.log("list_locations OK, count:", r.locations?.length ?? 0);
25
- } catch (e) {
26
- console.error("list_locations failed:", e.message || e);
27
- }
28
-
29
- console.log("Testing list_server_types...");
30
- try {
31
- const r = await ServerTypesService.getServerTypes({});
32
- console.log("list_server_types OK, count:", r.server_types?.length ?? 0);
33
- } catch (e) {
34
- console.error("list_server_types failed:", e.message || e);
35
- }
36
-
37
- }
38
-
39
- run();
@@ -1,187 +0,0 @@
1
- /**
2
- * Live lifecycle test: drives the built MCP server (dist/index.js) through a full
3
- * create → modify → teardown cycle against the real Hetzner API.
4
- *
5
- * COSTS MONEY (a few cents; Hetzner bills hourly). Creates resources labeled
6
- * hcloud-mcp-test=<run-id> and always cleans up only that run's resources via a REST safety net in `finally`.
7
- *
8
- * Usage: HCLOUD_TOKEN=... node scripts/test-live-lifecycle.mjs (npm run build first)
9
- */
10
- import "dotenv/config";
11
- import { Client } from "@modelcontextprotocol/sdk/client/index.js";
12
- import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
13
-
14
- const token = process.env.HCLOUD_TOKEN;
15
- if (!token) { console.error("HCLOUD_TOKEN required"); process.exit(1); }
16
-
17
- const suffix = `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`;
18
- // Unique per run so the safety-net cleanup only ever touches this run's resources.
19
- const LABEL = { "hcloud-mcp-test": suffix };
20
- const results = [];
21
- const ok = (step, detail = "") => { results.push(["ok", step]); console.log(`ok ${step}${detail ? " — " + detail : ""}`); };
22
-
23
- const client = new Client({ name: "live-test", version: "0" });
24
- await client.connect(new StdioClientTransport({ command: "node", args: ["dist/index.js"], env: { ...process.env, HCLOUD_TOKEN: token } }));
25
-
26
- async function call(name, args = {}) {
27
- const r = await client.callTool({ name, arguments: args });
28
- const text = r.content[0].text;
29
- if (r.isError) throw new Error(`${name} failed: ${text}`);
30
- try { return JSON.parse(text); } catch { return text; }
31
- }
32
-
33
- // Collect every Action id in a response (action / actions / next_actions) and poll until done.
34
- async function waitActions(resp, label) {
35
- if (!resp || typeof resp !== "object") return;
36
- const ids = [resp.action?.id, ...(resp.actions ?? []).map((a) => a.id), ...(resp.next_actions ?? []).map((a) => a.id)].filter(Boolean);
37
- for (const id of ids) {
38
- for (let i = 0; i < 120; i++) {
39
- const r = await call("get_action", { id });
40
- const a = r.action ?? r;
41
- if (a.status === "success") break;
42
- if (a.status === "error") throw new Error(`${label}: action ${id} error ${JSON.stringify(a.error)}`);
43
- await new Promise((r) => setTimeout(r, 2000));
44
- if (i === 119) throw new Error(`${label}: action ${id} timed out`);
45
- }
46
- }
47
- }
48
-
49
- async function rest(method, path, body) {
50
- const r = await fetch(`https://api.hetzner.cloud/v1${path}`, {
51
- method, headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },
52
- body: body ? JSON.stringify(body) : undefined,
53
- });
54
- return r.status === 204 ? {} : r.json();
55
- }
56
-
57
- async function safetyNetCleanup() {
58
- const sel = `label_selector=${encodeURIComponent(`hcloud-mcp-test=${suffix}`)}`;
59
- const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
60
- for (const lb of (await rest("GET", `/load_balancers?${sel}`)).load_balancers ?? []) await rest("DELETE", `/load_balancers/${lb.id}`);
61
- for (const v of (await rest("GET", `/volumes?${sel}`)).volumes ?? []) { if (v.server) { await rest("POST", `/volumes/${v.id}/actions/detach`); await sleep(5000); } await rest("DELETE", `/volumes/${v.id}`); }
62
- for (const f of (await rest("GET", `/floating_ips?${sel}`)).floating_ips ?? []) { if (f.server) { await rest("POST", `/floating_ips/${f.id}/actions/unassign`); await sleep(3000); } await rest("DELETE", `/floating_ips/${f.id}`); }
63
- for (const s of (await rest("GET", `/servers?${sel}`)).servers ?? []) await rest("DELETE", `/servers/${s.id}`);
64
- await sleep(10000);
65
- for (const p of (await rest("GET", `/primary_ips?${sel}`)).primary_ips ?? []) { if (p.assignee_id) continue; await rest("DELETE", `/primary_ips/${p.id}`); }
66
- for (const fw of (await rest("GET", `/firewalls?${sel}`)).firewalls ?? []) await rest("DELETE", `/firewalls/${fw.id}`);
67
- for (const n of (await rest("GET", `/networks?${sel}`)).networks ?? []) await rest("DELETE", `/networks/${n.id}`);
68
- const left = [];
69
- for (const [path, key] of [["/servers", "servers"], ["/load_balancers", "load_balancers"], ["/volumes", "volumes"], ["/floating_ips", "floating_ips"], ["/primary_ips", "primary_ips"], ["/firewalls", "firewalls"], ["/networks", "networks"]]) {
70
- const items = (await rest("GET", `${path}?${sel}`))[key] ?? [];
71
- if (items.length) left.push(`${key}:${items.map((i) => i.id).join(",")}`);
72
- }
73
- return left;
74
- }
75
-
76
- let failed = null;
77
- try {
78
- // --- Pick location / type / image the way the skill says ---
79
- const types = await call("list_server_types");
80
- const pr = await call("get_pricing");
81
- const pricing = pr.pricing ?? pr;
82
- const priceOf = (typeName, loc) => {
83
- const st = pricing.server_types.find((t) => t.name === typeName);
84
- const p = st?.prices.find((x) => x.location === loc);
85
- return p ? Number(p.price_hourly.gross) : Infinity;
86
- };
87
- const EU = ["fsn1", "nbg1", "hel1"];
88
- let best = null;
89
- for (const t of types) {
90
- if (t.architecture !== "x86") continue;
91
- for (const l of t.locations ?? []) {
92
- if (!EU.includes(l.name) || !l.available || l.deprecation) continue;
93
- const price = priceOf(t.name, l.name);
94
- if (!best || price < best.price) best = { type: t.name, location: l.name, price };
95
- }
96
- }
97
- if (!best) throw new Error("no available x86 type in EU");
98
- const images = await call("list_images");
99
- const image = images.find((i) => i.name === "ubuntu-24.04" && i.architecture === "x86" && !i.deprecation);
100
- if (!image) throw new Error("ubuntu-24.04 x86 not found");
101
- ok("select type/location/image", `${best.type} @ ${best.location} (€${best.price}/h), ${image.name}`);
102
-
103
- // --- Network + subnet ---
104
- const net = (await call("create_network", { name: `t-net-${suffix}`, ip_range: "10.77.0.0/16", labels: LABEL })).network;
105
- await waitActions(await call("add_network_subnet", { id: net.id, type: "cloud", network_zone: "eu-central", ip_range: "10.77.1.0/24" }), "subnet");
106
- ok("create_network + add_network_subnet(type=cloud)");
107
-
108
- // --- Firewall with /32 CIDR ---
109
- const fwResp = await call("create_firewall", {
110
- name: `t-fw-${suffix}`, labels: LABEL,
111
- rules: [{ direction: "in", protocol: "tcp", port: "22", source_ips: ["203.0.113.57/32"] }],
112
- });
113
- await waitActions(fwResp, "firewall");
114
- const fw = fwResp.firewall;
115
- ok("create_firewall");
116
-
117
- // --- Server with location (no datacenter) ---
118
- const srvResp = await call("create_server", {
119
- name: `t-srv-${suffix}`, server_type: best.type, image: image.name, location: best.location,
120
- networks: [net.id], firewalls: [{ firewall: fw.id }], labels: LABEL,
121
- });
122
- const srv = srvResp.server;
123
- await waitActions(srvResp, "create_server");
124
- const srvInfo = (await call("get_server", { id: srv.id }));
125
- if (srvInfo.location?.name !== best.location) throw new Error(`server location mismatch: ${JSON.stringify(srvInfo.location)}`);
126
- if ("datacenter" in srvInfo) console.log(" note: server response still includes datacenter");
127
- ok("create_server(location)", `id ${srv.id}, location ${srvInfo.location.name}`);
128
-
129
- // --- Primary IP: create by location, swap onto server ---
130
- const pip = (await call("create_primary_ip", { name: `t-pip-${suffix}`, type: "ipv4", location: best.location, auto_delete: false, labels: LABEL })).primary_ip;
131
- if (pip.assignee_type !== "unassigned") throw new Error(`expected assignee_type unassigned, got ${pip.assignee_type}`);
132
- ok("create_primary_ip(location)", `assignee_type=${pip.assignee_type}`);
133
- const oldPipId = srvInfo.public_net.ipv4.id;
134
- await waitActions(await call("power_off_server", { id: srv.id }), "power_off");
135
- await waitActions(await call("unassign_primary_ip", { id: oldPipId }), "unassign old pip");
136
- await waitActions(await call("assign_primary_ip", { id: pip.id, server: srv.id }), "assign new pip");
137
- await waitActions(await call("power_on_server", { id: srv.id }), "power_on");
138
- await call("delete_primary_ip", { id: oldPipId });
139
- const after = await call("get_server", { id: srv.id });
140
- if (after.public_net.ipv4.id !== pip.id) throw new Error("primary ip swap did not stick");
141
- ok("primary IP swap + delete old");
142
-
143
- // --- Assigned IP delete must fail (must_be_unassigned) ---
144
- try { await call("delete_primary_ip", { id: pip.id }); throw new Error("deleting assigned primary IP unexpectedly succeeded"); }
145
- catch (e) { if (!/must_be_unassigned|assigned/i.test(String(e.message))) throw e; ok("delete assigned primary IP rejected", "must_be_unassigned"); }
146
-
147
- // --- Volume attach/detach/delete ---
148
- const volResp = await call("create_volume", { name: `t-vol-${suffix}`, size: 10, location: best.location, labels: LABEL });
149
- await waitActions(volResp, "create_volume");
150
- const vol = volResp.volume;
151
- await waitActions(await call("attach_volume", { id: vol.id, server: srv.id }), "attach_volume");
152
- await waitActions(await call("detach_volume", { id: vol.id }), "detach_volume");
153
- await call("delete_volume", { id: vol.id });
154
- ok("volume create/attach/detach/delete");
155
-
156
- // --- Floating IP assign/unassign/delete ---
157
- const fip = (await call("create_floating_ip", { type: "ipv4", home_location: best.location, labels: LABEL })).floating_ip;
158
- await waitActions(await call("assign_floating_ip", { id: fip.id, server: srv.id }), "assign fip");
159
- await waitActions(await call("unassign_floating_ip", { id: fip.id }), "unassign fip");
160
- await call("delete_floating_ip", { id: fip.id });
161
- ok("floating IP assign/unassign/delete");
162
-
163
- // --- Load balancer: delete returns Action ---
164
- const lbResp = await call("create_load_balancer", { name: `t-lb-${suffix}`, load_balancer_type: "lb11", location: best.location, labels: LABEL });
165
- await waitActions(lbResp, "create_lb");
166
- const delLb = await call("delete_load_balancer", { id: lbResp.load_balancer.id });
167
- if (!delLb?.action?.id) throw new Error(`delete_load_balancer returned no action: ${JSON.stringify(delLb).slice(0, 200)}`);
168
- await waitActions(delLb, "delete_lb");
169
- ok("delete_load_balancer returns Action", `command=${delLb.action.command}`);
170
-
171
- // --- Teardown in skill order ---
172
- await waitActions(await call("delete_server", { id: srv.id }), "delete_server");
173
- await call("unassign_primary_ip", { id: pip.id }).catch(() => {}); // already unassigned by server delete
174
- await call("delete_primary_ip", { id: pip.id });
175
- await call("delete_firewall", { id: fw.id });
176
- await call("delete_network", { id: net.id });
177
- ok("teardown via tools");
178
- } catch (e) {
179
- failed = e;
180
- console.error(`FAIL ${e.message}`);
181
- } finally {
182
- await client.close().catch(() => {});
183
- const left = await safetyNetCleanup();
184
- console.log(left.length ? `LEFTOVER RESOURCES: ${left.join(" ")}` : `cleanup verified: no hcloud-mcp-test=${suffix} resources remain`);
185
- console.log(`\n${results.length} steps passed${failed ? ", 1 failed" : ""}`);
186
- process.exit(failed || left.length ? 1 : 0);
187
- }
@@ -1,62 +0,0 @@
1
- /**
2
- * Integration test: spawn the MCP server and call list_locations via the MCP client.
3
- * Run from project root: node scripts/test-mcp-list.mjs
4
- * Uses HCLOUD_TOKEN from environment or .env.
5
- */
6
- import "dotenv/config";
7
- import { Client } from "@modelcontextprotocol/sdk/client/index.js";
8
- import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
9
- import { CallToolResultSchema } from "@modelcontextprotocol/sdk/types.js";
10
- import { fileURLToPath } from "url";
11
- import { dirname, join } from "path";
12
-
13
- const __dirname = dirname(fileURLToPath(import.meta.url));
14
- const projectRoot = join(__dirname, "..");
15
-
16
- const token = process.env.HCLOUD_TOKEN;
17
- if (!token) {
18
- console.error("Error: HCLOUD_TOKEN environment variable is required");
19
- process.exit(1);
20
- }
21
-
22
- const transport = new StdioClientTransport({
23
- command: "node",
24
- args: [join(projectRoot, "dist/index.js")],
25
- env: { ...process.env, HCLOUD_TOKEN: token },
26
- cwd: projectRoot,
27
- });
28
-
29
- const client = new Client(
30
- { name: "test-client", version: "1.0.0" },
31
- { capabilities: {} }
32
- );
33
-
34
- async function main() {
35
- console.log("Connecting to MCP server...");
36
- await client.connect(transport);
37
-
38
- console.log("Calling list_locations...");
39
- const result = await client.callTool(
40
- { name: "list_locations", arguments: {} },
41
- CallToolResultSchema
42
- );
43
- console.log("isError:", result.isError);
44
- if (result.content?.length) {
45
- for (const part of result.content) {
46
- if (part.type === "text") {
47
- const preview = part.text.slice(0, 500);
48
- console.log("content (preview):", preview + (part.text.length > 500 ? "..." : ""));
49
- }
50
- }
51
- } else {
52
- console.log("content:", result.content);
53
- }
54
-
55
- await client.close();
56
- process.exit(result.isError ? 1 : 0);
57
- }
58
-
59
- main().catch((err) => {
60
- console.error("Test failed:", err);
61
- process.exit(1);
62
- });