Skip to content

Plugin Authoring

idapixl edited this page Sep 7, 2026 · 2 revisions

Plugin Authoring

Build custom plugins to extend cortex-engine with new tools.

Structure

tools-<name>/
├── src/
│   └── index.ts        # Plugin definition and tool handlers
├── package.json        # cortex-engine as peerDependency
├── tsconfig.json
├── README.md
└── tests/
    └── index.test.ts

Minimal Example

// src/index.ts
import type { ToolPlugin, ToolDefinition, ToolContext } from '@fozikio/cortex-engine';

const myCustomTool: ToolDefinition = {
  name: 'my_custom_tool',
  description: 'A tool that does something useful for the agent',
  // category and whenToUse are required — they drive tool discovery and the
  // [category] prefix the agent sees in the MCP description.
  category: 'meta',
  whenToUse: 'When you need to summarise what the graph already knows about an input',
  inputSchema: {
    type: 'object',
    properties: {
      input: {
        type: 'string',
        description: 'The input to process',
      },
    },
    required: ['input'],
  },
  // Handlers take (args, ctx) — the engine context, not a "cortex instance".
  handler: async (args: Record<string, unknown>, ctx: ToolContext) => {
    const input = args['input'] as string;
    const store = ctx.namespaces.getStore(ctx.namespaces.getDefaultNamespace());
    const related = await store.searchText(input, 10);

    return {
      processed: true,
      related_count: related.length,
      summary: `Found ${related.length} related memories`,
    };
  },
};

// The loader reads the *default* export and expects { name, tools }.
const myPlugin: ToolPlugin = {
  name: 'my-plugin',
  tools: [myCustomTool],
};

export default myPlugin;

package.json

{
  "name": "@fozikio/tools-my-plugin",
  "version": "0.1.0",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "peerDependencies": {
    "cortex-engine": ">=0.4.0"
  },
  "scripts": {
    "build": "tsc",
    "test": "vitest"
  }
}

Using Firestore

Plugins access Firestore through the cortex instance:

handler: async (cortex, params) => {
  const db = cortex.firestore;

  // Read from a custom collection
  const doc = await db.collection('my_collection').doc(params.id).get();

  // Write
  await db.collection('my_collection').add({
    content: params.input,
    created_at: new Date(),
  });

  return { success: true };
}

Conventions

Follow these patterns for consistency with the official plugin ecosystem:

  1. Naming: tools-<name> for the repo, @fozikio/tools-<name> for the npm package
  2. Tool names: Use snake_case — e.g., thread_create, journal_write
  3. Return values: Always return an object, never a bare string or number
  4. Descriptions: Write tool descriptions for LLMs — be clear about when to use the tool and what it returns
  5. Error handling: Throw descriptive errors, don't return error objects

Official Plugins

Reference implementations to study:

Plugin Tools Complexity
tools-vitals 2 Simple — good starting point
tools-journal 2 Simple — read/write pattern
tools-threads 4 Medium — CRUD with state
tools-graph 4 Advanced — graph analysis
tools-reasoning 6 Advanced — multi-step cognition

Publishing

npm run build
npm publish --access public

Then tell people to install it:

npm install @fozikio/tools-my-plugin

And register it:

// A plugin is loaded by package specifier, not by importing it yourself.
// List it under `plugins` in the cortex config; the engine imports the
// package and reads its default export:

//   cortex:
//     plugins:
//       - "@fozikio/tools-my-plugin"

// The loader only trusts packages resolved from node_modules under
// @fozikio/* or cortex-*, so a local plugin must be linked there
// (npm link / workspace) rather than loaded from a relative path.

Clone this wiki locally