cognitive-modules-cli 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,403 @@
1
+ /**
2
+ * Cognitive Modules MCP Server
3
+ *
4
+ * Provides MCP (Model Context Protocol) interface for Claude Code, Cursor, etc.
5
+ *
6
+ * Start with:
7
+ * cog mcp
8
+ */
9
+
10
+ import { Server } from '@modelcontextprotocol/sdk/server/index.js';
11
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
12
+ import {
13
+ CallToolRequestSchema,
14
+ ListToolsRequestSchema,
15
+ ListResourcesRequestSchema,
16
+ ReadResourceRequestSchema,
17
+ ListPromptsRequestSchema,
18
+ GetPromptRequestSchema,
19
+ } from '@modelcontextprotocol/sdk/types.js';
20
+
21
+ import { loadModule, findModule, listModules, getDefaultSearchPaths } from '../modules/loader.js';
22
+ import { runModule } from '../modules/runner.js';
23
+ import { getProvider } from '../providers/index.js';
24
+ import type { CognitiveModule, ModuleResult } from '../types.js';
25
+
26
+ // =============================================================================
27
+ // Server Setup
28
+ // =============================================================================
29
+
30
+ const server = new Server(
31
+ {
32
+ name: 'cognitive-modules',
33
+ version: '1.3.0',
34
+ },
35
+ {
36
+ capabilities: {
37
+ tools: {},
38
+ resources: {},
39
+ prompts: {},
40
+ },
41
+ }
42
+ );
43
+
44
+ const cwd = process.cwd();
45
+ const searchPaths = getDefaultSearchPaths(cwd);
46
+
47
+ // =============================================================================
48
+ // Tools
49
+ // =============================================================================
50
+
51
+ server.setRequestHandler(ListToolsRequestSchema, async () => {
52
+ return {
53
+ tools: [
54
+ {
55
+ name: 'cognitive_run',
56
+ description: 'Run a Cognitive Module to get structured AI analysis results',
57
+ inputSchema: {
58
+ type: 'object',
59
+ properties: {
60
+ module: {
61
+ type: 'string',
62
+ description: 'Module name, e.g. "code-reviewer", "task-prioritizer"',
63
+ },
64
+ args: {
65
+ type: 'string',
66
+ description: 'Input arguments, e.g. code snippet or task list',
67
+ },
68
+ provider: {
69
+ type: 'string',
70
+ description: 'LLM provider (optional), e.g. "openai", "anthropic"',
71
+ },
72
+ model: {
73
+ type: 'string',
74
+ description: 'Model name (optional), e.g. "gpt-4o", "claude-3-5-sonnet"',
75
+ },
76
+ },
77
+ required: ['module', 'args'],
78
+ },
79
+ },
80
+ {
81
+ name: 'cognitive_list',
82
+ description: 'List all installed Cognitive Modules',
83
+ inputSchema: {
84
+ type: 'object',
85
+ properties: {},
86
+ },
87
+ },
88
+ {
89
+ name: 'cognitive_info',
90
+ description: 'Get detailed information about a Cognitive Module',
91
+ inputSchema: {
92
+ type: 'object',
93
+ properties: {
94
+ module: {
95
+ type: 'string',
96
+ description: 'Module name',
97
+ },
98
+ },
99
+ required: ['module'],
100
+ },
101
+ },
102
+ ],
103
+ };
104
+ });
105
+
106
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
107
+ const { name, arguments: args } = request.params;
108
+
109
+ try {
110
+ switch (name) {
111
+ case 'cognitive_run': {
112
+ const { module: moduleName, args: inputArgs, provider: providerName, model } = args as {
113
+ module: string;
114
+ args: string;
115
+ provider?: string;
116
+ model?: string;
117
+ };
118
+
119
+ // Find module
120
+ const moduleData = await findModule(moduleName, searchPaths);
121
+ if (!moduleData) {
122
+ return {
123
+ content: [
124
+ {
125
+ type: 'text',
126
+ text: JSON.stringify({ ok: false, error: `Module '${moduleName}' not found` }),
127
+ },
128
+ ],
129
+ };
130
+ }
131
+
132
+ // Create provider
133
+ const provider = getProvider(providerName, model);
134
+
135
+ // Run module
136
+ const result = await runModule(moduleData, provider, {
137
+ input: { query: inputArgs, code: inputArgs },
138
+ useV22: true,
139
+ });
140
+
141
+ return {
142
+ content: [
143
+ {
144
+ type: 'text',
145
+ text: JSON.stringify(result, null, 2),
146
+ },
147
+ ],
148
+ };
149
+ }
150
+
151
+ case 'cognitive_list': {
152
+ const modules = await listModules(searchPaths);
153
+ return {
154
+ content: [
155
+ {
156
+ type: 'text',
157
+ text: JSON.stringify(
158
+ {
159
+ modules: modules.map((m) => ({
160
+ name: m.name,
161
+ location: m.location,
162
+ format: m.format,
163
+ tier: m.tier,
164
+ })),
165
+ count: modules.length,
166
+ },
167
+ null,
168
+ 2
169
+ ),
170
+ },
171
+ ],
172
+ };
173
+ }
174
+
175
+ case 'cognitive_info': {
176
+ const { module: moduleName } = args as { module: string };
177
+
178
+ const moduleData = await findModule(moduleName, searchPaths);
179
+ if (!moduleData) {
180
+ return {
181
+ content: [
182
+ {
183
+ type: 'text',
184
+ text: JSON.stringify({ ok: false, error: `Module '${moduleName}' not found` }),
185
+ },
186
+ ],
187
+ };
188
+ }
189
+
190
+ return {
191
+ content: [
192
+ {
193
+ type: 'text',
194
+ text: JSON.stringify(
195
+ {
196
+ ok: true,
197
+ name: moduleData.name,
198
+ version: moduleData.version,
199
+ responsibility: moduleData.responsibility,
200
+ tier: moduleData.tier,
201
+ format: moduleData.format,
202
+ inputSchema: moduleData.inputSchema,
203
+ outputSchema: moduleData.outputSchema,
204
+ },
205
+ null,
206
+ 2
207
+ ),
208
+ },
209
+ ],
210
+ };
211
+ }
212
+
213
+ default:
214
+ return {
215
+ content: [
216
+ {
217
+ type: 'text',
218
+ text: JSON.stringify({ ok: false, error: `Unknown tool: ${name}` }),
219
+ },
220
+ ],
221
+ };
222
+ }
223
+ } catch (error) {
224
+ return {
225
+ content: [
226
+ {
227
+ type: 'text',
228
+ text: JSON.stringify({
229
+ ok: false,
230
+ error: error instanceof Error ? error.message : String(error),
231
+ }),
232
+ },
233
+ ],
234
+ };
235
+ }
236
+ });
237
+
238
+ // =============================================================================
239
+ // Resources
240
+ // =============================================================================
241
+
242
+ server.setRequestHandler(ListResourcesRequestSchema, async () => {
243
+ const modules = await listModules(searchPaths);
244
+
245
+ return {
246
+ resources: [
247
+ {
248
+ uri: 'cognitive://modules',
249
+ name: 'All Modules',
250
+ description: 'List of all installed Cognitive Modules',
251
+ mimeType: 'application/json',
252
+ },
253
+ ...modules.map((m) => ({
254
+ uri: `cognitive://module/${m.name}`,
255
+ name: m.name,
256
+ description: m.responsibility || `Cognitive Module: ${m.name}`,
257
+ mimeType: 'text/markdown',
258
+ })),
259
+ ],
260
+ };
261
+ });
262
+
263
+ server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
264
+ const { uri } = request.params;
265
+
266
+ if (uri === 'cognitive://modules') {
267
+ const modules = await listModules(searchPaths);
268
+ return {
269
+ contents: [
270
+ {
271
+ uri,
272
+ mimeType: 'application/json',
273
+ text: JSON.stringify(modules.map((m) => m.name), null, 2),
274
+ },
275
+ ],
276
+ };
277
+ }
278
+
279
+ const match = uri.match(/^cognitive:\/\/module\/(.+)$/);
280
+ if (match) {
281
+ const moduleName = match[1];
282
+ const moduleData = await findModule(moduleName, searchPaths);
283
+
284
+ if (!moduleData) {
285
+ return {
286
+ contents: [
287
+ {
288
+ uri,
289
+ mimeType: 'text/plain',
290
+ text: `Module '${moduleName}' not found`,
291
+ },
292
+ ],
293
+ };
294
+ }
295
+
296
+ return {
297
+ contents: [
298
+ {
299
+ uri,
300
+ mimeType: 'text/markdown',
301
+ text: moduleData.prompt,
302
+ },
303
+ ],
304
+ };
305
+ }
306
+
307
+ return {
308
+ contents: [
309
+ {
310
+ uri,
311
+ mimeType: 'text/plain',
312
+ text: `Unknown resource: ${uri}`,
313
+ },
314
+ ],
315
+ };
316
+ });
317
+
318
+ // =============================================================================
319
+ // Prompts
320
+ // =============================================================================
321
+
322
+ server.setRequestHandler(ListPromptsRequestSchema, async () => {
323
+ return {
324
+ prompts: [
325
+ {
326
+ name: 'code_review',
327
+ description: 'Generate a code review prompt',
328
+ arguments: [
329
+ {
330
+ name: 'code',
331
+ description: 'The code to review',
332
+ required: true,
333
+ },
334
+ ],
335
+ },
336
+ {
337
+ name: 'task_prioritize',
338
+ description: 'Generate a task prioritization prompt',
339
+ arguments: [
340
+ {
341
+ name: 'tasks',
342
+ description: 'The tasks to prioritize',
343
+ required: true,
344
+ },
345
+ ],
346
+ },
347
+ ],
348
+ };
349
+ });
350
+
351
+ server.setRequestHandler(GetPromptRequestSchema, async (request) => {
352
+ const { name, arguments: args } = request.params;
353
+
354
+ switch (name) {
355
+ case 'code_review': {
356
+ const code = args?.code ?? '';
357
+ return {
358
+ messages: [
359
+ {
360
+ role: 'user',
361
+ content: {
362
+ type: 'text',
363
+ text: `Please use the cognitive_run tool to review the following code:\n\n\`\`\`\n${code}\n\`\`\`\n\nCall: cognitive_run("code-reviewer", "${code.slice(0, 100)}...")`,
364
+ },
365
+ },
366
+ ],
367
+ };
368
+ }
369
+
370
+ case 'task_prioritize': {
371
+ const tasks = args?.tasks ?? '';
372
+ return {
373
+ messages: [
374
+ {
375
+ role: 'user',
376
+ content: {
377
+ type: 'text',
378
+ text: `Please use the cognitive_run tool to prioritize the following tasks:\n\n${tasks}\n\nCall: cognitive_run("task-prioritizer", "${tasks}")`,
379
+ },
380
+ },
381
+ ],
382
+ };
383
+ }
384
+
385
+ default:
386
+ throw new Error(`Unknown prompt: ${name}`);
387
+ }
388
+ });
389
+
390
+ // =============================================================================
391
+ // Server Start
392
+ // =============================================================================
393
+
394
+ export async function serve(): Promise<void> {
395
+ const transport = new StdioServerTransport();
396
+ await server.connect(transport);
397
+ console.error('Cognitive Modules MCP Server started');
398
+ }
399
+
400
+ // Allow running directly
401
+ if (import.meta.url === `file://${process.argv[1]}`) {
402
+ serve().catch(console.error);
403
+ }
@@ -4,3 +4,4 @@
4
4
 
5
5
  export * from './loader.js';
6
6
  export * from './runner.js';
7
+ export * from './subagent.js';
@@ -131,11 +131,17 @@ async function loadModuleV2(modulePath: string): Promise<CognitiveModule> {
131
131
  schema_output_alias: (compatRaw.schema_output_alias as 'data' | 'output') ?? 'data'
132
132
  };
133
133
 
134
- // Parse meta config (including risk_rule)
134
+ // Parse meta config (including risk_rule) with validation
135
135
  const metaRaw = (manifest.meta as Record<string, unknown>) || {};
136
+ const rawRiskRule = metaRaw.risk_rule as string | undefined;
137
+ const validRiskRules = ['max_changes_risk', 'max_issues_risk', 'explicit'];
138
+ const validatedRiskRule = rawRiskRule && validRiskRules.includes(rawRiskRule)
139
+ ? rawRiskRule as 'max_changes_risk' | 'max_issues_risk' | 'explicit'
140
+ : undefined;
141
+
136
142
  const metaConfig: MetaConfig = {
137
143
  required: metaRaw.required as string[] | undefined,
138
- risk_rule: metaRaw.risk_rule as 'max_changes_risk' | 'max_issues_risk' | 'explicit' | undefined,
144
+ risk_rule: validatedRiskRule,
139
145
  };
140
146
 
141
147
  return {