Load addons from Python entry points (mitmproxy.addons) (#7755) by johnnylemonny · Pull Request #8480 · mitmproxy/mitmproxy · GitHub
Skip to content

Load addons from Python entry points (mitmproxy.addons) (#7755) - #8480

Open
johnnylemonny wants to merge 1 commit into
mitmproxy:mainfrom
johnnylemonny:feature/addon-entrypoints
Open

johnnylemonny wants to merge 1 commit into
mitmproxy:mainfrom
johnnylemonny:feature/addon-entrypoints

Conversation

@johnnylemonny

Copy link
Copy Markdown

Fixes: #7755


Description

Currently, third-party addons must be passed via CLI flags (-s /path/to/addon.py), loaded via configuration, or imported in custom wrapper scripts.
This PR introduces automatic discovery and loading of addons registered under the entry point group mitmproxy.addons.

This enables packaging third-party addons into standalone Python packages (e.g. on PyPI) that are automatically activated when installed into the same environment or virtualenv as mitmproxy.

Key changes:

  1. Entry Point Discovery (mitmproxy.addons.entrypoint_addons):
    • Queries importlib.metadata.entry_points(group="mitmproxy.addons").
    • Supports classes (automatically instantiated), zero-argument factories/functions (invoked), pre-instantiated addon objects, or sequences of addons returned by a factory.
    • Automatically assigns the entry point name as addon.name if the loaded addon does not already specify one.
    • Catches and logs errors gracefully with logger.error if a broken or incompatible third-party addon fails to import or instantiate, ensuring mitmproxy starts reliably regardless of third-party issues.
  2. Integrated into default_addons():
    • Discovered entry point addons are appended at the end of default addons, preserving existing core precedence.
  3. Tests:
    • Added exhaustive unit tests in test/mitmproxy/addons/test_entrypoints.py covering all discovery patterns, error resilience, and integration with default_addons().
    • Verified 100% individual coverage with uv run tox -e individual_coverage.
  4. Changelog:
    • Updated CHANGELOG.md under ## Unreleased: mitmproxy next.

How to use

In pyproject.toml of an addon package:

[project.entry-points."mitmproxy.addons"]
my_addon = "my_package.addon:MyAddon"

Once installed into the environment (pip install my_package / uv pip install my_package), mitmproxy will automatically discover and load my_addon.


Checklist

  • Implemented entry point discovery in mitmproxy.addons.
  • Unit tests added and passing (uv run pytest test/mitmproxy/addons/test_entrypoints.py).
  • 100% test coverage verified (uv run tox -e individual_coverage -- test/mitmproxy/addons/test_entrypoints.py).
  • Linting and code formatting verified (uv run ruff check . and uv run ruff format --check .).
  • Type checking passed (uv run mypy).
  • CHANGELOG.md updated.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant