TestPilot's Tools as an MCP Server
Why This Matters
Tools hard-wired into one application help one application. Served over MCP (the Model Context Protocol), the same tools work for any client that speaks the protocol: your triage service, Claude Code, Claude Desktop, or another team’s agent. And the boundary between “the agent” and “the systems it may touch” becomes explicit.
MCP in one picture
client (TestPilot, Claude Code, …) ──MCP──► testpilot-mcp server ──► CI API, logs, git, issue tracker
An MCP server can offer three kinds of things:
| Kind | What it is | TestPilot example |
|---|---|---|
| Tools | Actions the model can call | get_test_history, read_log, search_issues |
| Resources | Data the client can read and add to context | The category definitions, the golden set |
| Prompts | Reusable prompt templates | “Triage this failure” |
Tools are chosen by the model; resources are usually chosen by the application or the user.
Build testpilot-mcp
Use the official MCP TypeScript SDK and reuse the tool functions from lesson 24:
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({ name: "testpilot-mcp", version: "1.0.0" });
server.registerTool(
"get_test_history",
{
description: "Returns the outcome of one test in recent CI runs, newest first, plus the failure rate.",
inputSchema: { testId: z.string(), runs: z.number().int().min(1).max(50).optional() },
},
async ({ testId, runs }) => ({ content: [{ type: "text", text: JSON.stringify(await getTestHistory(testId, runs ?? 20)) }] }),
);
await server.connect(new StdioServerTransport());
This is the v1 SDK (@modelcontextprotocol/sdk), which TestPilot uses and which still gets fixes. Since July 2026 the main line is v2, with new package names (@modelcontextprotocol/server, @modelcontextprotocol/client) and inputSchema as a full z.object(...). Check the SDK’s documentation for the version you install; the shape changes, the idea does not.
Connect TestPilot to the server
Instead of calling the tool functions directly, TestPilot’s loop becomes an MCP client: it lists the server’s tools, passes them to Claude, and forwards each tool_use to the server. The loop from lesson 27 stays the same; only runTool changes.
A real run: same loop, two kinds of plumbing
The same failure, t-004, triaged twice with solution/lesson-31-mcp-server: once with the tools in-process, once through testpilot-mcp:
Three things to notice:
- Same verdict, same ticket. Both runs say
flaky(0.85) and findQA-118throughsearch_issues. Moving the tools behind MCP changed the plumbing, not the outcome. - Different tool calls. The first run fetched the history, the log, the diff and the issues. The second only searched the issues and read the diff, because the failure rate was already in the evidence. The model decides per run what it needs, so two runs of the same agent rarely call exactly the same tools. Test the loop with a scripted model (lesson 29); measure the decisions with the golden set (module 3).
- A bug in our own fixtures. Both runs only saw the few log lines captured in the run file, not the full CI log. The repository’s
.gitignoreignores*.log, so the fake CI logs were never committed, andread_logfell back to the captured lines in every clone. Claude Code found it in module 6 when tworead_logtests failed on a fresh checkout. The full log oft-004containstimeout 0ms (test override): the test switched off Playwright’s own waiting, which points at the test rather than at timing. Run it again aftergit pulland compare the verdict. The lesson for your own tools: a tool that silently falls back to less data needs a test that runs on a clean checkout, not only on your machine.
Configuration and secrets
The server holds the credentials for the CI system and the issue tracker, read from its own environment. The model never sees them, and neither does the client. That is one of MCP’s quiet benefits: the secrets live in one small, reviewable place.
Keep write tools out of the shared server until module 7’s controls exist, or expose create_issue only as a proposal tool.
Exercise
Build testpilot-mcp with the four read-only tools, run TestPilot against it, and compare the verdicts with lesson 27. They should be the same; only the plumbing changed.
Compare with solution/lesson-31-mcp-server, which covers lessons 24–31 (see docs/exercises.md, module 4).
