MCP servers and clients: the mcp mod
The Model Context Protocol (MCP) is how AI programs, like Claude Code and Claude Desktop, use tools, resources and prompts that other programs give them. mods/mcp.luma makes a Luma program an MCP server, so an AI program can call Luma functions, and is a client of other MCP servers. It is written in Luma, on web servers, web requests and JSON. Copy it next to your program and import it.
A server
import "mcp"
// A note to save.
obj Note {
title: str // a short title
text: str
}
s: mcp.server = mcp.server("notes", "1.0")
s.tool("save", "Call this to save a note for the user", schema(Note), fn(input: json): str {
n: Note = convert(input)
file("notes/${n.title}.txt").write(n.text)
return "saved"
})
s.stdio()An AI program starts it and talks to it on its stdin and stdout. In Claude Code, add it with claude mcp add notes -- luma run notes.luma (or the program luma build made); Claude Desktop takes the same command in its settings file. Then asking “save a note about green tea” calls save.
| Method | What it does |
|---|---|
mcp.server(name, version) | a server, with the name and version it tells clients |
s.tool(name, about, schema, run) | a tool: its name, when to call it, its input’s schema (usually schema of an obj), and the function that runs it, fn(input: json): str |
s.resource(uri, about, read) | something to read, by its URI: read is fn(): str, called each time it is read |
s.prompt(name, about, args, make) | a ready-made prompt the user can pick: args are the names of its arguments, and make, fn(args: {str: str}): str, writes it |
s.instructions(text) | what the AI program is told about the server |
s.stdio() | serves on stdin and stdout, until stdin ends |
s.serve(port) | serves over HTTP at http://127.0.0.1:port/mcp, for this computer only |
s.listen(host, port, limit) | the same on another address ("" for all of them), answering limit requests (0: no end) |
- On stdio, stdout belongs to the protocol. A tool that
prints breaks the connection: write messages withstderr.write, which AI programs show in their logs. - An error a tool throws goes to the AI program as a failed call, with its message, and the server goes on.
- Over HTTP, requests from web pages of other sites (an
Originthat isn’tlocalhostor127.0.0.1) are refused, so a page in a browser can’t use the tools.
Resources and prompts
s.resource("notes://all", "The titles of all notes", fn(): str {
names: [str] = []
folder("notes").files().walk(f) {
names[] = f.name()
}
return names.join("\n")
})
s.prompt("review", "Review a note", ["title"], fn(args: {str: str}): str {
return "Read the note ${args["title"]} and suggest improvements."
})The client
mcp.start(list) starts a server that runs on stdio, a program and its arguments, as AI programs start them; mcp.connect(url) connects to one over HTTP:
files: mcp.client = mcp.start(["npx", "-y", "@modelcontextprotocol/server-filesystem", "/home/ann/notes"])
print(files.call("read_text_file", {"path": "/home/ann/notes/tea.txt"}))
files.close()c: mcp.client = mcp.connect("http://127.0.0.1:3000/mcp")
c.tools().walk(t) {
print("${t.name}: ${t.about}")
}
print(c.call("save", {"title": "tea", "text": "buy green tea"}))| Method | What it does |
|---|---|
c.tools() | the server’s tools, a [mcp.info]: name, about, input (its schema) |
c.call(name, input) | runs a tool and gives its text |
c.read(uri) | a resource’s text |
c.prompt(name, args) | a prompt made with its arguments, a {str: str} |
c.close() | ends a server on stdio: its input ends, and after 2 seconds it is stopped |
c.name, c.guide, c.version | the server’s name, its instructions, the protocol version spoken |
A server’s tools become Claude’s with the claude mod:
c.tools().walk(t) {
ai.tool(t.name, t.about, t.input, fn(input: json): str {
return c.call(t.name, input)
})
}
print(ai.ask("Save a note about green tea."))Versions
- The server speaks both revisions of the protocol: 2026-07-28, where every request says its version and there is no handshake, and the ones before it (2024-11-05 to 2025-11-25), which start with
initialize. It keeps no sessions. - The client asks for 2026-07-28 first, else 2025-11-25 (sending back the session a server gives); on stdio, a server that doesn’t answer within 5 seconds is taken to be an older one. Answers can come as JSON or as an event stream. Lines a server on stdio writes that aren’t the answer are passed over, and a question it asks the client (like
roots/list) is told the client doesn’t do that. - Both are tested against Anthropic’s official Python SDK.
Errors
| Code | When |
|---|---|
mcp.FAILED (100) | the server answered with an error, or a tool call failed; the message says what |
mcp.BROKEN (101) | not an MCP server, or an answer that isn’t one |
mcp.AUTH (102) | the server wants a login (401 or 403): OAuth isn’t supported yet |
Not available yet
- Servers asking the client for things (sampling, elicitation, roots), subscriptions to changes, progress, tasks, resource templates, pictures in tool results.
- OAuth, for servers on the internet that need a login.