@husar.ai/cli 0.3.0 → 0.3.3
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.
- package/dist/functions/generate.js +3 -1
- package/dist/functions/generate.js.map +1 -1
- package/dist/mcp.js +484 -1
- package/dist/mcp.js.map +1 -1
- package/dist/zeus/const.js +0 -100
- package/dist/zeus/const.js.map +1 -1
- package/dist/zeus/index.d.ts +0 -456
- package/dist/zeus/index.js +0 -5
- package/dist/zeus/index.js.map +1 -1
- package/package.json +4 -2
- package/src/functions/generate.ts +3 -2
- package/src/mcp.ts +545 -1
- package/src/zeus/const.ts +0 -100
- package/src/zeus/index.ts +0 -456
package/src/mcp.ts
CHANGED
|
@@ -8,9 +8,137 @@ import { parser } from './functions/parser.js';
|
|
|
8
8
|
import { generateCms } from './functions/generate.js';
|
|
9
9
|
import path from 'path';
|
|
10
10
|
import { HusarConfigType } from '@/types/config.js';
|
|
11
|
+
import {
|
|
12
|
+
buildClientSchema,
|
|
13
|
+
printSchema,
|
|
14
|
+
buildSchema,
|
|
15
|
+
GraphQLSchema,
|
|
16
|
+
isObjectType,
|
|
17
|
+
getNamedType,
|
|
18
|
+
isNonNullType,
|
|
19
|
+
isListType,
|
|
20
|
+
GraphQLObjectType,
|
|
21
|
+
} from 'graphql';
|
|
22
|
+
import { getOrmToolSpecs } from '@husar.ai/genai';
|
|
11
23
|
|
|
12
24
|
const server = new McpServer({ name: 'mcp-husar', version: '1.0.0' });
|
|
13
25
|
|
|
26
|
+
// Minimal GraphQL introspection query compatible with GraphQL 16
|
|
27
|
+
const INTROSPECTION_QUERY = `
|
|
28
|
+
query IntrospectionQuery {
|
|
29
|
+
__schema {
|
|
30
|
+
queryType { name }
|
|
31
|
+
mutationType { name }
|
|
32
|
+
subscriptionType { name }
|
|
33
|
+
types {
|
|
34
|
+
kind
|
|
35
|
+
name
|
|
36
|
+
description
|
|
37
|
+
fields(includeDeprecated: true) {
|
|
38
|
+
name
|
|
39
|
+
description
|
|
40
|
+
args { name description type { kind name ofType { kind name ofType { kind name } } } defaultValue }
|
|
41
|
+
type { kind name ofType { kind name ofType { kind name } } }
|
|
42
|
+
isDeprecated
|
|
43
|
+
deprecationReason
|
|
44
|
+
}
|
|
45
|
+
inputFields { name description type { kind name ofType { kind name ofType { kind name } } } defaultValue }
|
|
46
|
+
interfaces { kind name ofType { kind name ofType { kind name } } }
|
|
47
|
+
enumValues(includeDeprecated: true) { name description isDeprecated deprecationReason }
|
|
48
|
+
possibleTypes { kind name ofType { kind name ofType { kind name } } }
|
|
49
|
+
}
|
|
50
|
+
directives { name description locations args { name description type { kind name ofType { kind name ofType { kind name } } } defaultValue } }
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
`;
|
|
54
|
+
|
|
55
|
+
// Heuristic, local-only query suggester based on SDL and a text task
|
|
56
|
+
function tokenize(s: string): string[] {
|
|
57
|
+
return (s || '')
|
|
58
|
+
.toLowerCase()
|
|
59
|
+
.replace(/[^a-z0-9_]+/g, ' ')
|
|
60
|
+
.split(' ')
|
|
61
|
+
.filter(Boolean);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function scoreField(taskTokens: string[], fieldName: string, returnTypeName: string, argNames: string[]): number {
|
|
65
|
+
const nameTokens = tokenize(fieldName).concat(tokenize(returnTypeName)).concat(argNames.flatMap(tokenize));
|
|
66
|
+
let score = 0;
|
|
67
|
+
for (const t of taskTokens) if (nameTokens.includes(t)) score += 2;
|
|
68
|
+
// lightweight substring bonus
|
|
69
|
+
for (const t of taskTokens) if (fieldName.includes(t)) score += 1;
|
|
70
|
+
return score;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function pickFieldsForSelection(type: GraphQLObjectType): string[] {
|
|
74
|
+
const fields = Object.values(type.getFields());
|
|
75
|
+
const preferred = ['id', '_id', 'slug', 'name', 'title', 'key', 'display', 'createdAt', 'updatedAt'];
|
|
76
|
+
const names = fields.map((f) => f.name);
|
|
77
|
+
const out: string[] = [];
|
|
78
|
+
for (const p of preferred) if (names.includes(p)) out.push(p);
|
|
79
|
+
for (const n of names) if (out.length < 6 && !out.includes(n)) out.push(n);
|
|
80
|
+
return out.slice(0, 6);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function variablesTemplateFromArgs(args: readonly { name: string; type: unknown }[]): Record<string, unknown> {
|
|
84
|
+
const tmpl: Record<string, unknown> = {};
|
|
85
|
+
for (const a of args) tmpl[a.name] = null;
|
|
86
|
+
return tmpl;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function unwrapNamed(type: any): { named: any; list: boolean } {
|
|
90
|
+
let t = type;
|
|
91
|
+
let list = false;
|
|
92
|
+
while (isNonNullType(t) || isListType(t)) {
|
|
93
|
+
if (isListType(t)) list = true;
|
|
94
|
+
t = (t as any).ofType;
|
|
95
|
+
}
|
|
96
|
+
return { named: t, list };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function suggestQueryFromSDL({ sdl, task, rootType = 'Query' }: { sdl: string; task: string; rootType?: string }) {
|
|
100
|
+
const schema: GraphQLSchema = buildSchema(sdl);
|
|
101
|
+
const root = schema.getType(rootType);
|
|
102
|
+
if (!root || !isObjectType(root)) throw new Error(`Root type ${rootType} not found or not an object`);
|
|
103
|
+
const fields = Object.values(root.getFields());
|
|
104
|
+
const taskTokens = tokenize(task);
|
|
105
|
+
let best = fields[0];
|
|
106
|
+
let bestScore = -Infinity;
|
|
107
|
+
for (const f of fields) {
|
|
108
|
+
const { named } = unwrapNamed(f.type as any);
|
|
109
|
+
const retName = (named as any).name || '';
|
|
110
|
+
const s = scoreField(
|
|
111
|
+
taskTokens,
|
|
112
|
+
f.name,
|
|
113
|
+
retName,
|
|
114
|
+
f.args.map((a) => a.name),
|
|
115
|
+
);
|
|
116
|
+
if (s > bestScore) {
|
|
117
|
+
best = f;
|
|
118
|
+
bestScore = s;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
const argsDecl = best.args.map((a) => `$${a.name}: ${(a.type as any).toString()}`).join(', ');
|
|
122
|
+
const argsUse = best.args.map((a) => `${a.name}: $${a.name}`).join(', ');
|
|
123
|
+
const { named, list } = unwrapNamed(best.type as any);
|
|
124
|
+
let selection = '__typename';
|
|
125
|
+
if (isObjectType(named)) {
|
|
126
|
+
const sel = pickFieldsForSelection(named as GraphQLObjectType);
|
|
127
|
+
selection = sel.join('\n ');
|
|
128
|
+
}
|
|
129
|
+
const opName = 'GeneratedQuery';
|
|
130
|
+
const query = `query ${opName}${argsDecl ? `(${argsDecl})` : ''} {
|
|
131
|
+
${best.name}${argsUse ? `(${argsUse})` : ''} {
|
|
132
|
+
${list ? `nodes { ${selection} }` : selection}
|
|
133
|
+
}
|
|
134
|
+
}`;
|
|
135
|
+
const variables = variablesTemplateFromArgs(best.args);
|
|
136
|
+
const candidates = fields
|
|
137
|
+
.map((f) => ({ name: f.name, type: (getNamedType(f.type as any) as any)?.name || String(f.type) }))
|
|
138
|
+
.slice(0, 20);
|
|
139
|
+
return { query, variables, pickedField: best.name, rootType, candidates };
|
|
140
|
+
}
|
|
141
|
+
|
|
14
142
|
const getConfig = async (pathToProject?: string) => {
|
|
15
143
|
return new ConfigMaker<HusarConfigType>('husar', {
|
|
16
144
|
decoders: {},
|
|
@@ -88,7 +216,7 @@ const redirectConsoleToStderr = () => {
|
|
|
88
216
|
};
|
|
89
217
|
};
|
|
90
218
|
|
|
91
|
-
const createCmsParseFileHandler = ({ timeoutMs =
|
|
219
|
+
const createCmsParseFileHandler = ({ timeoutMs = 300_000 }: { timeoutMs?: number }) => {
|
|
92
220
|
return async ({ path: file, type, name: passed, workspaceRoot }: CmsParseFileInput) => {
|
|
93
221
|
const restoreConsole = redirectConsoleToStderr();
|
|
94
222
|
try {
|
|
@@ -179,6 +307,422 @@ export const startMcpServer = async () => {
|
|
|
179
307
|
}
|
|
180
308
|
}) as any);
|
|
181
309
|
|
|
310
|
+
// Tool: husar_graphql_query (read-only helper)
|
|
311
|
+
const husarGraphQLQueryInput = {
|
|
312
|
+
query: z.string().min(1).describe('GraphQL query string.'),
|
|
313
|
+
variables: z
|
|
314
|
+
.union([z.string(), z.record(z.any())])
|
|
315
|
+
.optional()
|
|
316
|
+
.describe('Optional variables as JSON string or object.'),
|
|
317
|
+
endpointPath: z
|
|
318
|
+
.string()
|
|
319
|
+
.optional()
|
|
320
|
+
.default('api/graphql')
|
|
321
|
+
.describe('Endpoint path relative to host, defaults to api/graphql'),
|
|
322
|
+
workspaceRoot: z.string().optional().describe('Location of husar.json if not in current working directory.'),
|
|
323
|
+
} as const;
|
|
324
|
+
const husarGraphQLQuerySchema = z.object(husarGraphQLQueryInput);
|
|
325
|
+
|
|
326
|
+
server.tool(
|
|
327
|
+
'husar_graphql_query',
|
|
328
|
+
'Run a read-only GraphQL query against the configured Husar host',
|
|
329
|
+
husarGraphQLQueryInput,
|
|
330
|
+
(async (args: unknown) => {
|
|
331
|
+
try {
|
|
332
|
+
const { query, variables, endpointPath, workspaceRoot } = husarGraphQLQuerySchema.parse(args ?? {});
|
|
333
|
+
const cfg = await getConfig(workspaceRoot);
|
|
334
|
+
const raw = cfg.get();
|
|
335
|
+
const host = raw.host;
|
|
336
|
+
const token = raw.adminToken;
|
|
337
|
+
if (!host) throw new Error('Missing host in husar.json');
|
|
338
|
+
if (!token) throw new Error('Missing adminToken in husar.json');
|
|
339
|
+
const url = new URL(endpointPath || 'api/graphql', host).toString();
|
|
340
|
+
const vars =
|
|
341
|
+
typeof variables === 'string' && variables.trim().length ? JSON.parse(variables) : (variables ?? {});
|
|
342
|
+
const res = await fetch(url, {
|
|
343
|
+
method: 'POST',
|
|
344
|
+
headers: { 'Content-Type': 'application/json', husar_token: token },
|
|
345
|
+
body: JSON.stringify({ query, variables: vars }),
|
|
346
|
+
});
|
|
347
|
+
const json = await res.json();
|
|
348
|
+
return { content: [{ type: 'text', text: JSON.stringify(json, null, 2) }] };
|
|
349
|
+
} catch (err) {
|
|
350
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
351
|
+
const stack = err instanceof Error && err.stack ? `\nStack: ${err.stack}` : '';
|
|
352
|
+
return { isError: true, content: [{ type: 'text', text: `Error: ${message}${stack}` }] };
|
|
353
|
+
}
|
|
354
|
+
}) as any,
|
|
355
|
+
);
|
|
356
|
+
|
|
357
|
+
// Tool: husar_graphql_schema (admin)
|
|
358
|
+
const husarGraphQLSchemaInput = {
|
|
359
|
+
endpointPath: z
|
|
360
|
+
.string()
|
|
361
|
+
.optional()
|
|
362
|
+
.default('api/graphql')
|
|
363
|
+
.describe('Endpoint path relative to host, defaults to api/graphql'),
|
|
364
|
+
workspaceRoot: z.string().optional().describe('Location of husar.json if not in current working directory.'),
|
|
365
|
+
} as const;
|
|
366
|
+
const husarGraphQLSchemaSchema = z.object(husarGraphQLSchemaInput);
|
|
367
|
+
|
|
368
|
+
server.tool(
|
|
369
|
+
'husar_graphql_schema',
|
|
370
|
+
'Fetch GraphQL schema (introspection) from Admin API',
|
|
371
|
+
husarGraphQLSchemaInput,
|
|
372
|
+
(async (args: unknown) => {
|
|
373
|
+
try {
|
|
374
|
+
const { endpointPath, workspaceRoot } = husarGraphQLSchemaSchema.parse(args ?? {});
|
|
375
|
+
const cfg = await getConfig(workspaceRoot);
|
|
376
|
+
const raw = cfg.get();
|
|
377
|
+
const host = raw.host;
|
|
378
|
+
const token = raw.adminToken;
|
|
379
|
+
if (!host) throw new Error('Missing host in husar.json');
|
|
380
|
+
if (!token) throw new Error('Missing adminToken in husar.json');
|
|
381
|
+
const url = new URL(endpointPath || 'api/graphql', host).toString();
|
|
382
|
+
const res = await fetch(url, {
|
|
383
|
+
method: 'POST',
|
|
384
|
+
headers: { 'Content-Type': 'application/json', husar_token: token },
|
|
385
|
+
body: JSON.stringify({ query: INTROSPECTION_QUERY }),
|
|
386
|
+
});
|
|
387
|
+
const json = await res.json();
|
|
388
|
+
return { content: [{ type: 'text', text: JSON.stringify(json, null, 2) }] };
|
|
389
|
+
} catch (err) {
|
|
390
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
391
|
+
const stack = err instanceof Error && err.stack ? `\nStack: ${err.stack}` : '';
|
|
392
|
+
return { isError: true, content: [{ type: 'text', text: `Error: ${message}${stack}` }] };
|
|
393
|
+
}
|
|
394
|
+
}) as any,
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
// Tool: husar_graphql_schema_sdl (admin) — human-friendly SDL
|
|
398
|
+
server.tool('husar_graphql_schema_sdl', 'Fetch Admin GraphQL schema and return SDL', husarGraphQLSchemaInput, (async (
|
|
399
|
+
args: unknown,
|
|
400
|
+
) => {
|
|
401
|
+
try {
|
|
402
|
+
const { endpointPath, workspaceRoot } = husarGraphQLSchemaSchema.parse(args ?? {});
|
|
403
|
+
const cfg = await getConfig(workspaceRoot);
|
|
404
|
+
const raw = cfg.get();
|
|
405
|
+
const host = raw.host;
|
|
406
|
+
const token = raw.adminToken;
|
|
407
|
+
if (!host) throw new Error('Missing host in husar.json');
|
|
408
|
+
if (!token) throw new Error('Missing adminToken in husar.json');
|
|
409
|
+
const url = new URL(endpointPath || 'api/graphql', host).toString();
|
|
410
|
+
const res = await fetch(url, {
|
|
411
|
+
method: 'POST',
|
|
412
|
+
headers: { 'Content-Type': 'application/json', husar_token: token },
|
|
413
|
+
body: JSON.stringify({ query: INTROSPECTION_QUERY }),
|
|
414
|
+
});
|
|
415
|
+
const json = await res.json();
|
|
416
|
+
if (!json?.data) throw new Error('No data from introspection');
|
|
417
|
+
const schema = buildClientSchema(json.data);
|
|
418
|
+
const sdl = printSchema(schema);
|
|
419
|
+
return { content: [{ type: 'text', text: sdl }] };
|
|
420
|
+
} catch (err) {
|
|
421
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
422
|
+
const stack = err instanceof Error && err.stack ? `\nStack: ${err.stack}` : '';
|
|
423
|
+
return { isError: true, content: [{ type: 'text', text: `Error: ${message}${stack}` }] };
|
|
424
|
+
}
|
|
425
|
+
}) as any);
|
|
426
|
+
|
|
427
|
+
// Tool: husar_content_graphql_query (read-only helper for Content API)
|
|
428
|
+
const husarContentGraphQLQueryInput = {
|
|
429
|
+
query: z.string().min(1).describe('GraphQL query string.'),
|
|
430
|
+
variables: z
|
|
431
|
+
.union([z.string(), z.record(z.any())])
|
|
432
|
+
.optional()
|
|
433
|
+
.describe('Optional variables as JSON string or object.'),
|
|
434
|
+
endpointPath: z
|
|
435
|
+
.string()
|
|
436
|
+
.optional()
|
|
437
|
+
.default('content/graphql')
|
|
438
|
+
.describe('Endpoint path relative to host, defaults to content/graphql'),
|
|
439
|
+
apiKey: z.string().optional().describe('Content API key. If omitted, apiKeyEnv is used.'),
|
|
440
|
+
apiKeyEnv: z.string().optional().describe('Environment variable name containing the Content API key.'),
|
|
441
|
+
workspaceRoot: z.string().optional().describe('Location of husar.json if not in current working directory.'),
|
|
442
|
+
} as const;
|
|
443
|
+
const husarContentGraphQLQuerySchema = z.object(husarContentGraphQLQueryInput);
|
|
444
|
+
|
|
445
|
+
server.tool(
|
|
446
|
+
'husar_content_graphql_query',
|
|
447
|
+
'Run a read-only GraphQL query against the Content API (api-key required)',
|
|
448
|
+
husarContentGraphQLQueryInput,
|
|
449
|
+
(async (args: unknown) => {
|
|
450
|
+
try {
|
|
451
|
+
const { query, variables, endpointPath, apiKey, apiKeyEnv, workspaceRoot } =
|
|
452
|
+
husarContentGraphQLQuerySchema.parse(args ?? {});
|
|
453
|
+
const cfg = await getConfig(workspaceRoot);
|
|
454
|
+
const raw = cfg.get();
|
|
455
|
+
const host = raw.host;
|
|
456
|
+
if (!host) throw new Error('Missing host in husar.json');
|
|
457
|
+
const keyFromEnv = apiKeyEnv ? process.env[apiKeyEnv] : undefined;
|
|
458
|
+
const keyFromConfig = (raw as any).contentApiKey as string | undefined;
|
|
459
|
+
const usedKey = apiKey || keyFromEnv || keyFromConfig;
|
|
460
|
+
if (!usedKey) throw new Error('Missing apiKey. Provide apiKey, apiKeyEnv, or set contentApiKey in husar.json');
|
|
461
|
+
const url = new URL(endpointPath || 'content/graphql', host).toString();
|
|
462
|
+
const vars =
|
|
463
|
+
typeof variables === 'string' && variables.trim().length ? JSON.parse(variables) : (variables ?? {});
|
|
464
|
+
const res = await fetch(url, {
|
|
465
|
+
method: 'POST',
|
|
466
|
+
headers: { 'Content-Type': 'application/json', 'api-key': usedKey },
|
|
467
|
+
body: JSON.stringify({ query, variables: vars }),
|
|
468
|
+
});
|
|
469
|
+
const json = await res.json();
|
|
470
|
+
return { content: [{ type: 'text', text: JSON.stringify(json, null, 2) }] };
|
|
471
|
+
} catch (err) {
|
|
472
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
473
|
+
const stack = err instanceof Error && err.stack ? `\nStack: ${err.stack}` : '';
|
|
474
|
+
return { isError: true, content: [{ type: 'text', text: `Error: ${message}${stack}` }] };
|
|
475
|
+
}
|
|
476
|
+
}) as any,
|
|
477
|
+
);
|
|
478
|
+
|
|
479
|
+
// Tool: husar_content_graphql_schema (content)
|
|
480
|
+
const husarContentGraphQLSchemaInput = {
|
|
481
|
+
endpointPath: z
|
|
482
|
+
.string()
|
|
483
|
+
.optional()
|
|
484
|
+
.default('content/graphql')
|
|
485
|
+
.describe('Endpoint path relative to host, defaults to content/graphql'),
|
|
486
|
+
apiKey: z.string().optional().describe('Content API key. If omitted, apiKeyEnv is used.'),
|
|
487
|
+
apiKeyEnv: z.string().optional().describe('Environment variable name containing the Content API key.'),
|
|
488
|
+
workspaceRoot: z.string().optional().describe('Location of husar.json if not in current working directory.'),
|
|
489
|
+
} as const;
|
|
490
|
+
const husarContentGraphQLSchemaSchema = z.object(husarContentGraphQLSchemaInput);
|
|
491
|
+
|
|
492
|
+
server.tool(
|
|
493
|
+
'husar_content_graphql_schema',
|
|
494
|
+
'Fetch GraphQL schema (introspection) from Content API',
|
|
495
|
+
husarContentGraphQLSchemaInput,
|
|
496
|
+
(async (args: unknown) => {
|
|
497
|
+
try {
|
|
498
|
+
const { endpointPath, apiKey, apiKeyEnv, workspaceRoot } = husarContentGraphQLSchemaSchema.parse(args ?? {});
|
|
499
|
+
const cfg = await getConfig(workspaceRoot);
|
|
500
|
+
const raw = cfg.get();
|
|
501
|
+
const host = raw.host;
|
|
502
|
+
if (!host) throw new Error('Missing host in husar.json');
|
|
503
|
+
const keyFromEnv = apiKeyEnv ? process.env[apiKeyEnv] : undefined;
|
|
504
|
+
const keyFromConfig = (raw as any).contentApiKey as string | undefined;
|
|
505
|
+
const usedKey = apiKey || keyFromEnv || keyFromConfig;
|
|
506
|
+
if (!usedKey) throw new Error('Missing apiKey. Provide apiKey, apiKeyEnv, or set contentApiKey in husar.json');
|
|
507
|
+
const url = new URL(endpointPath || 'content/graphql', host).toString();
|
|
508
|
+
const res = await fetch(url, {
|
|
509
|
+
method: 'POST',
|
|
510
|
+
headers: { 'Content-Type': 'application/json', 'api-key': usedKey },
|
|
511
|
+
body: JSON.stringify({ query: INTROSPECTION_QUERY }),
|
|
512
|
+
});
|
|
513
|
+
const json = await res.json();
|
|
514
|
+
return { content: [{ type: 'text', text: JSON.stringify(json, null, 2) }] };
|
|
515
|
+
} catch (err) {
|
|
516
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
517
|
+
const stack = err instanceof Error && err.stack ? `\nStack: ${err.stack}` : '';
|
|
518
|
+
return { isError: true, content: [{ type: 'text', text: `Error: ${message}${stack}` }] };
|
|
519
|
+
}
|
|
520
|
+
}) as any,
|
|
521
|
+
);
|
|
522
|
+
|
|
523
|
+
// Tool: husar_content_graphql_schema_sdl (content) — human-friendly SDL
|
|
524
|
+
server.tool(
|
|
525
|
+
'husar_content_graphql_schema_sdl',
|
|
526
|
+
'Fetch Content GraphQL schema and return SDL',
|
|
527
|
+
husarContentGraphQLSchemaInput,
|
|
528
|
+
(async (args: unknown) => {
|
|
529
|
+
try {
|
|
530
|
+
const { endpointPath, apiKey, apiKeyEnv, workspaceRoot } = husarContentGraphQLSchemaSchema.parse(args ?? {});
|
|
531
|
+
const cfg = await getConfig(workspaceRoot);
|
|
532
|
+
const raw = cfg.get();
|
|
533
|
+
const host = raw.host;
|
|
534
|
+
if (!host) throw new Error('Missing host in husar.json');
|
|
535
|
+
const keyFromEnv = apiKeyEnv ? process.env[apiKeyEnv] : undefined;
|
|
536
|
+
const keyFromConfig = (raw as any).contentApiKey as string | undefined;
|
|
537
|
+
const usedKey = apiKey || keyFromEnv || keyFromConfig;
|
|
538
|
+
if (!usedKey) throw new Error('Missing apiKey. Provide apiKey, apiKeyEnv, or set contentApiKey in husar.json');
|
|
539
|
+
const url = new URL(endpointPath || 'content/graphql', host).toString();
|
|
540
|
+
const res = await fetch(url, {
|
|
541
|
+
method: 'POST',
|
|
542
|
+
headers: { 'Content-Type': 'application/json', 'api-key': usedKey },
|
|
543
|
+
body: JSON.stringify({ query: INTROSPECTION_QUERY }),
|
|
544
|
+
});
|
|
545
|
+
const json = await res.json();
|
|
546
|
+
if (!json?.data) throw new Error('No data from introspection');
|
|
547
|
+
const schema = buildClientSchema(json.data);
|
|
548
|
+
const sdl = printSchema(schema);
|
|
549
|
+
return { content: [{ type: 'text', text: sdl }] };
|
|
550
|
+
} catch (err) {
|
|
551
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
552
|
+
const stack = err instanceof Error && err.stack ? `\nStack: ${err.stack}` : '';
|
|
553
|
+
return { isError: true, content: [{ type: 'text', text: `Error: ${message}${stack}` }] };
|
|
554
|
+
}
|
|
555
|
+
}) as any,
|
|
556
|
+
);
|
|
557
|
+
|
|
558
|
+
// Tool: husar_graphql_suggest_query — given SDL + task, build a query
|
|
559
|
+
const husarSuggestInput = {
|
|
560
|
+
sdl: z.string().min(1).describe('GraphQL schema SDL'),
|
|
561
|
+
task: z.string().min(1).describe('Task description, e.g., "list posts by tag"'),
|
|
562
|
+
rootType: z.string().optional().default('Query'),
|
|
563
|
+
} as const;
|
|
564
|
+
const husarSuggestSchema = z.object(husarSuggestInput);
|
|
565
|
+
|
|
566
|
+
server.tool(
|
|
567
|
+
'husar_graphql_suggest_query',
|
|
568
|
+
'Suggest a GraphQL query and variables template from SDL + task',
|
|
569
|
+
husarSuggestInput,
|
|
570
|
+
(async (args: unknown) => {
|
|
571
|
+
try {
|
|
572
|
+
const { sdl, task, rootType } = husarSuggestSchema.parse(args ?? {});
|
|
573
|
+
const suggestion = suggestQueryFromSDL({ sdl, task, rootType });
|
|
574
|
+
return { content: [{ type: 'text', text: JSON.stringify(suggestion, null, 2) }] };
|
|
575
|
+
} catch (err) {
|
|
576
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
577
|
+
const stack = err instanceof Error && err.stack ? `\nStack: ${err.stack}` : '';
|
|
578
|
+
return { isError: true, content: [{ type: 'text', text: `Error: ${message}${stack}` }] };
|
|
579
|
+
}
|
|
580
|
+
}) as any,
|
|
581
|
+
);
|
|
582
|
+
|
|
583
|
+
// Tool: husar_graphql_run_suggestion — suggest and run (Admin API)
|
|
584
|
+
const husarRunSuggestionInput = {
|
|
585
|
+
sdl: z.string().min(1).describe('GraphQL schema SDL'),
|
|
586
|
+
task: z.string().min(1).describe('Task to execute'),
|
|
587
|
+
variables: z
|
|
588
|
+
.union([z.string(), z.record(z.any())])
|
|
589
|
+
.optional()
|
|
590
|
+
.describe('Override variables (JSON)'),
|
|
591
|
+
endpointPath: z.string().optional().default('api/graphql'),
|
|
592
|
+
workspaceRoot: z.string().optional(),
|
|
593
|
+
} as const;
|
|
594
|
+
const husarRunSuggestionSchema = z.object(husarRunSuggestionInput);
|
|
595
|
+
|
|
596
|
+
server.tool(
|
|
597
|
+
'husar_graphql_run_suggestion',
|
|
598
|
+
'Generate a query from SDL + task and run it on Admin API',
|
|
599
|
+
husarRunSuggestionInput,
|
|
600
|
+
(async (args: unknown) => {
|
|
601
|
+
try {
|
|
602
|
+
const { sdl, task, variables, endpointPath, workspaceRoot } = husarRunSuggestionSchema.parse(args ?? {});
|
|
603
|
+
const cfg = await getConfig(workspaceRoot);
|
|
604
|
+
const raw = cfg.get();
|
|
605
|
+
const host = raw.host;
|
|
606
|
+
const token = raw.adminToken;
|
|
607
|
+
if (!host) throw new Error('Missing host in husar.json');
|
|
608
|
+
if (!token) throw new Error('Missing adminToken in husar.json');
|
|
609
|
+
const { query, variables: tmpl } = suggestQueryFromSDL({ sdl, task, rootType: 'Query' });
|
|
610
|
+
const overrides =
|
|
611
|
+
typeof variables === 'string' && variables.trim().length ? JSON.parse(variables) : (variables ?? {});
|
|
612
|
+
const merged = { ...tmpl, ...overrides };
|
|
613
|
+
const url = new URL(endpointPath || 'api/graphql', host).toString();
|
|
614
|
+
const res = await fetch(url, {
|
|
615
|
+
method: 'POST',
|
|
616
|
+
headers: { 'Content-Type': 'application/json', husar_token: token },
|
|
617
|
+
body: JSON.stringify({ query, variables: merged }),
|
|
618
|
+
});
|
|
619
|
+
const json = await res.json();
|
|
620
|
+
return {
|
|
621
|
+
content: [{ type: 'text', text: JSON.stringify({ query, variables: merged, result: json }, null, 2) }],
|
|
622
|
+
};
|
|
623
|
+
} catch (err) {
|
|
624
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
625
|
+
const stack = err instanceof Error && err.stack ? `\nStack: ${err.stack}` : '';
|
|
626
|
+
return { isError: true, content: [{ type: 'text', text: `Error: ${message}${stack}` }] };
|
|
627
|
+
}
|
|
628
|
+
}) as any,
|
|
629
|
+
);
|
|
630
|
+
|
|
631
|
+
// Tool: husar_content_graphql_run_suggestion — suggest and run (Content API)
|
|
632
|
+
const husarContentRunSuggestionInput = {
|
|
633
|
+
sdl: z.string().min(1).describe('GraphQL schema SDL'),
|
|
634
|
+
task: z.string().min(1).describe('Task to execute'),
|
|
635
|
+
variables: z
|
|
636
|
+
.union([z.string(), z.record(z.any())])
|
|
637
|
+
.optional()
|
|
638
|
+
.describe('Override variables (JSON)'),
|
|
639
|
+
endpointPath: z.string().optional().default('content/graphql'),
|
|
640
|
+
apiKey: z.string().optional().describe('Content API key'),
|
|
641
|
+
apiKeyEnv: z.string().optional().describe('Env var name containing Content API key'),
|
|
642
|
+
workspaceRoot: z.string().optional(),
|
|
643
|
+
} as const;
|
|
644
|
+
const husarContentRunSuggestionSchema = z.object(husarContentRunSuggestionInput);
|
|
645
|
+
|
|
646
|
+
server.tool(
|
|
647
|
+
'husar_content_graphql_run_suggestion',
|
|
648
|
+
'Generate a query from SDL + task and run it on Content API',
|
|
649
|
+
husarContentRunSuggestionInput,
|
|
650
|
+
(async (args: unknown) => {
|
|
651
|
+
try {
|
|
652
|
+
const { sdl, task, variables, endpointPath, apiKey, apiKeyEnv, workspaceRoot } =
|
|
653
|
+
husarContentRunSuggestionSchema.parse(args ?? {});
|
|
654
|
+
const cfg = await getConfig(workspaceRoot);
|
|
655
|
+
const raw = cfg.get();
|
|
656
|
+
const host = raw.host;
|
|
657
|
+
if (!host) throw new Error('Missing host in husar.json');
|
|
658
|
+
const keyFromEnv = apiKeyEnv ? process.env[apiKeyEnv] : undefined;
|
|
659
|
+
const keyFromConfig = (raw as any).contentApiKey as string | undefined;
|
|
660
|
+
const usedKey = apiKey || keyFromEnv || keyFromConfig;
|
|
661
|
+
if (!usedKey) throw new Error('Missing apiKey. Provide apiKey/apiKeyEnv or set contentApiKey in husar.json');
|
|
662
|
+
const { query, variables: tmpl } = suggestQueryFromSDL({ sdl, task, rootType: 'Query' });
|
|
663
|
+
const overrides =
|
|
664
|
+
typeof variables === 'string' && variables.trim().length ? JSON.parse(variables) : (variables ?? {});
|
|
665
|
+
const merged = { ...tmpl, ...overrides };
|
|
666
|
+
const url = new URL(endpointPath || 'content/graphql', host).toString();
|
|
667
|
+
const res = await fetch(url, {
|
|
668
|
+
method: 'POST',
|
|
669
|
+
headers: { 'Content-Type': 'application/json', 'api-key': usedKey },
|
|
670
|
+
body: JSON.stringify({ query, variables: merged }),
|
|
671
|
+
});
|
|
672
|
+
const json = await res.json();
|
|
673
|
+
return {
|
|
674
|
+
content: [{ type: 'text', text: JSON.stringify({ query, variables: merged, result: json }, null, 2) }],
|
|
675
|
+
};
|
|
676
|
+
} catch (err) {
|
|
677
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
678
|
+
const stack = err instanceof Error && err.stack ? `\nStack: ${err.stack}` : '';
|
|
679
|
+
return { isError: true, content: [{ type: 'text', text: `Error: ${message}${stack}` }] };
|
|
680
|
+
}
|
|
681
|
+
}) as any,
|
|
682
|
+
);
|
|
683
|
+
|
|
684
|
+
// Register Admin ORM tools so CLI MCP provides same toolset as Admin Agent
|
|
685
|
+
try {
|
|
686
|
+
const ormSpecs = getOrmToolSpecs();
|
|
687
|
+
const runAgentToolMutation = `mutation RunAgentTool($input: AgentToolRunInput!) {\n admin {\n runAgentTool(input: $input) { ok error executed result }\n }\n}`;
|
|
688
|
+
for (const spec of ormSpecs) {
|
|
689
|
+
// We pass raw JSON Schema parameters to MCP server; SDK will forward as-is.
|
|
690
|
+
server.tool(spec.name, spec.description, spec.parameters, (async (args: unknown) => {
|
|
691
|
+
try {
|
|
692
|
+
const cfg = await getConfig(undefined);
|
|
693
|
+
const raw = cfg.get();
|
|
694
|
+
const host = raw.host;
|
|
695
|
+
const token = raw.adminToken;
|
|
696
|
+
if (!host) throw new Error('Missing host in husar.json');
|
|
697
|
+
if (!token) throw new Error('Missing adminToken in husar.json');
|
|
698
|
+
const url = new URL('api/graphql', host).toString();
|
|
699
|
+
const input = { name: spec.name, args };
|
|
700
|
+
const res = await fetch(url, {
|
|
701
|
+
method: 'POST',
|
|
702
|
+
headers: { 'Content-Type': 'application/json', husar_token: token },
|
|
703
|
+
body: JSON.stringify({ query: runAgentToolMutation, variables: { input } }),
|
|
704
|
+
});
|
|
705
|
+
const json = await res.json();
|
|
706
|
+
const payload = json?.data?.admin?.runAgentTool ?? json;
|
|
707
|
+
// Ensure result is serializable text content for MCP
|
|
708
|
+
return { content: [{ type: 'text', text: JSON.stringify(payload, null, 2) }] };
|
|
709
|
+
} catch (err) {
|
|
710
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
711
|
+
const stack = err instanceof Error && err.stack ? `\nStack: ${err.stack}` : '';
|
|
712
|
+
return { isError: true, content: [{ type: 'text', text: `Error: ${message}${stack}` }] };
|
|
713
|
+
}
|
|
714
|
+
}) as any);
|
|
715
|
+
}
|
|
716
|
+
} catch (e) {
|
|
717
|
+
// In case tool spec import fails, keep MCP operational for other tools
|
|
718
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
719
|
+
try {
|
|
720
|
+
process.stderr.write(`[mcp] Failed to register ORM tools: ${msg}\n`);
|
|
721
|
+
} catch {
|
|
722
|
+
/* ignore */
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
182
726
|
const transport = new StdioServerTransport();
|
|
183
727
|
server.connect(transport);
|
|
184
728
|
try {
|