foley.agent.mcp#

The MCP surface — foley’s façade as agent-callable tools (py2mcp, #12, report 05/10).

Exposes the SELECT→preview→WEAVE loop to an LLM agent (or any MCP client) as a set of JSON-safe tools built with py2mcp (mk_mcp_server over stdio). The single rule: tools take/return only JSON (ids, scores, license summaries, timeline dicts, store keys) — never a Candidate, a WeaveResult, or numpy audio. Heavy objects stay server-side; audio is referenced by a byte-store key.

The tools are the verbs of foley.agent.verbs (#193), the table the CLI is built from too; this module serves them (stdio or authenticated HTTP) and re-exports them, so mcp.foley_find and mcp.TOOLS keep working. py2mcp / fastmcp are imported LAZILY inside build_mcp_server() only, so import foley and import foley.agent stay dol-only.

Module Attributes

MIN_TOKEN_CHARS

The shortest bearer token make_http_app() accepts.

Functions

accepted_tokens(tokens, *[, min_chars])

tokens as the set a bearer gate accepts, or a ValueError (fail-closed).

build_mcp_server(*[, library, session, ...])

Build the foley MCP server (lazy py2mcp); registers the JSON-safe tool surface.

make_http_app(*, auth[, path, json_response])

Build a bearer-auth-gated ASGI app serving the foley MCP tools over streamable HTTP.

serve(*[, name, runtime])

Build and run the foley MCP server over stdio (blocks); enforces an offline posture.

serve_http(*[, host, port, path])

Build and serve the foley MCP tools over authenticated streamable HTTP (blocks).

foley.agent.mcp.MIN_TOKEN_CHARS = 16#

The shortest bearer token make_http_app() accepts. A short token is guessable, and the floor also refuses a passphrase that $FOLEY_MCP_TOKEN would split into words.

foley.agent.mcp.accepted_tokens(tokens, *, min_chars=16)[source]#

tokens as the set a bearer gate accepts, or a ValueError (fail-closed).

An empty or blank token is refused, never dropped: a request with no Authorization header reads as the empty token, so accepting one would open the server, and a --token "$UNSET" should stop it rather than start it open.

>>> sorted(accepted_tokens(["a" * 16, "b" * 20]))
['aaaaaaaaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbbb']
>>> accepted_tokens([""])
Traceback (most recent call last):
...
ValueError: a bearer token is blank or shorter than 16 characters (token #1); use e.g. `openssl rand -hex 32`
Return type:

frozenset

foley.agent.mcp.build_mcp_server(*, library=None, session='default', runtime=None, byte_store=None, include=None, name='foley', max_usd=None)[source]#

Build the foley MCP server (lazy py2mcp); registers the JSON-safe tool surface.

Validates that every source declares a data_egress (fail-closed), binds the injectable library / runtime / byte-store, and hands the resolved tool functions to py2mcp.mk_mcp_server. Never starts a server or touches the network.

Parameters:
Returns:

A fastmcp.FastMCP server.

foley.agent.mcp.make_http_app(*, auth, path='/mcp', json_response=False, **server_kwargs)[source]#

Build a bearer-auth-gated ASGI app serving the foley MCP tools over streamable HTTP.

HTTP exposes the tool surface to the network, so auth is **required and fail-closed**: pass auth={'bearer_tokens': [...]} (accepted tokens, each at least MIN_TOKEN_CHARS long, see accepted_tokens()) — a request without a matching Authorization: Bearer <token> header gets a 401. The returned app (a Starlette/ASGI callable) mounts in any ASGI host (uvicorn, gunicorn, a parent FastAPI). py2mcp / fastmcp are imported lazily inside build_mcp_server(), so import foley stays dol-only.

Parameters:
  • auth (dict) – {'bearer_tokens': [...]} — required; no token, or a blank or short one, raises (fail-closed).

  • path (str) – The MCP HTTP mount path.

  • json_response (bool) – Return a single JSON response instead of an SSE stream (simple clients).

  • **server_kwargs – Passed to build_mcp_server().

Returns:

An ASGI application (the bearer-gated MCP HTTP app).

Raises:

ValueError – If auth carries no usable bearer token (no anonymous HTTP access).

foley.agent.mcp.serve(*, name='foley', runtime=None, **kwargs)[source]#

Build and run the foley MCP server over stdio (blocks); enforces an offline posture.

Return type:

None

foley.agent.mcp.serve_http(*, host='127.0.0.1', port=8000, auth, path='/mcp', **kwargs)[source]#

Build and serve the foley MCP tools over authenticated streamable HTTP (blocks).

Wraps make_http_app() and runs it with uvicorn, under the runtime posture (_posture()), as serve() does. auth is required (fail-closed).

Return type:

None