@octocodeai/freellama-mcp-server 0.5.0 → 0.6.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.
@@ -0,0 +1,108 @@
1
+ # FreeLlama MCP configuration
2
+
3
+ Settings for the [FreeLlama MCP server](README.md).
4
+
5
+ ## Environment variables
6
+
7
+ The following settings have environment-variable overrides. Set them in the MCP client's
8
+ server-launch config (for example `.mcp.json`'s `env` block) or the launching shell.
9
+
10
+ | Variable | Default | Affects |
11
+ |---|---|---|
12
+ | `FREELLAMA_OLLAMA_ENDPOINT` | `http://127.0.0.1:11434` | Default Ollama endpoint for `doctor`, the `models` detail/raw/library views, and all `ollama_*` tools |
13
+ | `FREELLAMA_SERVE_ENDPOINT` | `http://127.0.0.1:11435` | Default `endpoint` for serve-backed tools and `delegate_research` |
14
+ | `FREELLAMA_MCP_DEFAULT_MODEL` | unset (routes to an installed coding model) | `delegate_research`'s default `model` |
15
+ | `FREELLAMA_MCP_DEFAULT_ADAPTER` | `bash` | `delegate_research`'s adapter (`octocode` to switch) |
16
+ | `FREELLAMA_MCP_MAX_TURNS` | `8` | Max agent turns for `delegate_research` |
17
+ | `FREELLAMA_MCP_DELEGATE_TIMEOUT_SECONDS` | `180` | Whole-subprocess timeout for `delegate_research` |
18
+ | `FREELLAMA_MCP_PULL_TIMEOUT_SECONDS` | `1200` | Default `ollama_manage` `"pull"` timeout |
19
+ | `FREELLAMA_MCP_FETCH_TIMEOUT_SECONDS` | `30` | Timeout for other direct Ollama HTTP calls |
20
+ | `FREELLAMA_MCP_ALLOWED_ROOTS` | checkout: this repository; published: must be set | Platform-separated allowlist of directories `delegate_research` may read (`:` on macOS/Linux, `;` on Windows) |
21
+ | `FREELLAMA_MCP_MODEL_EVIDENCE` | checkout: `packages/mcp/evidence/model-evidence.json`; published: unset | Path to per-model research grades (see "Runtime evidence") |
22
+ | `FREELLAMA_EXTERNAL_COST_MODEL` | unset | External-model label in estimated avoided-cost telemetry; set all three cost variables or none |
23
+ | `FREELLAMA_EXTERNAL_COST_INPUT_USD_PER_M` | unset | External input USD per million tokens |
24
+ | `FREELLAMA_EXTERNAL_COST_OUTPUT_USD_PER_M` | unset | External output USD per million tokens |
25
+ | `FREELLAMA_CONTROL_TIMEOUT_SECONDS` | `30` | Decision-only serve calls; read by `packages/rust-core/src/napi.rs` |
26
+ | `FREELLAMA_TASK_TIMEOUT_SECONDS` | `900` | Generation calls; read by `packages/rust-core/src/napi.rs` |
27
+ | `FREELLAMA_AUTH_TOKEN_FILE` | unset | Bearer-token file used by the native client and research adapters for all serve routes |
28
+ | `FREELLAMA_AGENT_TOKEN_CALIBRATION_DIR` | platform data directory | Prompt-free, model-specific token-estimator calibration shared across adapter processes |
29
+ | `FREELLAMA_MCP_AUTOSTART_SERVE` | `1` | When the default loopback serve endpoint does not answer, start `freellama serve` as a child of the MCP server (stopped with it). `0` disables |
30
+ | `FREELLAMA_SERVE_BINARY` | checkout `target/release`/`debug`, then the platform package | Binary used for serve autostart |
31
+ | `FREELLAMA_AGENT_OS_SANDBOX` | `auto` | Bash adapter OS confinement: `bwrap` on Linux, `sandbox-exec` on macOS, when present. `off` keeps only the restricted shell |
32
+ | `FREELLAMA_AGENT_OCTOCODE_PACKAGE` | `octocode@19.1.0` | Pinned npm spec the Octocode adapter runs through `npx` |
33
+
34
+ Serve-side placement settings (read by `freellama serve`, not the MCP process):
35
+
36
+ | Variable | Default | Affects |
37
+ |---|---|---|
38
+ | `FREELLAMA_EVICT_IDLE_MODELS` | on | Unload idle, unpinned models using reuse and load costs when a cold load needs room. `0` turns eviction off |
39
+ | `FREELLAMA_CPU_NUM_THREAD` | half the logical cores (not set on macOS) | `num_thread` for CPU-placed tasks only; GPU tasks leave it to Ollama |
40
+ | `FREELLAMA_MAX_QUEUE_WAIT_SECONDS` | `120` | How long a managed task queues for a slot or memory |
41
+ | `OLLAMA_NUM_PARALLEL`, `OLLAMA_KV_CACHE_TYPE`, `OLLAMA_FLASH_ATTENTION` | Ollama's | Read as hints for the memory estimate: parallel slots multiply the KV cache, and `q8_0`/`q4_0` shrink it only with flash attention. Set them for serve the same way as for Ollama |
42
+
43
+ Research-adapter settings are deployment defaults. The optional `delegate_research.agent` object
44
+ overrides the per-call budget (`maxTurns`, `contextTokens`, `outputTokens`); explicit per-call
45
+ fields win. Sampling, keep-alive, timeouts, and placement preference for research come from the
46
+ operator's `FREELLAMA_AGENT_*` variables or the adapter defaults.
47
+ The research subprocess inherits only these server variables: `FREELLAMA_*`; `PATH`, `HOME`,
48
+ `USER`, `LOGNAME`; `LANG`, `LC_ALL`, `LC_CTYPE`, `TZ`; `TMPDIR`, `TEMP`, `TMP`; Windows `SYSTEMROOT`,
49
+ `SYSTEMDRIVE`, `WINDIR`, `COMSPEC`, `PATHEXT`, `APPDATA`, `LOCALAPPDATA`, `USERPROFILE`;
50
+ `HTTP_PROXY`, `HTTPS_PROXY`, `NO_PROXY` (and lowercase forms); `NODE_EXTRA_CA_CERTS`,
51
+ `SSL_CERT_FILE`, `SSL_CERT_DIR`, `REQUESTS_CA_BUNDLE`; `XDG_CACHE_HOME`, `XDG_DATA_HOME`,
52
+ `npm_config_cache`.
53
+
54
+ | Variable | Default | Affects |
55
+ |---|---:|---|
56
+ | `FREELLAMA_AGENT_NUM_CTX` | `8192` | Ollama context window |
57
+ | `FREELLAMA_AGENT_EXECUTION_PREFERENCE` | `auto` | Managed coding-agent backend hint |
58
+ | `FREELLAMA_AGENT_MIN_PLACEMENT_EVIDENCE` | `configured` | Set `observed` to fail closed on cold/mismatched placement |
59
+ | `FREELLAMA_AGENT_NUM_PREDICT` | `512` | Reserved output budget |
60
+ | `FREELLAMA_AGENT_TEMPERATURE` / `FREELLAMA_AGENT_SEED` | `0` / `42` | Repeatable decoding |
61
+ | `FREELLAMA_AGENT_THINK` / `FREELLAMA_AGENT_KEEP_ALIVE` | `false` / `5m` | Ollama reasoning and residency |
62
+ | `FREELLAMA_AGENT_REQUEST_TIMEOUT_SECONDS` | `600` | Each Ollama chat attempt |
63
+ | `FREELLAMA_AGENT_TOOL_TIMEOUT_SECONDS` | Bash `30`, Octocode `45` | Each local tool call |
64
+ | `FREELLAMA_AGENT_RETRY_ATTEMPTS` / `FREELLAMA_AGENT_RETRY_BACKOFF_SECONDS` | `2` / `5` | Conversation-level retry |
65
+ | `FREELLAMA_AGENT_MAX_PARSE_REPAIRS` / `FREELLAMA_AGENT_PARSE_REPAIR_ECHO_CHARS` | `2` / `500` | Invalid JSON correction limit and retained reply text |
66
+ | `FREELLAMA_AGENT_CHARS_PER_TOKEN` | `4` | First-call token estimate; later calibrated from Ollama |
67
+ | `FREELLAMA_AGENT_SAFETY_MARGIN_TOKENS` | `256` | Input headroom beyond `num_predict` |
68
+ | `FREELLAMA_AGENT_IMAGE_TOKEN_ESTIMATE` | `1024` | Bounded charge per image |
69
+ | `FREELLAMA_AGENT_KEEP_RECENT` | `2` | Recent observations preserved verbatim |
70
+ | `FREELLAMA_AGENT_COMPACT_PREVIEW_CHARS` | `180` | Old-observation breadcrumb size |
71
+ | `FREELLAMA_AGENT_COMPACT_RETAIN_RATIO` | `0.8` | Current payload retained per emergency pass |
72
+ | `FREELLAMA_AGENT_CLIP_HEAD_RATIO` | `0.667` | Head share when an emergency clip preserves both ends |
73
+ | `FREELLAMA_AGENT_OBSERVATION_PAGE_CHARS` | `3000` | Lossless observation page target |
74
+ | `FREELLAMA_AGENT_PINNED_OVERFLOW` | `error` | Preserve system/task bytes; `clip` is explicit opt-in |
75
+
76
+ Invalid values fail before an Ollama request. Each successful result returns
77
+ `contextManagement` with the resolved policy, calibration source/scale, estimated input budget,
78
+ and compaction count. Ollama has no stable preflight tokenizer endpoint, so “exact before the first
79
+ call” is not claimed. Model-specific calibration persists after successful calls, so later
80
+ processes start conservatively from prior `prompt_eval_count` evidence without sharing estimates
81
+ between model templates.
82
+
83
+ **Security:** `delegate_research` grants a local model read access to `workspacePath`. The path is
84
+ confined to `FREELLAMA_MCP_ALLOWED_ROOTS` (default: this repository in a checkout; unset in a published
85
+ install until you set it), resolved through symlinks so a link inside an allowed root can't escape
86
+ it. The default `bash` adapter runs an allowlist of read-only tools in a restricted shell with a
87
+ scrubbed environment and, where available, an OS sandbox; it also rejects command substitution,
88
+ home-directory paths, `..`, and absolute paths outside that workspace. Production deployments should generate a permission-restricted token with
89
+ `freellama auth-token`, start `serve` with `--auth-token-file`, and set
90
+ `FREELLAMA_AUTH_TOKEN_FILE` for MCP. Authentication covers managed and raw passthrough routes; use
91
+ an external TLS and authorization layer for untrusted or multi-tenant networks.
92
+
93
+ ## Runtime evidence
94
+
95
+ Admission weights can be set by task kind in the server runtime file's `[task_costs]` table.
96
+ Inspect `doctor {view:"full"}` at `platform_health.admission.costs.base_by_task` for effective
97
+ weights and `run_task {preview:true}` for current cost and capacity advice. The table changes
98
+ managed admission; it does not create Ollama
99
+ slots or relax memory guards. Existing queued work keeps its captured cost. See
100
+ [monitoring controls](https://github.com/bgauryy/FreeLlama/blob/main/docs/MONITORING.md#adaptive-concurrency).
101
+
102
+ **No measurements are compiled in.** In a repository checkout, per-model research grades load from
103
+ `packages/mcp/evidence/model-evidence.json`. A published package does not include that repository
104
+ file, so its evidence table is empty unless you set `FREELLAMA_MCP_MODEL_EVIDENCE`. An unmeasured
105
+ model yields a `verify` verdict, and an unreadable evidence file is reported on stderr and treated
106
+ as empty. Tool descriptions state the rules; measurement details live in
107
+ [`skills/freellama/references/`](https://github.com/bgauryy/FreeLlama/tree/main/skills/freellama/references/) and
108
+ [`docs/dev/evidence/`](https://github.com/bgauryy/FreeLlama/tree/main/docs/dev/evidence/).
package/DEVELOPING.md ADDED
@@ -0,0 +1,109 @@
1
+ # Developing the FreeLlama MCP server
2
+
3
+ Build, test, and release notes for the [FreeLlama MCP server](README.md).
4
+
5
+ ## Build
6
+
7
+ Build the native addon from the repository root and this package in place.
8
+
9
+ ```bash
10
+ # From the repository root (yarn workspaces — one install for every package)
11
+ corepack enable
12
+ yarn install
13
+ yarn build
14
+ ```
15
+
16
+ `yarn build` writes **one** MCP file, `packages/mcp/dist/index.js` (shebang, executable). During
17
+ checkout development it loads `native/freellama.<triple>.node`. Published installs resolve the
18
+ matching `@octocodeai/freellama-native-<target>` optional package instead, so the base MCP package remains
19
+ portable. Research adapters are copied into `adapters/` for packed installs.
20
+
21
+ Piecewise, if you are iterating on one layer:
22
+
23
+ ```bash
24
+ # 1. Native addon → packages/mcp/native/freellama.<triple>.node
25
+ yarn build:native
26
+
27
+ # 2. This server → dist/index.js via esbuild (then copies the research adapters into adapters/)
28
+ yarn workspace @octocodeai/freellama-mcp-server build
29
+ ```
30
+
31
+ `yarn start` needs `dist/index.js` and the compiled `.node` addon — `yarn build` from the repository
32
+ root produces both in a checkout. `dist/index.js` loads `native/index.js`, which loads a local addon
33
+ first and then a matching optional platform package. The `.node` binary is gitignored (a build
34
+ artifact). `native/index.js` is a small hand-written loader; `native/index.d.ts` is generated from
35
+ `napi.rs` by `yarn build:native`. Both are checked in; do not edit the declarations by hand —
36
+ change the Rust doc comments or `ts_args_type`, rebuild, and commit. CI rebuilds it and fails
37
+ on any diff.
38
+
39
+ ## Test
40
+
41
+ Vitest, in three tiers (all TypeScript, under `test/`):
42
+
43
+ | Tier | Command (from this package) | Checks | Needs |
44
+ |---|---|---|---|
45
+ | `test/unit/` | `yarn test` (watch: `yarn test:watch`) | pure functions straight from `src/*.ts` — no build step, this is the TDD loop | nothing, ~0.5s |
46
+ | `test/integration/` | `yarn test:integration` | the built server over the real MCP protocol: tool contract, structured content, guardrails; rebuilds `dist/` itself | Ollama at `FREELLAMA_OLLAMA_ENDPOINT` (default :11434) |
47
+ | `test/e2e/` | `yarn test:e2e` | every tool against the live system: real routing/execution, pull → delete round trip (net-zero), a real delegated research run | serve + Ollama + models, ~40s |
48
+
49
+ The same commands work from the repository root (`yarn test` there also runs the CLI package's tests).
50
+ The integration/e2e tiers fail fast with a readable message when Ollama is down, and the lifecycle
51
+ test refuses to delete a model already installed on the test system.
52
+
53
+ For default-endpoint autostart, a spawn failure, early exit, or readiness timeout rejects the
54
+ request while leaving the MCP transport available for diagnosis or retry. Autostart does not apply
55
+ to an explicit non-default endpoint; see `FREELLAMA_MCP_AUTOSTART_SERVE` in
56
+ [configuration](CONFIGURATION.md#environment-variables).
57
+
58
+ ## Platform support
59
+
60
+ The native addon is named by target triple (`freellama.<platform>-<arch>[-<abi>].node`). The base
61
+ package has eight npm optional dependencies, one each for macOS arm64/x64, Linux arm64/x64 with
62
+ glibc or musl, and Windows arm64/x64 with MSVC. `native/index.js` tries the checkout artifact first,
63
+ then the host's optional package. A release is supported only when all eight artifact packages pass
64
+ the publish verifier; an installation made with `--omit=optional` cannot load the addon. `engines`
65
+ requires Node >= 20.
66
+
67
+ Machine discovery distinguishes total host memory (`memory_bytes`) from known unified memory
68
+ (`unified_memory_bytes`). Library-tag `fitsInMemory` is a conservative host-memory preflight and
69
+ includes `fitScope: "host_memory_budget_only"`; inspect `/api/ps` for actual accelerator placement.
70
+
71
+ Start the serve-backed tools' dependency from the repository root:
72
+
73
+ ```bash
74
+ cargo run --release -- serve
75
+ ```
76
+
77
+ See `skills/freellama/references/proxy-vs-serve.md` for the `proxy` vs `serve` distinction (only
78
+ `serve` has the `/_freellama/v1/*` routes these tools call).
79
+
80
+ ## Publish
81
+
82
+ `packages/mcp/` is the portable JavaScript package. Its `package.json` lists the platform packages
83
+ as exact-version optional dependencies; it intentionally does not embed a `.node` binary. Publish
84
+ the eight `packages/native/*` packages first, then publish `@octocodeai/freellama` (`npx @octocodeai/freellama`) and `@octocodeai/freellama-mcp-server` (`npx @octocodeai/freellama-mcp-server`)
85
+ at the same version. `yarn release:verify:publish` refuses a release if any of the ten packages
86
+ omits `assets/logo.jpg`, or if any platform package has a missing, empty, or unpacked
87
+ executable/addon pair.
88
+
89
+ ```bash
90
+ cd packages/mcp
91
+ npm pack --dry-run # confirm exactly what a publish would ship
92
+ ```
93
+
94
+ Treat `npm publish` as a public release action. Follow the repository
95
+ [release procedure](https://github.com/bgauryy/FreeLlama/blob/main/RELEASE.md) and obtain the repository owner's release authorization first.
96
+
97
+ ## Why native bindings
98
+
99
+ - **No routing reimplementation.** `run_task`, `run_task {preview:true}`, and the installed-model list are HTTP
100
+ calls into a running `freellama serve`, matching the CLI tool map in `packages/cli/src/tools.rs`. `doctor` talks to Ollama
101
+ and the host's portable machine-discovery layer directly. `ollama_manage` / `ollama_delete` hit Ollama's HTTP API. `delegate_research`
102
+ is a subprocess. Routing decisions still live in exactly one place — the Rust core; see
103
+ [what the MCP layer computes](README.md#what-the-mcp-layer-computes).
104
+ - **One connection pool.** `packages/rust-core/src/napi.rs` holds a single `reqwest::Client` in a
105
+ `OnceLock` and clones the handle per request, matching the server side.
106
+ - **`unsafe_code` stays isolated.** The crate is `deny(unsafe_code)` except `napi.rs`, which
107
+ carries an explicit `#[allow(unsafe_code)]` for napi-derive's FFI glue.
108
+ - **The napi build is off by default** (`cargo build` never touches it); the addon is built with
109
+ `napi build --features napi` from the repo root.