Repository navigation
Plugin Authoring
idapixl edited this page Sep 7, 2026
·
2 revisions
Build custom plugins to extend cortex-engine with new tools.
tools-<name>/
├── src/
│ └── index.ts # Plugin definition and tool handlers
├── package.json # cortex-engine as peerDependency
├── tsconfig.json
├── README.md
└── tests/
└── index.test.ts
// 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;{
"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"
}
}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 };
}Follow these patterns for consistency with the official plugin ecosystem:
-
Naming:
tools-<name>for the repo,@fozikio/tools-<name>for the npm package -
Tool names: Use
snake_case— e.g.,thread_create,journal_write - Return values: Always return an object, never a bare string or number
- Descriptions: Write tool descriptions for LLMs — be clear about when to use the tool and what it returns
- Error handling: Throw descriptive errors, don't return error objects
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 |
npm run build
npm publish --access publicThen tell people to install it:
npm install @fozikio/tools-my-pluginAnd 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.