Add stable public Python API (mkdocs.build / mkdocs.serve) - #76
Merged
Merged
Conversation
Add mkdocs.build() and mkdocs.serve() as thin, documented wrappers over the internal commands, giving scripts and build tools a stable way to build and serve documentation programmatically instead of calling CLI internals or spawning subprocesses. - mkdocs.build() loads the config (file or file object), fires plugin startup/shutdown events like the CLI does, and returns the loaded MkDocsConfig so callers can inspect site_dir etc. All config options can be overridden via keyword arguments; Path values are accepted. - mkdocs.serve() mirrors the serve command and blocks until shutdown. - The build/serve CLI commands now go through the same public functions, so the CLI and the API can never drift apart. - New user guide page with examples plus API reference entries.
This was referenced Aug 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements stable public Python API** (upstream mkdocs#1240 "API for running mkdocs"): a stable, documented, programmatic API for building and serving docs — no more
from mkdocs.__main__ import build_commandhacks or subprocess calls.What changed
mkdocs.build()— programmatic equivalent ofmkdocs build:-), withPathacceptedsite_name,nav,strict, ...)on_startup/on_shutdownevents exactly like the CLIMkDocsConfig(e.g.config.site_dir)mkdocs.serve()— programmatic equivalent ofmkdocs serve(blocks until shutdown), with the same override semanticsmkdocs build/mkdocs servecommands now call the same public functions, so CLI and API can never drift apartcommands.serve.serve()'sconfig_fileannotation tostr | IO | None(the CLI already passes file objects at runtime)dev-guide/api.md(mkdocstrings)mkdocs/tests/api_tests.py(build end-to-end incl.Path, file objects, kwargs overrides, dirty mode; serve argument forwarding); updated CLI serve tests for the normalizedwatchlistVerification
uvx prek run -apasses (incl. mypy)py.typedalready ships with the package, so the new API is covered by PEP 561 type declarationsExamples