---
title: MCP
summary: "`host.mcp` and `ExtensionModule.mcp`: give the assistant an MCP server's tools as `<ns>_<tool>`, or call them from the view, with caching, approvals and errors."
audience: both
stability: experimental
since: protocol 1
---

# MCP

An extension can hand the assistant the tools of an MCP server (Streamable HTTP transport) instead of declaring
actions: SpaceMIX lists the server's tools and offers each one as `<ns>_<tool>`. The extension only supplies the
connection, credentials included, from its own settings.

This page is about an extension's own MCP servers. A server can also be connected to the whole SpaceMIX server, with no extension, in **Settings → MCP connections**; those tools are named `<connection id>_<tool>`, run on the server and aren't in `host.tools`. See [MCP connections](../mcp-connections.md).

There are two parts:

- `ExtensionModule.mcp`: the servers the **assistant** gets tools from.
- `host.mcp`: the same client for the extension's **view**, to show or call a server's tools itself.

```ts
interface McpServer {
  name: string;                     // unique within the extension; shown in approval prompts
  url: string;                      // the MCP endpoint, e.g. "https://crm.example.com/mcp"
  headers?: Record<string, string>; // sent with every request
  include?: string[];               // keep only these tools (server-side names)
  exclude?: string[];               // leave these out, applied after include
}

interface McpTool {
  name: string;
  description?: string;
  inputSchema: { type: "object"; properties?: Record<string, unknown>; required?: string[]; [k: string]: unknown };
  annotations?: { title?: string; readOnlyHint?: boolean; destructiveHint?: boolean; idempotentHint?: boolean; openWorldHint?: boolean };
}

interface McpApi {
  listTools(server: McpServer, options?: { fresh?: boolean }): Promise<McpTool[]>;
  callTool(server: McpServer, name: string, args: Record<string, unknown>): Promise<unknown>;
}

function mcpToolNeedsApproval(tool: McpTool): boolean;
```

**Permission:** a `net:` permission covering the server's host. `host.mcp` is always present, but its calls reject
with `<Name> doesn't have the "net:" permission.` without one, and the module's `mcp` resolver is never called.

**Host check.** Before any request the server's URL must be `http:` or `https:` and its host allowed, with the same
rules and the same `net:*` prompt as [net.md](net.md#the-net-prompt). Two differences, both looser: here a
`net:*.example.com` permission also covers `example.com` itself, and `net:example.com` covers any port. Write
permissions to the stricter `host.net.fetch` rules.

**Where the request comes from.** Unlike `host.net.fetch`, MCP requests are made by the **SpaceMIX window**
(`fetch` in the app's web view), not by the server. The MCP server must allow cross-origin requests from SpaceMIX's
origin (CORS), and it must be reachable from the user's machine.

## `ExtensionModule.mcp`

```ts
mcp?: (host: SpacemixHost) => Promise<McpServer[]>;
```

Called in the extension's frame each time the assistant starts a turn and each time `host.tools.list` runs, so
credentials come from current settings. Return `[]` while the extension isn't connected. It has 60 seconds (see
[actions.md](actions.md)); an error, or a server whose host isn't allowed, leaves that extension's MCP tools out for
the turn and the other tools still work.

For each server the host lists its tools (cached, see below), applies `include` then `exclude`, and offers each as
a tool:

- **Name:** `<ns>_<tool name>`, with characters other than `A–Z a–z 0–9 _ -` replaced by `_`, cut to 64
  characters. A name already taken (by a collection or action tool, or an earlier server) is left out. A
  [member's build](../permissions.md#tool-names) comes after all of those, and also loses names under another
  extension's namespace.
- **Title:** `annotations.title`, else the name in words. **Description:** the server's, cut to 1 000
  characters.
- **Arguments** are passed to the server as the model sent them; the host doesn't validate them against the
  schema (MCP schemas may use JSON Schema features the host doesn't know). They must be an object.
- **Approval:** `mcpToolNeedsApproval(tool)` decides (below).
- **Read-only** (usable by widgets, reported by `host.tools.list`): only when `annotations.readOnlyHint` is `true`.

## `host.mcp.listTools`

```ts
listTools(server: McpServer, options?: { fresh?: boolean }): Promise<McpTool[]>;
```

`tools/list`, following `nextCursor` pages. Cached for 5 minutes per server URL and headers (shared with the
assistant's listing); `fresh: true` lists again. A failure isn't cached. `include` and `exclude` are ignored here:
the view gets every tool. Bridge method: `mcp.listTools`.

## `host.mcp.callTool`

```ts
callTool(server: McpServer, name: string, args: Record<string, unknown>): Promise<unknown>;
```

`tools/call` with `name` as the server knows it. Resolves with the result's `structuredContent` when the server
sends one; otherwise the text parts joined with newlines, parsed as JSON when they are JSON, else the string; when
there is no text but other content, `{ content: [...] }` with only the parts' `type` kept for non-text parts.
Bridge method: `mcp.callTool`. Frame timeout: 10 minutes.

## Errors

Both methods reject with a readable message for:

| When | Message (example) |
|---|---|
| Host not allowed | `Not allowed to reach crm.example.com` |
| Unreachable (including CORS refusals) | `Couldn't reach https://crm.example.com/mcp: Failed to fetch` |
| 401 or 403 | `The <name> MCP server rejected the credentials (HTTP 401). Check the extension's connection settings.` |
| Other HTTP error | `The <name> MCP server answered HTTP 500: <first 300 characters>` |
| JSON-RPC error | the server's error message |
| Tool result with `isError` | the tool's own text |

The client sends `initialize` once per server per window (protocol version `2025-06-18`, client name `SpaceMIX`)
and re-initializes once if the server says it isn't initialized. It keeps no session and accepts both JSON and
event-stream responses.

## `mcpToolNeedsApproval`

```ts
function mcpToolNeedsApproval(tool: McpTool): boolean;
```

Exported from `@spacemix/sdk`. Whether the assistant asks the user before running the tool:

- `readOnlyHint: true`: no.
- `destructiveHint: true`, or `readOnlyHint`/`destructiveHint` set to `false`: yes.
- Neither hint set (other annotations don't count): no if the name, or the part after the last `.`, `/` or `:`, starts with a read verb (`get`, `list`,
  `find`, `search`, `read`, `describe`, `query`, `count`, `fetch`) followed by a non-lowercase letter or the end
  (`list_people`, `getCompany`); otherwise yes.

Views use it to show which tools ask first.

## Example

```ts
import type { ExtensionModule, McpServer, SpacemixHost } from "@spacemix/sdk";
import { host } from "@spacemix/sdk";

interface Connection {
  url: string;
  token: string;
}

async function servers(h: SpacemixHost): Promise<McpServer[]> {
  const c = await h.storage.get<Connection>("connection");
  if (!c?.url || !c.token) return [];
  return [{ name: "crm", url: `${c.url}/mcp`, headers: { Authorization: `Bearer ${c.token}` } }];
}

export async function toolNames(): Promise<string[]> {
  const [server] = await servers(host);
  return server ? (await host.mcp.listTools(server)).map((t) => t.name) : [];
}

export const mcp: ExtensionModule["mcp"] = servers;
```

## Stability

Experimental: requests run in the app's web view, so they depend on the server's CORS policy, and the host check
differs from `host.net.fetch` for `net:*.<domain>` (see the open questions in [contract.md](../../contract.md#open-questions-before-we-freeze)). Both may
change to go through the server like `host.net.fetch`.
