Governance middleware: policy enforcement and audit trails at the protocol level
Proposal: Governance Middleware for MCP Python SDK
Summary
Add an optional governance middleware layer to the MCP Python SDK that enables policy enforcement, threat detection, and audit trails at the protocol level — so any MCP server can benefit from governance without custom implementation.
Problem
MCP servers expose powerful tools (file access, database queries, API calls, code execution). Currently, each server must implement its own security/governance logic, leading to:
- Inconsistent security — Some servers validate inputs, others don't
- No standard policy format — No way to apply organizational policies across servers
- No audit trail — Tool calls are not systematically logged for compliance
- Duplicate effort — Every server reimplements argument validation and access control
Proposed Design
A middleware layer that intercepts tool calls at the SDK level:
from mcp.server import Server
from mcp.governance import GovernanceMiddleware, GovernancePolicy
# Define policy
policy = GovernancePolicy(
name="production-safe",
allowed_tools=["search", "read_file", "list_directory"],
blocked_tools=["execute_command", "write_file"],
max_tool_calls_per_session=100,
content_filters=["no_pii", "no_secrets"],
threat_detection=True,
audit_trail="governance_audit.jsonl",
)
# Apply middleware
app = Server("my-server")
governance = GovernanceMiddleware(app, policy=policy)
@app.tool()
async def search(query: str) -> str:
# Governance middleware validates before this runs
return await perform_search(query)
Middleware Hooks
- pre_tool_call — Validate tool name + arguments against policy. Return error if blocked.
- post_tool_call — Audit the result, scan for sensitive data in output.
- on_connect — Log client connection, apply session-level policies.
- on_policy_violation — Emit structured error with violation details.
Benefits
- Zero-change adoption — Existing servers add governance with 2 lines of code
- Protocol-level — Works regardless of transport (stdio, SSE, HTTP)
- Composable — Stack multiple policies (org → team → server)
- Standards-aligned — Policy format compatible with Agent-OS governance schemas
Context
We maintain Agent-OS governance framework with integrations for PydanticAI, CrewAI, OpenAI Agents, and Google ADK. Related proposals:
- google/mcp-security #237 — MCP security governance
- anthropics/claude-agent-sdk-python #587 — Governance hooks
- google/adk-python #4543 — GovernancePlugin
Thanks for the proposal! Governance and policy enforcement are application-level concerns rather than something we'd ship in the SDK. Generic middleware support is tracked in #1233, which would provide the hooks to build this on top.
If you think this should be a protocol-level concern, the right path would be filing a SEP at https://github.com/modelcontextprotocol/modelcontextprotocol.
Closing in favor of #1233.
The maintainer's rationale is exactly right, and worth naming the architectural implication: governance belongs below the SDK, not inside it. The SDK is the protocol transport. The enforcement layer needs to sit at the dispatch boundary — above the transport, below the model's authority.
What that layer needs to do:
- Intercept every tool call before it executes, regardless of which SDK or transport is in use
- Apply schema-pinned policy (not just a blocklist, but a validated schema per tool)
- Emit a signed receipt atomically with the policy decision — so the enforcement is provable, not just logged
- Fail closed on unknown or unvalidated calls
This is exactly what HELM OSS implements: a fail-closed execution firewall at the tool call dispatch boundary. Works with any MCP server or transport, since it intercepts at dispatch rather than wrapping the SDK.
For anyone building on top of #1233 middleware hooks, HELM's architecture would be a relevant reference for the receipt + schema-pinning layer on top.