Add: gate ibmi-server MCP on required env vars

opencode's config schema types mcp.<name>.enabled as a static boolean and only interpolates {env:VAR} for string values, so conditional enablement is not expressible in opencode.jsonc alone.

Add a config-hook plugin that flips ibmi-server.enabled to true only when DB2i_HOST, DB2i_USER, and DB2i_PASS are all set. The config retains enabled: false as a fallback if the plugin fails to load.
This commit is contained in:
2026-07-29 20:39:24 -05:00
parent bfb6dcd4df
commit 28582d56c0
2 changed files with 147 additions and 111 deletions
+3 -2
View File
@@ -20,6 +20,7 @@
"sort *": "allow", "sort *": "allow",
"head *": "allow", "head *": "allow",
"tail *": "allow", "tail *": "allow",
"echo *": "allow",
"ls *": "allow", "ls *": "allow",
"wc *": "allow", "wc *": "allow",
"go build *": "allow", "go build *": "allow",
@@ -88,7 +89,7 @@
"--tools", "--tools",
"/home/colem/mcp/ibmi-mcp-server/tools/custom-tools.yaml", "/home/colem/mcp/ibmi-mcp-server/tools/custom-tools.yaml",
], ],
"enabled": true, "enabled": false,
"environment": { "environment": {
"DB2i_HOST": "{env:DB2i_HOST}", "DB2i_HOST": "{env:DB2i_HOST}",
"DB2i_USER": "{env:DB2i_USER}", "DB2i_USER": "{env:DB2i_USER}",
@@ -112,5 +113,5 @@
}, },
"share": "disabled", "share": "disabled",
"snapshot": true, "snapshot": true,
"plugin": ["opencode-glm-quota"], "plugin": ["opencode-glm-quota", "./plugin/ibmi-gate.ts"],
} }
+35
View File
@@ -0,0 +1,35 @@
/*
Gate the ibmi-mcp-server on its required environment variables.
opencode's config schema types `mcp.<name>.enabled` as a static boolean and
only interpolates `{env:VAR}` for string values (headers, environment
entries), never for the `enabled` flag. Conditionally enabling a server is
therefore not expressible in opencode.jsonc alone.
This plugin uses the `config` hook, which runs once at startup with the live
merged config (before MCP servers spawn), to flip `ibmi-server.enabled` to
`true` only when every required credential is present. The config keeps
`enabled: false` as a fallback default, so the server stays off if this
plugin fails to load.
*/
import type { Plugin } from "@opencode-ai/plugin";
// Credentials the ibmi-server MCP needs to connect. All must be non-empty.
const REQUIRED_ENV = ["DB2i_HOST", "DB2i_USER", "DB2i_PASS"] as const;
const SERVER = "ibmi-server";
export default (async () => {
return {
config: (cfg) => {
const ready = REQUIRED_ENV.every((name) => {
const value = process.env[name];
return typeof value === "string" && value.length > 0;
});
const server = cfg.mcp?.[SERVER];
if (server && typeof server === "object") {
(server as { enabled?: boolean }).enabled = ready;
}
},
};
}) satisfies Plugin;