mcp-key-rotator 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. mcp_key_rotator-0.2.0/.dockerignore +14 -0
  2. mcp_key_rotator-0.2.0/CHANGELOG.md +47 -0
  3. mcp_key_rotator-0.2.0/CONTRIBUTING.md +50 -0
  4. mcp_key_rotator-0.2.0/Dockerfile +16 -0
  5. mcp_key_rotator-0.2.0/LICENSE +21 -0
  6. mcp_key_rotator-0.2.0/MANIFEST.in +6 -0
  7. mcp_key_rotator-0.2.0/PKG-INFO +223 -0
  8. mcp_key_rotator-0.2.0/README.md +188 -0
  9. mcp_key_rotator-0.2.0/SECURITY.md +73 -0
  10. mcp_key_rotator-0.2.0/docs/ADAPTERS.md +70 -0
  11. mcp_key_rotator-0.2.0/docs/AI_AGENT_INSTALL.md +85 -0
  12. mcp_key_rotator-0.2.0/docs/ARCHITECTURE.md +134 -0
  13. mcp_key_rotator-0.2.0/docs/CLIENTS.md +40 -0
  14. mcp_key_rotator-0.2.0/docs/CONFIGURATION.md +114 -0
  15. mcp_key_rotator-0.2.0/docs/DOCKER.md +50 -0
  16. mcp_key_rotator-0.2.0/docs/INSTALL.md +110 -0
  17. mcp_key_rotator-0.2.0/docs/PROVIDERS.md +151 -0
  18. mcp_key_rotator-0.2.0/docs/RELEASING.md +44 -0
  19. mcp_key_rotator-0.2.0/docs/RESEARCH.md +76 -0
  20. mcp_key_rotator-0.2.0/docs/TRANSPORTS.md +77 -0
  21. mcp_key_rotator-0.2.0/examples/generic.yaml +26 -0
  22. mcp_key_rotator-0.2.0/examples/github.yaml +11 -0
  23. mcp_key_rotator-0.2.0/examples/openrouter.yaml +18 -0
  24. mcp_key_rotator-0.2.0/mcp-key-rotator.example.yaml +26 -0
  25. mcp_key_rotator-0.2.0/pyproject.toml +62 -0
  26. mcp_key_rotator-0.2.0/scripts/verify_distributions.py +33 -0
  27. mcp_key_rotator-0.2.0/setup.cfg +4 -0
  28. mcp_key_rotator-0.2.0/src/mcp_key_rotator/__init__.py +7 -0
  29. mcp_key_rotator-0.2.0/src/mcp_key_rotator/__main__.py +3 -0
  30. mcp_key_rotator-0.2.0/src/mcp_key_rotator/adapters/__init__.py +50 -0
  31. mcp_key_rotator-0.2.0/src/mcp_key_rotator/adapters/base.py +161 -0
  32. mcp_key_rotator-0.2.0/src/mcp_key_rotator/adapters/builtin.py +161 -0
  33. mcp_key_rotator-0.2.0/src/mcp_key_rotator/adapters/search.py +175 -0
  34. mcp_key_rotator-0.2.0/src/mcp_key_rotator/adapters/testing.py +65 -0
  35. mcp_key_rotator-0.2.0/src/mcp_key_rotator/cli.py +634 -0
  36. mcp_key_rotator-0.2.0/src/mcp_key_rotator/clients.py +793 -0
  37. mcp_key_rotator-0.2.0/src/mcp_key_rotator/config.py +400 -0
  38. mcp_key_rotator-0.2.0/src/mcp_key_rotator/credentials.py +59 -0
  39. mcp_key_rotator-0.2.0/src/mcp_key_rotator/diagnostics.py +92 -0
  40. mcp_key_rotator-0.2.0/src/mcp_key_rotator/errors.py +25 -0
  41. mcp_key_rotator-0.2.0/src/mcp_key_rotator/http_proxy.py +1210 -0
  42. mcp_key_rotator-0.2.0/src/mcp_key_rotator/http_stdio.py +604 -0
  43. mcp_key_rotator-0.2.0/src/mcp_key_rotator/models.py +134 -0
  44. mcp_key_rotator-0.2.0/src/mcp_key_rotator/processes.py +143 -0
  45. mcp_key_rotator-0.2.0/src/mcp_key_rotator/proxy.py +324 -0
  46. mcp_key_rotator-0.2.0/src/mcp_key_rotator/quota.py +55 -0
  47. mcp_key_rotator-0.2.0/src/mcp_key_rotator/registry.py +67 -0
  48. mcp_key_rotator-0.2.0/src/mcp_key_rotator/security.py +60 -0
  49. mcp_key_rotator-0.2.0/src/mcp_key_rotator/selection.py +31 -0
  50. mcp_key_rotator-0.2.0/src/mcp_key_rotator/setup.py +703 -0
  51. mcp_key_rotator-0.2.0/src/mcp_key_rotator/state.py +474 -0
  52. mcp_key_rotator-0.2.0/src/mcp_key_rotator/telemetry.py +65 -0
  53. mcp_key_rotator-0.2.0/src/mcp_key_rotator/tui.py +445 -0
  54. mcp_key_rotator-0.2.0/src/mcp_key_rotator.egg-info/PKG-INFO +223 -0
  55. mcp_key_rotator-0.2.0/src/mcp_key_rotator.egg-info/SOURCES.txt +76 -0
  56. mcp_key_rotator-0.2.0/src/mcp_key_rotator.egg-info/dependency_links.txt +1 -0
  57. mcp_key_rotator-0.2.0/src/mcp_key_rotator.egg-info/entry_points.txt +3 -0
  58. mcp_key_rotator-0.2.0/src/mcp_key_rotator.egg-info/requires.txt +15 -0
  59. mcp_key_rotator-0.2.0/src/mcp_key_rotator.egg-info/top_level.txt +1 -0
  60. mcp_key_rotator-0.2.0/tests/__init__.py +1 -0
  61. mcp_key_rotator-0.2.0/tests/helpers.py +42 -0
  62. mcp_key_rotator-0.2.0/tests/mock_mcp_server.py +86 -0
  63. mcp_key_rotator-0.2.0/tests/test_adapter_contracts.py +54 -0
  64. mcp_key_rotator-0.2.0/tests/test_adapters.py +178 -0
  65. mcp_key_rotator-0.2.0/tests/test_cli.py +121 -0
  66. mcp_key_rotator-0.2.0/tests/test_cli_v2.py +218 -0
  67. mcp_key_rotator-0.2.0/tests/test_clients.py +417 -0
  68. mcp_key_rotator-0.2.0/tests/test_config.py +143 -0
  69. mcp_key_rotator-0.2.0/tests/test_http_proxy.py +524 -0
  70. mcp_key_rotator-0.2.0/tests/test_http_stdio.py +527 -0
  71. mcp_key_rotator-0.2.0/tests/test_live_adapters.py +38 -0
  72. mcp_key_rotator-0.2.0/tests/test_operations.py +95 -0
  73. mcp_key_rotator-0.2.0/tests/test_proxy.py +228 -0
  74. mcp_key_rotator-0.2.0/tests/test_release.py +43 -0
  75. mcp_key_rotator-0.2.0/tests/test_search_adapters.py +254 -0
  76. mcp_key_rotator-0.2.0/tests/test_selection.py +313 -0
  77. mcp_key_rotator-0.2.0/tests/test_setup.py +369 -0
  78. mcp_key_rotator-0.2.0/tests/test_tui.py +196 -0
@@ -0,0 +1,14 @@
1
+ .git
2
+ .github
3
+ .venv
4
+ __pycache__
5
+ *.py[cod]
6
+ *.db
7
+ *.db-shm
8
+ *.db-wal
9
+ .env
10
+ .env.*
11
+ .mcp-key-rotator
12
+ config.local.yaml
13
+ keys.yaml
14
+ tests
@@ -0,0 +1,47 @@
1
+ # Changelog
2
+
3
+ ## 0.2.0 - 2026-12-03
4
+
5
+ Guided installation and native HTTP transport release.
6
+
7
+ - Added the `mkr` dashboard, Textual setup wizard, offline doctor, redacted status/config views,
8
+ key administration, client management, and explicit uninstall workflow.
9
+ - Added deterministic `mkr setup --agent --apply --json` with structured missing-input results,
10
+ stable exit codes, environment/upstream/client discovery, previews, timestamped backups,
11
+ atomic writes, idempotence, and per-invocation rollback.
12
+ - Added optional OS credential-vault references and hidden interactive onboarding. Credentials
13
+ remain outside generated YAML, client JSON, telemetry, logs, and AI-agent output.
14
+ - Simplified to configuration schema v2 with built-in provider defaults, generated key names,
15
+ platform paths, persistent round-robin defaults, and actionable legacy-file migration.
16
+ - Added native setup support for OpenCode, Claude Code, Claude Desktop, Codex CLI, Cursor,
17
+ Gemini CLI, and generic JSON clients while preserving unrelated settings.
18
+ - Added pinned-key Streamable HTTP and explicitly negotiated legacy HTTP+SSE upstreams, a
19
+ stdio-to-HTTP connector, and an authenticated loopback Streamable HTTP ASGI listener with
20
+ isolated sessions and translated session identifiers.
21
+ - Added conservative Brave Search and Exa adapters, plus a versioned adapter plugin SDK and
22
+ offline contract-test kit.
23
+ - Added opt-in POSIX process-group/Windows job cleanup and allowlisted local JSON telemetry.
24
+ No request replay, cross-key retry, automatic reconnect, or local event-cache resumption was
25
+ introduced.
26
+ - Expanded offline tests and CI across Windows, macOS, Linux, and Python 3.11–3.14, including
27
+ exact-wheel uv/pipx installation and Docker execution.
28
+ - Added build provenance, SBOM generation, GitHub Releases, and PyPI Trusted Publishing.
29
+
30
+ Configuration v2 is intentionally incompatible with v0.1 YAML. Setup backs up a legacy file
31
+ before replacing it and preserves existing SQLite health history.
32
+
33
+ ## 0.1.0 - 2026-12-03
34
+
35
+ Initial public MVP.
36
+
37
+ - Environment-only credentials with random or persistent round-robin selection.
38
+ - Per-stdio-session subprocess and credential pinning.
39
+ - SQLite health, selection, usage counters, cooldowns, monthly windows, and manual reset.
40
+ - Conservative generic classification and GitHub, OpenRouter, and Tavily adapters.
41
+ - On-demand GitHub, OpenRouter, and Tavily quota/rate-limit probes.
42
+ - Redacted diagnostics and minimal child environments.
43
+ - Arbitrary stdio MCP server configuration and provider examples.
44
+ - Offline unit/integration/concurrency tests, cross-platform CI, and optional Docker image.
45
+
46
+ Native Streamable HTTP/SSE proxying and automatic in-flight request retry are intentionally
47
+ not included.
@@ -0,0 +1,50 @@
1
+ # Contributing
2
+
3
+ ## Local setup
4
+
5
+ Python 3.11+ is required.
6
+
7
+ ```bash
8
+ python -m venv .venv
9
+ source .venv/bin/activate # Windows: .venv\Scripts\activate
10
+ python -m pip install -e ".[dev]"
11
+ ruff check .
12
+ python -m unittest discover -v
13
+ ```
14
+
15
+ Tests must use fake credentials and local mocks by default. Do not add paid provider calls or
16
+ live credentials to the normal suite. Any future opt-in provider contract test must be skipped
17
+ unless explicitly enabled and should use read-only endpoints where possible.
18
+
19
+ ## Design constraints
20
+
21
+ - Keep provider rules in adapters, not selection/state/proxy code.
22
+ - Preserve one pinned credential for the entire stdio process or upstream HTTP session.
23
+ - Never automatically replay a paid or non-idempotent operation on another key.
24
+ - Do not classify all 429 responses as monthly exhaustion.
25
+ - Do not scan successful tool output for quota errors.
26
+ - Preserve newline-delimited JSON-RPC and forward MCP traffic unchanged.
27
+ - Use short SQLite transactions and avoid storing secret values or raw payloads.
28
+ - Add tests for every new failure class, reset rule, concurrency path, or redaction behavior.
29
+ - Keep error messages free of credentials and raw provider response bodies.
30
+
31
+ ## Provider adapters
32
+
33
+ Built-ins live in `src/mcp_key_rotator/adapters/builtin.py` and `search.py`. External packages
34
+ can register a `ProviderAdapter` subclass through the `mcp_key_rotator.adapters` entry-point
35
+ group and should run the offline mixin in `mcp_key_rotator.adapters.testing`:
36
+
37
+ ```toml
38
+ [project.entry-points."mcp_key_rotator.adapters"]
39
+ acme = "acme_mcp_adapter:AcmeAdapter"
40
+ ```
41
+
42
+ Set `adapter: acme` in YAML. A new built-in should include official documentation links,
43
+ mocked response fixtures, classification tests for non-quota 429s, and safe behavior when
44
+ fields are absent or malformed.
45
+
46
+ ## Commits and pull requests
47
+
48
+ Keep changes focused and commits small. Update documentation and examples with behavior
49
+ changes. Run Ruff and the full test suite before submitting. Explain any manually verified
50
+ integration that automated tests cannot cover, and identify residual limitations plainly.
@@ -0,0 +1,16 @@
1
+ FROM python:3.13-slim
2
+
3
+ ENV PYTHONDONTWRITEBYTECODE=1 \
4
+ PYTHONUNBUFFERED=1
5
+
6
+ WORKDIR /opt/mcp-key-rotator
7
+ COPY pyproject.toml README.md LICENSE ./
8
+ COPY src ./src
9
+ RUN python -m pip install --no-cache-dir .
10
+
11
+ RUN useradd --create-home --uid 10001 rotator
12
+ USER rotator
13
+ WORKDIR /home/rotator
14
+
15
+ ENTRYPOINT ["mcp-key-rotator"]
16
+ CMD ["--help"]
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 MCP Key Rotator contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,6 @@
1
+ include LICENSE README.md CHANGELOG.md CONTRIBUTING.md SECURITY.md
2
+ include mcp-key-rotator.example.yaml Dockerfile .dockerignore
3
+ recursive-include docs *.md
4
+ recursive-include examples *.yaml
5
+ recursive-include scripts *.py
6
+ recursive-include tests *.py
@@ -0,0 +1,223 @@
1
+ Metadata-Version: 2.4
2
+ Name: mcp-key-rotator
3
+ Version: 0.2.0
4
+ Summary: Guided provider-agnostic API-key rotation for stdio and HTTP MCP servers
5
+ Author: MCP Key Rotator contributors
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/pawandeep006-rgb/mcp-key-rotator
8
+ Project-URL: Repository, https://github.com/pawandeep006-rgb/mcp-key-rotator
9
+ Project-URL: Issues, https://github.com/pawandeep006-rgb/mcp-key-rotator/issues
10
+ Keywords: mcp,api-keys,rotation,quota,proxy
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Environment :: Console
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Programming Language :: Python :: 3.14
18
+ Classifier: Topic :: Security
19
+ Requires-Python: >=3.11
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Requires-Dist: PyYAML<7,>=6.0
23
+ Requires-Dist: httpx<1,>=0.28
24
+ Requires-Dist: keyring<26,>=25
25
+ Requires-Dist: platformdirs<5,>=4
26
+ Requires-Dist: starlette<2,>=1.3.1
27
+ Requires-Dist: textual<9,>=3
28
+ Requires-Dist: tomli-w<2,>=1.2
29
+ Requires-Dist: uvicorn<1,>=0.35
30
+ Requires-Dist: tzdata>=2024.1; platform_system == "Windows"
31
+ Provides-Extra: dev
32
+ Requires-Dist: build<2,>=1.2; extra == "dev"
33
+ Requires-Dist: ruff<1,>=0.8; extra == "dev"
34
+ Dynamic: license-file
35
+
36
+ # MCP Key Rotator
37
+
38
+ [![CI](https://github.com/pawandeep006-rgb/mcp-key-rotator/actions/workflows/ci.yml/badge.svg)](https://github.com/pawandeep006-rgb/mcp-key-rotator/actions/workflows/ci.yml)
39
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
40
+ [![Python 3.11+](https://img.shields.io/badge/python-3.11%2B-blue.svg)](pyproject.toml)
41
+
42
+ Provider-agnostic API-key rotation and quota management for **stdio, Streamable HTTP, and
43
+ legacy HTTP+SSE MCP servers**. One local session receives one credential and keeps it for that
44
+ session's lifetime—no mid-session account changes and no automatic replay of tool calls.
45
+
46
+ **v0.2.0** adds guided setup, AI-agent automation, an interactive dashboard, OS credential
47
+ vault support, native HTTP transports, six client integrations, optional process-tree cleanup,
48
+ and local structured telemetry.
49
+
50
+ ## Install in one prompt
51
+
52
+ Give an AI coding agent the prompt in **[docs/AI_AGENT_INSTALL.md](docs/AI_AGENT_INSTALL.md)**.
53
+ It installs the released package, detects supported clients and credential variable names,
54
+ reuses existing upstream MCP servers, applies safe changes with backups, and runs offline
55
+ diagnostics. It never needs credential values in chat.
56
+
57
+ ## Install manually
58
+
59
+ ```bash
60
+ # Install uv first if needed: https://docs.astral.sh/uv/getting-started/installation/
61
+ uv tool install mcp-key-rotator
62
+ mkr setup
63
+ ```
64
+
65
+ `uv` manages Python automatically. `pipx install mcp-key-rotator` and
66
+ `python -m pip install mcp-key-rotator` are supported fallbacks. The guided wizard works on
67
+ Windows, macOS, and Linux and does not require YAML editing.
68
+
69
+ See the **[illustrated installation guide](docs/INSTALL.md)** for platform commands, credential
70
+ vault setup, upgrades, and uninstall.
71
+
72
+ ## Everyday commands
73
+
74
+ | Command | Purpose |
75
+ | --- | --- |
76
+ | `mkr` | Open the keyboard-accessible key-health dashboard. |
77
+ | `mkr setup` | Guided setup with preview, backups, validation, and rollback. |
78
+ | `mkr setup --agent --apply --json` | Noninteractive AI-agent setup; no secret prompts. |
79
+ | `mkr doctor [--json]` | Offline diagnostics and plain-language repair guidance. |
80
+ | `mkr status [--json]` | Redacted key health, eligibility, counters, cooldowns, and resets. |
81
+ | `mkr keys ...` | List, add, reset, disable, enable, or reclassify keys. |
82
+ | `mkr clients ...` | Detect, add, remove, or generate generic client configuration. |
83
+ | `mkr config ...` | Locate, validate, or display redacted configuration. |
84
+ | `mkr check PROVIDER` | Explicit read-only quota probe where supported; never automatic. |
85
+ | `mkr run PROVIDER` | Session-pinned stdio connector used by client registrations. |
86
+ | `mkr serve PROVIDER` | Advanced authenticated loopback Streamable HTTP endpoint. |
87
+ | `mkr uninstall --yes` | Remove managed client entries; data removal is separately explicit. |
88
+
89
+ ## Architecture
90
+
91
+ ### Stdio upstream
92
+
93
+ ```text
94
+ MCP client ── newline-delimited JSON-RPC ──► mkr run PROVIDER
95
+ │ select once per client session
96
+ │ inject only that key into a minimal environment
97
+ ▼
98
+ existing MCP server subprocess
99
+ ```
100
+
101
+ Traffic is forwarded unchanged. Only JSON-RPC errors, failed MCP tool results, and optionally
102
+ stderr are inspected for health. Successful tool content is not scanned.
103
+
104
+ ### HTTP upstream
105
+
106
+ `mkr run` connects stdio clients to Streamable HTTP or legacy HTTP+SSE upstreams. `mkr serve`
107
+ exposes a Streamable HTTP upstream through an authenticated loopback Streamable HTTP listener.
108
+ Concurrent local sessions have independent upstream sessions, pinned keys, and translated
109
+ `MCP-Session-Id` values. Protocol versions and `Last-Event-ID` are forwarded.
110
+
111
+ The listener requires `MKR_HTTP_TOKEN` (or `--auth-env NAME`), rejects non-loopback clients,
112
+ and validates Host and Origin headers. Prefer stdio when a client cannot securely configure
113
+ HTTP authorization. See [TRANSPORTS.md](docs/TRANSPORTS.md).
114
+
115
+ ## Safe defaults
116
+
117
+ - Environment auto-detection or optional OS credential-vault storage; no secrets in generated
118
+ YAML, client JSON, logs, backups, telemetry, or AI chats.
119
+ - Persistent round-robin selection; random selection remains an advanced option.
120
+ - SQLite WAL, short `BEGIN IMMEDIATE` transactions, migrations, lock timeouts, and crash-safe
121
+ state transitions in the platform application-data directory.
122
+ - Conservative distinction between resettable quota exhaustion, temporary rate limits, invalid
123
+ authentication, billing/credit failures, transient failures, and unknown tool errors.
124
+ - Provider reset timestamps, lazy derived resets, manual recovery, custom regex patterns, and
125
+ optional read-only provider quota probes.
126
+ - Built-in GitHub, OpenRouter, Tavily, Brave Search, and Exa adapters plus a versioned plugin
127
+ SDK and [offline contract-test kit](docs/ADAPTERS.md).
128
+ - Timestamped backups, atomic client/YAML writes, concurrent-edit refusal, idempotent setup,
129
+ and complete per-invocation rollback.
130
+
131
+ Keys need **independent quota pools** for rotation to increase capacity. Multiple keys on one
132
+ account may share credits or rate limits.
133
+
134
+ ## Configuration v2
135
+
136
+ A generated configuration can be as small as:
137
+
138
+ ```yaml
139
+ version: 2
140
+ providers:
141
+ github:
142
+ keys:
143
+ - env: GITHUB_TOKEN_1
144
+ - keyring: github/secondary
145
+ server:
146
+ command: github-mcp-server
147
+ args: [stdio]
148
+ ```
149
+
150
+ The adapter, credential injection name, strategy, state path, and reset policy have safe
151
+ provider defaults. Generic providers specify `credential_env`; advanced stdio environment,
152
+ process cleanup, HTTP headers, and quota patterns remain available when genuinely needed.
153
+ See [CONFIGURATION.md](docs/CONFIGURATION.md) and [examples/](examples/).
154
+
155
+ Configuration v2 is an intentional clean break from v0.1. Setup backs up legacy files before
156
+ replacing them and never deletes existing SQLite health history. The v0.1 MVP remains available
157
+ at tag `v0.1.0`.
158
+
159
+ ## Client compatibility
160
+
161
+ Automatic configuration is provided for **OpenCode, Claude Code, Claude Desktop, Codex CLI,
162
+ Cursor, and Gemini CLI**. A generic JSON generator supports other clients. See the
163
+ [compatibility matrix](docs/CLIENTS.md) for native paths, formats, and desktop validation
164
+ boundaries.
165
+
166
+ ## Health semantics
167
+
168
+ | Observation | Stored effect | Future sessions |
169
+ | --- | --- | --- |
170
+ | Resettable spending/monthly quota exhausted | `exhausted` with reset timestamp | Excluded until reset or manual recovery |
171
+ | HTTP 429 / documented temporary limit | Active with cooldown | Temporarily skipped, **not** monthly exhausted |
172
+ | Invalid authentication | `invalid` | Excluded until recovery/replacement |
173
+ | Credit, billing, or nonresetting budget failure | `billing` | Excluded pending billing action |
174
+ | Transient provider failure | Active with short cooldown | Temporarily skipped |
175
+ | Unknown protocol/tool error | No provider-specific transition | Forwarded unchanged |
176
+
177
+ A failure never switches the current session's key. `mkr check` is on demand because provider
178
+ read endpoints can themselves be rate limited. Usage counters count observed MCP tool outcomes,
179
+ not provider tokens or credits.
180
+
181
+ ## Telemetry and process cleanup
182
+
183
+ Set `MKR_TELEMETRY=true` and `MKR_LOG_PATH=/private/path/events.jsonl` to opt into local JSON
184
+ operational events. Only allowlisted session lifecycle, key name, classification, return code,
185
+ duration, transport, and termination reason fields are accepted. There is no hosted metrics
186
+ service or mandatory daemon.
187
+
188
+ Set `server.cleanup_tree: true` after compatibility testing to place stdio children in a POSIX
189
+ process group or Windows job/tree. The default remains direct-child supervision.
190
+
191
+ ## Limitations
192
+
193
+ - Stdio uses newline-delimited JSON-RPC, as required by the supported MCP stdio transport.
194
+ - No automatic request replay, reconnect, or cross-key retry: a disconnected or failed tool
195
+ call may already have executed or incurred cost.
196
+ - HTTP resumption delegates event replay to the upstream server; the proxy does not maintain
197
+ its own event cache.
198
+ - `mkr serve` currently requires an HTTP upstream; use `mkr run` for stdio upstreams.
199
+ - OAuth-only remote MCP authorization is not converted into static API-key rotation.
200
+ - Error detection depends on upstream servers preserving useful error details.
201
+ - Desktop clients require a restart and some cannot be launched automatically in CI.
202
+
203
+ ## Development and verification
204
+
205
+ ```bash
206
+ python -m pip install -e ".[dev]"
207
+ ruff check .
208
+ python -m unittest discover -v
209
+ python -m build
210
+ ```
211
+
212
+ Tests use local mocks and subprocesses only; normal CI makes no paid provider calls. The
213
+ release pipeline covers Windows/macOS/Linux, Python 3.11–3.14, exact-wheel uv/pipx installs,
214
+ secret and dependency scans, package/Docker execution, SBOMs, and provenance attestations.
215
+
216
+ See [CONTRIBUTING.md](CONTRIBUTING.md), [ARCHITECTURE.md](docs/ARCHITECTURE.md),
217
+ [PROVIDERS.md](docs/PROVIDERS.md), [SECURITY.md](SECURITY.md), [RELEASING.md](docs/RELEASING.md),
218
+ and [CHANGELOG.md](CHANGELOG.md). An [optional Docker image](docs/DOCKER.md) is included; host
219
+ installation is generally simpler when wrapping other local runtimes.
220
+
221
+ ## License
222
+
223
+ [MIT](LICENSE)
@@ -0,0 +1,188 @@
1
+ # MCP Key Rotator
2
+
3
+ [![CI](https://github.com/pawandeep006-rgb/mcp-key-rotator/actions/workflows/ci.yml/badge.svg)](https://github.com/pawandeep006-rgb/mcp-key-rotator/actions/workflows/ci.yml)
4
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
5
+ [![Python 3.11+](https://img.shields.io/badge/python-3.11%2B-blue.svg)](pyproject.toml)
6
+
7
+ Provider-agnostic API-key rotation and quota management for **stdio, Streamable HTTP, and
8
+ legacy HTTP+SSE MCP servers**. One local session receives one credential and keeps it for that
9
+ session's lifetime—no mid-session account changes and no automatic replay of tool calls.
10
+
11
+ **v0.2.0** adds guided setup, AI-agent automation, an interactive dashboard, OS credential
12
+ vault support, native HTTP transports, six client integrations, optional process-tree cleanup,
13
+ and local structured telemetry.
14
+
15
+ ## Install in one prompt
16
+
17
+ Give an AI coding agent the prompt in **[docs/AI_AGENT_INSTALL.md](docs/AI_AGENT_INSTALL.md)**.
18
+ It installs the released package, detects supported clients and credential variable names,
19
+ reuses existing upstream MCP servers, applies safe changes with backups, and runs offline
20
+ diagnostics. It never needs credential values in chat.
21
+
22
+ ## Install manually
23
+
24
+ ```bash
25
+ # Install uv first if needed: https://docs.astral.sh/uv/getting-started/installation/
26
+ uv tool install mcp-key-rotator
27
+ mkr setup
28
+ ```
29
+
30
+ `uv` manages Python automatically. `pipx install mcp-key-rotator` and
31
+ `python -m pip install mcp-key-rotator` are supported fallbacks. The guided wizard works on
32
+ Windows, macOS, and Linux and does not require YAML editing.
33
+
34
+ See the **[illustrated installation guide](docs/INSTALL.md)** for platform commands, credential
35
+ vault setup, upgrades, and uninstall.
36
+
37
+ ## Everyday commands
38
+
39
+ | Command | Purpose |
40
+ | --- | --- |
41
+ | `mkr` | Open the keyboard-accessible key-health dashboard. |
42
+ | `mkr setup` | Guided setup with preview, backups, validation, and rollback. |
43
+ | `mkr setup --agent --apply --json` | Noninteractive AI-agent setup; no secret prompts. |
44
+ | `mkr doctor [--json]` | Offline diagnostics and plain-language repair guidance. |
45
+ | `mkr status [--json]` | Redacted key health, eligibility, counters, cooldowns, and resets. |
46
+ | `mkr keys ...` | List, add, reset, disable, enable, or reclassify keys. |
47
+ | `mkr clients ...` | Detect, add, remove, or generate generic client configuration. |
48
+ | `mkr config ...` | Locate, validate, or display redacted configuration. |
49
+ | `mkr check PROVIDER` | Explicit read-only quota probe where supported; never automatic. |
50
+ | `mkr run PROVIDER` | Session-pinned stdio connector used by client registrations. |
51
+ | `mkr serve PROVIDER` | Advanced authenticated loopback Streamable HTTP endpoint. |
52
+ | `mkr uninstall --yes` | Remove managed client entries; data removal is separately explicit. |
53
+
54
+ ## Architecture
55
+
56
+ ### Stdio upstream
57
+
58
+ ```text
59
+ MCP client ── newline-delimited JSON-RPC ──► mkr run PROVIDER
60
+ │ select once per client session
61
+ │ inject only that key into a minimal environment
62
+ ▼
63
+ existing MCP server subprocess
64
+ ```
65
+
66
+ Traffic is forwarded unchanged. Only JSON-RPC errors, failed MCP tool results, and optionally
67
+ stderr are inspected for health. Successful tool content is not scanned.
68
+
69
+ ### HTTP upstream
70
+
71
+ `mkr run` connects stdio clients to Streamable HTTP or legacy HTTP+SSE upstreams. `mkr serve`
72
+ exposes a Streamable HTTP upstream through an authenticated loopback Streamable HTTP listener.
73
+ Concurrent local sessions have independent upstream sessions, pinned keys, and translated
74
+ `MCP-Session-Id` values. Protocol versions and `Last-Event-ID` are forwarded.
75
+
76
+ The listener requires `MKR_HTTP_TOKEN` (or `--auth-env NAME`), rejects non-loopback clients,
77
+ and validates Host and Origin headers. Prefer stdio when a client cannot securely configure
78
+ HTTP authorization. See [TRANSPORTS.md](docs/TRANSPORTS.md).
79
+
80
+ ## Safe defaults
81
+
82
+ - Environment auto-detection or optional OS credential-vault storage; no secrets in generated
83
+ YAML, client JSON, logs, backups, telemetry, or AI chats.
84
+ - Persistent round-robin selection; random selection remains an advanced option.
85
+ - SQLite WAL, short `BEGIN IMMEDIATE` transactions, migrations, lock timeouts, and crash-safe
86
+ state transitions in the platform application-data directory.
87
+ - Conservative distinction between resettable quota exhaustion, temporary rate limits, invalid
88
+ authentication, billing/credit failures, transient failures, and unknown tool errors.
89
+ - Provider reset timestamps, lazy derived resets, manual recovery, custom regex patterns, and
90
+ optional read-only provider quota probes.
91
+ - Built-in GitHub, OpenRouter, Tavily, Brave Search, and Exa adapters plus a versioned plugin
92
+ SDK and [offline contract-test kit](docs/ADAPTERS.md).
93
+ - Timestamped backups, atomic client/YAML writes, concurrent-edit refusal, idempotent setup,
94
+ and complete per-invocation rollback.
95
+
96
+ Keys need **independent quota pools** for rotation to increase capacity. Multiple keys on one
97
+ account may share credits or rate limits.
98
+
99
+ ## Configuration v2
100
+
101
+ A generated configuration can be as small as:
102
+
103
+ ```yaml
104
+ version: 2
105
+ providers:
106
+ github:
107
+ keys:
108
+ - env: GITHUB_TOKEN_1
109
+ - keyring: github/secondary
110
+ server:
111
+ command: github-mcp-server
112
+ args: [stdio]
113
+ ```
114
+
115
+ The adapter, credential injection name, strategy, state path, and reset policy have safe
116
+ provider defaults. Generic providers specify `credential_env`; advanced stdio environment,
117
+ process cleanup, HTTP headers, and quota patterns remain available when genuinely needed.
118
+ See [CONFIGURATION.md](docs/CONFIGURATION.md) and [examples/](examples/).
119
+
120
+ Configuration v2 is an intentional clean break from v0.1. Setup backs up legacy files before
121
+ replacing them and never deletes existing SQLite health history. The v0.1 MVP remains available
122
+ at tag `v0.1.0`.
123
+
124
+ ## Client compatibility
125
+
126
+ Automatic configuration is provided for **OpenCode, Claude Code, Claude Desktop, Codex CLI,
127
+ Cursor, and Gemini CLI**. A generic JSON generator supports other clients. See the
128
+ [compatibility matrix](docs/CLIENTS.md) for native paths, formats, and desktop validation
129
+ boundaries.
130
+
131
+ ## Health semantics
132
+
133
+ | Observation | Stored effect | Future sessions |
134
+ | --- | --- | --- |
135
+ | Resettable spending/monthly quota exhausted | `exhausted` with reset timestamp | Excluded until reset or manual recovery |
136
+ | HTTP 429 / documented temporary limit | Active with cooldown | Temporarily skipped, **not** monthly exhausted |
137
+ | Invalid authentication | `invalid` | Excluded until recovery/replacement |
138
+ | Credit, billing, or nonresetting budget failure | `billing` | Excluded pending billing action |
139
+ | Transient provider failure | Active with short cooldown | Temporarily skipped |
140
+ | Unknown protocol/tool error | No provider-specific transition | Forwarded unchanged |
141
+
142
+ A failure never switches the current session's key. `mkr check` is on demand because provider
143
+ read endpoints can themselves be rate limited. Usage counters count observed MCP tool outcomes,
144
+ not provider tokens or credits.
145
+
146
+ ## Telemetry and process cleanup
147
+
148
+ Set `MKR_TELEMETRY=true` and `MKR_LOG_PATH=/private/path/events.jsonl` to opt into local JSON
149
+ operational events. Only allowlisted session lifecycle, key name, classification, return code,
150
+ duration, transport, and termination reason fields are accepted. There is no hosted metrics
151
+ service or mandatory daemon.
152
+
153
+ Set `server.cleanup_tree: true` after compatibility testing to place stdio children in a POSIX
154
+ process group or Windows job/tree. The default remains direct-child supervision.
155
+
156
+ ## Limitations
157
+
158
+ - Stdio uses newline-delimited JSON-RPC, as required by the supported MCP stdio transport.
159
+ - No automatic request replay, reconnect, or cross-key retry: a disconnected or failed tool
160
+ call may already have executed or incurred cost.
161
+ - HTTP resumption delegates event replay to the upstream server; the proxy does not maintain
162
+ its own event cache.
163
+ - `mkr serve` currently requires an HTTP upstream; use `mkr run` for stdio upstreams.
164
+ - OAuth-only remote MCP authorization is not converted into static API-key rotation.
165
+ - Error detection depends on upstream servers preserving useful error details.
166
+ - Desktop clients require a restart and some cannot be launched automatically in CI.
167
+
168
+ ## Development and verification
169
+
170
+ ```bash
171
+ python -m pip install -e ".[dev]"
172
+ ruff check .
173
+ python -m unittest discover -v
174
+ python -m build
175
+ ```
176
+
177
+ Tests use local mocks and subprocesses only; normal CI makes no paid provider calls. The
178
+ release pipeline covers Windows/macOS/Linux, Python 3.11–3.14, exact-wheel uv/pipx installs,
179
+ secret and dependency scans, package/Docker execution, SBOMs, and provenance attestations.
180
+
181
+ See [CONTRIBUTING.md](CONTRIBUTING.md), [ARCHITECTURE.md](docs/ARCHITECTURE.md),
182
+ [PROVIDERS.md](docs/PROVIDERS.md), [SECURITY.md](SECURITY.md), [RELEASING.md](docs/RELEASING.md),
183
+ and [CHANGELOG.md](CHANGELOG.md). An [optional Docker image](docs/DOCKER.md) is included; host
184
+ installation is generally simpler when wrapping other local runtimes.
185
+
186
+ ## License
187
+
188
+ [MIT](LICENSE)
@@ -0,0 +1,73 @@
1
+ # Security policy
2
+
3
+ ## Supported versions
4
+
5
+ Security fixes are applied to the latest 0.2.x release and `main`. Version 0.1.0 is retained as
6
+ the original MVP tag but does not have a long-term support branch.
7
+
8
+ ## Reporting a vulnerability
9
+
10
+ Do not open a public issue containing credentials, private request/response data, or an
11
+ unpatched exploit. Use GitHub's **Private vulnerability reporting** for
12
+ `pawandeep006-rgb/mcp-key-rotator` when available. Otherwise open a minimal public issue that
13
+ requests a private contact without including sensitive details.
14
+
15
+ Include the affected version, platform, reproduction using fake credentials, impact, and a
16
+ suggested fix if known. Never attach a real API key or state dump containing private metadata.
17
+
18
+ ## Credential handling rules
19
+
20
+ - Configuration accepts environment variable names or OS credential-vault references, never
21
+ literal key values. Do not put credentials in URLs, headers maps, command arguments, YAML,
22
+ client JSON/TOML/JSONC, issue reports, or AI chats.
23
+ - Interactive vault input is hidden. Agent mode never prompts for or prints credential values.
24
+ Preview and cancellation do not modify the vault; apply snapshots prior entries and restores
25
+ them, or removes newly created entries, if setup rolls back.
26
+ - Never commit `.env`, private local YAML, SQLite state, debug environment dumps, provider
27
+ responses containing authorization headers, or private telemetry files.
28
+ - Use least-privilege keys from independent quota pools and revoke a suspected leak at the
29
+ provider immediately. Rotating a leaked key in MCP Key Rotator is not a substitute for
30
+ revocation.
31
+ - Keep state, backups, configuration, keyring databases, and telemetry readable only by the
32
+ owning user. Timestamped backups preserve the original file contents and permissions; manage
33
+ their retention according to the sensitivity of the original files.
34
+ - Client dry-run diffs are semantic previews of generated launch fields with environment values
35
+ redacted; existing entry values are not rendered. `mkr config show` also redacts environment
36
+ values, arguments, and URLs. Review paths and generated settings before sharing output.
37
+
38
+ ## Process and network boundaries
39
+
40
+ - Stdio children receive a minimal environment and one selected credential. Explicitly inherited
41
+ variables and literal `server.env` values are administrator-controlled; never place secrets
42
+ in literal settings.
43
+ - `cleanup_tree` is opt-in because process-group/job behavior can affect descendants launched by
44
+ an upstream server. Test it with each server before enabling.
45
+ - Upstream HTTP URLs must use HTTPS except loopback development, cannot include user information,
46
+ fragments, or credential-like query parameters, and redirects are not followed by legacy
47
+ endpoint negotiation. Credential-like command arguments are rejected; discovery refuses to
48
+ migrate them into YAML.
49
+ - Legacy HTTP+SSE POST endpoints must remain on the original scheme, host, and effective port.
50
+ This prevents endpoint events from redirecting credentials to another origin.
51
+ - `mkr serve` binds only to loopback, requires a separate bearer token, checks client/Host/Origin,
52
+ limits request bodies, and disables access logs. It is not an Internet-facing gateway. Do not
53
+ expose it through a tunnel or reverse proxy without an independent security review.
54
+ - TLS certificate verification uses `httpx` defaults. Do not disable it.
55
+
56
+ ## Operational safety
57
+
58
+ There is no automatic request replay, cross-key retry, or stream reconnect. This avoids repeating
59
+ non-idempotent tools or incurring duplicate cost after an ambiguous failure. The proxy does not
60
+ implement OAuth and must not be used to bypass a server's authorization or quota policy.
61
+
62
+ Telemetry is local and opt-in. It allowlists operational fields and drops request/response
63
+ bodies, headers, successful tool content, and arbitrary metadata. Diagnostic provider checks are
64
+ explicit and read-only where supported; ordinary setup, doctor, tests, and CI make no provider
65
+ calls.
66
+
67
+ ## Release integrity
68
+
69
+ CI includes secret scanning, dependency auditing, package execution, cross-platform installation,
70
+ and Docker execution. Tagged releases build distributions and an SBOM, create GitHub build
71
+ provenance attestations, and publish to PyPI through Trusted Publishing rather than a stored
72
+ PyPI password. Verify the tag, release assets, and provenance when installing in a high-assurance
73
+ environment.