Skip to content

aaa.mcp generator

See Generating an MCP server.

load_spec

load_spec(
    source: str
    | PathLike[str]
    | Mapping[str, Any]
    | None = None,
    *,
    environ: Mapping[str, str] | None = None,
    overlay: Mapping[str, Any] | None = None,
    overlays: Sequence[str | PathLike[str]] = (),
) -> MCPSpec

Build the spec: the file (with its includes) or mapping (lowest), then overlay files (overlays= and AAA_MCP_OVERLAY), then AAA_MCP__* environment variables, then overlay (command-line flags).

MCPSpec

Bases: _M

launches_processes

launches_processes() -> bool

Whether serving this spec starts a subprocess (a stdio upstream): see AAA_MCP_ALLOW_SUBPROCESS.

code_refs

code_refs() -> frozenset[str]

Everything in this spec that makes the server run code: modules and objects it imports, algorithms and engines it builds, and the commands it launches. A hot reload may only use references that were already in use when the server started (see aaa.mcp.reload).

imports_code

imports_code() -> bool

Whether compiling this spec imports modules (and so runs code): see the kill-switch in aaa.mcp.compile.

acompile_spec async

acompile_spec(
    spec: MCPSpec,
    *,
    live: Mapping[str, Any] | None = None,
    engine: Engine | None = None,
    approval: ApprovalCallback | None = None,
    allow_import: bool | None = None,
    allow_subprocess: bool | None = None,
) -> CompiledServer

Build the server. If the spec has mcp sources the returned server holds live connections: close them with await core.aclose() (or async with core:). If compilation fails, anything already connected is closed before the error propagates.

compile_spec

compile_spec(spec: MCPSpec, **kw: Any) -> CompiledServer

acompile_spec for code that is not already in an event loop.

CompiledServer

CompiledServer(
    registry: ToolRegistry,
    *,
    name: str = "aaa",
    engine: Engine | None = None,
    algorithms: Mapping[str, Algorithm] | None = None,
    approval: ApprovalCallback | None = None,
    expose_approval_tools: bool = False,
    tenant: str = "mcp",
    user: str | None = None,
    agent_budget: Budget | None = None,
    max_result_chars: int = 100000,
    title: str | None = None,
    version: str = "0.1",
    instructions: str | None = None,
    tool_meta: Mapping[str, MCPToolMeta] | None = None,
    structured_output: bool = False,
    resources: ResourceSet | None = None,
    prompts: Mapping[str, PromptDef] | None = None,
    tool_policy: Mapping[str, ToolRule] | None = None,
    default_scopes: Iterable[str] | None = None,
    clock: Callable[[], float] | None = None,
    profiles: ProfileSet | None = None,
)

Bases: MCPToolServer

An MCPToolServer built from a spec, plus what was decided: the spec itself and a human-readable report.

aclose async

aclose() -> None

Close the connections to upstream MCP servers (and stop the processes this server launched).

describe

describe(core: CompiledServer) -> dict[str, Any]

Exactly what a client (and so a model) would be shown, for review: tool names, titles, descriptions and hints.

Secret

Secret(name: str)

A value read from the environment when needed. Its repr and str never show it.

SpecError

Bases: ValueError

The configuration is invalid or cannot be used. The message names the field and says how to fix it.

MCPToolMeta dataclass

MCPToolMeta(
    title: str | None = None,
    annotations: MCPAnnotations | None = None,
    output_schema: dict[str, Any] | None = None,
)

Per-tool presentation overrides (what a config's tools.overrides produces).

MCPAnnotations dataclass

MCPAnnotations(
    title: str | None = None,
    read_only: bool | None = None,
    destructive: bool | None = None,
    idempotent: bool | None = None,
    open_world: bool | None = None,
)

MCP tool hints. They are hints for clients, not guarantees: nothing here is enforced by the server.

MCPServerBuilder

MCPServerBuilder(name: str = 'aaa')

add_tools

add_tools(
    tools: Any,
    *,
    tags: list[str] | None = None,
    prefix: str = "",
    key: str | None = None,
) -> Self

Tools you already hold in Python: a ToolRegistry, a Tool, a list of tools or an MCPToolServer.

add_ref

add_ref(
    ref: str,
    *,
    tags: list[str] | None = None,
    prefix: str = "",
) -> Self

Tools from package.module:attribute, imported when the server is compiled.

add_module

add_module(
    module: str,
    *,
    allow: list[str] | None = None,
    exclude: list[str] | None = None,
    path: str | PathLike[str] | None = None,
    strict: bool = True,
    default_approval: bool = True,
    no_approval: list[str] | None = None,
    read_only: list[str] | None = None,
    tags: list[str] | None = None,
    prefix: str = "",
) -> Self

Expose a module's functions (its __all__, or allow). They require approval, and so stay hidden, unless listed in no_approval / read_only. See the generator guide for the rules.

override

override(
    name: str,
    *,
    title: str | None = None,
    description: str | None = None,
    requires_approval: bool | None = None,
    output_schema: dict[str, Any] | None = None,
    read_only: bool | None = None,
    destructive: bool | None = None,
    idempotent: bool | None = None,
    open_world: bool | None = None,
) -> Self

Change how the tool exposed as name looks. Hints given here replace the ones derived from the tool.

agent

agent(
    name: str,
    *,
    algorithm: Algorithm | str,
    description: str,
    deadline_s: float,
    max_steps: int | None = None,
    max_tokens: int | None = None,
    max_cost: float | None = None,
    input_schema: Mapping[str, Any] | None = None,
    input_template: Mapping[str, Any] | None = None,
    output: str = "text",
    tags: list[str] | None = None,
    read_only: bool | None = None,
    open_world: bool | None = None,
) -> Self

Expose an algorithm as a tool. algorithm is the object or a module:attribute reference; a deadline is mandatory.

files

files(
    name: str,
    root: str | PathLike[str],
    *,
    pattern: str = "**/*",
    shared: bool = False,
    allow_hidden: bool = False,
    title: str | None = None,
    description: str | None = None,
) -> Self

Files under root as resources, one subdirectory per tenant unless shared=True.

index

index(
    name: str,
    index: Any,
    *,
    tenant_key: str | None = None,
    shared: bool = False,
    k: int = 5,
    max_chars: int = 800,
) -> Self

Search over an Index (an object, or a module:attribute reference). Say how documents are tenant-scoped (tenant_key) or shared=True.

prompt

prompt(
    name: str,
    messages: list[str | tuple[str, str]],
    *,
    arguments: list[dict[str, Any]] | None = None,
    title: str | None = None,
    description: str | None = None,
) -> Self

A prompt template. Messages are texts (user role) or (role, text) pairs; {argument} placeholders must be declared in arguments.

engine

engine(engine: Engine | str) -> Self

The engine agent tools run on: an object, or a module:attribute reference the server builds itself.

approval

approval(callback: ApprovalCallback) -> Self

Decide, per call, whether a tool that requires approval may run (see MCPToolServer). Fails closed.

dump

dump(path: str | PathLike[str] | None = None) -> str

Write the configuration as JSON/TOML/YAML (by extension). Fails if it holds live Python objects.

dump_spec

dump_spec(
    spec: MCPSpec, path: str | PathLike[str] | None = None
) -> str

The spec as text in the format path's extension names (JSON if no path), written to path when one is given. Defaults are left out, so the file says only what you chose. Specs with live sources refer to Python objects a file cannot carry, so they cannot be written.

AgentToolSpec

Bases: _M

Expose one algorithm as its own tool. algorithm is a module:attribute reference (an Algorithm) or the key of an object passed to compile_spec(..., live={...}). input_template builds the algorithm's input from the validated arguments: string values may contain {argument} placeholders (plain names only; no attribute access, no format specs) and a value that is exactly one placeholder keeps the argument's own type.

BudgetSpec

Bases: _M

What one agent run may spend. deadline_s is mandatory: a run that parks on a human approval (which MCP cannot give) would otherwise wait forever.

Options dataclass

Options(
    name: str,
    transport: str = "stdio",
    docker: bool = True,
    fmt: str = "toml",
)

render

render(
    spec: MCPSpec, config_text: str, opts: Options
) -> dict[str, str]

Every file of the project, as {relative path: text}. The configuration file is included as given (it is the user's).

write_project

write_project(
    dest: Path,
    files: dict[str, str],
    opts: Options,
    *,
    force: bool = False,
) -> list[str]

Write the project. Refuses to touch a non-empty directory (or overwrite a file) unless force.

check_project

check_project(dest: Path) -> list[str]

Problems: generated files that are missing or differ from their template.

update_project

update_project(dest: Path) -> tuple[list[str], list[str]]

Regenerate files you have not edited. Returns (rewritten, skipped because you edited them).

McpSource

Bases: _Source

The tools of ANOTHER MCP server, folded into this one.

transport = "stdio" launches command (an argument vector, never a shell) with a minimal environment: only env (literal, non-secret values) and the variables named in env_from. transport = "http" connects to an https url through a transport that resolves the host once, requires a public address and allowed_hosts, pins the connection and follows no redirects.

The upstream is untrusted input: its tools require approval unless it marks them read-only (approval = "destructive"), its annotations are not trusted unless trust_annotations, its descriptions are capped, and its results are capped. One connection serves every caller, so it is NOT tenant-isolated; tenant and user are never forwarded to it.

CompiledServer

CompiledServer(
    registry: ToolRegistry,
    *,
    name: str = "aaa",
    engine: Engine | None = None,
    algorithms: Mapping[str, Algorithm] | None = None,
    approval: ApprovalCallback | None = None,
    expose_approval_tools: bool = False,
    tenant: str = "mcp",
    user: str | None = None,
    agent_budget: Budget | None = None,
    max_result_chars: int = 100000,
    title: str | None = None,
    version: str = "0.1",
    instructions: str | None = None,
    tool_meta: Mapping[str, MCPToolMeta] | None = None,
    structured_output: bool = False,
    resources: ResourceSet | None = None,
    prompts: Mapping[str, PromptDef] | None = None,
    tool_policy: Mapping[str, ToolRule] | None = None,
    default_scopes: Iterable[str] | None = None,
    clock: Callable[[], float] | None = None,
    profiles: ProfileSet | None = None,
)

Bases: MCPToolServer

An MCPToolServer built from a spec, plus what was decided: the spec itself and a human-readable report.

aclose async

aclose() -> None

Close the connections to upstream MCP servers (and stop the processes this server launched).

ToolPolicy

Bases: ToolLimits

Limits for every tool whose final name matches one of the globs. For each setting the first matching policy wins; a tool's own overrides entry beats every policy. A glob that matches no tool is an error.

ToolLimits

Bases: _M

Per-tool limits and visibility. A caller who may not see a tool gets exactly the answer for a tool that does not exist.

ToolRule dataclass

ToolRule(
    timeout_s: float | None = None,
    rate_per_minute: int | None = None,
    burst: int | None = None,
    max_concurrency: int | None = None,
    scopes: frozenset[str] | None = None,
    tenants: frozenset[str] | None = None,
)

visible_to

visible_to(
    tenant: str, scopes: frozenset[str] | None
) -> bool

scopes=None is a caller the host did not restrict. Otherwise the caller needs one of the tool's scopes.

compose

compose(
    path: str | PathLike[str],
    overlays: Sequence[str | PathLike[str]] = (),
) -> Composed

The top file with its includes, then each overlay (later wins). Raises SpecError naming the file at fault.

build

build(
    spec: MCPSpec,
    config: str | Path,
    *,
    client: str = "generic",
    transport: str = "stdio",
    url: str | None = None,
    name: str | None = None,
    overlays: list[str] | None = None,
) -> dict[str, Any]

{"mcpServers": {name: entry}} for the single server spec describes.

ReloadableServer

ReloadableServer(
    first: CompiledServer,
    load: Callable[[], MCPSpec],
    compile: Callable[[MCPSpec], Awaitable[CompiledServer]],
    *,
    min_interval_s: float = 2.0,
    grace_s: float = 30.0,
)

Bases: MCPToolServer

Serves whatever the last successful reload compiled. Construct with await ReloadableServer.start(load, compile).

reload async

reload(*, force: bool = False) -> ReloadResult

Build the configuration again and swap it in if it is sound. Never raises for a bad configuration.