pi-lean-ctx 3.4.6 → 3.5.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/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # pi-lean-ctx
2
2
 
3
- [Pi Coding Agent](https://github.com/badlogic/pi-mono) extension with **first-class MCP support** — routes all tool output through [lean-ctx](https://leanctx.com) for **60–90% token savings** and exposes **25+ MCP tools** natively in Pi.
3
+ CLI-first [Pi Coding Agent](https://github.com/badlogic/pi-mono) extension that routes Pi’s built-in tools through [lean-ctx](https://leanctx.com) for **60–90% token savings**.
4
+
5
+ - **Default**: CLI-only (no MCP required)
6
+ - **Optional**: enable MCP tools (`LEAN_CTX_PI_ENABLE_MCP=1`) or run `lean-ctx init --agent pi --mode mcp`
4
7
 
5
8
  ## What it does
6
9
 
@@ -10,15 +13,27 @@ Overrides Pi's built-in tools to route them through `lean-ctx`:
10
13
 
11
14
  | Tool | Compression |
12
15
  |------|------------|
13
- | `bash` | All shell commands compressed via lean-ctx's 90+ patterns |
16
+ | `bash` | All shell commands compressed via lean-ctx's 95+ patterns |
14
17
  | `read` | Smart mode selection (full/map/signatures) based on file type and size |
15
18
  | `grep` | Results grouped and compressed via ripgrep + lean-ctx |
16
19
  | `find` | File listings compressed and .gitignore-aware |
17
20
  | `ls` | Directory output compressed |
18
21
 
19
- ### MCP Tools (Embedded Bridge)
22
+ ### Direct lean-ctx CLI tool
23
+
24
+ The extension registers a `lean_ctx` tool that runs `lean-ctx` directly (no nested compression).
25
+ Use it for commands like:
26
+
27
+ - `lean-ctx overview`
28
+ - `lean-ctx session …`
29
+ - `lean-ctx knowledge …`
30
+ - `lean-ctx gain` / `lean-ctx stats`
31
+ - `lean-ctx index …`
32
+
33
+ ### Optional MCP Tools (Embedded Bridge)
20
34
 
21
- Additionally, pi-lean-ctx spawns lean-ctx as an MCP server and registers all advanced tools directly in Pi:
35
+ By default, pi-lean-ctx does **not** start an MCP server. If enabled, it spawns `lean-ctx` as an MCP
36
+ server and registers advanced tools directly in Pi:
22
37
 
23
38
  | Tool | Purpose |
24
39
  |------|---------|
@@ -45,7 +60,7 @@ Additionally, pi-lean-ctx spawns lean-ctx as an MCP server and registers all adv
45
60
  | `ctx_cache` | Cache management |
46
61
  | `ctx_execute` | Direct command execution |
47
62
 
48
- These MCP tools are the same ones available in Cursor, Claude Code, VS Code, and all other supported editors now first-class in Pi.
63
+ If you don’t want MCP: keep it disabled and use the CLI overrides + `lean_ctx` tool only.
49
64
 
50
65
  ## Install
51
66
 
@@ -72,9 +87,10 @@ lean-ctx init --agent pi
72
87
 
73
88
  These tools invoke the `lean-ctx` binary via CLI with `LEAN_CTX_COMPRESS=1`. The output is parsed for compression stats and displayed with a token savings footer.
74
89
 
75
- ### MCP bridge (all other tools)
90
+ ### Optional MCP bridge (all other tools)
76
91
 
77
- On startup, pi-lean-ctx spawns the `lean-ctx` binary as an MCP server (JSON-RPC over stdio). It discovers available tools via `list_tools`, filters out those already covered by CLI overrides, and registers the rest as native Pi tools.
92
+ If you enable the MCP bridge, pi-lean-ctx spawns the `lean-ctx` binary as an MCP server (JSON-RPC over stdio).
93
+ It discovers available tools via `list_tools`, filters out those already covered by CLI overrides, and registers the rest as native Pi tools.
78
94
 
79
95
  If `lean-ctx` is already configured as an MCP server via [pi-mcp-adapter](https://github.com/nicobailon/pi-mcp-adapter) in `~/.pi/agent/mcp.json`, the embedded bridge is skipped to avoid duplicate tools.
80
96
 
@@ -82,6 +98,21 @@ If `lean-ctx` is already configured as an MCP server via [pi-mcp-adapter](https:
82
98
 
83
99
  If the MCP server process crashes, the bridge automatically reconnects (up to 3 attempts with exponential backoff). If reconnection fails, CLI-based tools continue working normally — only the advanced MCP tools become unavailable.
84
100
 
101
+ ## Enabling MCP (optional)
102
+
103
+ Set an environment variable and restart Pi:
104
+
105
+ ```bash
106
+ export LEAN_CTX_PI_ENABLE_MCP=1
107
+ pi
108
+ ```
109
+
110
+ Or configure MCP via `lean-ctx init`:
111
+
112
+ ```bash
113
+ lean-ctx init --agent pi --mode mcp
114
+ ```
115
+
85
116
  ## pi-mcp-adapter compatibility
86
117
 
87
118
  If you prefer using [pi-mcp-adapter](https://github.com/nicobailon/pi-mcp-adapter) to manage your MCP servers, lean-ctx integrates automatically:
@@ -134,7 +165,7 @@ The `read` tool automatically selects the optimal lean-ctx mode:
134
165
 
135
166
  Use `/lean-ctx` in Pi to check:
136
167
  - Which binary is being used
137
- - MCP bridge status (embedded vs. adapter, connected/disconnected)
168
+ - MCP bridge status (disabled / embedded / adapter)
138
169
  - Number and names of registered MCP tools
139
170
 
140
171
  ## Disabling specific tools
@@ -63,12 +63,28 @@ const grepSchema = Type.Object({
63
63
  limit: Type.Optional(Type.Number({ description: "Maximum number of matches (default: 100)" })),
64
64
  });
65
65
 
66
+ const leanCtxSchema = Type.Object({
67
+ args: Type.Array(
68
+ Type.String({
69
+ description:
70
+ "Arguments after 'lean-ctx'. Example: ['overview'] or ['knowledge','recall','Pi']",
71
+ }),
72
+ ),
73
+ });
74
+
66
75
  function shellQuote(value: string): string {
67
76
  if (!value) return "''";
68
77
  if (/^[A-Za-z0-9_./=:@,+%^-]+$/.test(value)) return value;
69
78
  return `'${value.replace(/'/g, `'\\''`)}'`;
70
79
  }
71
80
 
81
+ function envFlag(name: string): boolean {
82
+ const raw = process.env[name];
83
+ if (!raw) return false;
84
+ const v = raw.trim().toLowerCase();
85
+ return v === "1" || v === "true" || v === "yes" || v === "on";
86
+ }
87
+
72
88
  function resolveBinary(): string {
73
89
  const envBin = process.env.LEAN_CTX_BIN;
74
90
  if (envBin && existsSync(envBin)) return envBin;
@@ -259,7 +275,7 @@ async function execLeanCtx(pi: ExtensionAPI, args: string[]) {
259
275
  return result.stdout;
260
276
  }
261
277
 
262
- export default function (pi: ExtensionAPI) {
278
+ export default async function (pi: ExtensionAPI) {
263
279
  const baseBashTool = createBashToolDefinition(process.cwd(), {
264
280
  spawnHook: ({ command, cwd, env }) => {
265
281
  const bin = resolveBinary();
@@ -490,12 +506,33 @@ export default function (pi: ExtensionAPI) {
490
506
  },
491
507
  });
492
508
 
493
- const mcpBridge = new McpBridge(resolveBinary());
509
+ pi.registerTool({
510
+ name: "lean_ctx",
511
+ label: "lean_ctx",
512
+ description:
513
+ "Run lean-ctx CLI directly (CLI-first; no MCP required). "
514
+ + "Use this for advanced commands like session/knowledge/overview/gain/stats/index/pack.",
515
+ promptSnippet: "Run lean-ctx CLI directly",
516
+ parameters: leanCtxSchema,
517
+ async execute(_toolCallId, params) {
518
+ const output = await execLeanCtx(pi, params.args);
519
+ return {
520
+ content: [{ type: "text", text: output.trimEnd() }],
521
+ details: { source: "lean-ctx", args: params.args },
522
+ };
523
+ },
524
+ });
525
+
526
+ const enableMcpBridge = envFlag("LEAN_CTX_PI_ENABLE_MCP");
527
+ const adapterConfigured = isMcpAdapterConfigured();
528
+ const mcpBridge = enableMcpBridge && !adapterConfigured ? new McpBridge(resolveBinary()) : null;
494
529
 
495
- if (!isMcpAdapterConfigured()) {
496
- mcpBridge.start(pi).catch((err) => {
530
+ if (mcpBridge) {
531
+ try {
532
+ await mcpBridge.start(pi);
533
+ } catch (err) {
497
534
  console.error(`[pi-lean-ctx] MCP bridge startup failed: ${err}`);
498
- });
535
+ }
499
536
  }
500
537
 
501
538
  pi.registerCommand("lean-ctx", {
@@ -503,29 +540,37 @@ export default function (pi: ExtensionAPI) {
503
540
  handler: async (_args, ctx) => {
504
541
  const bin = resolveBinary();
505
542
  const found = existsSync(bin);
506
- const status = mcpBridge.getStatus();
543
+ const status = mcpBridge?.getStatus();
507
544
 
508
545
  const lines: string[] = [];
509
546
  lines.push(found ? `Binary: ${bin}` : "Binary: NOT FOUND — install: cargo install lean-ctx");
510
- lines.push(`MCP bridge: ${status.mode} (${status.connected ? "connected" : "disconnected"})`);
511
- lines.push(`Reconnect attempts: ${status.reconnectAttempts}`);
512
- lines.push(`MCP tools: ${status.toolCount} registered`);
513
- if (status.toolNames.length > 0) {
514
- lines.push(` ${status.toolNames.join(", ")}`);
515
- }
516
- if (status.lastHungTool) {
517
- lines.push(`Last hung tool: ${status.lastHungTool}`);
518
- }
519
- if (status.lastRetry) {
520
- lines.push(
521
- `Last retry: ${status.lastRetry.toolName} (${status.lastRetry.reason}) at ${status.lastRetry.timestamp}`,
522
- );
523
- }
524
- if (status.lastError) {
525
- lines.push(`Last bridge error: ${status.lastError}`);
547
+ if (adapterConfigured) {
548
+ lines.push("MCP bridge: adapter-configured (extension bridge disabled)");
549
+ } else if (!enableMcpBridge) {
550
+ lines.push("MCP bridge: disabled (CLI-first)");
551
+ lines.push(" Enable: set LEAN_CTX_PI_ENABLE_MCP=1 and restart Pi");
552
+ } else if (status) {
553
+ lines.push(`MCP bridge: ${status.mode} (${status.connected ? "connected" : "disconnected"})`);
554
+ lines.push(`Reconnect attempts: ${status.reconnectAttempts}`);
555
+ lines.push(`MCP tools: ${status.toolCount} registered`);
556
+ if (status.toolNames.length > 0) {
557
+ lines.push(` ${status.toolNames.join(", ")}`);
558
+ }
559
+ if (status.lastHungTool) {
560
+ lines.push(`Last hung tool: ${status.lastHungTool}`);
561
+ }
562
+ if (status.lastRetry) {
563
+ lines.push(
564
+ `Last retry: ${status.lastRetry.toolName} (${status.lastRetry.reason}) at ${status.lastRetry.timestamp}`,
565
+ );
566
+ }
567
+ if (status.lastError) {
568
+ lines.push(`Last bridge error: ${status.lastError}`);
569
+ }
526
570
  }
527
571
 
528
- ctx.ui.notify(lines.join("\n"), found && status.connected ? "info" : "warning");
572
+ const ok = found && (adapterConfigured || !enableMcpBridge || (status?.connected ?? false));
573
+ ctx.ui.notify(lines.join("\n"), ok ? "info" : "warning");
529
574
  },
530
575
  });
531
576
  }
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "pi-lean-ctx",
3
- "version": "3.4.6",
4
- "description": "Pi Coding Agent extension with first-class MCP support \u2014 routes bash, read, grep, find, and ls through lean-ctx CLI, and exposes all 49 lean-ctx MCP tools (ctx_session, ctx_knowledge, ctx_semantic_search, ctx_impact, ctx_architecture, ctx_workflow, ctx_gain, etc.) natively in Pi",
3
+ "version": "3.5.0",
4
+ "description": "Pi Coding Agent extension (CLI-first) \u2014 routes bash/read/grep/find/ls through lean-ctx CLI for strong token savings. Optional MCP bridge can register advanced tools.",
5
5
  "keywords": [
6
6
  "pi-package",
7
7
  "lean-ctx",
8
+ "cli",
8
9
  "token-optimization",
9
10
  "compression",
10
11
  "mcp"