agentleFS
AliKaramiAliKarami/MikroMCP/CLAUDE.md

MikroMCP

CLAUDE.md71 starsChanged 3 months agoMITView on GitHub

For the full feature roadmap (milestones v0.4–v1.0, planned tools and subsystems) see ROADMAP.md. Run npm test before pushing a branch (runs vitest, tsc, and eslint). MCP clients speak JSON-RPC to MikroMCP over stdio or HTTP/SSE; MikroMCP routes each tool call through a per-router circuit breaker (and retry engine for read tools) before hitting the RouterOS REST API over HTTPS. Every tool lives in src/domain/tools/. Either add to an existing file or create a new one and export from index.ts.

  • Commits and pushes

The file

Show its rules and text

We split this CLAUDE.md into the rules it gives an agent

22 rules. About 0 are common, 8 are distinctive, and 14 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈8

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 14

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  • could be automatic: a setting could enforce it
  1. Very specific:

    Run npm test before pushing a branch (runs vitest, tsc, and eslint).

    line 18
  2. Very specific:

    Every tool lives in src/domain/tools/.

    line 41
  3. Distinctive:

    Every write tool must check for existing state before acting:

    line 109
  4. Very specific:

    Test groups follow: metadata → input schema → handler - <action>.

    line 202
  5. Distinctive:

    Use inline Zod schemas in schema tests so they don't depend on internal exports.

    line 208
  6. Distinctive:

    Before pushing, build both so a template or link error is caught locally:

    line 317
  7. Very specific:

    Keep CHANGELOG.md's [Unreleased] section current throughout development.

    line 328
  8. Distinctive:

    All work happens on branches — never commit directly to main.

    line 340
  9. Distinctive:

    Create a branch from main using the naming convention above.

    line 365
  10. Distinctive:

    Delete the branch after merge.

    line 369
  11. Distinctive:

    Run when merging a release PR into main:

    line 403
  12. Very specific:

    Update README version badge: version-v<X.Y.Z>.

    line 407
  13. Very specific:

    Update server.json — change BOTH the top-level "version" field AND packages[0].version to "X.Y.Z".

    line 408
  14. Very specific:

    Add the release to ROADMAP.md and mirror it in docs/wiki/Roadmap.md (flip 🔜 → ✅, or add a section for a release that had no planned …

    line 409
  15. Very specific:

    Update docs/wiki/Available-Tools.md for any new/changed tools, and site/src/data/content.ts (toolCount, features, examples) for anything …

    line 410
  16. Very specific:

    Commit on the release branch: chore: bump version to X.Y.Z.

    line 413
  17. Very specific:

    Don't add retry/circuit-breaker logic inside a tool handler — that's handled by tool-registry.ts

    line 421
  18. Very specific:

    Don't access credentials inside a tool — credentials are resolved by tool-registry.ts before calling the handler.

    line 422
  19. Distinctive:

    Don't log sensitive fields (passwords, auth headers)

    line 423
  20. Very specific:

    Don't use let for variables that are assigned once

    line 424
  21. Very specific:

    Don't use as any — use as unknown as T or refine the type

    line 425
  22. Very specific:

    Don't paginate with server-side limit/offset on RouterOS endpoints that don't support it — fetch all and paginate client-side (see …

    line 426

≈ “Common” is an estimate: at least 1 in 5 of the 12,729 CLAUDE.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
CLAUDE.md

MikroMCP — Claude Code Guide

For the full feature roadmap (milestones v0.4–v1.0, planned tools and subsystems) see ROADMAP.md.

Commands

npm run dev          # tsx watch hot-reload (development)
npm run build        # tsup → dist/main.js (ESM)
npm start            # run built server
npm test             # vitest run once
npm run test:watch   # vitest watch mode
npm run typecheck    # tsc --noEmit (no output, type checking only)
npm run lint         # eslint src/
npm run format       # prettier --write src/ test/

Run npm test before pushing a branch (runs vitest, tsc, and eslint).

Architecture in one sentence

MCP clients speak JSON-RPC to MikroMCP over stdio or HTTP/SSE; MikroMCP routes each tool call through a per-router circuit breaker (and retry engine for read tools) before hitting the RouterOS REST API over HTTPS.

Key file map

FileWhat it does
src/main.tsEntry point — loads config, wires transport
src/mcp/tool-registry.tsRegisters all tools with the MCP server; injects circuit breaker, retry, correlation ID, credentials
src/domain/tools/index.tsAdd new tool arrays here to expose them
src/domain/tools/tool-definition.tsToolDefinition, ToolContext, ToolResult interfaces
src/domain/errors/error-types.tsMikroMCPError, ErrorCategory enum, Recoverability
src/domain/errors/error-enricher.tsMaps HTTP/network errors to MikroMCPError
src/adapter/rest-client.tsRouterOSRestClient — get, getOne, create, update, remove, execute
src/config/app-config.tsReads MIKROMCP_* env vars
src/config/router-registry.tsLoads config/routers.yaml; getRouter(id) throws if missing
src/types.tsRouterConfig, RouterOSRecord, QueryOptions, Role

Adding a tool — the pattern

Every tool lives in src/domain/tools/. Either add to an existing file or create a new one and export from index.ts.

import { z } from "zod";
import type { ToolDefinition, ToolContext, ToolResult } from "./tool-definition.js";
import type { RouterOSRecord } from "../../types.js";
import { enrichError } from "../errors/error-enricher.js";
import { MikroMCPError, ErrorCategory } from "../errors/error-types.js";
import { createLogger } from "../../observability/logger.js";

const log = createLogger("my-tools");   // module name, not tool name

const myInputSchema = z.object({
  routerId: z.string().describe("Target router identifier from the router registry"),
  // all params with .describe() for AI clients
  limit: z.number().int().min(1).max(500).default(100).describe("..."),
  dryRun: z.boolean().default(false).describe("Preview changes without applying"),
}).strict();  // always .strict() — reject extra fields

const myTool: ToolDefinition = {
  name: "my_tool",
  title: "My Tool",
  description: "What this does. Describe idempotency and dry-run behavior.",
  inputSchema: myInputSchema,
  annotations: {
    readOnlyHint: true,      // true → auto-retry enabled in tool-registry
    destructiveHint: false,  // true → circuit breaker trips on failure
    idempotentHint: true,
    openWorldHint: false,
  },
  async handler(params: Record<string, unknown>, context: ToolContext): Promise<ToolResult> {
    const parsed = myInputSchema.parse(params);

    log.info({ routerId: context.routerId }, "Doing thing");

    try {
      const records = await context.routerClient.get<RouterOSRecord>("ros/path", {
        filter: { key: "value" },   // optional server-side filter
        limit: undefined,           // pass undefined to fetch all, paginate client-side
        offset: undefined,
      });

      return {
        content: `Found ${records.length} things.`,
        structuredContent: { routerId: context.routerId, records },
      };
    } catch (err) {
      throw enrichError(err, { routerId: context.routerId, tool: "my_tool" });
    }
  },
};

export const myTools: ToolDefinition[] = [myTool];

Then in src/domain/tools/index.ts:

import { myTools } from "./my-tools.js";

export const allTools: ToolDefinition[] = [
  ...systemTools, ...interfaceTools, ...ipTools,
  ...dhcpTools, ...routeTools, ...firewallTools,
  ...myTools,   // add here
];

Idempotency pattern (write tools)

Every write tool must check for existing state before acting:

// 1. Look up existing resource
const existing = await context.routerClient.get<RouterOSRecord>("path", {
  filter: { key: parsed.key },
});

// 2a. Exists and matches → return already_exists (not an error)
if (existing && sameConfig) {
  return { content: "Already exists.", structuredContent: { action: "already_exists", ... } };
}

// 2b. Exists but different config → throw CONFLICT
if (existing && differentConfig) {
  throw new MikroMCPError({
    category: ErrorCategory.CONFLICT,
    code: "MY_CONFLICT",
    message: "Exists with different config: ...",
    details: { existing: ..., requested: ... },
    recoverability: {
      retryable: false,
      suggestedAction: "Remove the existing resource first.",
      alternativeTools: ["remove_tool"],
    },
  });
}

// 3. Dry-run before create
if (parsed.dryRun) {
  return { content: "Dry run: would create ...", structuredContent: { action: "dry_run", diff } };
}

// 4. Create
const created = await context.routerClient.create("path", { key: value });
return { content: "Created.", structuredContent: { action: "created", route: created } };

Error handling rules

  • Always wrap the handler body in try/catch and call enrichError(err, context) in the catch — this maps HTTP/network errors to typed MikroMCPError.
  • Re-throw MikroMCPError as-is — never wrap it: if (err instanceof MikroMCPError) throw err;
  • Throw MikroMCPError directly for domain errors (NOT_FOUND, CONFLICT, VALIDATION) — don't throw raw Error.
  • enrichError is idempotent — safe to call on any error including ones already enriched.

RouterOSRestClient methods

get<T>(path: string, options?: { filter?, proplist?, limit?, offset? }): Promise<T[]>
getOne<T>(path: string, id: string): Promise<T>
create(path: string, data: Record<string, string>): Promise<RouterOSRecord>
update(path: string, id: string, data: Record<string, string>): Promise<void>
remove(path: string, id: string): Promise<void>
execute<T>(path: string, data?: Record<string, unknown>): Promise<T>

RouterOS record fields use kebab-case ("dst-address", "routing-table"). The special ID field is ".id". RouterOS sends everything as strings, but response-parser.ts converts records: "true"/"false" become real JS booleans and numeric strings become numbers (unsafe 64-bit integers stay strings to preserve precision). For boolean-ish fields, always use isTrue(value) from adapter/response-parser.js — it handles parsed booleans, raw "true"/"false", and "yes"/"no". For any other field, compare with sameValue(recordValue, desired) from the same module — never record.x === String(desired) (it silently never matches once the parser has turned the wire string into a number). Note RouterOSRecord values are typed string | number | boolean.

Code conventions

  • ESM with .js extensions in all imports, even for .ts source files: from "../../adapter/rest-client.js"
  • No comments explaining what the code does — only add a comment when the WHY is non-obvious (hidden constraint, RouterOS quirk, workaround)
  • No // Section header dividers unless the file is long enough to need navigation
  • Zod schemas always .strict() — never allow extra fields through
  • z.transform() is fine for normalisation (e.g., plain IP → CIDR /32)
  • Logger at module top: const log = createLogger("module-name"); — never create inside handler
  • Prettier config: semi: true, trailingComma: "all", singleQuote: false, printWidth: 100, tabWidth: 2
  • TypeScript strict mode — no any, no @ts-ignore

Test conventions

Unit tests live in test/unit/ mirroring src/ (file naming: <module>.test.ts) and run via npm test. Integration tests live in test/integration/ and run only via npm run test:integration against a live RouterOS CHR in Docker (see docs/wiki/Development.md) — never put a live-router test under test/unit/.

import { describe, it, expect, vi } from "vitest";
import { myTools } from "../../../src/domain/tools/my-tools.js";  // .js extension

const myTool = myTools[0];

// Build a minimal mock context
function makeContext(records: Record<string, unknown>[]): ToolContext {
  return {
    routerId: "test-router",
    correlationId: "test-corr",
    routerClient: {
      get: vi.fn().mockResolvedValue(records),
      create: vi.fn().mockResolvedValue({ ".id": "*1" }),
      remove: vi.fn().mockResolvedValue(undefined),
    } as unknown as RouterOSRestClient,
  };
}

Test groups follow: metadata → input schema → handler - <action>. Always test:

  • Correct tool count and names in the exported array
  • Correct annotations (especially readOnlyHint)
  • Input schema: valid input with defaults, rejection of extra fields, rejection of out-of-range values
  • Handler: happy path, idempotency (already_exists), conflict, dry-run, not-found

Use inline Zod schemas in schema tests so they don't depend on internal exports.

Transport

Two transports:

  • stdio (default): MCP client spawns the process, communicates over stdin/stdout
  • HTTP/SSE (MIKROMCP_TRANSPORT=http): stateless StreamableHTTPServerTransport, listens at POST /mcp and GET /mcp on MIKROMCP_PORT (default 3000)

Environment variables

VariableDefaultNotes
MIKROMCP_TRANSPORTstdiostdio or http
MIKROMCP_CONFIG_PATH~/.mikromcp/routers.yamlPath to router registry
MIKROMCP_DEFAULT_ROUTER—Router id used when a tool call omits routerId (falls back to the sole router if only one is configured)
MIKROMCP_LOG_LEVELinfotrace debug info warn error
MIKROMCP_PORT3000HTTP transport only
MIKROMCP_IDENTITIES_PATH~/.mikromcp/identities.yamlPath to identity/token registry
MIKROMCP_STDIO_IDENTITY—Named identity for stdio transport (omit for built-in superadmin)
MIKROMCP_CONFIRMATION_SECRET—HMAC secret for confirmation tokens (required in HTTP mode when identities with role readonly or operator are configured)
MIKROMCP_AUDIT_LOG_PATH—NDJSON audit log file path (omit to disable file sink)
MIKROMCP_SNAPSHOT_RETENTION_DAYS30Age in days after which config snapshots are pruned at startup
ROUTER_<PREFIX>_USER—Per-router credential (matches envPrefix in YAML)
ROUTER_<PREFIX>_PASS—Per-router credential

RouterOS paths (reference)

DataREST path
System identity/resourcesystem/identity, system/resource
System clocksystem/clock
System packagessystem/package
System scriptssystem/script
System schedulersystem/scheduler
Interfacesinterface
Bridge interfacesinterface/bridge, interface/bridge/port
WiFi interfacesinterface/wifi (ROS 7.13+) or interface/wireless
WireGuard interfacesinterface/wireguard, interface/wireguard/peers
IP addressesip/address
DNS settings / static entriesip/dns, ip/dns/static
Routesip/route
Routing rulesrouting/rule
Routing tablesrouting/table
BGP peersrouting/bgp/session
OSPF neighborsrouting/ospf/neighbor
DHCP leasesip/dhcp-server/lease
Firewall filterip/firewall/filter
Firewall NATip/firewall/nat
Firewall mangleip/firewall/mangle
Firewall address listsip/firewall/address-list
IPSec peers / policiesip/ipsec/peer, ip/ipsec/policy
Certificatescertificate
Users / groupsuser, user/group
Filesfile
Containerscontainer
System loglog
Ping (tool)tool/ping
Traceroute (tool)tool/traceroute
Torch (tool)tool/torch

Keeping documentation in sync

Documentation is updated in the same PR that ships the change — never as a follow-up.

Trigger → required doc updates

What changedDocs to update
New tool addeddocs/wiki/Available-Tools.md (add full parameter table + example prompt), README.md (tool count, Available Tools table row), CHANGELOG.md ([Unreleased] → Added)
Tool added/renamed/removed (skill sync)skills/mikromcp/references/tool-map.md (the lockstep test test/unit/skill/tool-map-sync.test.ts will fail until updated)
Tool added/renamed/removed (Available-Tools sync)docs/wiki/Available-Tools.md (the lockstep test test/unit/docs/available-tools-sync.test.ts will fail until updated)
Tool parameter changeddocs/wiki/Available-Tools.md (update parameter table — the lockstep test test/unit/docs/available-tools-params-sync.test.ts fails until it lists exactly the schema's parameters), CHANGELOG.md ([Unreleased] → Changed)
routers.yaml / identities.yaml schema changeddocs/wiki/Configuration.md and config/*.example.yaml (the lockstep test test/unit/docs/configuration-yaml-sync.test.ts loads every full YAML example on that page through the real registries)
Tool removed or renameddocs/wiki/Available-Tools.md (remove/rename entry), README.md (update table), CHANGELOG.md ([Unreleased] → Removed)
New transport or auth mechanismdocs/wiki/Architecture.md, docs/wiki/Connecting-to-AI-Assistants.md
New CLI command or install pathdocs/wiki/Getting-Started.md
Bug fix shippedCHANGELOG.md ([Unreleased] → Fixed) only; no wiki update needed unless user-facing behaviour changes
Milestone completedROADMAP.md (flip 🔜 → ✅), docs/wiki/Roadmap.md (mirror status), README.md (roadmap note)
Anything user-facingThe two websites — see Keeping the websites in sync below

Keeping the websites in sync

Two sites ship from this repo, and both are part of the same PR as the code change — never a follow-up:

SiteSourceDeployed to
Landing pagesite/ (Astro)https://mikromcp.com
Documentationdocs-site/ (Astro + Starlight)https://docs.mikromcp.com

docs.mikromcp.com is generated, not hand-written. Its pages come from docs/wiki/*.md via docs-site/scripts/sync-wiki.mjs, which runs automatically on predev/prebuild. Edit docs/wiki/ and never docs-site/src/content/docs/ — that directory is overwritten on every build. A new wiki page also needs an entry in WIKI_ORDER in sync-wiki.mjs.

Visual system. Everything derives from a four-stop cool ramp defined in site/src/styles/theme.css — --c1 teal #2DD4BF → --c2 cyan → --c3 blue → --c4 violet #8B5CF6, exposed as --grad-brand. Rules of thumb when adding to the page:

  • Reach for a token (--c1…--c4, --grad-brand, .grad-text, .panel, .panel-field) rather than a new hex value. docs-site/src/styles/custom.css mirrors the ramp; change both together.
  • Colour arrives as bounded fields — a full-bleed section background or a large rounded panel — not as a blurred glow floating behind text.
  • Illustration is inline SVG in site/src/components/Shapes.astro (fan, mesh, pipe), never an image asset. Astro's scoped styles do not reach inside a child component, so a figure must be wrapped in a host-owned positioned element: <div class="fan"><Shapes name="fan" /></div>. The svg itself is sized by the global .shape-svg rule.
  • Page rhythm alternates: colour field → plain ground → panel. Two colour fields in a row flattens the effect.

mikromcp.com has a single source of truth for product facts: site/src/data/content.ts. Tool count, version, feature list, example prompts and FAQ all live there, and site/src/pages/llms.txt.ts and llms-full.txt.ts generate the LLM-facing text files from it. Never hardcode a tool count or version in a component.

What changedSite updates required
Tool added/removedsite/src/data/content.ts → toolCount (the lockstep test test/unit/docs/tool-count-sync.test.ts fails until every listed file agrees)
New capability worth advertisingsite/src/data/content.ts → features, and an examples entry if there is a prompt a user would paste
Version bumpedNothing by hand — npm version runs scripts/sync-version.mjs, which regenerates both src/version.ts and site/src/data/version.ts. test/unit/docs/site-version-sync.test.ts catches it if the script was bypassed
Wiki page added/edited/renameddocs/wiki/*.md, plus WIKI_ORDER in docs-site/scripts/sync-wiki.mjs for a new page
Install path or CLI flow changedsite/src/components/QuickStart.astro, docs/wiki/Getting-Started.md
Milestone shippedROADMAP.md and docs/wiki/Roadmap.md — the docs site publishes the wiki copy, so a roadmap that stops at an old version is publicly visible

Before pushing, build both so a template or link error is caught locally:

npm --prefix site run build
npm --prefix docs-site run build

Roadmap files are deliberately excluded from tool-count-sync.test.ts: their counts are historical statements about past releases ("99 → 117 tools") and must not be rewritten to the current count.

CHANGELOG discipline

Keep CHANGELOG.md's [Unreleased] section current throughout development. Every PR that touches user-facing behaviour adds a line there. At release time, that section becomes the ## [X.Y.Z] entry — no archaeology required.

Available-Tools.md format

Each tool entry must have: section header with tool name and Read/Write/Destructive tag, one-line description, parameter table with Type / Default / Description columns, and an Example prompt that a user could actually paste.

Tool count in README.md and docs/wiki/Architecture.md must stay accurate.

Git development process

Branch conventions

All work happens on branches — never commit directly to main. main is always releasable.

Branch prefixUse for
feat/<short-description>New features or tools
fix/<short-description>Bug fixes
chore/<short-description>Version bumps, dependency updates, config, CI
docs/<short-description>Documentation-only changes
refactor/<short-description>Code restructuring with no behaviour change

Branch names use kebab-case: feat/wifi-scan-tool, fix/circuit-breaker-timeout.

Commit message conventions

Follow Conventional Commits:

<type>(<optional scope>): <short summary>

Types: feat, fix, chore, docs, refactor, test, perf.
Breaking changes: append ! after the type, e.g. feat!: rename tool parameter.

Pull request workflow

  1. Create a branch from main using the naming convention above.
  2. Make commits, run npm test before pushing.
  3. Open a PR targeting main. PR title must follow the same Conventional Commits format as the commit messages.
  4. Merge using squash merge so main history stays one-commit-per-feature.
  5. Delete the branch after merge.

Tag and release conventions

Tags follow strict semver: vMAJOR.MINOR.PATCH.

  • PATCH — bug fixes only, no new tools or breaking changes
  • MINOR — new tools, new features, backwards-compatible changes
  • MAJOR — breaking changes to config, tool names, or behaviour

Tags are created only on main after the release PR merges.

Changelog

CHANGELOG.md lives at the repo root and follows Keep a Changelog format.

  • Each release section covers only changes since the previous release — no cumulative history.
  • Sections within a release (use only those that apply): Added, Changed, Fixed, Removed, Deprecated, Security.
  • An [Unreleased] section at the top accumulates changes while a release is in progress.

Example entry:

## [1.1.0] - 2026-06-01

### Added
- `list_wifi_clients` tool — lists associated WiFi stations per interface
- `manage_wifi_interface` tool — enable/disable WiFi interfaces

### Fixed
- Circuit breaker no longer trips on 404 responses from read tools

Release checklist

Run when merging a release PR into main:

  1. Move all items from [Unreleased] in CHANGELOG.md to a new ## [X.Y.Z] - YYYY-MM-DD section.
  2. npm version <major|minor|patch> — bumps package.json + package-lock.json. (npm version auto-runs scripts/sync-version.mjs, which regenerates src/version.ts and site/src/data/version.ts, so the mikromcp.com footer follows the release.)
  3. Update README version badge: version-v<X.Y.Z>.
  4. Update server.json — change BOTH the top-level "version" field AND packages[0].version to "X.Y.Z".
  5. Add the release to ROADMAP.md and mirror it in docs/wiki/Roadmap.md (flip 🔜 → ✅, or add a section for a release that had no planned milestone). docs.mikromcp.com publishes the wiki copy.
  6. Update docs/wiki/Available-Tools.md for any new/changed tools, and site/src/data/content.ts (toolCount, features, examples) for anything worth advertising on the landing page.
  7. npm test — the lockstep tests fail if any doc, wiki page, or site file still states the old tool count or version.
  8. Build both sites: npm --prefix site run build && npm --prefix docs-site run build.
  9. Commit on the release branch: chore: bump version to X.Y.Z.
  10. Open PR, squash-merge into main.
  11. git tag vX.Y.Z && git push origin vX.Y.Z
  12. CI release.yml auto-runs: builds binaries, pushes Docker images, creates GitHub Release (uses the release section from CHANGELOG.md as release notes).
  13. Deploy the two sites (see site/DEPLOY.md and docs-site/DEPLOY.md).

What not to do

  • Don't add retry/circuit-breaker logic inside a tool handler — that's handled by tool-registry.ts
  • Don't access credentials inside a tool — credentials are resolved by tool-registry.ts before calling the handler; the handler gets an already-authenticated routerClient
  • Don't log sensitive fields (passwords, auth headers)
  • Don't use let for variables that are assigned once
  • Don't use as any — use as unknown as T or refine the type
  • Don't paginate with server-side limit/offset on RouterOS endpoints that don't support it — fetch all and paginate client-side (see list_routes for the pattern)

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.