MCP Servers
McpServer is a client for remote MCP servers over Streamable HTTP. A server's
tools go in a request like #[tool] functions, through the same hooks, and its resources and prompts are there to
read.
Using a server's tools
import '@formata/ai';
Ai ai: {}
McpServer wiki: {
url: 'https://mcp.deepwiki.com/mcp'
prefix: 'wiki_' // before each tool's name, so two servers' tools can't share one
}
#[main]
fn main() {
using request = new {
model: 'anthropic/claude-sonnet-5-5',
messages: [new { role: 'user', content: 'In the dev-formata-io/stof repo, what is a #[type]?' }],
tools: await self.wiki.tools(),
};
const run = await self.ai.agent(request);
pln(run.text);
}
The host allows the server's host like any other (doc.allowHttp(['mcp.deepwiki.com'])). A server that needs a
token gets it the way providers get keys: key: 'wiki' names a key the host gave with
secret('wiki', token, ['mcp.example.com']), and it's sent as a bearer token.
tools() reads the server's whole list each time it's called, so call it when you want the list fresh. A tool call
goes to the server through the approve and result hooks, and the model sees the result's text (a tool's failure is an
error result, so it can try again). The result is cut at the Ai's max_result, like any tool's.
Calling it yourself
const result = await self.wiki.call('read_wiki_structure', {'repoName': 'dev-formata-io/stof'});
pln(<McpServer>.text(result)); // the text a model would see
// a server with resources and prompts
const resources = await self.docs.resources(); // and templates()
const contents = await self.docs.read('docs://refunds');
const prompt = await self.docs.prompt('triage', {'ticket': '1042'}); // description and messages
A prompt's messages are ready for a request's messages. discover() gives what the server says about itself, and
request(method, params) sends any other request.
Old and new servers
The client speaks MCP 2026-07-28, which is stateless. An older server (2025-11-25 back to 2025-03-26) is found out on
the first request, and then the client does the initialize handshake and keeps its session. Nothing changes for your
code.
Errors
Errors are thrown as maps like the Ai's: kind (auth, bad_request, server, network), message, retry,
status, and the JSON-RPC code and data when the server sent an error. A refusal (a 401, or a 403 for a token
missing a scope) also has challenge, the server's WWW-Authenticate parameters, and resource, the server's URL:
what a login needs.
Logging in (OAuth)
Logging in takes a browser, so the host does it, and the TypeScript and Python documents have it built in. mcpLogin
follows the MCP authorization spec: it finds the server's authorization server, gets a client id (yours, a Client ID
Metadata Document URL, or dynamic registration), sends the user to log in with PKCE, checks the answer, trades the
code for tokens, and gives the document the access token as the server's key. Your part is authorize: open the login
page, and give back the URL the browser was sent back to.
McpServer linear: { url: 'https://mcp.linear.app/mcp', key: 'linear' }
const tokens = await doc.mcpLogin({
server: 'https://mcp.linear.app/mcp',
secret: 'linear', // the McpServer's key
redirectUri: 'http://127.0.0.1:8765/callback',
authorize: async (url) => openBrowserAndWaitForCallback(url),
});
// keep `tokens` somewhere safe; when the access token expires:
await doc.mcpRefresh('linear', tokens);
tokens = doc.mcp_login('https://mcp.linear.app/mcp', 'linear', 'http://127.0.0.1:8765/callback', open_browser_and_wait)
tokens = doc.mcp_refresh('linear', tokens)
A 403 with error: 'insufficient_scope' means logging in again for more scopes (pass the error's challenge).
official/ai/live/mcp.ts has a whole login with a local callback server, to copy from.
Not here yet
The stdio transport (servers run as local processes), the deprecated HTTP+SSE transport, subscriptions, and progress notifications. A server that asks the client for input (elicitation, sampling) gets an error.