Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
75 lines (67 loc) · 3.36 KB
/
Copy pathindex.ts
File metadata and controls
75 lines (67 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) 2026, MariaDB plc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 2.0,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
// the GNU General Public License, version 2.0, for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
// MariaDB dev extension for the Pi coding agent (pi.dev), Pi 1.0 or later.
//
// Two moving parts make up this plugin:
// 1. Skills — vendored under ./skills and declared via the package.json `pi`
// field, so pi loads them contextually. This extension does not touch them.
// 2. The native mariadb-shell MCP server — registered here with Pi's built-in
// MCP support (`pi.registerMcpServer`), so installing the package is all it
// takes. The launcher script finds or installs a suitable mariadb-shell.
//
// Registrations are per session and not written to any mcp.json, so `pi mcp list`
// (which loads no extensions) does not show the server; `/mcp` inside pi does. A
// `mariadb` entry in ~/.pi/agent/mcp.json or .pi/mcp.json takes precedence over
// this one, which is how a user overrides the command or its environment.
//
// It also keeps the agent from using the MariaDB/MySQL command-line clients for
// what the MCP tools do (see ./cli-guard.ts): the system prompt says to use the
// tools, and a bash or powershell call that uses a client for such a task is
// blocked with the tool to call instead. Starting pi with
// MARIADB_PLUGIN_ALLOW_CLI=1 turns both off.
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { SYSTEM_PROMPT_RULE, blockReason, findClient, guardDisabled } from "./cli-guard.ts";
const MCP_SERVER_NAME = "mariadb";
// Plugin root = one level up from src/. Resolved from import.meta.url so it is
// correct wherever pi installs the package.
const PLUGIN_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const LAUNCHER = join(
PLUGIN_ROOT,
"scripts",
process.platform === "win32" ? "mariadb-mcp-launcher.cmd" : "mariadb-mcp-launcher.sh",
);
export default function (pi: ExtensionAPI) {
pi.registerMcpServer(MCP_SERVER_NAME, {
command: LAUNCHER,
args: [],
description:
"MariaDB Shell: connect to MariaDB servers, run SQL, inspect schemas, " +
"manage sandbox instances, versioned schemas (MSM) and MySQL migrations",
});
if (guardDisabled()) return;
pi.on("before_agent_start", (event) => {
const guidelines = (event.systemPromptOptions.promptGuidelines ??= []);
if (!guidelines.includes(SYSTEM_PROMPT_RULE)) guidelines.push(SYSTEM_PROMPT_RULE);
});
pi.on("tool_call", (event) => {
if (event.toolName !== "bash" && event.toolName !== "powershell") return;
const command = (event.input as { command?: unknown }).command;
if (typeof command !== "string") return;
const finding = findClient(command);
if (finding) return { block: true, reason: blockReason(finding) };
});
}