MCP Agent Harness
Provides a mountable SSE/ASGI app for FastAPI applications, enabling the harness's MCP tools and endpoints to be exposed alongside FastAPI routes.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@MCP Agent Harnessrun diagnostics and show the trace ID for this call"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
MCP Agent Harness
Generic, SRE-hardened Model Context Protocol (MCP) harness for autonomous agents. Designed to prevent LLM context exhaustion, enforce data boundaries, and maintain fail-closed observability across agentic workloads.
Prerequisites
Docker
>= 24.0Docker Compose
>= 2.20GNU Make
>= 4.3(Optional for local development) Python
>= 3.11
Related MCP server: Graft
Quickstart (Containerized)
No local Python environment or dependency installation required. All lifecycle steps run inside isolated containers:
# 1. Build the hardened container image
make build
# 2. Run the integration test suite
make test
# 3. Spin up the harness and verify live endpoint health
make demo
# 4. Run the security, leak, and Trivy CVE audit gate
make auditArchitecture & Codebase Map
mcp-agent-harness/
├── src/
│ └── mcp_agent_harness/
│ ├── __init__.py # Public exports: AgentHarness, guards, telemetry
│ ├── server.py # AgentHarness, ToolList, tool decorator, SSE app mounting
│ ├── guards.py # DataFrame blockade, TokenStarvationError, context & log capping
│ ├── telemetry.py # Correlated trace_id generation and context logging
│ └── __main__.py # CLI entrypoint for health checks and standalone daemon
├── tests/
│ ├── test_guards.py # Unit tests for DataFrame blockade, context truncation, log capping
│ ├── test_server.py # Sync/async tool execution, leak detection, registry checks
│ └── test_integration_sse.py # FastAPI SSE endpoint mounting and streaming tests
├── scripts/
│ ├── audit_image.sh # Fail-closed Trivy CVE scanner & prohibited file leak audit
│ └── check_blacklist.sh # Clean-room sensitive pattern scanner
├── Dockerfile # Multi-stage hardened Debian container definition
├── docker-compose.yml # Service definitions for daemon and test runner
└── Makefile # Standardized automation targetsSRE Hardening Principles
Token Starvation Guards: Prevents raw DataFrames (
pandas,polars) from leaking into LLM context windows. Enforces scalar metric summaries.Context Bounds: Automatically truncates bulk
<DATA>payloads and caps log output lines to prevent context window blowouts.Telemetry Tracing: Generates correlated
trace_idfor every tool invocation.Fail-Closed Isolation: Unhandled exceptions return structured error envelopes without terminating SSE streams.
API & Configuration Reference
AgentHarness
Main server class wrapping FastMCP with SRE guards.
from mcp_agent_harness import AgentHarness
harness = AgentHarness(
name="analytics-mcp",
instructions="Operational analytics tools for agents.",
register_builtins=True,
)Parameter | Type | Default | Description |
|
|
| Identifier for the MCP server instance. |
|
|
| System instructions exposed to MCP clients. |
|
|
| Registers default diagnostic tools ( |
Methods
@harness.tool(*args, **kwargs): Decorator registering sync orasynctools with automatic trace injection and DataFrame blockades.harness.list_tools() -> ToolList: Returns registered tool names (supports both sync iteration and asyncawait).harness.call_tool(name: str, *args, **kwargs) -> Any: Directly invokes a registered tool by name.harness.sse_app() -> Any: Generates the Starlette/ASGI app for mounting into FastAPI.
Guard Utilities (mcp_agent_harness.guards)
Configurable standalone filters to protect agent prompt budgets:
Function | Parameters | Default | Description |
|
|
| Strips raw |
|
|
| Caps multiline log dumps to prevent terminal context flooding. |
|
| N/A | Recursively traverses dicts, lists, sets, and tuples, raising |
Guardrails in Action
1. Synchronous & Asynchronous Tools
from mcp_agent_harness import AgentHarness
harness = AgentHarness(name="system-tools")
# Synchronous tool
@harness.tool()
def get_system_load() -> dict:
return {"load_1m": 0.42, "load_5m": 0.35}
# Asynchronous tool
@harness.tool()
async def fetch_cluster_metrics(region: str) -> dict:
# Non-blocking async execution
return {"region": region, "active_nodes": 12, "healthy": True}2. Token Starvation Guard (DataFrame Blockade)
The harness blocks raw tabular data from entering LLM context. Returning a raw DataFrame immediately triggers TokenStarvationError, captured into a structured error envelope:
import pandas as pd
from mcp_agent_harness import AgentHarness
harness = AgentHarness()
# ❌ BAD: Attempting to return raw DataFrame
@harness.tool()
def query_orders() -> pd.DataFrame:
return pd.DataFrame({"order_id": [1, 2], "amount": [99.5, 14.2]})
# Invoking this tool returns a safe error envelope instead of leaking rows:
# {
# "status": "error",
# "message": "Raw DataFrames are strictly prohibited in MCP tools. Return scalar metrics or token-compressed strings.",
# "trace_id": "mcp-3a1b4c9e"
# }
# ✅ GOOD: Return compact scalar metrics
@harness.tool()
def query_order_metrics() -> dict:
df = pd.DataFrame({"order_id": [1, 2], "amount": [99.5, 14.2]})
return {
"order_count": len(df),
"total_revenue": float(df["amount"].sum()),
"average_order": float(df["amount"].mean()),
}3. Context & Log Bounds
from mcp_agent_harness import truncate_context, cap_log_lines
raw_logs = "INFO: Step completed\n" * 500
# Safe for LLM prompts: capped at 200 lines
clean_logs = cap_log_lines(raw_logs, max_lines=200)
raw_prompt = "User query: analyze output <DATA>large blob...</DATA>"
# Strips bulk data XML tags and caps character length
bounded_prompt = truncate_context(raw_prompt, max_chars=1000)Telemetry & Error Envelopes
Every tool invocation automatically initializes an OpenTelemetry-compatible trace_id (mcp-xxxxxxxx).
When an unhandled exception occurs inside a tool, the harness isolates the failure:
The underlying SSE stream remains open.
The failure is logged with the correlated trace ID.
The agent receives a predictable JSON envelope:
{
"status": "error",
"message": "Division by zero",
"trace_id": "mcp-e4f912a7"
}Integration with FastAPI
Install the package in your consumer application:
pip install "git+https://github.com/emmanuelol/mcp-agent-harness.git@v0.1.0"Mount into FastAPI:
from fastapi import FastAPI
from mcp_agent_harness import AgentHarness
harness = AgentHarness(
name="production-harness",
instructions="Production operational agent harness.",
)
@harness.tool()
async def health_summary() -> dict:
return {"status": "operational", "uptime_pct": 99.98}
app = FastAPI(title="Agent Platform")
@app.get("/health")
def health() -> dict:
return {"status": "ok"}
# Mount MCP Server-Sent Events endpoint
app.mount("/mcp", harness.sse_app())Run with Uvicorn:
uvicorn main:app --host 0.0.0.0 --port 8000This server cannot be deployed
Maintenance
Related MCP Connectors
MCP server for your apps' tools and custom tools, plus hosted AI agents and approval-gated workflows
Hosted MCP runtime where your agent publishes its own tools by tool call and keeps working.
MCP server for progressive tool usage at any scale (see https://klavis.ai)
Enable secure connectivity between Sentry issues and debugging data, and LLM clients, using a Model Context Protocol (MCP) server.
Related MCP Servers
- AlicenseBqualityAmaintenanceA lightweight server implementation that exposes Python functions as discoverable tools via HTTP using the Machine-to-Machine Communication Protocol (MCP). Enables remote execution of Python functions through a JSON-RPC interface with async support and type safety.414 PyPIMIT
- AlicenseNot gradedqualityCmaintenanceEnables building agent-ready APIs that expose tools as both HTTP and MCP endpoints from a single server definition, with automatic OpenAPI, discovery docs, and interactive API reference.5Apache 2.0
- AlicenseAqualityCmaintenanceA production-ready starting point for MCP servers, adding per-tool timeouts, concurrency ceilings, error boundaries, secret redaction, correlation IDs, and structured logging so tools are safe and observable for LLM callers.3MIT
- AlicenseNot gradedqualityCmaintenanceEnables turning any Python function into an auto-registered MCP tool and serving them over a FastMCP server with auto-discovery, optional auth, and request context.MIT