MCP Gateway: Full Project Overview
- Purpose: One-stop guidance for LLMs to develop, run, test, document, and deploy the MCP Context Forge gateway.
- Tech: FastAPI + Pydantic + SQLAlchemy; optional Redis; Alembic migrations; hybrid plugin framework (native + external MCP).
Core Capabilities
- Unified gateway for MCP servers: tools, prompts, and resources via HTTP/SSE/WebSocket/STDIO/streamable HTTP.
- Plugin framework: AI safety, content filtering, policy enforcement, and transformations with pre/post hooks (42 built-in plugins).
- Federation and administration: register servers, list tools/prompts/resources, manage teams/tokens, admin UI.
- Agent-to-Agent (A2A) communication between MCP agents.
- LLM proxying and chat interface with integrated tools/resources.
- Two-layer security: Token Scoping (data filtering) + RBAC (action authorization).
- Comprehensive observability: OpenTelemetry, Prometheus metrics, structured logging.
Project Structure
- App:
mcpgateway/(FastAPI entrypoints, db.py ORM models, 50+ services, 19 routers, 15 middleware, transports, plugins framework, alembic migrations) - Plugins:
plugins/(42 built-in native plugins and external plugin example) - MCP Servers:
mcp-servers/(5 Go servers, 20 Python servers, scaffolding templates) - Docs:
docs/(MkDocs site + docs Makefile) - Charts:
charts/(Helm chartmcp-stack) - Tests:
tests/{unit,integration,e2e,performance,security,fuzz,playwright} - Infrastructure:
infra/(PostgreSQL, Redis, monitoring Docker Compose) - Deployment:
deployment/(k8s, knative, terraform, ansible)
Environment & Setup
- Requirements: Python 3.11+, GNU Make
- Create venv and install dev deps:
make venvmake install-dev
- Copy env and set secrets:
cp .env.example .env- Set
JWT_SECRET_KEY, optional Redis/DB settings
- Optional helpers:
- JWT:
python -m mcpgateway.utils.create_jwt_token --username admin@example.com --exp 10080 --secret KEY - Expose stdio server through wrapper:
python -m mcpgateway.translate --stdio "uvx mcp-server-git" --port 9000
- JWT:
Run the Gateway
- Dev (reload, :8000):
make dev - Prod (Gunicorn, :4444):
make serve - Prod with TLS (self-signed certs in ./certs):
make certsmake serve-ssl
- CLI entry:
mcpgateway --host 0.0.0.0 --port 4444
Configuration
- Copy
.env.example→.env; verify withmake check-env. - Plugin config path via
PLUGIN_CONFIG_FILE=plugins/config.yaml. - Enable plugin framework:
PLUGINS_ENABLED=truein.env. - Prefer environment variables for security-sensitive settings.
Plugins (Overview)
- Architecture: hybrid model supports native (in‑process) Python plugins and external MCP servers; unified hook interface.
- Hooks (production):
prompt_pre_fetch,prompt_post_fetch,tool_pre_invoke,tool_post_invoke,resource_pre_fetch,resource_post_fetch. - Configuration:
plugins/config.yamlwithplugins,plugin_dirs,plugin_settings. - Modes:
enforce | enforce_ignore_error | permissive | disabled; priority ascending. - Built‑ins: Argument Normalizer, PII filter, regex search/replace, denylist, resource filter; OPA external example.
- Default ordering (lower runs first): Argument Normalizer (40) → PII Filter (50) → Resource Filter (75) → Deny/Regex (100+/150). This ensures inputs are stabilized before detection/redaction.
- Authoring helpers:
- Bootstrap templates:
mcpplugins bootstrap --destination <dir> --type native|external - External runtime default: Streamable HTTP at
http://localhost:8000/mcp
- Bootstrap templates:
- See also:
llms/plugins-llms.md(deep-dive + testing patterns)
API Usage
- Auth: HTTP Bearer (JWT). Generate token and send
Authorization: Bearer <token>. - Core endpoints (auth):
/servers,/tools,/prompts,/resources,/rpc,/sse. - JSON‑RPC 2.0 tool invocation via
POST /rpc. - Health:
/health, Readiness:/ready. - See:
llms/api.mdfor curl examples and parameter details.
Testing & Quality
- Quick runs:
- Unit tests:
make test - Doctest + test:
make doctest test - Coverage (md/HTML/XML/badge/annotated):
make coverage; HTML atdocs/docs/coverage/index.html - HTML coverage only:
make htmlcov
- Unit tests:
- Selective pytest:
pytest -k "fragment",pytest -m "not slow" - Lint & static analysis:
- Format & hooks:
make autoflake isort black pre-commit - Static:
make pylint flake8, full lint:make lint - Security/docs QA (as configured):
make bandit interrogate verify check-manifest
- Format & hooks:
- PR readiness (recommended):
make doctest test htmlcov smoketest lint-web flake8 bandit interrogate pylint verify
- Structure & conventions:
- Tests in
tests/{unit,integration,e2e,playwright}; markslow|ui|api|smoke|e2eto keep defaults fast. - Prefer editing existing test files; target coverage gaps first.
- Tests in
- See:
llms/testing.mdfor a coverage-first workflow and tips.
Documentation (MkDocs)
- Location:
docs/(content indocs/docs/only) - First-time setup:
cd docs && make venv - Live preview:
make serve→ http://127.0.0.1:8000 (hot reload) - Build site:
make build(exports combined HTML/Docx todocs/site/out/) - Deploy (GitHub Pages):
make deploy - Authoring tips: use
.pagesper folder to define nav; images underdocs/docs/images/. - See:
llms/mkdocs.mdfor full details.
Kubernetes (Helm)
- Chart:
charts/mcp-stack(gateway + Postgres + Redis; optional UIs) - Common tasks (from chart dir):
- Lint/validate:
make validate-all - Template/dry-run:
make test-template/make test-dry-run - Install/upgrade:
make install/make upgrade - Package/push:
make package/make push
- Lint/validate:
- Dev overrides via
my-values.yaml. - See:
llms/helm.mdfor workflows and examples.
Security
- Secrets: never commit; set via
.envor KubernetesSecret. - Auth: set
JWT_SECRET_KEY; emit bearer tokens with the token utility for API calls. - TLS: generate with
make certsand runmake serve-ssl. - Plugins: external URLs validated; STDIO scripts must be
.py. Timeouts and payload size guards in plugin executor.
Authentication & RBAC
- Two-layer security model:
- Layer 1 (Token Scoping): Controls what resources users CAN SEE via
teamsJWT claim - Layer 2 (RBAC): Controls what actions users CAN DO via permission checks
- Layer 1 (Token Scoping): Controls what resources users CAN SEE via
- Token scoping quick reference:
- Missing
teamskey → PUBLIC-ONLY (secure default) teams: null+is_admin: true→ ADMIN BYPASS (unrestricted)teams: []→ PUBLIC-ONLY (even for admins)teams: ["team-id"]→ Team + Public resources
- Missing
- Built-in roles:
platform_admin(global, all permissions),team_admin,developer,viewer(team-scoped) - Resource visibility:
public(all users),team(team members),private(owner only) - Key implementation:
normalize_token_teams()inmcpgateway/auth.pyis single source of truth - Full documentation:
docs/docs/manage/rbac.md,docs/docs/architecture/multitenancy.md
Makefile Reference (root)
- Dev/prod:
make dev,make serve,make serve-ssl,make certs - Quality:
make lint,make lint-web,make check-manifest - Tests:
make test,make doctest,make htmlcov,make coverage - Clean:
make clean(caches, build artefacts, venv, coverage, docs, certs)
Contributing
- Conventional Commits; sign off (
git commit -s); link issues. - Include tests/docs for behavior changes. Keep code typed (Py ≥ 3.11), formatted, and lint‑clean.
- Do not mention competitive assistants in PRs; avoid effort estimates.