Skip to content

Code Mode

Chris edited this page Jul 18, 2026 · 126 revisions

Code Mode: Advanced Multi-Step Autonomous Execution

Tools Resources Prompts
OAuth Code Mode

Value Proposition Empower autonomous agent capabilities by orchestrating complex, multi-step database operations within a unified, high-performance execution context. Significantly reduce network latency and maximize token efficiency. Read the full value proposition.


Comprehensive API Access

Execute database operations within a secure, high-performance sandbox.

⚡ Minimize Network Latency

// Get all tables and their row counts in one call
const tables = await mysql.core.listTables();
const results = [];
for (const t of tables.tables) {
  const count = await mysql.core.readQuery(
    `SELECT COUNT(*) as n FROM \`${t.name}\``,
  );
  results.push({ table: t.name, rows: count.rows[0].n });
}
return results;

API Reference

The mysql object exposes all other tool groups via the API. Use mysql.help() in Code Mode. This lists all available tools and categories:

Group Methods Example
mysql.core includes methods like readQuery, writeQuery, listTables, etc. mysql.core.readQuery("SELECT 1")
mysql.json includes JSON-specific methods mysql.json.extract(...)

Naming Conventions

Tool names strip prefixes and convert to camelCase. A key exception exists:

  • Core tools map to mysql.core
  • Non-core tool groups map to their respective namespace.
Tool Name API Call Alias
mysql_read_query mysql.core.readQuery(query) -
mysql_check_version mysql.core.checkVersion() -
mysql_json_extract mysql.json.extract(args) -

Positional Shorthand

Common tools accept positional arguments for convenience:

// These are equivalent:
await mysql.core.readQuery("SELECT * FROM users");
await mysql.core.readQuery({ query: "SELECT * FROM users" });

Built-in Help

// Full API overview
mysql.help();
// Group-specific help with examples
mysql.core.help();

Note: The complete Code Mode API reference is available to agents dynamically. Gotchas and tool parameter aliases are also included. Access these via the mysql://help and mysql://help/[group] resources.

TypeScript Types

The server automatically injects TypeScript definitions. This enables rich IntelliSense and validation. You write type-safe code validated before execution. Dynamic types enforce tool filters.

Introspecting Runtime Schemas

Agents can dynamically introspect the database schema during Code Mode execution. Use this to discover tables, columns, and relationships. Do this before constructing complex queries:

// Discover schema dynamically
const schema = await mysql.core.listTables();
const tableDetails = await mysql.core.describeTable(schema.tables[0].name);

// Alternatively, query INFORMATION_SCHEMA directly
const keys = await mysql.core.readQuery(`
  SELECT COLUMN_NAME, REFERENCED_TABLE_NAME 
  FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
  WHERE TABLE_NAME = '${schema.tables[0].name}'
`);

Token Efficiency

Code Mode replaces multiple sequential tool calls with one execution:

Approach Tool Calls Typical Tokens
Individual tools Multiple sequential calls Baseline usage
Code Mode Unified execution Substantial token savings

Tip

Maximize Token Savings: Instruct your AI agent to prefer Code Mode over individual tool calls:

"When using mysql-mcp, prefer mysql_execute_code for multi-step database operations. This choice minimizes token usage."

For maximum savings, use --tool-filter codemode to run Code Mode exclusively. See configuration details below.

MCP clients automatically receive server instructions during initialization. Your client might lack native MCP instruction support. If so, point your agents to the Tools wiki page. This ensures optimal Code Mode usage.

Telemetry Note: When using HTTP transport and the --metrics-export flag, metrics are exported via the Prometheus /metrics endpoint. This includes token usage, execution times, and sandbox telemetry.


Built-in Shortcuts

The system automatically includes Code Mode in curated presets. When filtering by individual tool groups, Code Mode must be added explicitly.

Deploying Code Mode Only

You can run with only Code Mode enabled. This single tool provides access to an extensive suite of capabilities. It uses the mysql.* API:

{
  "mcpServers": {
    "mysql-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "@neverinfamous/mysql-mcp",
        "--transport",
        "stdio",
        "--tool-filter",
        "codemode"
      ],
      "env": {
        "MYSQL_HOST": "localhost",
        "MYSQL_PORT": "3306",
        "MYSQL_USER": "your_user",
        "MYSQL_PASSWORD": "your_password",
        "MYSQL_DATABASE": "your_database",
        "CODEMODE_MAX_RESULT_SIZE": "102400"
      }
    }
  }
}

This streamlines the interface by exposing exclusively the mysql_execute_code capability. Agents seamlessly write JavaScript against our strictly typed mysql.* SDK, delivering exactly the required data in a single execution.

Explore Related Topics

MySQL MCP Documentation

Value Proposition Enforce strict execution boundaries and maximize LLM context efficiency for secure, autonomous database interactions. Read the full value proposition

🏠 Home


Launch Your Setup


Connect Ecosystem Tools


Security & Compliance


Scale Your Operations


Explore External Links

Clone this wiki locally