Skip to content

cli: tensorfold mcp, a Model Context Protocol server on stdio for this machine's models and local servers - #3549

Draft
ashhart wants to merge 3 commits into
mainfrom
lead/cli-mcp
Draft

ashhart wants to merge 3 commits into
mainfrom
lead/cli-mcp

Conversation

@ashhart

@ashhart ashhart commented Nov 16, 2026

Copy link
Copy Markdown
Owner

AI coding assistants (Claude Code, Codex, Cursor) drive tools through the Model Context Protocol. Today an assistant can only use TensorFold by typing tensorfold models, pull and serve for the user. tensorfold mcp is an MCP server on stdio, so the assistant can see what this machine runs, download a model, and start or stop a local server by calling tools.

What this changes

  • Protocol: newline-delimited JSON-RPC 2.0 on stdio.

    • It implements the current MCP revision, 2026-07-28 (per-request _meta, server/discover), plus the initialize handshake of 2025-11-25 and 2025-06-18 for clients that open with it. An initialize asking for an older revision gets 2025-11-25.
    • Batches get -32600, since the spec dropped them in 2025-06-18.
    • stdout carries only protocol messages, and logs go to stderr. Stdin's end ends the session.
  • Seven tools:

    • machine: the host facts tensorfold benchmark already records, with no hostname or serials, plus version, backend and model types.
    • models: the cached checkpoints a family serves, from the same cache scan as serve --router (hub.cachedRepos).
    • info: tensorfold info's own lines and numbers.
    • pull: downloads with tensorfold pull's code. Each printed line becomes a progress notification when the client sent a progress token.
    • serve: starts tensorfold serve on a free 127.0.0.1 port with a fresh 64-hex TENSORFOLD_API_KEY, and returns the base URL, the served name and the key, which go only to the client that asked. Serving the same model and settings again returns the running server. A cancelled serve stops its loading child.
    • stop: stops a server this session started.
    • status: those servers, without keys. An exited one shows its last log lines.
  • Safety:

    • Tools never delete anything.
    • pull takes repo ids only. info and serve take a cached repo id, or the absolute path of an existing directory holding config.json. Relative paths, ~, .. and ids the Hub would reject are refused.
    • Every failure comes back as an MCP tool error, never a crash.
    • Every server this session started is stopped when stdin closes, or on SIGTERM, SIGINT or SIGHUP.
  • Threads: pull and serve run on their own threads (at most 8), so a cancel and stdin's end still get through. A pull can't be stopped midway, because pull's code has no cancel point; it finishes, and the next pull resumes it.

  • The serve child: started through the shared serve child (Managed.spawn) with the router's options and nothing added: its own process group, no benchmark guard, a stop flag and a failure reason. It keeps the user's TENSORFOLD_REQUEST_LOG and seed salt, as the router's children do; docs/mcp.md says so.

  • Windows: serve is refused by name, since the serve child is POSIX. The other tools work, and the native build compiles for Windows and Linux.

  • Docs: docs/mcp.md shows how to add it to Claude Code (claude mcp add tensorfold -- tensorfold mcp), Codex (~/.codex/config.toml), Cursor and other mcpServers clients.

  • Issue: n/a. This came from the 16 Nov check of competitor gap docs against main (colibri's coli mcp and omlx expose hardware, model, install and serve tools to assistants); no issue or PR covered it.

  • Stacked on: server: tensorfold serve --router answers for several models at one address, each a serve child loaded on demand #3540 (serve --router), which stacks on cli: tensorfold chat talks to a model in the terminal, through a serve child of its own or a running server's --url #3538 (tensorfold chat). This PR adds d6eaa9810a and reuses their shared serve child, hub.cachedRepos and info's memory(). models.zig and info.zig are unchanged, so it doesn't touch cli: tensorfold models lists what fits this machine, with a rough one-stream speed #3483's files.

  • atc overlap --json: to run after review.

  • Specs read: the MCP specification at modelcontextprotocol.io (2026-07-28 and 2025-11-25), and Claude Code's and Codex's MCP setup docs. No MCP implementation's source.

Checks on this head

  • Head commit: d6eaa98 on server: tensorfold serve --router answers for several models at one address, each a serve child loaded on demand #3540's head 827bebc (main e7e2424).
  • Lean checker: 337 on base and head, 0 new (tools/pr_check.py: 0 errors, 1 advisory warning, the PR number).
  • Tests:
    • zig build test-mcp-host (new): 21/21, four runs green.
    • Mutation checks: no batch refusal, no API key, and no stop at session end each fail their test. Removing the child's key and process group fails three serve tests.
    • The router's, chat's and the benchmark's steps: 31/31 steps, 311/311 tests.
    • zig build test-server-cpu: 1531/1540 (9 skipped).
    • zig build test-cli test-cli-memory-host test-info-config test-pull-host: 93/108 (the same 15 skips as main).
    • zig build test-golden: 322/335 equal, 0 unexpected; security 55/55.
    • The native build for Windows and Linux, and server-cpu, compile. The real binary answers both protocol revisions on stdio, with only JSON on stdout and stderr empty.
    • zig fmt --check, git diff --check: clean.
  • CI: pending.
  • Not run: a real model served through serve on Metal or CUDA, a real Claude Code, Codex or Cursor session, Windows at runtime, and Linux's test-host.

Receipt

n/a: a CLI front for existing commands that can't change what the engine computes. Host checks are above.

Checklist

  • The title and description match the head commit.

  • New tests fail before the change, pass after it, and skip cleanly without their dependency.

  • Comments and docstrings are one line. No measurements or history in the source.

  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

  • Synthena (Ash's Agent)

ashhart added a commit that referenced this pull request Nov 16, 2026
…ddress, each a serve child loaded on demand

A request's model field picks the child, which the router starts if need be and relays byte for byte,
with --max-loaded, --idle-unload and each model's memory floor bounding what stays loaded, and
--router-config FILE.toml listing the models in [[models]] tables. Single-model serve is unchanged.
…s machine's models and local servers

An AI coding assistant starts `tensorfold mcp` and calls seven tools: machine (the
benchmark's allowlisted host facts), models (the cache scan `tensorfold models` lists),
info (info's own lines, and its floor as the router reads it), pull (tensorfold pull's
code, its lines as progress notifications), and serve, stop and status. serve starts
`tensorfold serve` through the serve child the router and chat use: a free 127.0.0.1
port, a process group of its own and a fresh key, returned with the base URL and the
served name. Every server the session started stops when stdin ends or a stop signal
arrives. Windows refuses serve by name; the other tools work there.

The server speaks MCP 2026-07-28 (per-request revision in _meta, server/discover) and
the initialize handshake of 2025-11-25 and 2025-06-18: one JSON-RPC message a line,
batches and bad JSON refused with JSON-RPC errors, tool problems as tool errors, and
stdout for protocol messages only.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant