kleinanzeigen-mcp-v2 1.2.1 → 1.2.2

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.en.md CHANGED
@@ -28,9 +28,9 @@ For search only, leave out the `KLEINANZEIGEN_BOT_*` variables. For your own ads
28
28
 
29
29
  1. Install the [kleinanzeigen-bot](https://github.com/Second-Hand-Friends/kleinanzeigen-bot/releases) binary and put
30
30
  it on `PATH` (or set `KLEINANZEIGEN_BOT_PATH`). Drafts and category fields need the `preview` release.
31
- 2. Sign in once in a terminal (SMS code or CAPTCHA may be required). Read the password without echo so it stays out
32
- of your shell history: `printf 'Password: ' && read -rs KLEINANZEIGEN_BOT_PASSWORD && export KLEINANZEIGEN_BOT_PASSWORD`,
33
- then `KLEINANZEIGEN_BOT_USERNAME=you@example.com npx -y kleinanzeigen-mcp-v2@1 login`. The browser profile in the
31
+ 2. Sign in once in a terminal (SMS code or CAPTCHA may be required): `npx -y kleinanzeigen-mcp-v2@1 login`, run
32
+ outside a checkout of this repo. If `KLEINANZEIGEN_BOT_USERNAME` or `KLEINANZEIGEN_BOT_PASSWORD` is not set there,
33
+ it asks for them (the password without echo) and passes them to that run only. The browser profile in the
34
34
  workspace works like a password; keep the workspace out of cloud sync folders.
35
35
  3. Ask your assistant to run `kleinanzeigen_check_setup`.
36
36
 
package/README.md CHANGED
@@ -147,16 +147,15 @@ Die Dateinamen auf der Release-Seite folgen dem Muster `kleinanzeigen-bot-<os>-<
147
147
  ### 3. Einmal anmelden
148
148
 
149
149
  Wenn sich ein Browser zum ersten Mal anmeldet, verlangt Kleinanzeigen einen Code per SMS oder E-Mail, manchmal auch
150
- ein CAPTCHA. Das erledigst du einmal im Terminal, mit denselben Umgebungsvariablen wie dein MCP-Server. Tipp das
151
- Passwort nicht direkt in die Befehlszeile, sonst landet es in deiner Shell-History. Lies es so verdeckt ein:
150
+ ein CAPTCHA. Das erledigst du einmal im Terminal. Führ den Befehl außerhalb eines Checkouts dieses Repos aus (etwa in
151
+ deinem Home-Ordner), sonst findet `npx` das Kommando nicht:
152
152
 
153
153
  ```bash
154
- printf 'Kleinanzeigen-Passwort: ' && read -rs KLEINANZEIGEN_BOT_PASSWORD && export KLEINANZEIGEN_BOT_PASSWORD && echo
154
+ npx -y kleinanzeigen-mcp-v2@1 login
155
155
  ```
156
156
 
157
- ```bash
158
- KLEINANZEIGEN_BOT_USERNAME=du@example.com npx -y kleinanzeigen-mcp-v2@1 login
159
- ```
157
+ Die Umgebungsvariablen deines MCP-Servers gelten im Terminal nicht. Fehlen `KLEINANZEIGEN_BOT_USERNAME` oder
158
+ `KLEINANZEIGEN_BOT_PASSWORD`, fragt `login` danach (das Passwort verdeckt) und gibt sie nur an diesen Lauf weiter.
160
159
 
161
160
  Es öffnet sich ein Browserfenster. Schließ dort die Anmeldung ab und drück dann im Terminal ENTER. Die Sitzung wird
162
161
  im Browserprofil des Arbeitsordners gespeichert (`~/Kleinanzeigen/.browser-profile`), spätere Läufe arbeiten deshalb
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
  import { spawn } from "node:child_process";
3
+ import path from "node:path";
3
4
  import { parseArgs } from "node:util";
4
5
  import { constants } from "node:fs";
5
6
  import { access, chmod, copyFile, mkdir, readFile, readdir, realpath, rename, rm, stat, writeFile } from "node:fs/promises";
6
- import path from "node:path";
7
7
  import { setTimeout as setTimeout$1 } from "node:timers/promises";
8
8
  import { pino } from "pino";
9
9
  import { parse, stringify } from "yaml";
@@ -11,6 +11,7 @@ import os from "node:os";
11
11
  import { z } from "zod";
12
12
  import { CLIENT_CAPABILITIES_META_KEY, McpServer, ProtocolError, ProtocolErrorCode, ResourceTemplate, acceptedContent, inputRequired, inputResponse } from "@modelcontextprotocol/server";
13
13
  import { parse as parse$1 } from "node-html-parser";
14
+ import { createInterface } from "node:readline/promises";
14
15
  // src/logger.ts
15
16
  /**
16
17
  * Logs go to stderr only: on stdio, stdout carries protocol messages exclusively.
@@ -419,6 +420,23 @@ async function credentialValues(config) {
419
420
  for (const v of [login.username, login.password]) if (typeof v === "string" && !v.includes("${")) values.push(v);
420
421
  return [...new Set(values.filter((v) => typeof v === "string" && v.trim().length >= 4))];
421
422
  }
423
+ /**
424
+ * The login fields of config.yaml whose ${VAR} placeholder names an environment variable that is
425
+ * not set. kleinanzeigen-bot refuses to start then, so `login` asks for them first.
426
+ */
427
+ function missingCredentials(doc, env = process.env) {
428
+ const login = record$1(doc.login);
429
+ const missing = [];
430
+ for (const field of ["username", "password"]) {
431
+ const value = login[field];
432
+ if (typeof value !== "string") continue;
433
+ for (const [, variable] of value.matchAll(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g)) if (variable && !env[variable] && !missing.some((m) => m.variable === variable)) missing.push({
434
+ field,
435
+ variable
436
+ });
437
+ }
438
+ return missing;
439
+ }
422
440
  /** Records a successful interactive sign-in (see LOGIN_MARKER_FILE). */
423
441
  async function markLogin(config) {
424
442
  await writeFile(path.join(config.workspace, LOGIN_MARKER_FILE), `${(/* @__PURE__ */ new Date()).toISOString()}\n`, { mode: 384 });
@@ -865,7 +883,7 @@ function loadConfig(env = process.env) {
865
883
  };
866
884
  }
867
885
  // package.json
868
- var version = "1.2.1";
886
+ var version = "1.2.2";
869
887
  // src/prompts/index.ts
870
888
  const text = (lines) => lines.filter((l) => typeof l === "string" && l !== "").join("\n");
871
889
  function registerPrompts({ server, config }) {
@@ -4298,8 +4316,53 @@ function createServer({ config, fetch }) {
4298
4316
  registerPrompts(deps);
4299
4317
  return server;
4300
4318
  }
4319
+ // src/util/prompt.ts
4320
+ var PromptCancelled = class extends Error {
4321
+ name = "PromptCancelled";
4322
+ };
4323
+ /** Asks for one line in the terminal. With `hidden`, the input is not echoed (for passwords). */
4324
+ async function ask(question, { hidden = false } = {}) {
4325
+ if (!hidden) {
4326
+ const rl = createInterface({
4327
+ input: process.stdin,
4328
+ output: process.stdout
4329
+ });
4330
+ try {
4331
+ return (await rl.question(question)).trim();
4332
+ } finally {
4333
+ rl.close();
4334
+ }
4335
+ }
4336
+ const stdin = process.stdin;
4337
+ process.stdout.write(question);
4338
+ return new Promise((resolve, reject) => {
4339
+ let value = "";
4340
+ const finish = (err) => {
4341
+ stdin.off("data", onData);
4342
+ stdin.setRawMode(false);
4343
+ stdin.pause();
4344
+ process.stdout.write("\n");
4345
+ if (err) reject(err);
4346
+ else resolve(value);
4347
+ };
4348
+ const onData = (chunk) => {
4349
+ for (const ch of chunk) {
4350
+ if (ch === "\r" || ch === "\n") return finish();
4351
+ if (ch === "" || ch === "") return finish(new PromptCancelled());
4352
+ if (ch === "" || ch === "\b") value = value.slice(0, -1);
4353
+ else if (ch >= " ") value += ch;
4354
+ }
4355
+ };
4356
+ stdin.setRawMode(true);
4357
+ stdin.setEncoding("utf8");
4358
+ stdin.on("data", onData);
4359
+ stdin.resume();
4360
+ });
4361
+ }
4301
4362
  // src/index.ts
4302
4363
  const BOT_INSTALL_URL = "https://github.com/Second-Hand-Friends/kleinanzeigen-bot#installation";
4364
+ /** Where kleinanzeigen-bot writes its log, relative to the workspace it runs in. */
4365
+ const BOT_LOG_FILE = "kleinanzeigen-bot.log";
4303
4366
  const HELP = `kleinanzeigen-mcp-v2 ${SERVER_VERSION}
4304
4367
 
4305
4368
  Aufruf:
@@ -4311,6 +4374,35 @@ Aufruf:
4311
4374
  --help, -h zeigt diese Hilfe
4312
4375
 
4313
4376
  Die Konfiguration kommt aus Umgebungsvariablen, siehe .env.example und README.md.`;
4377
+ /**
4378
+ * kleinanzeigen-bot stops with only a line in its log file when a ${VAR} placeholder of config.yaml
4379
+ * is unset, and the MCP client's environment does not reach this terminal. So ask for those values
4380
+ * here and hand them to the bot alone. Returns undefined when the user gave none.
4381
+ */
4382
+ async function askForCredentials(missing, configFile) {
4383
+ const values = {};
4384
+ if (missing.length === 0) return values;
4385
+ console.log(`\n${configFile} liest die Zugangsdaten aus ${missing.map((m) => m.variable).join(" und ")}, hier nicht gesetzt.
4386
+ Gib sie ein (sie gehen nur an diesen Lauf von kleinanzeigen-bot und werden nicht gespeichert):`);
4387
+ try {
4388
+ for (const { field, variable } of missing) {
4389
+ const value = await ask(field === "password" ? "Kleinanzeigen-Passwort: " : "Kleinanzeigen-E-Mail: ", { hidden: field === "password" });
4390
+ if (!value) {
4391
+ console.error(`Ohne ${variable} kann sich kleinanzeigen-bot nicht anmelden. Abgebrochen.`);
4392
+ return;
4393
+ }
4394
+ values[variable] = value;
4395
+ }
4396
+ } catch (err) {
4397
+ if (err instanceof PromptCancelled) {
4398
+ console.error("Abgebrochen.");
4399
+ return;
4400
+ }
4401
+ throw err;
4402
+ }
4403
+ console.log("");
4404
+ return values;
4405
+ }
4314
4406
  /** Runs kleinanzeigen-bot's interactive login in this terminal, with the workspace's browser profile. */
4315
4407
  async function login(config) {
4316
4408
  if (!process.stdin.isTTY) {
@@ -4318,11 +4410,14 @@ async function login(config) {
4318
4410
  return 1;
4319
4411
  }
4320
4412
  const ws = await ensureWorkspace(config);
4321
- const runConfig = await writeRunConfig(config, { headless: false });
4322
4413
  console.log(`Arbeitsordner: ${ws.dir}`);
4414
+ const credentials = await askForCredentials(missingCredentials(await readConfigDoc(config)), ws.configFile);
4415
+ if (!credentials) return 1;
4416
+ const runConfig = await writeRunConfig(config, { headless: false });
4323
4417
  console.log("Es öffnet sich ein Browserfenster. Melde dich dort bei Kleinanzeigen an (inklusive SMS-Code");
4324
4418
  console.log("oder CAPTCHA), komm dann hierher zurück und drück ENTER.\n");
4325
4419
  const bin = await resolveBotPath(config.botPath);
4420
+ let started = true;
4326
4421
  const code = await new Promise((resolve) => {
4327
4422
  const child = spawn(bin, [
4328
4423
  "login",
@@ -4331,15 +4426,21 @@ async function login(config) {
4331
4426
  "--lang=en"
4332
4427
  ], {
4333
4428
  cwd: ws.dir,
4429
+ env: {
4430
+ ...process.env,
4431
+ ...credentials
4432
+ },
4334
4433
  stdio: "inherit"
4335
4434
  });
4336
4435
  child.on("error", (err) => {
4337
4436
  console.error(err.code === "ENOENT" ? `kleinanzeigen-bot wurde unter "${config.botPath}" nicht gefunden. Installation: ${BOT_INSTALL_URL}` : String(err));
4437
+ started = false;
4338
4438
  resolve(1);
4339
4439
  });
4340
4440
  child.on("close", (code) => resolve(code ?? 1));
4341
4441
  });
4342
4442
  if (code === 0) await markLogin(config).catch(() => void 0);
4443
+ else if (started) console.error(`\nkleinanzeigen-bot hat mit Code ${code} abgebrochen. Details: ${path.join(ws.dir, BOT_LOG_FILE)}`);
4343
4444
  return code;
4344
4445
  }
4345
4446
  async function main() {