No harness ships with every tool and every rule a team needs. Modern harnesses are built to be extended in three main ways: the Model Context Protocol (MCP) connects external tools and data through a standard interface; hooks run your own code at fixed points in the agent's lifecycle to enforce rules or add context; and skills package instructions, scripts and reference material that the agent loads only when a task needs them. This lesson explains each mechanism, when to use which, how to build a small MCP server and a policy hook, and the security questions every extension raises.
- Explain MCP's client-server model, its primitives and its transports
- Build a small MCP server that exposes a tool
- Use lifecycle hooks to enforce policy deterministically
- Package knowledge as skills with progressive disclosure
- Assess the security risks of third-party extensions
The Model Context Protocol
MCP is an open protocol for connecting agents to tools and data. The harness runs an MCP client; each integration is an MCP server that exposes capabilities in a standard form. Write the integration once as a server, and any MCP-capable harness can use it. Servers run locally as a subprocess speaking over stdio, or remotely over Streamable HTTP, where authorisation is typically handled with OAuth.
| Primitive | Controlled by | Example |
|---|---|---|
| Tools | The model decides to call them | orders_search, create_ticket |
| Resources | The application or user attaches them | A file, a database schema, a document |
| Prompts | The user picks them | A reusable "review this PR" template |
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("orders")
ORDERS = {"A-1042": {"email": "sam@example.com", "status": "delivered", "total": 59.0}}
@mcp.tool()
def orders_search(customer_email: str) -> str:
"""Find a customer's orders. Use before any refund to get the order ID."""
rows = [f"{oid} | {o['status']} | {o['total']:.2f}"
for oid, o in ORDERS.items() if o["email"] == customer_email]
return "\n".join(rows) or f"No orders for {customer_email}."
if __name__ == "__main__":
mcp.run() # stdio transport by defaultThe function's name, type hints and docstring become the tool's name, schema and description, so everything from the tool design lesson applies: clear names, precise types, actionable results. Test servers with the MCP Inspector before connecting them to an agent.
Hooks: deterministic control
Instructions in a prompt are suggestions; the model usually follows them, but not always. Hooks are code the harness runs at lifecycle events (session start, before a tool runs, after a tool runs, when the agent tries to stop), and they always run. Use them for rules that must hold every time: block edits to protected files, run a formatter after every edit, add the current branch to context at session start, or require tests before stopping. The details differ by harness, but most pass the event as JSON and let the hook allow, block with a reason, or add context.
#!/usr/bin/env python3
"""Reads the pending tool call as JSON on stdin; exit code 2 blocks it, stderr is the reason."""
import fnmatch
import json
import sys
PROTECTED = ["*.env", "migrations/*", "tests/*", ".github/workflows/*"]
event = json.load(sys.stdin)
path = event.get("tool_input", {}).get("file_path", "")
if event.get("tool_name") in ("edit", "write") and any(fnmatch.fnmatch(path, p) for p in PROTECTED):
print(f"{path} is protected; explain the needed change to the user instead.", file=sys.stderr)
sys.exit(2)
sys.exit(0)A good rule of thumb: if breaking a rule once would be unacceptable, put it in a hook or the permission policy, not only in the prompt.
Skills: knowledge on demand
Teams accumulate procedures: how to cut a release, how to write a migration, how to use the internal design system. Putting all of that in the system prompt wastes context on every call. A skill packages one procedure as a folder with a short description, detailed instructions and any helper scripts or reference files. The harness keeps only each skill's name and description in context; when a task matches, the agent loads the full instructions, and reads deeper files only if needed. This progressive disclosure keeps context small while making a large library of know-how available.
skills/release/
SKILL.md name, one-line description, then step-by-step instructions
checklist.md detailed pre-release checks, read only when needed
scripts/
bump_version.py helper the instructions tell the agent to runProject instruction files (for example an AGENTS.md at the repository root) are the always-loaded counterpart: short, essential conventions such as build and test commands. Keep them brief and move long procedures into skills.
Choosing a mechanism
- New capability or data source (query the ticket system): an MCP server, or a built-in tool.
- Rule that must always hold (never edit migrations): a hook or permission rule.
- Procedure or expertise used sometimes (how we release): a skill.
- Conventions needed almost every time (test command, code style): project instructions.
Security of extensions
Every extension runs with real power. A local MCP server is a program on your machine; a remote one sees the data you send it; a hook executes arbitrary commands; a skill's scripts run in your environment. Treat them as dependencies: install from trusted sources, pin versions, review code and permissions, and give remote servers the narrowest OAuth scopes. Watch for tool poisoning, where a malicious server's tool descriptions contain hidden instructions, and remember that tool results from an MCP server are untrusted content for prompt-injection purposes.