mcp-harness 0.1.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 (56) hide show
  1. mcp_harness-0.1.0/.gitignore +28 -0
  2. mcp_harness-0.1.0/CHANGELOG.md +39 -0
  3. mcp_harness-0.1.0/LICENSE +201 -0
  4. mcp_harness-0.1.0/PKG-INFO +222 -0
  5. mcp_harness-0.1.0/README.md +157 -0
  6. mcp_harness-0.1.0/examples/azure_governed_server.py +59 -0
  7. mcp_harness-0.1.0/examples/local_governed_server.py +121 -0
  8. mcp_harness-0.1.0/examples/policies/tool-access.yaml +18 -0
  9. mcp_harness-0.1.0/examples/quickstart_server.py +58 -0
  10. mcp_harness-0.1.0/pyproject.toml +125 -0
  11. mcp_harness-0.1.0/src/mcp_harness/__init__.py +56 -0
  12. mcp_harness-0.1.0/src/mcp_harness/auth/__init__.py +16 -0
  13. mcp_harness-0.1.0/src/mcp_harness/auth/anonymous.py +36 -0
  14. mcp_harness-0.1.0/src/mcp_harness/auth/api_key.py +93 -0
  15. mcp_harness-0.1.0/src/mcp_harness/auth/azure_ad.py +132 -0
  16. mcp_harness-0.1.0/src/mcp_harness/auth/base.py +68 -0
  17. mcp_harness-0.1.0/src/mcp_harness/auth/chained.py +36 -0
  18. mcp_harness-0.1.0/src/mcp_harness/cli.py +297 -0
  19. mcp_harness-0.1.0/src/mcp_harness/core/__init__.py +20 -0
  20. mcp_harness-0.1.0/src/mcp_harness/core/context.py +98 -0
  21. mcp_harness-0.1.0/src/mcp_harness/core/harness.py +299 -0
  22. mcp_harness-0.1.0/src/mcp_harness/core/middleware.py +130 -0
  23. mcp_harness-0.1.0/src/mcp_harness/core/principal.py +73 -0
  24. mcp_harness-0.1.0/src/mcp_harness/errors.py +72 -0
  25. mcp_harness-0.1.0/src/mcp_harness/governance/__init__.py +43 -0
  26. mcp_harness-0.1.0/src/mcp_harness/governance/audit.py +139 -0
  27. mcp_harness-0.1.0/src/mcp_harness/governance/cost_tracking.py +253 -0
  28. mcp_harness-0.1.0/src/mcp_harness/governance/quotas.py +203 -0
  29. mcp_harness-0.1.0/src/mcp_harness/governance/sinks.py +123 -0
  30. mcp_harness-0.1.0/src/mcp_harness/observability/__init__.py +13 -0
  31. mcp_harness-0.1.0/src/mcp_harness/observability/logging.py +100 -0
  32. mcp_harness-0.1.0/src/mcp_harness/observability/metrics.py +128 -0
  33. mcp_harness-0.1.0/src/mcp_harness/observability/tracing.py +88 -0
  34. mcp_harness-0.1.0/src/mcp_harness/policy/__init__.py +13 -0
  35. mcp_harness-0.1.0/src/mcp_harness/policy/allow_list.py +151 -0
  36. mcp_harness-0.1.0/src/mcp_harness/policy/redaction.py +97 -0
  37. mcp_harness-0.1.0/src/mcp_harness/py.typed +0 -0
  38. mcp_harness-0.1.0/src/mcp_harness/resilience/__init__.py +6 -0
  39. mcp_harness-0.1.0/src/mcp_harness/resilience/circuit_breaker.py +107 -0
  40. mcp_harness-0.1.0/src/mcp_harness/resilience/retry.py +90 -0
  41. mcp_harness-0.1.0/src/mcp_harness/testing/__init__.py +10 -0
  42. mcp_harness-0.1.0/src/mcp_harness/testing/client.py +72 -0
  43. mcp_harness-0.1.0/src/mcp_harness/testing/fixtures.py +49 -0
  44. mcp_harness-0.1.0/tests/conftest.py +25 -0
  45. mcp_harness-0.1.0/tests/test_audit.py +67 -0
  46. mcp_harness-0.1.0/tests/test_auth.py +67 -0
  47. mcp_harness-0.1.0/tests/test_cli.py +56 -0
  48. mcp_harness-0.1.0/tests/test_cli_scaffold.py +60 -0
  49. mcp_harness-0.1.0/tests/test_cost_tracking.py +82 -0
  50. mcp_harness-0.1.0/tests/test_fastmcp_integration.py +53 -0
  51. mcp_harness-0.1.0/tests/test_from_fastmcp.py +59 -0
  52. mcp_harness-0.1.0/tests/test_observability.py +66 -0
  53. mcp_harness-0.1.0/tests/test_pipeline.py +107 -0
  54. mcp_harness-0.1.0/tests/test_policy.py +95 -0
  55. mcp_harness-0.1.0/tests/test_quotas.py +79 -0
  56. mcp_harness-0.1.0/tests/test_resilience.py +79 -0
@@ -0,0 +1,28 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *.egg-info/
5
+ .eggs/
6
+ build/
7
+ dist/
8
+ *.egg
9
+
10
+ # Virtual environments
11
+ .venv/
12
+ venv/
13
+ env/
14
+
15
+ # Test / coverage
16
+ .pytest_cache/
17
+ .mypy_cache/
18
+ .ruff_cache/
19
+ .coverage
20
+ htmlcov/
21
+
22
+ # Tooling
23
+ *.log
24
+ .DS_Store
25
+
26
+ # Local example output (generated by running examples)
27
+ *.jsonl
28
+ !tests/**/*.jsonl
@@ -0,0 +1,39 @@
1
+ # Changelog
2
+
3
+ All notable changes to `mcp-harness` are documented here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project adheres to
5
+ [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.1.0] - 2026-06-27
10
+
11
+ Initial public release. v0.1 MVP plus the surrounding governance core.
12
+
13
+ ### Added
14
+ - **Core** β€” `Harness` with an onion middleware pipeline that is fully decoupled from the MCP
15
+ SDK. `@harness.tool()` registration, `harness.dispatch()` for direct/testing invocation, and
16
+ `harness.run()` which integrates with the official `mcp` SDK (FastMCP) when installed.
17
+ - **`Harness.from_fastmcp(server, ...)`** β€” wrap an existing `FastMCP` server, routing its
18
+ already-registered tools through the governance pipeline while preserving their input schemas.
19
+ The one-line "add governance to the server I already have" path.
20
+ - **CLI scaffolding** β€” `mcp-harness wrap <server.py>` generates a governed companion file for an
21
+ existing FastMCP server (original untouched), and `mcp-harness init [name]` scaffolds a new
22
+ governed server.
23
+ - **Auth** β€” `AnonymousAuth` (default), `APIKeyAuth` (with rotation hook + principal resolver),
24
+ `ChainedAuth`, and an experimental `AzureADAuth` (JWT validation, requires the `auth` extra).
25
+ - **Governance** β€” `CostTracking` (token counting, pluggable pricing, cost-center resolution,
26
+ pluggable sinks), `Quotas` (per-principal token bucket, per-team caps, per-tool concurrency),
27
+ and `AuditLog` (async, non-blocking, shape-only records).
28
+ - **Observability** β€” `StructuredLogging` (JSON + correlation ids), `OTELTracing` (optional,
29
+ `otel` extra), `Metrics` (Prometheus when available, else in-memory).
30
+ - **Policy** β€” `AllowList` / `DenyList` (YAML-loadable, optional argument constraints) and a
31
+ basic `PIIRedactor`.
32
+ - **Resilience** β€” `CircuitBreaker` and `Retry` decorators for individual tools.
33
+ - **Testing** β€” `HarnessTestClient`, `MockPrincipal`, and pytest fixtures (auto-registered as a
34
+ pytest plugin).
35
+ - **CLI** β€” `mcp-harness daily-rollup` to produce per-cost-center spend reports from cost JSONL.
36
+ - Examples: `quickstart_server.py`, `local_governed_server.py`, `azure_governed_server.py`.
37
+
38
+ [Unreleased]: https://github.com/nagenshukla/mcp-harness/compare/v0.1.0...HEAD
39
+ [0.1.0]: https://github.com/nagenshukla/mcp-harness/releases/tag/v0.1.0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or Derivative
95
+ Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work, excluding
103
+ those notices that do not pertain to any part of the Derivative
104
+ Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and do
117
+ not modify the License. You may add Your own attribution notices
118
+ within Derivative Works that You distribute, alongside or as an
119
+ addendum to the NOTICE text from the Work, provided that such
120
+ additional attribution notices cannot be construed as modifying
121
+ the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 Nagendra Shukla
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,222 @@
1
+ Metadata-Version: 2.4
2
+ Name: mcp-harness
3
+ Version: 0.1.0
4
+ Summary: Enterprise governance middleware for MCP servers: cost attribution, auth, observability, quotas, audit, and policy β€” composable around the official MCP Python SDK.
5
+ Project-URL: Homepage, https://github.com/nagenshukla/mcp-harness
6
+ Project-URL: Repository, https://github.com/nagenshukla/mcp-harness
7
+ Project-URL: Issues, https://github.com/nagenshukla/mcp-harness/issues
8
+ Project-URL: Changelog, https://github.com/nagenshukla/mcp-harness/blob/main/CHANGELOG.md
9
+ Author: Nagendra Shukla
10
+ License-Expression: Apache-2.0
11
+ License-File: LICENSE
12
+ Keywords: cost-attribution,enterprise,governance,mcp,middleware,model-context-protocol,observability
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Intended Audience :: System Administrators
16
+ Classifier: License :: OSI Approved :: Apache Software License
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
24
+ Classifier: Topic :: System :: Monitoring
25
+ Classifier: Typing :: Typed
26
+ Requires-Python: >=3.10
27
+ Requires-Dist: pyyaml>=6.0
28
+ Provides-Extra: all
29
+ Requires-Dist: cryptography>=41; extra == 'all'
30
+ Requires-Dist: mcp>=1.2.0; extra == 'all'
31
+ Requires-Dist: opentelemetry-api>=1.20; extra == 'all'
32
+ Requires-Dist: opentelemetry-exporter-otlp>=1.20; extra == 'all'
33
+ Requires-Dist: opentelemetry-sdk>=1.20; extra == 'all'
34
+ Requires-Dist: prometheus-client>=0.19; extra == 'all'
35
+ Requires-Dist: pyjwt>=2.8; extra == 'all'
36
+ Requires-Dist: tiktoken>=0.5; extra == 'all'
37
+ Provides-Extra: auth
38
+ Requires-Dist: cryptography>=41; extra == 'auth'
39
+ Requires-Dist: pyjwt>=2.8; extra == 'auth'
40
+ Provides-Extra: azure
41
+ Requires-Dist: azure-eventhub>=5.11; extra == 'azure'
42
+ Requires-Dist: azure-monitor-opentelemetry>=1.2; extra == 'azure'
43
+ Provides-Extra: dev
44
+ Requires-Dist: build>=1.2; extra == 'dev'
45
+ Requires-Dist: mcp>=1.2.0; extra == 'dev'
46
+ Requires-Dist: mypy>=1.10; extra == 'dev'
47
+ Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
48
+ Requires-Dist: pytest>=8.0; extra == 'dev'
49
+ Requires-Dist: ruff>=0.5; extra == 'dev'
50
+ Requires-Dist: twine>=5.0; extra == 'dev'
51
+ Requires-Dist: types-pyyaml>=6.0; extra == 'dev'
52
+ Provides-Extra: metrics
53
+ Requires-Dist: prometheus-client>=0.19; extra == 'metrics'
54
+ Provides-Extra: otel
55
+ Requires-Dist: opentelemetry-api>=1.20; extra == 'otel'
56
+ Requires-Dist: opentelemetry-exporter-otlp>=1.20; extra == 'otel'
57
+ Requires-Dist: opentelemetry-sdk>=1.20; extra == 'otel'
58
+ Provides-Extra: redis
59
+ Requires-Dist: redis>=5.0; extra == 'redis'
60
+ Provides-Extra: server
61
+ Requires-Dist: mcp>=1.2.0; extra == 'server'
62
+ Provides-Extra: tokens
63
+ Requires-Dist: tiktoken>=0.5; extra == 'tokens'
64
+ Description-Content-Type: text/markdown
65
+
66
+ # mcp-harness
67
+
68
+ **Enterprise governance middleware for MCP servers.** Cost attribution, auth, observability,
69
+ quotas, audit, and policy β€” composable around the official [MCP Python SDK](https://github.com/modelcontextprotocol/python-sdk).
70
+
71
+ [![CI](https://github.com/nagenshukla/mcp-harness/actions/workflows/test.yml/badge.svg)](https://github.com/nagenshukla/mcp-harness/actions/workflows/test.yml)
72
+ [![PyPI](https://img.shields.io/pypi/v/mcp-harness.svg)](https://pypi.org/project/mcp-harness/)
73
+ [![Python](https://img.shields.io/pypi/pyversions/mcp-harness.svg)](https://pypi.org/project/mcp-harness/)
74
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)
75
+
76
+ ---
77
+
78
+ ## Why this exists
79
+
80
+ Most public MCP servers are toy examples. The gap between *"works on my laptop"* and *"I can ship
81
+ this to 500 engineers under SOC 2, GDPR, and a Finance team that wants per-business-unit cost
82
+ allocation"* is enormous β€” and almost none of that gap is in the protocol. It's in the boring
83
+ middleware around it.
84
+
85
+ `mcp-harness` **is** that middleware. It doesn't fork the protocol, doesn't reinvent the SDK, and
86
+ doesn't try to be an agent framework. It's a set of composable decorators and middleware that wrap
87
+ the official SDK so the resulting server can be safely run inside a company.
88
+
89
+ The headline use case: **your CFO asks who's spending the AI budget, and you can answer by end of
90
+ week one.**
91
+
92
+ ## Install
93
+
94
+ ```bash
95
+ pip install mcp-harness # core pipeline (no MCP SDK required)
96
+ pip install 'mcp-harness[server]' # + the official MCP SDK, to actually serve tools
97
+ pip install 'mcp-harness[all]' # + OpenTelemetry, Prometheus, tiktoken, JWT
98
+ ```
99
+
100
+ The core middleware pipeline has **no hard dependency** on `mcp`, so you can unit-test all your
101
+ governance behaviour without a transport. Install the `server` extra to run a live server.
102
+
103
+ ## Hello, governed world
104
+
105
+ The same MCP server you'd write anyway, with the governance layer declared once at the top:
106
+
107
+ ```python
108
+ from mcp_harness import Harness
109
+ from mcp_harness.auth import APIKeyAuth
110
+ from mcp_harness.governance import CostTracking, Quotas, AuditLog
111
+ from mcp_harness.observability import OTELTracing, StructuredLogging
112
+ from mcp_harness.policy import AllowList
113
+
114
+ harness = Harness(
115
+ name="customer-data-mcp",
116
+ auth=APIKeyAuth(keys={"sk-finance": {"id": "svc-reports", "team": "finance"}}),
117
+ middleware=[
118
+ OTELTracing(service_name="customer-data-mcp"),
119
+ StructuredLogging(),
120
+ AllowList.from_yaml("policies/tool-access.yaml"),
121
+ Quotas(per_principal_per_minute=60),
122
+ CostTracking(
123
+ cost_center_resolver=lambda p: p.team,
124
+ sink="jsonl://costs.jsonl",
125
+ ),
126
+ AuditLog(sink="jsonl://audit.jsonl"),
127
+ ],
128
+ )
129
+
130
+ @harness.tool()
131
+ async def search_customer(customer_id: str) -> dict:
132
+ """Find a customer record by ID."""
133
+ return {"id": customer_id, "name": "ACME Corp"}
134
+
135
+ if __name__ == "__main__":
136
+ harness.run() # stdio by default; transport="streamable-http" for HTTP
137
+ ```
138
+
139
+ Then attribute spend:
140
+
141
+ ```console
142
+ $ mcp-harness daily-rollup costs.jsonl
143
+ cost_center calls in_tok out_tok cost_usd
144
+ ----------------------------------------------------------
145
+ finance 42 5\,210 9\,830 0.1284
146
+ platform 18 1\,940 3\,110 0.0451
147
+ ----------------------------------------------------------
148
+ TOTAL 60 7\,150 12\,940 0.1735
149
+ ```
150
+
151
+ ## How it works
152
+
153
+ A tiny **onion middleware pipeline**, decoupled from the SDK. `@harness.tool()` registers your
154
+ function; calls flow through each layer and into your tool. The same path runs whether the call
155
+ arrives from a live MCP client or from the in-process test client.
156
+
157
+ ```
158
+ client ─▢ FastMCP ─▢ wrapper ─┐
159
+ β”œβ”€β–Ά auth ─▢ policy ─▢ quotas ─▢ tracing ─▢ cost ─▢ audit ─▢ your tool
160
+ test/direct ─▢ dispatch β”€β”€β”€β”€β”€β”€β”˜
161
+ ```
162
+
163
+ Each layer is independently useful, independently testable, and opt-in. Adopt just `CostTracking`,
164
+ or stack the whole thing.
165
+
166
+ ## Modules
167
+
168
+ | Module | What you get |
169
+ | --- | --- |
170
+ | `mcp_harness.auth` | `APIKeyAuth` (with rotation), `AnonymousAuth`, `ChainedAuth`, experimental `AzureADAuth` (Entra ID JWT) |
171
+ | `mcp_harness.governance` | **`CostTracking`** (tokens β†’ \$ β†’ cost center), `Quotas` (rate / team cap / concurrency), `AuditLog` (async, shape-only) |
172
+ | `mcp_harness.observability` | `StructuredLogging` (JSON + correlation ids), `OTELTracing`, `Metrics` (Prometheus or in-memory) |
173
+ | `mcp_harness.policy` | `AllowList` / `DenyList` (YAML, argument constraints), `PIIRedactor` |
174
+ | `mcp_harness.resilience` | `CircuitBreaker`, `Retry` decorators for individual tools |
175
+ | `mcp_harness.testing` | `HarnessTestClient`, `MockPrincipal`, pytest fixtures |
176
+ | `mcp-harness` CLI | `daily-rollup` per-cost-center spend reports |
177
+
178
+ Optional integrations degrade gracefully: `OTELTracing` is a no-op (warned once) without the
179
+ `otel` extra; `Metrics` falls back to an in-memory backend without `prometheus-client`; cost token
180
+ counting uses a heuristic without `tiktoken`.
181
+
182
+ ## Testing your server
183
+
184
+ Governance is covered by ordinary unit tests β€” no transport, no mocks of the SDK:
185
+
186
+ ```python
187
+ from mcp_harness.testing import HarnessTestClient, MockPrincipal
188
+ from myserver import harness
189
+
190
+ async def test_finance_can_search(harness_client): # fixture auto-registered
191
+ client = harness_client(harness, principal=MockPrincipal("svc-a", team="finance"))
192
+ assert await client.call("search_customer", {"customer_id": "c-1"})
193
+ ```
194
+
195
+ ## Examples
196
+
197
+ - [`examples/quickstart_server.py`](examples/quickstart_server.py) β€” smallest governed server.
198
+ - [`examples/local_governed_server.py`](examples/local_governed_server.py) β€” full stack, zero
199
+ cloud deps. Run `python examples/local_governed_server.py --demo` to watch the governance layers
200
+ reject calls in real time.
201
+ - [`examples/azure_governed_server.py`](examples/azure_governed_server.py) β€” the design's
202
+ Azure-centric reference server.
203
+
204
+ ## Scope
205
+
206
+ **In scope:** auth, observability, cost attribution, quotas, audit, policy, resilience, testing.
207
+
208
+ **Out of scope (by design):** agent orchestration, a UI/dashboard, a tool registry, and anything
209
+ that breaks MCP wire-compatibility. A library that does a few things well beats one that does
210
+ fifteen poorly.
211
+
212
+ ## Compatibility & status
213
+
214
+ - Python **3.10+**. Wraps the official `mcp` SDK (`FastMCP`) without modifying the wire protocol.
215
+ - **Beta (v0.1).** The `Harness`, auth, `CostTracking`, `Quotas`, observability, `AllowList`, and
216
+ `AuditLog` APIs are real and tested. Cloud sinks (Azure Monitor, Event Hubs, Kinesis, Kafka),
217
+ Redis-backed quotas, `SchemaGuard`, and `AzureADAuth` hardening are extension points β€” base
218
+ interfaces ship, full wiring is on the roadmap. See [CHANGELOG.md](CHANGELOG.md).
219
+
220
+ ## License
221
+
222
+ [Apache-2.0](LICENSE).
@@ -0,0 +1,157 @@
1
+ # mcp-harness
2
+
3
+ **Enterprise governance middleware for MCP servers.** Cost attribution, auth, observability,
4
+ quotas, audit, and policy β€” composable around the official [MCP Python SDK](https://github.com/modelcontextprotocol/python-sdk).
5
+
6
+ [![CI](https://github.com/nagenshukla/mcp-harness/actions/workflows/test.yml/badge.svg)](https://github.com/nagenshukla/mcp-harness/actions/workflows/test.yml)
7
+ [![PyPI](https://img.shields.io/pypi/v/mcp-harness.svg)](https://pypi.org/project/mcp-harness/)
8
+ [![Python](https://img.shields.io/pypi/pyversions/mcp-harness.svg)](https://pypi.org/project/mcp-harness/)
9
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)
10
+
11
+ ---
12
+
13
+ ## Why this exists
14
+
15
+ Most public MCP servers are toy examples. The gap between *"works on my laptop"* and *"I can ship
16
+ this to 500 engineers under SOC 2, GDPR, and a Finance team that wants per-business-unit cost
17
+ allocation"* is enormous β€” and almost none of that gap is in the protocol. It's in the boring
18
+ middleware around it.
19
+
20
+ `mcp-harness` **is** that middleware. It doesn't fork the protocol, doesn't reinvent the SDK, and
21
+ doesn't try to be an agent framework. It's a set of composable decorators and middleware that wrap
22
+ the official SDK so the resulting server can be safely run inside a company.
23
+
24
+ The headline use case: **your CFO asks who's spending the AI budget, and you can answer by end of
25
+ week one.**
26
+
27
+ ## Install
28
+
29
+ ```bash
30
+ pip install mcp-harness # core pipeline (no MCP SDK required)
31
+ pip install 'mcp-harness[server]' # + the official MCP SDK, to actually serve tools
32
+ pip install 'mcp-harness[all]' # + OpenTelemetry, Prometheus, tiktoken, JWT
33
+ ```
34
+
35
+ The core middleware pipeline has **no hard dependency** on `mcp`, so you can unit-test all your
36
+ governance behaviour without a transport. Install the `server` extra to run a live server.
37
+
38
+ ## Hello, governed world
39
+
40
+ The same MCP server you'd write anyway, with the governance layer declared once at the top:
41
+
42
+ ```python
43
+ from mcp_harness import Harness
44
+ from mcp_harness.auth import APIKeyAuth
45
+ from mcp_harness.governance import CostTracking, Quotas, AuditLog
46
+ from mcp_harness.observability import OTELTracing, StructuredLogging
47
+ from mcp_harness.policy import AllowList
48
+
49
+ harness = Harness(
50
+ name="customer-data-mcp",
51
+ auth=APIKeyAuth(keys={"sk-finance": {"id": "svc-reports", "team": "finance"}}),
52
+ middleware=[
53
+ OTELTracing(service_name="customer-data-mcp"),
54
+ StructuredLogging(),
55
+ AllowList.from_yaml("policies/tool-access.yaml"),
56
+ Quotas(per_principal_per_minute=60),
57
+ CostTracking(
58
+ cost_center_resolver=lambda p: p.team,
59
+ sink="jsonl://costs.jsonl",
60
+ ),
61
+ AuditLog(sink="jsonl://audit.jsonl"),
62
+ ],
63
+ )
64
+
65
+ @harness.tool()
66
+ async def search_customer(customer_id: str) -> dict:
67
+ """Find a customer record by ID."""
68
+ return {"id": customer_id, "name": "ACME Corp"}
69
+
70
+ if __name__ == "__main__":
71
+ harness.run() # stdio by default; transport="streamable-http" for HTTP
72
+ ```
73
+
74
+ Then attribute spend:
75
+
76
+ ```console
77
+ $ mcp-harness daily-rollup costs.jsonl
78
+ cost_center calls in_tok out_tok cost_usd
79
+ ----------------------------------------------------------
80
+ finance 42 5\,210 9\,830 0.1284
81
+ platform 18 1\,940 3\,110 0.0451
82
+ ----------------------------------------------------------
83
+ TOTAL 60 7\,150 12\,940 0.1735
84
+ ```
85
+
86
+ ## How it works
87
+
88
+ A tiny **onion middleware pipeline**, decoupled from the SDK. `@harness.tool()` registers your
89
+ function; calls flow through each layer and into your tool. The same path runs whether the call
90
+ arrives from a live MCP client or from the in-process test client.
91
+
92
+ ```
93
+ client ─▢ FastMCP ─▢ wrapper ─┐
94
+ β”œβ”€β–Ά auth ─▢ policy ─▢ quotas ─▢ tracing ─▢ cost ─▢ audit ─▢ your tool
95
+ test/direct ─▢ dispatch β”€β”€β”€β”€β”€β”€β”˜
96
+ ```
97
+
98
+ Each layer is independently useful, independently testable, and opt-in. Adopt just `CostTracking`,
99
+ or stack the whole thing.
100
+
101
+ ## Modules
102
+
103
+ | Module | What you get |
104
+ | --- | --- |
105
+ | `mcp_harness.auth` | `APIKeyAuth` (with rotation), `AnonymousAuth`, `ChainedAuth`, experimental `AzureADAuth` (Entra ID JWT) |
106
+ | `mcp_harness.governance` | **`CostTracking`** (tokens β†’ \$ β†’ cost center), `Quotas` (rate / team cap / concurrency), `AuditLog` (async, shape-only) |
107
+ | `mcp_harness.observability` | `StructuredLogging` (JSON + correlation ids), `OTELTracing`, `Metrics` (Prometheus or in-memory) |
108
+ | `mcp_harness.policy` | `AllowList` / `DenyList` (YAML, argument constraints), `PIIRedactor` |
109
+ | `mcp_harness.resilience` | `CircuitBreaker`, `Retry` decorators for individual tools |
110
+ | `mcp_harness.testing` | `HarnessTestClient`, `MockPrincipal`, pytest fixtures |
111
+ | `mcp-harness` CLI | `daily-rollup` per-cost-center spend reports |
112
+
113
+ Optional integrations degrade gracefully: `OTELTracing` is a no-op (warned once) without the
114
+ `otel` extra; `Metrics` falls back to an in-memory backend without `prometheus-client`; cost token
115
+ counting uses a heuristic without `tiktoken`.
116
+
117
+ ## Testing your server
118
+
119
+ Governance is covered by ordinary unit tests β€” no transport, no mocks of the SDK:
120
+
121
+ ```python
122
+ from mcp_harness.testing import HarnessTestClient, MockPrincipal
123
+ from myserver import harness
124
+
125
+ async def test_finance_can_search(harness_client): # fixture auto-registered
126
+ client = harness_client(harness, principal=MockPrincipal("svc-a", team="finance"))
127
+ assert await client.call("search_customer", {"customer_id": "c-1"})
128
+ ```
129
+
130
+ ## Examples
131
+
132
+ - [`examples/quickstart_server.py`](examples/quickstart_server.py) β€” smallest governed server.
133
+ - [`examples/local_governed_server.py`](examples/local_governed_server.py) β€” full stack, zero
134
+ cloud deps. Run `python examples/local_governed_server.py --demo` to watch the governance layers
135
+ reject calls in real time.
136
+ - [`examples/azure_governed_server.py`](examples/azure_governed_server.py) β€” the design's
137
+ Azure-centric reference server.
138
+
139
+ ## Scope
140
+
141
+ **In scope:** auth, observability, cost attribution, quotas, audit, policy, resilience, testing.
142
+
143
+ **Out of scope (by design):** agent orchestration, a UI/dashboard, a tool registry, and anything
144
+ that breaks MCP wire-compatibility. A library that does a few things well beats one that does
145
+ fifteen poorly.
146
+
147
+ ## Compatibility & status
148
+
149
+ - Python **3.10+**. Wraps the official `mcp` SDK (`FastMCP`) without modifying the wire protocol.
150
+ - **Beta (v0.1).** The `Harness`, auth, `CostTracking`, `Quotas`, observability, `AllowList`, and
151
+ `AuditLog` APIs are real and tested. Cloud sinks (Azure Monitor, Event Hubs, Kinesis, Kafka),
152
+ Redis-backed quotas, `SchemaGuard`, and `AzureADAuth` hardening are extension points β€” base
153
+ interfaces ship, full wiring is on the roadmap. See [CHANGELOG.md](CHANGELOG.md).
154
+
155
+ ## License
156
+
157
+ [Apache-2.0](LICENSE).