Code Mode

How the code-mode sandbox works and how AI clients write TypeScript to query your data.

The MCP Server uses a code-mode approach instead of traditional tool definitions. Rather than exposing dozens of separate tools, it provides a single execute_code tool where AI writes TypeScript against the brakinglab.* namespace.

How It Works

AI writes TypeScript → esbuild transpiles → isolated-vm executes → results returned
  1. Type generation: The server auto-generates TypeScript definitions from its function catalog
  2. AI writes code: Using the brakinglab.* namespace with full IDE-like type hints
  3. Transpilation: esbuild converts TypeScript to JavaScript
  4. Sandbox execution: isolated-vm runs the code with no network or filesystem access
  5. Results: console.log() output and return values are sent back to the AI

Example: Coaching Analysis

Here’s what an AI might write when you ask “Where am I losing time at Spa?”:

// Get recent sessions at Spa
const sessions = await brakinglab.getSessions({ trackName: "Spa", limit: 5 });

// Find weakest corners
const weak = await brakinglab.getWeakCorners({ trackName: "Spa", limit: 5 });

// Get braking data for the latest session
const braking = await brakinglab.getBrakingZones({
  sessionId: sessions[0].id,
});

// Log findings
console.log("Weakest corners:", weak);
console.log("Braking zones:", braking);

// Save coaching report
await brakinglab.saveCoachingReport({
  sessionId: sessions[0].id,
  overview: {
    assessment: "needs_work",
    summary: "Three corners at Spa are costing significant time...",
  },
  pace: {
    bestLapMs: sessions[0].bestLapMs,
    averageLapMs: 138500,
    consistencyPct: 85,
    improvementTrend: "improving",
  },
  corners: weak.map((c) => ({
    cornerIndex: c.cornerIndex,
    cornerName: c.cornerName,
    verdict: "needs_work",
    issue: c.topIssue,
    detail: `Weakness score: ${c.weaknessScore}/100`,
  })),
  actionItems: [
    {
      priority: 1,
      action: "Focus on trail braking at La Source",
      why: "Releasing brake too quickly, losing 0.3s",
    },
  ],
});

Sandbox Constraints

The sandbox is designed for safety:

ConstraintValue
Timeout10 seconds per execution
Memory32 MB limit
API callsMax 20 per code block
Code sizeMax 10 KB
OutputMax 100 KB
NetworkNone (fully isolated)
FilesystemNone (fully isolated)

Type Safety

All functions use Zod schemas for input validation:

  • READ-ONLY functions accept flexible parameters
  • MUTATING and CREATIVE functions use .strict() schemas — unknown fields cause errors
  • Functions can accept _validateOnly: true to dry-run without writing

Validation errors return structured JSON with field-level feedback:

{
  "error": "Validation failed",
  "issues": [
    { "path": ["corners", 0, "verdict"], "message": "Required field" }
  ]
}

What AI Can and Cannot Do

Can do:

  • Query any data in your account (sessions, races, notepads, etc.)
  • Write coaching reports and readiness assessments
  • Create and manage race events, preparations, and track notes
  • Compare laps and generate race strategy
  • Compose multiple function calls for complex analysis

Cannot do:

  • Access other users’ data
  • Make network requests to external services
  • Access the filesystem
  • Run for more than 10 seconds
  • Delete your account or change settings