Skip to content

Feature/plugin multi tenancy with per tool plugin config - #4068

Merged
jonpspri merged 17 commits into
mainfrom
feature/plugin-multi-tenancy-with-per-tool-plugin-config
Apr 7, 2026
Merged

jonpspri merged 17 commits into
mainfrom
feature/plugin-multi-tenancy-with-per-tool-plugin-config

Conversation

@madhu-mohan-jaishankar

Copy link
Copy Markdown
Collaborator

🔗 Related Issue

Closes #


📝 Summary

This PR combines two related pieces of work that together form the foundation of plugin multi-tenancy in ContextForge.

1. Per-tool plugin binding API (/v1/tools/plugin_bindings)

Adds four new endpoints for configuring per-tool plugin policies on a per-team basis:

  • POST /v1/tools/plugin_bindings — create or update bindings (upsert)
  • GET /v1/tools/plugin_bindings — list all bindings
  • GET /v1/tools/plugin_bindings/{team_id} — list bindings for a specific team
  • DELETE /v1/tools/plugin_bindings/{id} — delete a binding by ID

Each binding is a (team_id, tool_name, plugin_id) triple stored in a new tool_plugin_bindings table. POSTing an existing triple performs an in-place upsert, preserving the original id and created_* fields. A single request can configure multiple teams and multiple tools at once.

Supported plugin types: OUTPUT_LENGTH_GUARD, RATE_LIMITER, SECRETS_DETECTION.

Security:

  • Non-admin callers are restricted to teams they belong to — cross-team requests return HTTP 403.
  • Platform admins and sidecar/automation tokens (is_admin: true, teams: null) bypass the team guard and can configure any team.
  • tools.manage_plugins permission is required for write operations; tools.read for reads.
  • tools.manage_plugins is seeded into the team_admin role on new installations and backfilled via migration for existing ones.

Performance: a single team_id IN (...) prefetch replaces the previous per-triple SELECT loop, and all inserts/updates are flushed in a single db.flush() call after the loop.

2. Tenant plugin manager (TenantPluginManagerFactory)

Adds a per-context plugin manager that dynamically loads and routes plugins per tenant/team at tool execution time. The TenantPluginManagerFactory resolves the correct plugin pipeline for the calling team rather than applying a single global plugin stack. The tool service, resource service, and prompt service are updated to use the tenant-aware manager when executing MCP operations.

Key changes:

  • plugins/framework/manager.py — TenantPluginManagerFactory with per-team plugin loading and lifecycle management
  • plugins/framework/__init__.py — factory registration and context propagation
  • plugins/framework/models.py — tenant-scoped plugin model additions
  • services/tool_service.py, resource_service.py, prompt_service.py — use tenant plugin manager at execution time
  • services/base_service.py — base plumbing for tenant context propagation

Together these two pieces form a complete multi-tenancy plugin system: the API lets teams declare which plugins apply to which tools, and the tenant manager enforces those policies at runtime.


🏷️ Type of Change

  • Bug fix
  • Feature / Enhancement
  • Documentation
  • Refactor
  • Chore (deps, CI, tooling)
  • Other (describe below)

🧪 Verification

Check Command Status
Lint suite make lint ✅
Unit tests make test ✅ 69 new tests passing
Coverage ≥ 80% make coverage

✅ Checklist

  • Code formatted (make black isort pre-commit)
  • Tests added/updated for changes
  • Documentation updated (if applicable)
  • No secrets or credentials committed

📓 Notes (optional)

New Alembic migrations (applied in order):

  • b1c2d3e4f5a6 — creates the tool_plugin_bindings table
  • c2d3e4f5a6b7 — backfills tools.manage_plugins into the team_admin role for existing installations

For sidecar/automated processes that need to configure bindings across all teams in a single request, use an API token with is_admin: true, teams: null and assign the platform_admin role.

@cafalchio
cafalchio force-pushed the feature/plugin-multi-tenancy-with-per-tool-plugin-config branch from 17c9fa3 to 3afa5b8 Compare April 7, 2026 18:49
@cafalchio
cafalchio marked this pull request as ready for review April 7, 2026 18:49
cafalchio and others added 17 commits April 7, 2026 21:25
Introduce context-scoped plugin isolation via a new TenantPluginManagerFactory
singleton. Each virtual server/tenant receives its own TenantPluginManager with
an independently merged plugin config, while the __global__ manager continues to
serve non-context-aware call sites.

- Add TenantPluginManager (disables Borg state sharing) and
  TenantPluginManagerFactory (async-safe, deduplicating cache/factory)
- Update framework __init__ accessors: init_plugin_manager_factory(),
  get_plugin_manager(server_id), shutdown/reset helpers
- Wire services (tool, prompt, resource, classification) to pass server_id
  when resolving plugin managers
- Update main.py lifespan to initialize factory eagerly at startup
- Add PluginConfigOverride model and shallow config merge logic
- Add architecture doc and 756-line test suite for tenant-scoped plugin execution

Signed-off-by: cafalchio <[email protected]>
Signed-off-by: cafalchio <[email protected]>
Signed-off-by: cafalchio <[email protected]>
@jonpspri
jonpspri force-pushed the feature/plugin-multi-tenancy-with-per-tool-plugin-config branch from 5312b42 to 4df392a Compare April 7, 2026 20:27
@jonpspri
jonpspri merged commit 03e2714 into main Apr 7, 2026
28 checks passed
@jonpspri
jonpspri deleted the feature/plugin-multi-tenancy-with-per-tool-plugin-config branch April 7, 2026 20:55
@cafalchio cafalchio linked an issue Apr 8, 2026 that may be closed by this pull request
4 tasks done
@cafalchio cafalchio linked an issue Apr 8, 2026 that may be closed by this pull request
1 of 10 tasks
crivetimihai added a commit that referenced this pull request Apr 8, 2026
…C, tests

Review fixes for the OAuth access token verification feature (#3715):

Security:
- Validate issuer URL and discovered jwks_uri against SSRF protections
  before outbound fetches (validate_core_url on both paths)
- Validate discovered metadata issuer matches queried issuer (RFC 8414 §3.3)
- Persist oauth_client_id from admin UI into oauth_config for audience
  validation; log warning when audience check is skipped

Correctness:
- Fall through to verify_credentials on jwt.DecodeError instead of
  early-rejecting (fixes 7 broken existing tests)
- Wrap _get_user_by_email_sync and _resolve_teams_from_db in try/except
  returning 503 on DB failures (parity with _auth_jwt path)
- Distinguish infra failures (503) from auth failures (401) via
  OAUTH_VERIFY_UNAVAILABLE sentinel in verify_oauth_access_token
- Fix internal MCP RPC RBAC to enable check_any_team for
  oauth_access_token (non-admin team-scoped users were denied)
- Add auth_method and set_trace_context_from_teams for observability
- Add TOOLS_MANAGE_PLUGINS to Permissions class (fixes team_admin
  bootstrap failure from #4068, see #4085)
- Fix DAR101 flake8 docstring issue in observability.py

Tests:
- 30+ new unit tests covering _auth_jwt OAuth routing,
  _try_oauth_access_token branches, verify_oauth_access_token edge
  cases, SSRF rejection, issuer mismatch, 503 vs 401 classification,
  DB error handling, and trace context

Signed-off-by: Mihai Criveti <[email protected]>
crivetimihai added a commit that referenced this pull request Apr 8, 2026
…C, tests

Review fixes for the OAuth access token verification feature (#3715):

Security:
- Validate issuer URL and discovered jwks_uri against SSRF protections
  before outbound fetches (validate_core_url on both paths)
- Validate discovered metadata issuer matches queried issuer (RFC 8414 §3.3)
- Persist oauth_client_id from admin UI into oauth_config for audience
  validation; log warning when audience check is skipped

Correctness:
- Fall through to verify_credentials on jwt.DecodeError instead of
  early-rejecting (fixes 7 broken existing tests)
- Wrap _get_user_by_email_sync and _resolve_teams_from_db in try/except
  returning 503 on DB failures (parity with _auth_jwt path)
- Distinguish infra failures (503) from auth failures (401) via
  OAUTH_VERIFY_UNAVAILABLE sentinel in verify_oauth_access_token
- Fix internal MCP RPC RBAC to enable check_any_team for
  oauth_access_token (non-admin team-scoped users were denied)
- Add auth_method and set_trace_context_from_teams for observability
- Add TOOLS_MANAGE_PLUGINS to Permissions class (fixes team_admin
  bootstrap failure from #4068, see #4085)
- Fix DAR101 flake8 docstring issue in observability.py

Tests:
- 30+ new unit tests covering _auth_jwt OAuth routing,
  _try_oauth_access_token branches, verify_oauth_access_token edge
  cases, SSRF rejection, issuer mismatch, 503 vs 401 classification,
  DB error handling, and trace context

Signed-off-by: Mihai Criveti <[email protected]>
jonpspri added a commit that referenced this pull request Apr 10, 2026
* add per-context plugin manager with TenantPluginManagerFactory

Introduce context-scoped plugin isolation via a new TenantPluginManagerFactory
singleton. Each virtual server/tenant receives its own TenantPluginManager with
an independently merged plugin config, while the __global__ manager continues to
serve non-context-aware call sites.

- Add TenantPluginManager (disables Borg state sharing) and
  TenantPluginManagerFactory (async-safe, deduplicating cache/factory)
- Update framework __init__ accessors: init_plugin_manager_factory(),
  get_plugin_manager(server_id), shutdown/reset helpers
- Wire services (tool, prompt, resource, classification) to pass server_id
  when resolving plugin managers
- Update main.py lifespan to initialize factory eagerly at startup
- Add PluginConfigOverride model and shallow config merge logic
- Add architecture doc and 756-line test suite for tenant-scoped plugin execution

Signed-off-by: cafalchio <[email protected]>

* ran pre-commit to update secrets

Signed-off-by: cafalchio <[email protected]>

* Update architecture, linted and secrets

Signed-off-by: cafalchio <[email protected]>

* updated secrets

Signed-off-by: cafalchio <[email protected]>

* audited and scan secrets

Signed-off-by: cafalchio <[email protected]>

* feat: add per-tool plugin binding API with validation and tests

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* test: improve router and service test quality for tool plugin bindings

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* fix: rebase migration onto cbedf4e580e0 to resolve multiple Alembic heads

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* chore: update secrets baseline with migration revision IDs

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* chore: mark migration revision IDs as non-secrets in baseline

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* feat: grant tools.manage_plugins to team_admin; fix service UUID and team guard

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* chore: add migration revision IDs to secrets baseline

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* feat: wire per-tool plugin bindings into tool invocation pipeline

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* Added dynamic plugin for rust endpoitns

Signed-off-by: cafalchio <[email protected]>

* Updated secrets and lint

Signed-off-by: cafalchio <[email protected]>

* fixed linter

Signed-off-by: cafalchio <[email protected]>

* test: Fix mocking problem in test_main_sighup

Signed-off-by: Jonathan Springer <[email protected]>

---------

Signed-off-by: cafalchio <[email protected]>
Signed-off-by: Madhu Mohan Jaishankar <[email protected]>
Signed-off-by: Jonathan Springer <[email protected]>
Co-authored-by: cafalchio <[email protected]>
Co-authored-by: Jonathan Springer <[email protected]>
claudia-gray pushed a commit that referenced this pull request Apr 13, 2026
* add per-context plugin manager with TenantPluginManagerFactory

Introduce context-scoped plugin isolation via a new TenantPluginManagerFactory
singleton. Each virtual server/tenant receives its own TenantPluginManager with
an independently merged plugin config, while the __global__ manager continues to
serve non-context-aware call sites.

- Add TenantPluginManager (disables Borg state sharing) and
  TenantPluginManagerFactory (async-safe, deduplicating cache/factory)
- Update framework __init__ accessors: init_plugin_manager_factory(),
  get_plugin_manager(server_id), shutdown/reset helpers
- Wire services (tool, prompt, resource, classification) to pass server_id
  when resolving plugin managers
- Update main.py lifespan to initialize factory eagerly at startup
- Add PluginConfigOverride model and shallow config merge logic
- Add architecture doc and 756-line test suite for tenant-scoped plugin execution

Signed-off-by: cafalchio <[email protected]>

* ran pre-commit to update secrets

Signed-off-by: cafalchio <[email protected]>

* Update architecture, linted and secrets

Signed-off-by: cafalchio <[email protected]>

* updated secrets

Signed-off-by: cafalchio <[email protected]>

* audited and scan secrets

Signed-off-by: cafalchio <[email protected]>

* feat: add per-tool plugin binding API with validation and tests

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* test: improve router and service test quality for tool plugin bindings

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* fix: rebase migration onto cbedf4e580e0 to resolve multiple Alembic heads

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* chore: update secrets baseline with migration revision IDs

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* chore: mark migration revision IDs as non-secrets in baseline

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* feat: grant tools.manage_plugins to team_admin; fix service UUID and team guard

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* chore: add migration revision IDs to secrets baseline

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* feat: wire per-tool plugin bindings into tool invocation pipeline

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* Added dynamic plugin for rust endpoitns

Signed-off-by: cafalchio <[email protected]>

* Updated secrets and lint

Signed-off-by: cafalchio <[email protected]>

* fixed linter

Signed-off-by: cafalchio <[email protected]>

* test: Fix mocking problem in test_main_sighup

Signed-off-by: Jonathan Springer <[email protected]>

---------

Signed-off-by: cafalchio <[email protected]>
Signed-off-by: Madhu Mohan Jaishankar <[email protected]>
Signed-off-by: Jonathan Springer <[email protected]>
Co-authored-by: cafalchio <[email protected]>
Co-authored-by: Jonathan Springer <[email protected]>
msureshkumar88 pushed a commit that referenced this pull request May 13, 2026
* add per-context plugin manager with TenantPluginManagerFactory

Introduce context-scoped plugin isolation via a new TenantPluginManagerFactory
singleton. Each virtual server/tenant receives its own TenantPluginManager with
an independently merged plugin config, while the __global__ manager continues to
serve non-context-aware call sites.

- Add TenantPluginManager (disables Borg state sharing) and
  TenantPluginManagerFactory (async-safe, deduplicating cache/factory)
- Update framework __init__ accessors: init_plugin_manager_factory(),
  get_plugin_manager(server_id), shutdown/reset helpers
- Wire services (tool, prompt, resource, classification) to pass server_id
  when resolving plugin managers
- Update main.py lifespan to initialize factory eagerly at startup
- Add PluginConfigOverride model and shallow config merge logic
- Add architecture doc and 756-line test suite for tenant-scoped plugin execution

Signed-off-by: cafalchio <[email protected]>

* ran pre-commit to update secrets

Signed-off-by: cafalchio <[email protected]>

* Update architecture, linted and secrets

Signed-off-by: cafalchio <[email protected]>

* updated secrets

Signed-off-by: cafalchio <[email protected]>

* audited and scan secrets

Signed-off-by: cafalchio <[email protected]>

* feat: add per-tool plugin binding API with validation and tests

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* test: improve router and service test quality for tool plugin bindings

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* fix: rebase migration onto cbedf4e580e0 to resolve multiple Alembic heads

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* chore: update secrets baseline with migration revision IDs

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* chore: mark migration revision IDs as non-secrets in baseline

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* feat: grant tools.manage_plugins to team_admin; fix service UUID and team guard

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* chore: add migration revision IDs to secrets baseline

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* feat: wire per-tool plugin bindings into tool invocation pipeline

Signed-off-by: Madhu Mohan Jaishankar <[email protected]>

* Added dynamic plugin for rust endpoitns

Signed-off-by: cafalchio <[email protected]>

* Updated secrets and lint

Signed-off-by: cafalchio <[email protected]>

* fixed linter

Signed-off-by: cafalchio <[email protected]>

* test: Fix mocking problem in test_main_sighup

Signed-off-by: Jonathan Springer <[email protected]>

---------

Signed-off-by: cafalchio <[email protected]>
Signed-off-by: Madhu Mohan Jaishankar <[email protected]>
Signed-off-by: Jonathan Springer <[email protected]>
Co-authored-by: cafalchio <[email protected]>
Co-authored-by: Jonathan Springer <[email protected]>
brian-hussey pushed a commit to brian-hussey/mcp-context-forge that referenced this pull request Oct 20, 2026
…C, tests

Review fixes for the OAuth access token verification feature (IBM#3715):

Security:
- Validate issuer URL and discovered jwks_uri against SSRF protections
  before outbound fetches (validate_core_url on both paths)
- Validate discovered metadata issuer matches queried issuer (RFC 8414 §3.3)
- Persist oauth_client_id from admin UI into oauth_config for audience
  validation; log warning when audience check is skipped

Correctness:
- Fall through to verify_credentials on jwt.DecodeError instead of
  early-rejecting (fixes 7 broken existing tests)
- Wrap _get_user_by_email_sync and _resolve_teams_from_db in try/except
  returning 503 on DB failures (parity with _auth_jwt path)
- Distinguish infra failures (503) from auth failures (401) via
  OAUTH_VERIFY_UNAVAILABLE sentinel in verify_oauth_access_token
- Fix internal MCP RPC RBAC to enable check_any_team for
  oauth_access_token (non-admin team-scoped users were denied)
- Add auth_method and set_trace_context_from_teams for observability
- Add TOOLS_MANAGE_PLUGINS to Permissions class (fixes team_admin
  bootstrap failure from IBM#4068, see IBM#4085)
- Fix DAR101 flake8 docstring issue in observability.py

Tests:
- 30+ new unit tests covering _auth_jwt OAuth routing,
  _try_oauth_access_token branches, verify_oauth_access_token edge
  cases, SSRF rejection, issuer mismatch, 503 vs 401 classification,
  DB error handling, and trace context

Signed-off-by: Mihai Criveti <[email protected]>
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.

[FEATURE][PLUGINS]: Phase2 - Plugin-Tool Policy Config CRUD API [EPIC][PLUGINS]: Support for Plugins Multi-tenancy and dynamic loading

3 participants