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
The shortest bearer token |
Functions
|
|
|
Build the foley MCP server (lazy |
|
Build a bearer-auth-gated ASGI app serving the foley MCP tools over streamable HTTP. |
|
Build and run the foley MCP server over stdio (blocks); enforces an offline posture. |
|
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_TOKENwould split into words.
- foley.agent.mcp.accepted_tokens(tokens, *, min_chars=16)[source]#
tokensas the set a bearer gate accepts, or aValueError(fail-closed).An empty or blank token is refused, never dropped: a request with no
Authorizationheader 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:
- 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 topy2mcp.mk_mcp_server. Never starts a server or touches the network.- Parameters:
library – The
foley.index.SoundLibrary(default: the shared one).session (
str) – The default session id.runtime – A
foley.runtime.RuntimeConfig(default: the active one).byte_store – A
MutableMapping[str, bytes]for previews / rendered mixes.include (
Optional[list[str]]) – Optional subset of tool names to expose.name (
str) – The MCP server name.max_usd (
Optional[float]) – The server’s cumulative spend cap across every paid tool call, for its lifetime (defaultfoley.cost.default_max_usd(): $1, or $3 under$FOLEY_LLM=anthropic).
- Returns:
A
fastmcp.FastMCPserver.
- 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
authis **required and fail-closed**: passauth={'bearer_tokens': [...]}(accepted tokens, each at leastMIN_TOKEN_CHARSlong, seeaccepted_tokens()) — a request without a matchingAuthorization: Bearer <token>header gets a401. The returned app (a Starlette/ASGI callable) mounts in any ASGI host (uvicorn, gunicorn, a parent FastAPI).py2mcp/fastmcpare imported lazily insidebuild_mcp_server(), soimport foleystays 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
authcarries 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:
- 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 theruntimeposture (_posture()), asserve()does.authis required (fail-closed).- Return type: