Inside the server
This page helps you find your way in the music_assistant/ package: what the central mass object is, which controller owns what, in which order the server starts, and the conventions that hold everywhere.
The mass object
Section titled “The mass object”MusicAssistant in music_assistant/mass.py is the server. Its one instance is passed to every controller and provider as mass. It holds:
- every core controller as an attribute, such as
mass.musicandmass.players; - the registry of provider manifests and loaded providers (
mass.get_provider,mass.load_provider,mass.unload_provider); - the event bus (
mass.signal_event,mass.subscribe); - the task helpers (
mass.create_task,mass.call_later); - the API command registry (
mass.command_handlers).
Core controllers
Section titled “Core controllers”A core controller is a subclass of CoreController (music_assistant/models/core_controller.py). It lives in its own folder under music_assistant/controllers/, has a setup() and close(), and can have its own config entries, which the user sees as the settings of that part of the server.
| Attribute | Owns | README |
|---|---|---|
mass.config |
The settings file: core, provider, player and queue config, and setup flows | - |
mass.discovery |
Network discovery: the shared zeroconf instance, mDNS and SSDP | discovery |
mass.cache |
The cache database and the use_cache decorator |
cache |
mass.tasks |
Long-running and scheduled work the user can see | tasks |
mass.webserver |
The HTTP server, the API, authentication and remote access | webserver |
mass.metadata |
Filling in metadata and serving images | metadata |
mass.music |
The library database, library sync, search and browse | music |
mass.players |
Every player, the protocols attached to it, groups | players |
mass.player_queues |
One queue per player and what plays next | player_queues |
mass.streams |
The stream server, the audio pipeline and audio analysis | streams |
mass.translations |
The translated strings of server objects | - |
mass.diagnostics |
The troubleshooting report users attach to an issue | - |
mass.dashboard |
Casting dashboards to display devices | - |
mass.statistics |
Play statistics, read from the library database | - |
mass.storage |
The storage locations the server can see | - |
mass.community_catalog |
Installing and loading community providers | community_catalog |
mass.ai |
The default AI engines that features use | - |
Startup order
Section titled “Startup order”flowchart TD
A["config.setup()"] --> B["discovery.start_zeroconf()"]
B --> C["Load provider manifests"]
C --> D["_load_core_controllers()"]
D --> E["translations set up first"]
E --> F["Other controllers set up in parallel"]
F --> G["post_setup() one controller at a time"]
G --> H["_register_api_commands()<br/>webserver and discovery set up"]
H --> I["One-off migrations"]
I --> J["_load_builtin_providers()"]
J --> K{"safe mode?"}
K -->|"no"| L["_load_providers()<br/>community_catalog.start()"]
K -->|"yes"| M["RUNNING"]
L --> M
How to read this
Section titled “How to read this”This is MusicAssistant.start() in mass.py, top to bottom.
- The config controller comes first, because everything else reads config. Discovery follows right after: the shared HTTP sessions resolve
.localnames through its zeroconf instance. - The manifests of all provider folders are read before any controller exists, so the controllers can see which providers there are.
- The controllers are created, the translations catalog is set up alone, and the rest are set up in parallel. Then
post_setup()runs for the controllers that need a second step once the others are ready. - Only then is the API registered and the webserver started, so no client can call a controller that is not ready.
- Built-in providers load and are awaited, also in safe mode. In safe mode nothing else loads. Otherwise the other configured providers load a few at a time, and the community catalog starts in the background.
stop() closes the player queues first, cancels the tracked tasks and timers, unloads all providers and then closes the controllers.
Conventions that hold everywhere
Section titled “Conventions that hold everywhere”Each line gives the name to search for in the code.
- Async only. No blocking IO in a coroutine: use an async library or
asyncio.to_thread.mass.verify_event_loop_threadraises when event and task helpers are called off the loop, andscripts/check_blocking_io.pyruns in pre-commit. - Background work.
mass.create_taskfor a task the server tracks and cancels on stop,mass.call_laterfor delayed or debounced work (both take atask_id). - Work the user sees. Library syncs and other long jobs go through
mass.tasks(register_scheduled_task,run_background_task), which shows them in the app with progress. - Events, not polling.
mass.signal_eventandmass.subscribewith anEventType. - Caching. The
use_cachedecorator incontrollers/cache/helpers.pycaches the result of a provider or controller method. See Cache and throttle your requests. - Throttling.
Throttler,ThrottlerManagerandthrottle_with_retriesinhelpers/throttle_retry.pykeep requests to a service within its limits. See Cache and throttle your requests. - Config entries.
get_config_entries()on a provider or controller returns theConfigEntrylist the app renders as settings. - Translations. English strings live in
strings.jsonfiles (one shared, one per provider or controller).scripts/build_translations.pycompiles them intotranslations/en.json. - Helpers. Look in
music_assistant/helpers/before you write a new utility.
Read deeper
Section titled “Read deeper”- mass.py: the
MusicAssistantclass andstart(). - AGENTS.md: code style and the rules every change follows.
- Providers
- The API and events
- Server
Music Assistant is a project of the Open Home Foundation.