# MCP server

URL: /docs/agents/mcp
Markdown: /docs/agents/mcp.md

> The sakura mcp serve server, which exposes the compiler to agents as Model Context Protocol tools, and the planned export of your own functions.

`sakura mcp serve` lets a coding agent call the compiler directly, as tools, instead of running
shell commands and parsing their output. Read this page to connect an agent such as Claude Code,
Codex or Cursor to Sakura Lang.

[Model Context Protocol](https://modelcontextprotocol.io) (MCP) is the standard way agents
discover and call tools. The command line (CLI), the language server for editors
(`sakura lsp`) and the MCP server all sit on the same compiler analysis, so they report the
same diagnostics for the same code. Only the packaging differs: MCP returns JSON objects, CLI
`--json` writes [JSON Lines](/docs/agents/diagnostics), and the language server uses its own
protocol.

## Client configuration [#client-configuration]

The server talks over standard input and output. For clients configured with an `mcpServers` entry, point `command` at the installed `sakura` and replace the root below with your project's absolute path. See the [client-specific setup](https://github.com/icantcodefyi/sakuralang/blob/main/docs/mcp.md) for Claude Code, Codex and Cursor.

```json
{
  "mcpServers": {
    "sakura": {
      "command": "sakura",
      "args": ["mcp", "serve", "--root", "/absolute/path/to/project"]
    }
  }
}
```

## Snapshots [#snapshots]

A **snapshot** is a frozen copy of the project's files that the server analyzes. Each analysis or patch result names the snapshot it came from (information tools such as `compiler_info` do not). `check` without a `snapshot` reads the files from disk. Snapshots make results reproducible and catch stale edits: a patch made against an old snapshot is reported as a conflict instead of being applied to the wrong text.

## Tools [#tools]

The server offers these ten tools.

| Tool | Parameters | Returns |
| --- | --- | --- |
| `compiler_info` | none | `compiler`, `version`, `schema_version`, `edition`, `tools`, `features`, `workspace_root`, `manifest`, `lock_digest`. |
| `check` | `snapshot`, `paths`, `budget_ms` | Diagnostic and hole records, a summary and `over_budget` ([Diagnostics](/docs/agents/diagnostics)). |
| `symbol_query` | `name`, `id`, or `type_pattern` | The `symbol_query` record that `sakura doc --json` writes: matching symbols with full signatures, including effect rows, error unions, and contracts. |
| `hole_context` | `snapshot`, `hole_id` | One [typed hole](/docs/agents/typed-holes) record. |
| `docs_query` | `topic` or `symbol`, `budget_tokens` | Documentation text within a token budget. |
| `explain_diagnostic` | `code` or `id` | The explanation for a code such as `V3001`, the same as `sakura explain`. |
| `format` | `snapshot`, `paths` | A patch with the canonical formatting. |
| `check_patch` | `snapshot`, `edits`, optional `frozen_specs` | Diagnostics for the patched code, plus the interface delta: which signatures the patch changes. |
| `apply_patch` | `snapshot`, `patch_id`, optional `frozen_specs`, `write` | A new snapshot, or a conflict. |
| `test` | `snapshot`, `selectors`, `seed`, `budget_ms` | Test results under deterministic handlers: `test` records and the `test_summary`, as `sakura test --json` writes them. |

In `compiler_info`, `features.backends` lists the compile targets this compiler has (including `wasm` when present). `features.verify` says whether the CLI can verify and whether a solver is installed; a missing solver does not remove the capability. `features.test_execution` says whether a test runner is configured. Verification is the CLI's [`sakura verify`](/docs/agents/verify); MCP `verify`, `refactor` and `impact` tools are planned, not built.

`check.budget_ms` is a soft time budget: going over it only sets `over_budget`. `test.budget_ms` is a hard wall-clock limit in milliseconds (60,000 by default, at most 300,000). `docs_query` estimates tokens from bytes; its default budget is 1,500 tokens.

### Checking a patch before applying it [#checking-a-patch-before-applying-it]

`check_patch` lets an agent try an edit without changing anything. The **interface delta** in its answer tells the agent, and the reviewer, whether the edit changes a public signature, effect row (the side effects a function may perform) or error union. `apply_patch` then makes the edit and returns the new snapshot.

> **Diagram:** Patching through MCP: every result names the snapshot it was computed from, so a stale patch conflicts instead of landing on the wrong text.
>
> 1. `check(snapshot)`: diagnostics and holes; hole_context for one hole.
> 2. `check_patch(edits, frozen_specs)`: diagnostics and the interface delta; nothing changes.
> 3. `apply_patch(patch_id, frozen_specs)`: commits it and returns a new snapshot. Then `test(snapshot)` or `conflict`.
> 4. `conflict`: the workspace moved on; nothing is applied. Then `check(snapshot)`.
> 5. `test(snapshot)`: tests under deterministic handlers. Then `check(snapshot)`.
>
> Frozen specs are opt-in per call: pass frozen_specs: "all" to both check_patch and apply_patch, and a change to a frozen signature, row, contract or permission is V8020.

**Frozen specs** stop an agent from passing a check by editing the spec. They are opt-in on each call: pass `frozen_specs: "all"` (or comma-separated symbol names, including `permissions`) to both `check_patch` and `apply_patch`. The check returns `frozen_spec_violations`; application rejects changes to the selected signatures, effect rows, error unions, contracts, tests/properties and permissions with `V8020`. Use the original base snapshot when applying. Omitting `frozen_specs` does not enforce a prior check's freeze policy. The CLI compares against Git: `sakura check --frozen-specs all --since <baseline>`.

### Authority [#authority]

Tools that run code, such as `test`, get their permissions from the server, never from the caller: a tool call cannot ask for more access. Tests run with deterministic default handlers (the same answers every run) and no network access.

## Exporting your own functions as tools [#exporting-your-own-functions-as-tools]

**Planned, not built:** `sakura mcp serve --tools a,b` would export functions from your own program as MCP tools, and an HTTP transport would sit beside the standard input and output one. What works today is `Tool.of`, which makes a tool for a model inside your program.

A tool function is marked `@tool("description")`. Its parameters and result must be `Schema` types, and its error union must be made of `Schema` tags. This complete example checks the implemented in-process path; it does not start an MCP export:

```sakura
import std/tool.{Tool}

@tool("Return a greeting")
fn greet(name: String) -> String { "hello, {name}" }

pub fn main() -> Unit {
  let _tool = Tool.of(greet)
}
```

`Tool.of(greet)` builds a `Tool` value. Its JSON Schema comes from the parameter types, with the refinement bounds the compiler can state (`minimum`, `maxLength`, …): `maxLength` is the byte bound, `minLength` ⌈n/4⌉; both admit every string the `where` admits. Every refinement is still checked when the arguments are decoded. When called, it validates the JSON arguments, runs the function under the caller's current handlers, and maps failures to a public `ToolError` with the tag name and payload. The same `Tool` values are used in-process by `Agent.run`, which takes them in the `tools` field of its `Agent` configuration record (see the [standard library](/docs/stdlib)).

In the planned MCP export, the model never supplies authority. The handlers installed by the serving program decide what `Http` can reach, and the manifest's `[permissions]` table is the ceiling.

## Stability [#stability]

The standard input and output server, the ten tools above, their parameters, and the snapshot, content-hash and patch-id protocol are **stable from v1.0.0-beta.1** under [RFC 0001](/docs/stability). A tool that returns a CLI record, such as `symbol_query` or `hole_context`, returns exactly the `--json` record of the same name, `kind` included. Function export, HTTP transport and the `verify`, `refactor` and `impact` tools are not built and carry no promise.

## Next [#next]

[Diagnostics](/docs/agents/diagnostics): the JSON format every tool returns.

## For contributors [#for-contributors]

The [design document](https://github.com/icantcodefyi/sakuralang/blob/main/docs/design.md)
covers the MCP interface (section 12.6) and function export (section 11.2).
