- cli: opt into server-v2 via KIMI_CODE_EXPERIMENTAL_FLAG, lazy-loaded behind a RoutedServer adapter so the v1 module graph stays untouched by default
- build: add hashImportsPlugin (rolldown/tsdown) to resolve #/ subpath imports of inlined agent-core-v2 when bundling server-v2
- server-v2: add --dangerous-bypass-auth flag disabling bearer auth on every route, surfaced on /meta via dangerous_bypass_auth
- server-v2: serve bundled web UI at GET / + SPA fallback when webAssetsDir is set (auth-exempt, mirroring v1)
- server-v2: snapshot auto reader reads state.json + wire.jsonl from disk with a hard timeout; legacy keeps the live assembly as escape hatch
- server-v2: POST /sessions/{id}/profile now updates title, metadata, and agent_config and broadcasts session.meta.updated on title change
- server-v2: session list/children project live ISessionActivity status and filter by status; add archived_only query and workspace_id validation
- server-v2: close cwd gap G3 — persist cwd on session metadata and surface it on the index so unregistered workspaces keep their cwd
- server-v2: GET /warnings surfaces agents-md-oversized; add GET /workspaces/{id}/skills session-less scan
- server-v2: broadcaster reads the cold watermark from the disk journal and broadcasts title updates to every connection
- agent-core-v2: fix plan-file path resolution in AgentPlanService
- server: rename IFileService to IFileStore
|
||
|---|---|---|
| .. | ||
| scratch | ||
| src | ||
| test | ||
| AGENTS.md | ||
| CHANGELOG.md | ||
| package.json | ||
| README.md | ||
| SECURITY.md | ||
| tsconfig.json | ||
| tsdown.config.ts | ||
| vitest.config.ts | ||
@moonshot-ai/server
Local REST + WebSocket server that exposes the Kimi Code SDK over a stable wire
protocol. It hosts agent-core sessions and serves them under a single
/api/v1 prefix. This package is private — it is not published on its own;
it ships inside the kimi CLI (apps/kimi-code) and is launched via
kimi server run.
What it does
- Hosts
agent-coresessions, prompts, tools, approvals, questions, and workspaces in process. - Exposes them over REST (Fastify) and WebSocket (
ws) under/api/v1. - Serves the built-in web UI (
apps/kimi-web) as static assets when awebAssetsDiris provided. - Publishes machine-readable contract docs:
/openapi.json,/asyncapi.json.
Running it
# From the repo root — dev server with auto-restart
pnpm dev:server
pnpm dev:server:restart
# Checks
pnpm --filter @moonshot-ai/server typecheck # tsc --noEmit
pnpm --filter @moonshot-ai/server test # vitest run
pnpm --filter @moonshot-ai/server build # tsdown
The public entry point is startServer(opts) in src/start.ts, which returns a
RunningServer. In production the CLI command kimi server run
(apps/kimi-code/src/cli/sub/server/run.ts) imports and calls it. This package
has no dev script of its own — always start it from the repo root or via the
CLI.
By default the server listens on 127.0.0.1:58627; e2e clients target it with
KIMI_SERVER_URL (default http://127.0.0.1:58627).
Architecture
apps/kimi-code (CLI) apps/kimi-web (browser)
│ │
└──────────┬───────────────────┘
│ REST + WebSocket, /api/v1
┌──────────▼───────────┐
│ @moonshot-ai/server │
│ Fastify REST │
│ ws gateway │
│ DI container │ ← @moonshot-ai/agent-core
│ agent-core sessions │ ← @moonshot-ai/agent-core
└──────────────────────┘
- REST (
src/routes/): domain modules aggregated byregisterApiV1Routes.ts. Routes are declared withmiddleware/defineRoute.ts, which bundles Zod validators with the OpenAPI response schema. - WebSocket (
src/ws/,src/services/gateway/): per-sessionseq,server_hello/ack/event/resync_requiredframes, replay and fan-out. - DI (
src/services/serviceCollection.ts): seeds the container from@moonshot-ai/agent-core(getSingletonServiceDescriptors()) and layers in server-owned gateways plusIApprovalService/IQuestionServiceimplementations. - OS service managers (
src/svc/): launchd / systemd / schtasks backends forkimi server install/start.
Wire protocol notes
- Envelope: every REST response is
{ code, msg, data, request_id }and the HTTP status is effectively always 200 — checkcode(0 = ok), not the status. :actionendpoints: some routes use an:id:actionsuffix (e.g./sessions/{id}:undo); the suffix is parsed byroutes/action-suffix.ts.- Single-instance lock: a running server acquires a lock; a second start on
the same home throws
ServerLockedError. Tests pass a uniquelockPath/port.
Related packages
@moonshot-ai/agent-core— the agent engine the server hosts, including the in-process DI service layer it wires together.@moonshot-ai/protocol— wire types and the AsyncAPI document.@moonshot-ai/node-sdk— typed in-process facade for user code (KimiHarness,Session); prefer it over hand-rolling REST/WS calls.@moonshot-ai/server-e2e— wire-level e2e client and scenarios against a running server.
Development
For conventions, gotchas, and the boot wiring order, see
packages/server/AGENTS.md. For the service naming and
registration rules, see
packages/services/AGENTS.md.