@lefine/mcp 0.1.0 → 1.0.0-rc.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/LICENSE +1 -1
- package/README.md +94 -35
- package/bin/lefine-mcp.js +17 -0
- package/bin/route.js +16 -0
- package/package.json +9 -13
- package/dist/lefine/credentials.js +0 -63
- package/dist/lefine/mcp-bridge.js +0 -117
- package/dist/lefine-mcp.js +0 -46
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,60 +1,119 @@
|
|
|
1
1
|
# @lefine/mcp
|
|
2
2
|
|
|
3
|
-
Lefine
|
|
4
|
-
website pages, and make localized videos, with you approving anything that goes live.
|
|
3
|
+
Lefine as an MCP server for Claude, Cursor, VS Code, Codex and other AI apps. It does two things:
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
1. **Passes through** Lefine's tools (`https://dash.lefine.ai/v1/mcp`) with your sign-in, so the agent can
|
|
6
|
+
translate your app, edit pages and drafts, and launch new markets. The list comes from the server, so a
|
|
7
|
+
new action reaches you without updating this package.
|
|
8
|
+
2. **Adds local tools** that only a process on your machine can offer: upload a picture or a whole folder,
|
|
9
|
+
import your translation files, and read which Lefine project a code folder belongs to.
|
|
10
|
+
|
|
11
|
+
Nothing goes live without a person: publishing asks first, in the app or on a Lefine approval page.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
7
14
|
|
|
8
15
|
```sh
|
|
9
|
-
npx @lefine/
|
|
16
|
+
npx @lefine/mcp # in a terminal: the setup wizard
|
|
10
17
|
```
|
|
11
18
|
|
|
12
|
-
|
|
13
|
-
|
|
19
|
+
The wizard finds your AI apps (Claude Code, Cursor, VS Code, Windsurf, Codex, Gemini CLI, Zed), adds Lefine to
|
|
20
|
+
the ones you pick, checks that the server answers and tells you the one click each app needs to sign in.
|
|
21
|
+
`--list`, `--doctor`, `--remove`, `--yes`, `--readonly`, `--project`, `--print` work as in `npx lefine setup`.
|
|
14
22
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
**Claude Desktop** (`claude_desktop_config.json`), and any client that starts a local server:
|
|
23
|
+
To run it as a server by hand, in a client config that starts a process (Claude Desktop, some IDEs):
|
|
18
24
|
|
|
19
25
|
```json
|
|
20
|
-
{
|
|
21
|
-
"mcpServers": {
|
|
22
|
-
"lefine": { "command": "npx", "args": ["-y", "@lefine/mcp"] }
|
|
23
|
-
}
|
|
24
|
-
}
|
|
26
|
+
{ "mcpServers": { "lefine": { "command": "npx", "args": ["-y", "@lefine/mcp"] } } }
|
|
25
27
|
```
|
|
26
28
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
copy:
|
|
30
|
-
|
|
31
|
-
```sh
|
|
32
|
-
claude mcp add --transport http lefine https://dash.lefine.ai/v1/mcp
|
|
33
|
-
```
|
|
29
|
+
When stdin is not a terminal (an AI app started it) this package is the stdio server. Existing configs that say
|
|
30
|
+
`npx -y @lefine/mcp` or `npx -y lefine mcp` keep working. Needs Node 20 or newer.
|
|
34
31
|
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
**Sign in** with `npx @lefine/mcp setup` (or `npx lefine login`), or set `LEFINE_TOKEN` to a key from
|
|
33
|
+
Lefine, Settings > API keys. Tick Content or Studio on the key if the agent needs them.
|
|
37
34
|
|
|
38
|
-
##
|
|
35
|
+
## Tools
|
|
39
36
|
|
|
40
|
-
|
|
37
|
+
From the server (always in sync, listed with `tools/list` and kept for 5 minutes):
|
|
41
38
|
|
|
42
39
|
| Tool | What it does |
|
|
43
40
|
|---|---|
|
|
44
|
-
| `lefine_context` | The workspace, the modules that are on,
|
|
41
|
+
| `lefine_context` | The workspace, the modules that are on, what the key may read, write and publish. Call it first. |
|
|
45
42
|
| `lefine_find` | Find an action by words, with its input schema. |
|
|
43
|
+
| `lefine_lookup` | Find a page, key, media file, app, project, type or global by name ("z5k privacy page"). |
|
|
46
44
|
| `lefine_read` | Run an action that only reads. |
|
|
47
|
-
| `lefine_write` | Run an action that writes drafts. Nothing goes live. |
|
|
48
|
-
| `lefine_publish` | Run an action that goes live
|
|
45
|
+
| `lefine_write` | Run an action that writes drafts. Nothing goes live. `steps` runs up to 50 at once. |
|
|
46
|
+
| `lefine_publish` | Run an action that goes live. Asks a person first; a batch is one yes for the exact list. |
|
|
47
|
+
| `lefine_edit` | Change fields of one page, global or key by name, as a draft. `dryRun` shows the diff. |
|
|
48
|
+
| `lefine_launch`, `lefine_launch_status` | Take the workspace into a new market; read how it went. |
|
|
49
|
+
|
|
50
|
+
Added here, answered on your machine:
|
|
51
|
+
|
|
52
|
+
| Tool | What it does |
|
|
53
|
+
|---|---|
|
|
54
|
+
| `lefine_project` | Reads `lefine.ts` and `lefine.config.*` in the working folder (as text, never run) for the app and project ids, so the agent does not ask you. |
|
|
55
|
+
| `lefine_upload_file` `{path, project?, alt?, folder?}` | One png, jpg, gif, webp, avif, mp4 or PDF (not svg) to a Content project's media library. Answers `mediaId` and `url`. |
|
|
56
|
+
| `lefine_upload_folder` `{path, project?, pattern?, skip?}` | Up to 50 files per call, 4 at a time, as a table. `pattern` is a glob (`*` top level, `**/*.png` deep). `skip` takes the next batch. |
|
|
57
|
+
| `lefine_import_locale_files` `{path, app?, format?, locale?, namespace?, dryRun?, confirmPlan?}` | `.json` `.arb` `.yaml` `.yml` `.xml` `.xlf` `.xliff` `.properties` `.po` files into a Strings app. First call is a dry run: counts and a plan id, nothing written. The second call (`dryRun: false`) is a normal Lefine write: the server may ask a person to approve it. `confirmPlan` is only the plan id (a stale one stops the import), not an approval. Imports are drafts. |
|
|
58
|
+
| `lefine_find_similar_key`, `lefine_add_key` | For coding agents: find a key before adding one; add refuses duplicates and bad ICU. |
|
|
59
|
+
|
|
60
|
+
`project` and `app` take a name, a slug or an id; an unclear name lists the candidates. Left out, they come from
|
|
61
|
+
`lefine.config.*` / `lefine.ts` in the working folder, or the workspace's only project.
|
|
62
|
+
|
|
63
|
+
## Flags
|
|
49
64
|
|
|
50
|
-
|
|
65
|
+
```
|
|
66
|
+
--read-only Only reading tools. Hides write, publish, edit, launch and the local write tools,
|
|
67
|
+
and asks the server for a read ceiling (?level=read), so a tool this package does
|
|
68
|
+
not know still cannot write.
|
|
69
|
+
--toolsets=a,b Only these modules: strings, content, studio, social, sites, brain, integrations.
|
|
70
|
+
Actions of other modules are refused; local tools of other modules are hidden.
|
|
71
|
+
--allow-dir <folder> Let local tools read this folder too (repeatable).
|
|
72
|
+
--tools-ttl <seconds> How long the server's tool list is kept (default 300; 0 asks every time).
|
|
73
|
+
--version, --help
|
|
74
|
+
```
|
|
51
75
|
|
|
52
|
-
|
|
76
|
+
Environment: `LEFINE_TOKEN`, `LEFINE_API_URL`, `LEFINE_PROFILE`, `LEFINE_MCP_READ_ONLY=1`,
|
|
77
|
+
`LEFINE_MCP_TOOLSETS`, `LEFINE_MCP_ALLOW_DIRS`, `LEFINE_MCP_TOOLS_TTL`. Logs go to stderr; stdout carries only
|
|
78
|
+
MCP messages.
|
|
79
|
+
|
|
80
|
+
## Security
|
|
81
|
+
|
|
82
|
+
What the local tools can read and write, exactly:
|
|
83
|
+
|
|
84
|
+
- **Read: only inside the folder the server runs in**, plus each `--allow-dir`. `../`, absolute paths elsewhere and
|
|
85
|
+
symlinks that lead out are refused. In any letter case, `.git`, `.ssh`, `.aws`, `.gnupg`, `.kube`, `.docker`,
|
|
86
|
+
`.config`, `.lefine`, `node_modules`, `.env*`, `.git-credentials`, `.pgpass`, `.netrc`, `.npmrc`, `.pypirc`,
|
|
87
|
+
`.htpasswd`, `id_rsa*`, `id_ed25519*`, `*.pem`, `*.key`, `*.p12`, `*.pfx`, shell history and `credentials*` are never
|
|
88
|
+
read, even inside an allowed folder.
|
|
89
|
+
- **No broad root.** If the server starts in `/`, your home directory or a parent of it (`/Users`, `/home`), the local
|
|
90
|
+
file tools are off (one line on stderr) until you pass `--allow-dir <project folder>`. `--allow-dir` itself is
|
|
91
|
+
refused for `/`, home, its parents, `~/.ssh`, `~/.aws` and the like.
|
|
92
|
+
- **Which files:** uploads take png, jpg, gif, webp, avif, mp4 and PDF (not svg), 8 MB, and the first bytes must
|
|
93
|
+
match the name. Locale import takes only `.json .arb .yaml .yml .xml .xlf .xliff .properties .po`; `format`
|
|
94
|
+
chooses the parser and never widens what is read.
|
|
95
|
+
- **Write: only through the server.** The local tools never call a write on the API themselves. Uploads, imports and
|
|
96
|
+
`lefine_add_key` are sent as the server's own `lefine_write` call, so the server's approvals (a person says yes in
|
|
97
|
+
Lefine), its MCP ledger and the `--read-only` ceiling apply. The dry run of an import stays local and writes
|
|
98
|
+
nothing. A plan id is not consent.
|
|
99
|
+
- **The key stays on your machine.** It goes only to the server it was saved for and never appears in tool
|
|
100
|
+
output. `lefine.ts` and `lefine.config.*` are read as text: a cloned repository cannot run code by starting
|
|
101
|
+
this server. Text from your files is marked as data in answers; do not let an agent treat it as instructions.
|
|
102
|
+
- **Publishing asks a person first,** unless you turned that off in Lefine settings.
|
|
103
|
+
- Not covered: a folder renamed between the path check and the read could redirect the open. Someone who can
|
|
104
|
+
do that already has your account's files.
|
|
105
|
+
|
|
106
|
+
A 401 means the key is wrong or expired: run `npx @lefine/mcp setup`.
|
|
107
|
+
|
|
108
|
+
## Remote, without this package
|
|
109
|
+
|
|
110
|
+
Clients that speak remote MCP can skip the package and sign in with OAuth, no key to copy:
|
|
53
111
|
|
|
54
|
-
|
|
112
|
+
```sh
|
|
113
|
+
claude mcp add --transport http --scope user lefine https://dash.lefine.ai/v1/mcp
|
|
114
|
+
```
|
|
55
115
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
- `lefine-mcp --version`, `lefine-mcp --help`.
|
|
116
|
+
Then `/mcp` in Claude Code, pick lefine, Authenticate. Read-only: `https://dash.lefine.ai/v1/mcp?level=read`.
|
|
117
|
+
The local tools above need this package, because the remote server cannot see your disk.
|
|
59
118
|
|
|
60
|
-
|
|
119
|
+
Every action and what it needs: `npx lefine actions` (add words to filter).
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// `npx @lefine/mcp` is the friendly front door to Lefine's MCP server.
|
|
3
|
+
// In a terminal, no arguments: the setup wizard (same code as `npx lefine setup`).
|
|
4
|
+
// `setup`, `--remove`, `--list`, `--doctor`, `--print`: the same as `lefine setup ...`.
|
|
5
|
+
// Started by an AI app (stdin is not a terminal): the stdio server, as before. Existing configs that say
|
|
6
|
+
// `npx -y @lefine/mcp` keep working.
|
|
7
|
+
import { readFileSync } from "node:fs";
|
|
8
|
+
import { createRequire } from "node:module";
|
|
9
|
+
import { dirname, join } from "node:path";
|
|
10
|
+
import { pathToFileURL } from "node:url";
|
|
11
|
+
import { routeArgs } from "./route.js";
|
|
12
|
+
|
|
13
|
+
const manifest = createRequire(import.meta.url).resolve("lefine/package.json");
|
|
14
|
+
const { bin } = JSON.parse(readFileSync(manifest, "utf-8"));
|
|
15
|
+
const args = process.argv.slice(2);
|
|
16
|
+
process.argv.splice(2, args.length, ...routeArgs(args, { stdinIsTTY: process.stdin.isTTY === true, stdoutIsTTY: process.stdout.isTTY === true }));
|
|
17
|
+
await import(pathToFileURL(join(dirname(manifest), bin.lefine)).href);
|
package/bin/route.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Which `lefine` command an `npx @lefine/mcp ...` call means. Pure, so a test can read it.
|
|
2
|
+
//
|
|
3
|
+
// started by an AI app (stdin is not a terminal), or `mcp`: the stdio server, as always
|
|
4
|
+
// `setup` and its flags (--remove --list --doctor --print --yes ...): the setup wizard, same as `lefine setup`
|
|
5
|
+
// no arguments in a terminal: the setup wizard (a person typed `npx @lefine/mcp`)
|
|
6
|
+
const SETUP_FLAGS = new Set(["--remove", "--list", "--doctor", "--print", "--yes", "-y", "--client", "--readonly", "--project", "--local", "--json", "--no-backup", "--no-check", "--api-url"]);
|
|
7
|
+
|
|
8
|
+
/** Arguments to hand to the `lefine` bin, after node and the script. */
|
|
9
|
+
export function routeArgs(args, { stdinIsTTY, stdoutIsTTY }) {
|
|
10
|
+
const [first] = args;
|
|
11
|
+
if (first === "setup") return args;
|
|
12
|
+
if (first === "mcp") return args;
|
|
13
|
+
if (first !== undefined && SETUP_FLAGS.has(first.split("=")[0])) return ["setup", ...args];
|
|
14
|
+
if (args.length === 0 && stdinIsTTY && stdoutIsTTY) return ["setup"];
|
|
15
|
+
return ["mcp", ...args];
|
|
16
|
+
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lefine/mcp",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Lefine
|
|
3
|
+
"version": "1.0.0-rc.0",
|
|
4
|
+
"description": "Set up Lefine in your AI apps with one command (npx @lefine/mcp), and the stdio server an app starts",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"bin": {
|
|
8
|
-
"lefine-mcp": "./
|
|
8
|
+
"lefine-mcp": "./bin/lefine-mcp.js"
|
|
9
9
|
},
|
|
10
10
|
"files": [
|
|
11
|
-
"
|
|
11
|
+
"bin",
|
|
12
12
|
"README.md",
|
|
13
13
|
"LICENSE"
|
|
14
14
|
],
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"repository": {
|
|
19
19
|
"type": "git",
|
|
20
|
-
"url": "https://github.com/lefinehq/
|
|
20
|
+
"url": "git+https://github.com/lefinehq/sdk.git",
|
|
21
21
|
"directory": "packages/lefine-mcp"
|
|
22
22
|
},
|
|
23
23
|
"homepage": "https://lefine.ai",
|
|
@@ -30,16 +30,12 @@
|
|
|
30
30
|
"claude"
|
|
31
31
|
],
|
|
32
32
|
"scripts": {
|
|
33
|
-
"
|
|
34
|
-
"typecheck": "tsc --noEmit",
|
|
35
|
-
"clean": "rm -rf dist",
|
|
36
|
-
"prepublishOnly": "npm run clean && npm run build"
|
|
37
|
-
},
|
|
38
|
-
"devDependencies": {
|
|
39
|
-
"@types/node": "^20.19.43",
|
|
40
|
-
"typescript": "~5.9.2"
|
|
33
|
+
"check:package": "bunx publint@0.3.25 --strict"
|
|
41
34
|
},
|
|
42
35
|
"engines": {
|
|
43
36
|
"node": ">=20"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"lefine": "^0.10.0"
|
|
44
40
|
}
|
|
45
41
|
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Lefine credentials: `~/.lefine/credentials.json` (dir 0700, file 0600).
|
|
3
|
-
* `LEFINE_TOKEN` wins over the file so CI never needs one.
|
|
4
|
-
*/
|
|
5
|
-
import { chmodSync, existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
6
|
-
import { homedir } from "node:os";
|
|
7
|
-
import { join } from "node:path";
|
|
8
|
-
export const DEFAULT_API_URL = "https://dash.lefine.ai";
|
|
9
|
-
export const DEFAULT_CDN_URL = "https://cdn.lefine.ai";
|
|
10
|
-
/** Read per call so tests (and users) can point `LEFINE_HOME` elsewhere. */
|
|
11
|
-
const configDir = () => process.env.LEFINE_HOME || join(homedir(), ".lefine");
|
|
12
|
-
export const credentialsPath = () => join(configDir(), "credentials.json");
|
|
13
|
-
export function saveCredentials(creds) {
|
|
14
|
-
const dir = configDir();
|
|
15
|
-
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
16
|
-
chmodSync(dir, 0o700);
|
|
17
|
-
const file = credentialsPath();
|
|
18
|
-
// `mode` only applies when the file is created; chmod covers an existing looser one.
|
|
19
|
-
writeFileSync(file, JSON.stringify(creds, null, 2) + "\n", { encoding: "utf-8", mode: 0o600 });
|
|
20
|
-
chmodSync(file, 0o600);
|
|
21
|
-
}
|
|
22
|
-
export function loadCredentials() {
|
|
23
|
-
const file = credentialsPath();
|
|
24
|
-
if (!existsSync(file))
|
|
25
|
-
return null;
|
|
26
|
-
try {
|
|
27
|
-
const parsed = JSON.parse(readFileSync(file, "utf-8"));
|
|
28
|
-
return typeof parsed.token === "string" && typeof parsed.apiUrl === "string"
|
|
29
|
-
? parsed
|
|
30
|
-
: null;
|
|
31
|
-
}
|
|
32
|
-
catch {
|
|
33
|
-
return null;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
export function clearCredentials() {
|
|
37
|
-
const file = credentialsPath();
|
|
38
|
-
if (!existsSync(file))
|
|
39
|
-
return false;
|
|
40
|
-
unlinkSync(file);
|
|
41
|
-
return true;
|
|
42
|
-
}
|
|
43
|
-
export const resolveApiUrl = (flag) => (flag || process.env.LEFINE_API_URL || DEFAULT_API_URL).replace(/\/+$/, "");
|
|
44
|
-
export function resolveLefineAuth(options) {
|
|
45
|
-
if (options.token) {
|
|
46
|
-
return { token: options.token, apiUrl: resolveApiUrl(options.apiUrl), source: "flag" };
|
|
47
|
-
}
|
|
48
|
-
if (process.env.LEFINE_TOKEN) {
|
|
49
|
-
return { token: process.env.LEFINE_TOKEN, apiUrl: resolveApiUrl(options.apiUrl), source: "env" };
|
|
50
|
-
}
|
|
51
|
-
const stored = loadCredentials();
|
|
52
|
-
if (stored) {
|
|
53
|
-
// A saved key is only ever sent to the server it was saved for. Any other
|
|
54
|
-
// origin (flag, env var) must not receive it.
|
|
55
|
-
const saved = stored.apiUrl.replace(/\/+$/, "");
|
|
56
|
-
const override = options.apiUrl || process.env.LEFINE_API_URL;
|
|
57
|
-
if (override && resolveApiUrl(override) !== saved) {
|
|
58
|
-
throw new Error(`This key belongs to ${saved}. Run \`lefine login --api-url ${resolveApiUrl(override)}\` to use another server.`);
|
|
59
|
-
}
|
|
60
|
-
return { token: stored.token, apiUrl: saved, source: "file" };
|
|
61
|
-
}
|
|
62
|
-
return null;
|
|
63
|
-
}
|
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The Lefine MCP bridge: stdio on one side, Lefine's remote MCP server (POST /v1/mcp) on the other.
|
|
3
|
-
* For clients that only start local servers (Claude Desktop config, some IDEs). It adds the access key
|
|
4
|
-
* and forwards each message as it is; the server holds every tool and every rule, so the bridge never
|
|
5
|
-
* needs a release when an action is added.
|
|
6
|
-
*
|
|
7
|
-
* Both MCP eras pass through. A modern (2026-07-28) message names its version in `_meta`, and the bridge
|
|
8
|
-
* mirrors it in the MCP-Protocol-Version, Mcp-Method and Mcp-Name headers the server checks. A legacy
|
|
9
|
-
* message gets the version the `initialize` answer agreed on.
|
|
10
|
-
*
|
|
11
|
-
* stdout carries only JSON-RPC, one message per line; anything for people goes to stderr.
|
|
12
|
-
*/
|
|
13
|
-
const NO_KEY = "Lefine is not signed in. Run `npx @lefine/cli login`, or set LEFINE_TOKEN to a key from Settings > API keys (tick Content or Studio there if the agent needs them).";
|
|
14
|
-
/** Header-safe ASCII, or the `=?base64?...?=` sentinel the spec defines for other values. */
|
|
15
|
-
function headerValue(value) {
|
|
16
|
-
if (/^[\x20-\x7e]*$/.test(value))
|
|
17
|
-
return value;
|
|
18
|
-
const bytes = new TextEncoder().encode(value);
|
|
19
|
-
return `=?base64?${Buffer.from(bytes).toString("base64")}?=`;
|
|
20
|
-
}
|
|
21
|
-
export function createBridge(deps) {
|
|
22
|
-
/** The legacy version `initialize` agreed on; sent on every later legacy request. */
|
|
23
|
-
let legacyVersion;
|
|
24
|
-
const reply = (id, code, message) => {
|
|
25
|
-
if (id === undefined || id === null)
|
|
26
|
-
return;
|
|
27
|
-
deps.write(JSON.stringify({ jsonrpc: "2.0", id, error: { code, message } }));
|
|
28
|
-
};
|
|
29
|
-
async function handle(line) {
|
|
30
|
-
const text = line.trim();
|
|
31
|
-
if (!text)
|
|
32
|
-
return;
|
|
33
|
-
let msg;
|
|
34
|
-
try {
|
|
35
|
-
msg = JSON.parse(text);
|
|
36
|
-
}
|
|
37
|
-
catch {
|
|
38
|
-
deps.write(JSON.stringify({ jsonrpc: "2.0", id: null, error: { code: -32700, message: "The message is not JSON." } }));
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
// A response from the client (to a server request) has no method; this server never sends requests.
|
|
42
|
-
if (typeof msg.method !== "string")
|
|
43
|
-
return;
|
|
44
|
-
if (!deps.auth) {
|
|
45
|
-
reply(msg.id, -32001, NO_KEY);
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
const meta = (msg.params?._meta ?? {});
|
|
49
|
-
const modernVersion = typeof meta["io.modelcontextprotocol/protocolVersion"] === "string" ? meta["io.modelcontextprotocol/protocolVersion"] : undefined;
|
|
50
|
-
const headers = {
|
|
51
|
-
"content-type": "application/json",
|
|
52
|
-
accept: "application/json, text/event-stream",
|
|
53
|
-
authorization: `Bearer ${deps.auth.token}`,
|
|
54
|
-
"user-agent": `lefine-mcp/${deps.clientVersion}`,
|
|
55
|
-
};
|
|
56
|
-
if (modernVersion) {
|
|
57
|
-
headers["mcp-protocol-version"] = modernVersion;
|
|
58
|
-
headers["mcp-method"] = msg.method;
|
|
59
|
-
const name = msg.params?.name ?? msg.params?.uri;
|
|
60
|
-
if (typeof name === "string")
|
|
61
|
-
headers["mcp-name"] = headerValue(name);
|
|
62
|
-
}
|
|
63
|
-
else if (legacyVersion && msg.method !== "initialize") {
|
|
64
|
-
headers["mcp-protocol-version"] = legacyVersion;
|
|
65
|
-
}
|
|
66
|
-
let res;
|
|
67
|
-
try {
|
|
68
|
-
res = await deps.fetch(`${deps.auth.apiUrl}/v1/mcp`, { method: "POST", headers, body: text });
|
|
69
|
-
}
|
|
70
|
-
catch {
|
|
71
|
-
reply(msg.id, -32603, `Could not reach Lefine at ${deps.auth.apiUrl}. Check the connection and try again.`);
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
if (res.status === 202)
|
|
75
|
-
return;
|
|
76
|
-
const body = await res.text();
|
|
77
|
-
let parsed = null;
|
|
78
|
-
try {
|
|
79
|
-
parsed = body ? JSON.parse(body) : null;
|
|
80
|
-
}
|
|
81
|
-
catch {
|
|
82
|
-
parsed = null;
|
|
83
|
-
}
|
|
84
|
-
if (!parsed) {
|
|
85
|
-
reply(msg.id, -32603, `Lefine answered HTTP ${res.status} without a JSON-RPC message.`);
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
if (msg.method === "initialize" && typeof parsed.result?.protocolVersion === "string")
|
|
89
|
-
legacyVersion = parsed.result.protocolVersion;
|
|
90
|
-
if (res.status === 401)
|
|
91
|
-
deps.log("lefine-mcp: the key was refused. Run `npx @lefine/cli login` again or check LEFINE_TOKEN.");
|
|
92
|
-
// The server answers some refusals (no key, bad JSON) before it reads the id; the client is waiting for its own.
|
|
93
|
-
if (parsed.id === null && msg.id !== undefined && msg.id !== null)
|
|
94
|
-
parsed = { ...parsed, id: msg.id };
|
|
95
|
-
deps.write(JSON.stringify(parsed));
|
|
96
|
-
}
|
|
97
|
-
return { handle };
|
|
98
|
-
}
|
|
99
|
-
/** Reads newline-delimited JSON-RPC from a stream and handles each message; messages may run concurrently. */
|
|
100
|
-
export async function runBridge(input, deps) {
|
|
101
|
-
const bridge = createBridge(deps);
|
|
102
|
-
const pending = new Set();
|
|
103
|
-
let buffer = "";
|
|
104
|
-
for await (const chunk of input) {
|
|
105
|
-
buffer += chunk.toString();
|
|
106
|
-
let at;
|
|
107
|
-
while ((at = buffer.indexOf("\n")) >= 0) {
|
|
108
|
-
const line = buffer.slice(0, at);
|
|
109
|
-
buffer = buffer.slice(at + 1);
|
|
110
|
-
const p = bridge.handle(line).finally(() => pending.delete(p));
|
|
111
|
-
pending.add(p);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
if (buffer.trim())
|
|
115
|
-
pending.add(bridge.handle(buffer));
|
|
116
|
-
await Promise.all(pending);
|
|
117
|
-
}
|
package/dist/lefine-mcp.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* `lefine-mcp`: Lefine's MCP server for clients that start a local process (Claude Desktop, IDE configs).
|
|
4
|
-
* It bridges stdio to https://dash.lefine.ai/v1/mcp with your key. Clients that speak remote MCP can
|
|
5
|
-
* use that URL directly instead.
|
|
6
|
-
*
|
|
7
|
-
* Key: LEFINE_TOKEN, else the one `lefine login` saved. No project env files are read, on purpose: a
|
|
8
|
-
* cloned project must not be able to redirect the key (same rule as the `lefine` CLI).
|
|
9
|
-
*/
|
|
10
|
-
import { readFileSync } from "node:fs";
|
|
11
|
-
import { resolveLefineAuth } from "./lefine/credentials.js";
|
|
12
|
-
import { runBridge } from "./lefine/mcp-bridge.js";
|
|
13
|
-
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf-8"));
|
|
14
|
-
const args = process.argv.slice(2);
|
|
15
|
-
if (args.includes("--version")) {
|
|
16
|
-
process.stdout.write(`${pkg.version}\n`);
|
|
17
|
-
process.exit(0);
|
|
18
|
-
}
|
|
19
|
-
if (args.includes("--help")) {
|
|
20
|
-
process.stderr.write([
|
|
21
|
-
"lefine-mcp: Lefine as a local MCP server (stdio).",
|
|
22
|
-
"",
|
|
23
|
-
"Sign in once: npx @lefine/cli login (or set LEFINE_TOKEN)",
|
|
24
|
-
'Claude Desktop: { "mcpServers": { "lefine": { "command": "npx", "args": ["-y", "@lefine/mcp"] } } }',
|
|
25
|
-
"Remote clients can skip this and use https://dash.lefine.ai/v1/mcp with the header Authorization: Bearer <key>.",
|
|
26
|
-
"",
|
|
27
|
-
].join("\n"));
|
|
28
|
-
process.exit(0);
|
|
29
|
-
}
|
|
30
|
-
let auth = null;
|
|
31
|
-
try {
|
|
32
|
-
const found = resolveLefineAuth({});
|
|
33
|
-
auth = found ? { token: found.token, apiUrl: found.apiUrl } : null;
|
|
34
|
-
}
|
|
35
|
-
catch (error) {
|
|
36
|
-
process.stderr.write(`lefine-mcp: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
37
|
-
}
|
|
38
|
-
if (!auth)
|
|
39
|
-
process.stderr.write("lefine-mcp: not signed in. Run `npx @lefine/cli login` or set LEFINE_TOKEN.\n");
|
|
40
|
-
await runBridge(process.stdin, {
|
|
41
|
-
auth,
|
|
42
|
-
fetch,
|
|
43
|
-
write: (line) => process.stdout.write(`${line}\n`),
|
|
44
|
-
log: (line) => process.stderr.write(`${line}\n`),
|
|
45
|
-
clientVersion: pkg.version,
|
|
46
|
-
});
|