chiasmus 0.1.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.
- package/LICENSE +201 -0
- package/README.md +124 -0
- package/dist/formalize/engine.d.ts +47 -0
- package/dist/formalize/engine.d.ts.map +1 -0
- package/dist/formalize/engine.js +189 -0
- package/dist/formalize/engine.js.map +1 -0
- package/dist/formalize/validate.d.ts +15 -0
- package/dist/formalize/validate.d.ts.map +1 -0
- package/dist/formalize/validate.js +124 -0
- package/dist/formalize/validate.js.map +1 -0
- package/dist/llm/anthropic.d.ts +27 -0
- package/dist/llm/anthropic.d.ts.map +1 -0
- package/dist/llm/anthropic.js +86 -0
- package/dist/llm/anthropic.js.map +1 -0
- package/dist/llm/mock.d.ts +21 -0
- package/dist/llm/mock.d.ts.map +1 -0
- package/dist/llm/mock.js +29 -0
- package/dist/llm/mock.js.map +1 -0
- package/dist/llm/openai-compatible.d.ts +20 -0
- package/dist/llm/openai-compatible.d.ts.map +1 -0
- package/dist/llm/openai-compatible.js +46 -0
- package/dist/llm/openai-compatible.js.map +1 -0
- package/dist/llm/types.d.ts +11 -0
- package/dist/llm/types.d.ts.map +1 -0
- package/dist/llm/types.js +2 -0
- package/dist/llm/types.js.map +1 -0
- package/dist/mcp-server.d.ts +12 -0
- package/dist/mcp-server.d.ts.map +1 -0
- package/dist/mcp-server.js +446 -0
- package/dist/mcp-server.js.map +1 -0
- package/dist/skills/bm25.d.ts +22 -0
- package/dist/skills/bm25.d.ts.map +1 -0
- package/dist/skills/bm25.js +71 -0
- package/dist/skills/bm25.js.map +1 -0
- package/dist/skills/learner.d.ts +21 -0
- package/dist/skills/learner.d.ts.map +1 -0
- package/dist/skills/learner.js +123 -0
- package/dist/skills/learner.js.map +1 -0
- package/dist/skills/library.d.ts +36 -0
- package/dist/skills/library.d.ts.map +1 -0
- package/dist/skills/library.js +173 -0
- package/dist/skills/library.js.map +1 -0
- package/dist/skills/starters.d.ts +3 -0
- package/dist/skills/starters.d.ts.map +1 -0
- package/dist/skills/starters.js +381 -0
- package/dist/skills/starters.js.map +1 -0
- package/dist/skills/types.d.ts +54 -0
- package/dist/skills/types.d.ts.map +1 -0
- package/dist/skills/types.js +2 -0
- package/dist/skills/types.js.map +1 -0
- package/dist/solvers/correction-loop.d.ts +34 -0
- package/dist/solvers/correction-loop.d.ts.map +1 -0
- package/dist/solvers/correction-loop.js +52 -0
- package/dist/solvers/correction-loop.js.map +1 -0
- package/dist/solvers/prolog-solver.d.ts +3 -0
- package/dist/solvers/prolog-solver.d.ts.map +1 -0
- package/dist/solvers/prolog-solver.js +93 -0
- package/dist/solvers/prolog-solver.js.map +1 -0
- package/dist/solvers/session.d.ts +11 -0
- package/dist/solvers/session.d.ts.map +1 -0
- package/dist/solvers/session.js +25 -0
- package/dist/solvers/session.js.map +1 -0
- package/dist/solvers/types.d.ts +44 -0
- package/dist/solvers/types.d.ts.map +1 -0
- package/dist/solvers/types.js +2 -0
- package/dist/solvers/types.js.map +1 -0
- package/dist/solvers/z3-solver.d.ts +3 -0
- package/dist/solvers/z3-solver.d.ts.map +1 -0
- package/dist/solvers/z3-solver.js +81 -0
- package/dist/solvers/z3-solver.js.map +1 -0
- package/package.json +85 -0
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
5
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
6
|
+
import { CallToolRequestSchema, ListToolsRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
|
|
7
|
+
import { SolverSession } from "./solvers/session.js";
|
|
8
|
+
import { SkillLibrary } from "./skills/library.js";
|
|
9
|
+
import { FormalizationEngine } from "./formalize/engine.js";
|
|
10
|
+
import { SkillLearner } from "./skills/learner.js";
|
|
11
|
+
import { createLLMFromEnv } from "./llm/anthropic.js";
|
|
12
|
+
export function getChiasmusHome() {
|
|
13
|
+
return process.env.CHIASMUS_HOME ?? join(homedir(), ".chiasmus");
|
|
14
|
+
}
|
|
15
|
+
const TOOLS = [
|
|
16
|
+
{
|
|
17
|
+
name: "chiasmus_verify",
|
|
18
|
+
description: `Submit raw formal logic to a solver and get a verified result.
|
|
19
|
+
|
|
20
|
+
SOLVERS:
|
|
21
|
+
z3 — SMT solver. Input is SMT-LIB format. Returns SAT + model, UNSAT, or error.
|
|
22
|
+
prolog — ISO Prolog. Input is facts/rules, query is a Prolog goal. Returns answers or error.
|
|
23
|
+
|
|
24
|
+
WHEN TO USE:
|
|
25
|
+
- Verify constraints: "can these rules ever conflict?"
|
|
26
|
+
- Check satisfiability: "is there a valid assignment?"
|
|
27
|
+
- Prove/disprove: "is this always true for all inputs?"
|
|
28
|
+
- Derive conclusions: "what follows from these facts and rules?"
|
|
29
|
+
|
|
30
|
+
NOTE: Do NOT include (check-sat) or (get-model) in Z3 input — the tool runs these automatically.
|
|
31
|
+
|
|
32
|
+
EXAMPLES:
|
|
33
|
+
Z3 (check if two integers sum to 10):
|
|
34
|
+
solver: "z3"
|
|
35
|
+
input: |
|
|
36
|
+
(declare-const x Int)
|
|
37
|
+
(declare-const y Int)
|
|
38
|
+
(assert (= (+ x y) 10))
|
|
39
|
+
(assert (> x 0))
|
|
40
|
+
(assert (> y 0))
|
|
41
|
+
|
|
42
|
+
Prolog (derive permissions):
|
|
43
|
+
solver: "prolog"
|
|
44
|
+
input: "role(alice, admin). can_access(X, resource) :- role(X, admin)."
|
|
45
|
+
query: "can_access(alice, resource)."`,
|
|
46
|
+
inputSchema: {
|
|
47
|
+
type: "object",
|
|
48
|
+
properties: {
|
|
49
|
+
solver: {
|
|
50
|
+
type: "string",
|
|
51
|
+
enum: ["z3", "prolog"],
|
|
52
|
+
description: "Which solver to use: z3 (SMT-LIB) or prolog (ISO Prolog)",
|
|
53
|
+
},
|
|
54
|
+
input: {
|
|
55
|
+
type: "string",
|
|
56
|
+
description: "The formal specification. For z3: SMT-LIB format. For prolog: facts and rules.",
|
|
57
|
+
},
|
|
58
|
+
query: {
|
|
59
|
+
type: "string",
|
|
60
|
+
description: "Prolog query goal (required for prolog, ignored for z3). Must end with a period.",
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
required: ["solver", "input"],
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
name: "chiasmus_skills",
|
|
68
|
+
description: `Search and list formalization templates in the skill library.
|
|
69
|
+
|
|
70
|
+
Returns matching templates with their skeletons, slot definitions, normalization recipes,
|
|
71
|
+
and usage metadata (reuse count, success rate).
|
|
72
|
+
|
|
73
|
+
Use this to find an appropriate template before calling chiasmus_verify or chiasmus_solve.
|
|
74
|
+
|
|
75
|
+
EXAMPLES:
|
|
76
|
+
Search for authorization templates:
|
|
77
|
+
query: "check if access control policies conflict"
|
|
78
|
+
|
|
79
|
+
List all Prolog templates:
|
|
80
|
+
solver: "prolog"
|
|
81
|
+
|
|
82
|
+
Get a specific template:
|
|
83
|
+
name: "policy-contradiction"`,
|
|
84
|
+
inputSchema: {
|
|
85
|
+
type: "object",
|
|
86
|
+
properties: {
|
|
87
|
+
query: {
|
|
88
|
+
type: "string",
|
|
89
|
+
description: "Natural language search query to find relevant templates",
|
|
90
|
+
},
|
|
91
|
+
name: {
|
|
92
|
+
type: "string",
|
|
93
|
+
description: "Get a specific template by exact name (overrides query)",
|
|
94
|
+
},
|
|
95
|
+
domain: {
|
|
96
|
+
type: "string",
|
|
97
|
+
description: "Filter by domain (authorization, configuration, dependency, validation, rules, analysis)",
|
|
98
|
+
},
|
|
99
|
+
solver: {
|
|
100
|
+
type: "string",
|
|
101
|
+
enum: ["z3", "prolog"],
|
|
102
|
+
description: "Filter by solver type",
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
name: "chiasmus_formalize",
|
|
109
|
+
description: `Find the best formalization template for a problem and return it with slot-filling instructions.
|
|
110
|
+
|
|
111
|
+
This is a GUIDED workflow: the tool finds the right template and tells you how to fill it.
|
|
112
|
+
You then fill the slots and submit the result via chiasmus_verify.
|
|
113
|
+
|
|
114
|
+
WORKFLOW:
|
|
115
|
+
1. Call chiasmus_formalize with your problem description
|
|
116
|
+
2. Read the returned template, slots, and normalization guidance
|
|
117
|
+
3. Fill the template slots based on the guidance
|
|
118
|
+
4. Submit the filled specification via chiasmus_verify
|
|
119
|
+
|
|
120
|
+
Use this when you want full control over the formalization process.`,
|
|
121
|
+
inputSchema: {
|
|
122
|
+
type: "object",
|
|
123
|
+
properties: {
|
|
124
|
+
problem: {
|
|
125
|
+
type: "string",
|
|
126
|
+
description: "Natural language description of the problem to formalize",
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
required: ["problem"],
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
name: "chiasmus_solve",
|
|
134
|
+
description: `End-to-end: formalize a problem and verify it automatically.
|
|
135
|
+
|
|
136
|
+
Uses an LLM to select a template, fill slots, and run the correction loop.
|
|
137
|
+
Requires ANTHROPIC_API_KEY to be set. Without it, falls back to returning
|
|
138
|
+
template instructions (same as chiasmus_formalize).
|
|
139
|
+
|
|
140
|
+
WHEN TO USE:
|
|
141
|
+
- You want a fully automated solve pipeline
|
|
142
|
+
- The problem maps to a known domain (authorization, config, dependency, rules, reachability)
|
|
143
|
+
|
|
144
|
+
Returns the verified solver result, the template used, and correction loop history.`,
|
|
145
|
+
inputSchema: {
|
|
146
|
+
type: "object",
|
|
147
|
+
properties: {
|
|
148
|
+
problem: {
|
|
149
|
+
type: "string",
|
|
150
|
+
description: "Natural language description of the problem to solve",
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
required: ["problem"],
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
name: "chiasmus_learn",
|
|
158
|
+
description: `Extract a reusable template from a verified solution and add it to the skill library.
|
|
159
|
+
|
|
160
|
+
After solving a problem with chiasmus_verify, call this to generalize the solution into a
|
|
161
|
+
reusable template. The template is stored as a candidate — it gets promoted to full status
|
|
162
|
+
after being successfully reused ${3} times.
|
|
163
|
+
|
|
164
|
+
Requires ANTHROPIC_API_KEY for template extraction (uses LLM to generalize).
|
|
165
|
+
|
|
166
|
+
WORKFLOW:
|
|
167
|
+
1. Solve a problem with chiasmus_verify
|
|
168
|
+
2. Call chiasmus_learn with the verified spec and problem description
|
|
169
|
+
3. The extracted template appears in chiasmus_skills searches
|
|
170
|
+
4. Future similar problems can use the template via chiasmus_formalize`,
|
|
171
|
+
inputSchema: {
|
|
172
|
+
type: "object",
|
|
173
|
+
properties: {
|
|
174
|
+
solver: {
|
|
175
|
+
type: "string",
|
|
176
|
+
enum: ["z3", "prolog"],
|
|
177
|
+
description: "Which solver was used for the verified spec",
|
|
178
|
+
},
|
|
179
|
+
spec: {
|
|
180
|
+
type: "string",
|
|
181
|
+
description: "The verified formal specification to generalize",
|
|
182
|
+
},
|
|
183
|
+
problem: {
|
|
184
|
+
type: "string",
|
|
185
|
+
description: "Natural language description of the problem that was solved",
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
required: ["solver", "spec", "problem"],
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
];
|
|
192
|
+
async function handleVerify(args) {
|
|
193
|
+
const solver = args.solver;
|
|
194
|
+
const input = args.input;
|
|
195
|
+
const query = args.query;
|
|
196
|
+
if (typeof solver !== "string" || typeof input !== "string") {
|
|
197
|
+
return {
|
|
198
|
+
content: [{ type: "text", text: JSON.stringify({
|
|
199
|
+
status: "error",
|
|
200
|
+
error: "Both 'solver' (string) and 'input' (string) are required",
|
|
201
|
+
}) }],
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
let result;
|
|
205
|
+
try {
|
|
206
|
+
if (solver === "z3") {
|
|
207
|
+
const session = await SolverSession.create("z3");
|
|
208
|
+
try {
|
|
209
|
+
result = await session.solve({ type: "z3", smtlib: input });
|
|
210
|
+
}
|
|
211
|
+
finally {
|
|
212
|
+
session.dispose();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
else if (solver === "prolog") {
|
|
216
|
+
if (!query) {
|
|
217
|
+
result = {
|
|
218
|
+
status: "error",
|
|
219
|
+
error: "The 'query' parameter is required for the prolog solver",
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
const session = await SolverSession.create("prolog");
|
|
224
|
+
try {
|
|
225
|
+
result = await session.solve({
|
|
226
|
+
type: "prolog",
|
|
227
|
+
program: input,
|
|
228
|
+
query,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
finally {
|
|
232
|
+
session.dispose();
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
result = {
|
|
238
|
+
status: "error",
|
|
239
|
+
error: `Unknown solver: ${solver}. Use "z3" or "prolog".`,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
catch (e) {
|
|
244
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
245
|
+
result = { status: "error", error: `Solver initialization failed: ${msg}` };
|
|
246
|
+
}
|
|
247
|
+
return {
|
|
248
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
function handleSkills(library, args) {
|
|
252
|
+
const name = args.name;
|
|
253
|
+
const query = args.query;
|
|
254
|
+
const domain = args.domain;
|
|
255
|
+
const solver = args.solver;
|
|
256
|
+
if (name) {
|
|
257
|
+
const result = library.get(name);
|
|
258
|
+
if (!result) {
|
|
259
|
+
return {
|
|
260
|
+
content: [{ type: "text", text: JSON.stringify({ error: `Template "${name}" not found` }) }],
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
return {
|
|
264
|
+
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
if (query) {
|
|
268
|
+
const results = library.search(query, { domain, solver });
|
|
269
|
+
return {
|
|
270
|
+
content: [{ type: "text", text: JSON.stringify(results, null, 2) }],
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
let all = library.list();
|
|
274
|
+
if (domain)
|
|
275
|
+
all = all.filter((s) => s.template.domain === domain);
|
|
276
|
+
if (solver)
|
|
277
|
+
all = all.filter((s) => s.template.solver === solver);
|
|
278
|
+
return {
|
|
279
|
+
content: [{ type: "text", text: JSON.stringify(all, null, 2) }],
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
async function handleFormalize(formalizer, args) {
|
|
283
|
+
if (typeof args.problem !== "string" || !args.problem) {
|
|
284
|
+
return {
|
|
285
|
+
content: [{ type: "text", text: JSON.stringify({ error: "The 'problem' parameter (string) is required" }) }],
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
const problem = args.problem;
|
|
289
|
+
const result = await formalizer.formalize(problem);
|
|
290
|
+
return {
|
|
291
|
+
content: [{
|
|
292
|
+
type: "text",
|
|
293
|
+
text: JSON.stringify({
|
|
294
|
+
template: result.template.name,
|
|
295
|
+
solver: result.template.solver,
|
|
296
|
+
domain: result.template.domain,
|
|
297
|
+
instructions: result.instructions,
|
|
298
|
+
}, null, 2),
|
|
299
|
+
}],
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
async function handleSolve(formalizer, library, args) {
|
|
303
|
+
if (typeof args.problem !== "string" || !args.problem) {
|
|
304
|
+
return {
|
|
305
|
+
content: [{ type: "text", text: JSON.stringify({ error: "The 'problem' parameter (string) is required" }) }],
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
const problem = args.problem;
|
|
309
|
+
// If no LLM configured, fall back to formalize
|
|
310
|
+
if (!formalizer) {
|
|
311
|
+
const dummyEngine = new FormalizationEngine(library, {
|
|
312
|
+
async complete() { return ""; },
|
|
313
|
+
});
|
|
314
|
+
const result = await dummyEngine.formalize(problem);
|
|
315
|
+
return {
|
|
316
|
+
content: [{
|
|
317
|
+
type: "text",
|
|
318
|
+
text: JSON.stringify({
|
|
319
|
+
fallback: true,
|
|
320
|
+
message: "No ANTHROPIC_API_KEY set. Returning template instructions instead. Fill the slots and use chiasmus_verify.",
|
|
321
|
+
template: result.template.name,
|
|
322
|
+
solver: result.template.solver,
|
|
323
|
+
instructions: result.instructions,
|
|
324
|
+
}, null, 2),
|
|
325
|
+
}],
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
const result = await formalizer.solve(problem);
|
|
329
|
+
return {
|
|
330
|
+
content: [{
|
|
331
|
+
type: "text",
|
|
332
|
+
text: JSON.stringify({
|
|
333
|
+
converged: result.converged,
|
|
334
|
+
rounds: result.rounds,
|
|
335
|
+
templateUsed: result.templateUsed,
|
|
336
|
+
result: result.result,
|
|
337
|
+
answers: result.answers,
|
|
338
|
+
history: result.history.map((h) => ({
|
|
339
|
+
round: h.round,
|
|
340
|
+
status: h.result.status,
|
|
341
|
+
error: h.result.status === "error" ? h.result.error : undefined,
|
|
342
|
+
})),
|
|
343
|
+
}, null, 2),
|
|
344
|
+
}],
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
async function handleLearn(learner, args) {
|
|
348
|
+
if (!learner) {
|
|
349
|
+
return {
|
|
350
|
+
content: [{ type: "text", text: JSON.stringify({
|
|
351
|
+
error: "No ANTHROPIC_API_KEY set. chiasmus_learn requires an LLM for template extraction.",
|
|
352
|
+
}) }],
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
if (typeof args.solver !== "string" ||
|
|
356
|
+
typeof args.spec !== "string" ||
|
|
357
|
+
typeof args.problem !== "string" ||
|
|
358
|
+
!args.solver || !args.spec || !args.problem) {
|
|
359
|
+
return {
|
|
360
|
+
content: [{ type: "text", text: JSON.stringify({
|
|
361
|
+
error: "Required parameters: solver (string), spec (string), problem (string)",
|
|
362
|
+
}) }],
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
const solver = args.solver;
|
|
366
|
+
const spec = args.spec;
|
|
367
|
+
const problem = args.problem;
|
|
368
|
+
if (solver !== "z3" && solver !== "prolog") {
|
|
369
|
+
return {
|
|
370
|
+
content: [{ type: "text", text: JSON.stringify({
|
|
371
|
+
error: `Unknown solver: ${solver}. Use "z3" or "prolog".`,
|
|
372
|
+
}) }],
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
const result = await learner.extractTemplate(solver, spec, problem);
|
|
376
|
+
if (!result) {
|
|
377
|
+
return {
|
|
378
|
+
content: [{ type: "text", text: JSON.stringify({
|
|
379
|
+
extracted: false,
|
|
380
|
+
reason: "Template was rejected — either invalid, too similar to an existing template, or LLM produced unparseable output",
|
|
381
|
+
}) }],
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
// Check promotions after learning
|
|
385
|
+
learner.checkPromotions();
|
|
386
|
+
return {
|
|
387
|
+
content: [{ type: "text", text: JSON.stringify({
|
|
388
|
+
extracted: true,
|
|
389
|
+
template: result.name,
|
|
390
|
+
domain: result.domain,
|
|
391
|
+
solver: result.solver,
|
|
392
|
+
signature: result.signature,
|
|
393
|
+
slots: result.slots.length,
|
|
394
|
+
promoted: false,
|
|
395
|
+
}, null, 2) }],
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
export async function createChiasmusServer(chiasmusHome, llmOverride) {
|
|
399
|
+
const home = chiasmusHome ?? getChiasmusHome();
|
|
400
|
+
const library = await SkillLibrary.create(home);
|
|
401
|
+
// Use override if provided, otherwise try env
|
|
402
|
+
const llm = llmOverride !== undefined ? llmOverride : createLLMFromEnv();
|
|
403
|
+
const formalizer = llm ? new FormalizationEngine(library, llm) : null;
|
|
404
|
+
const learner = llm ? new SkillLearner(library, llm) : null;
|
|
405
|
+
const server = new Server({ name: "chiasmus", version: "0.1.0" }, { capabilities: { tools: {} } });
|
|
406
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
407
|
+
tools: TOOLS,
|
|
408
|
+
}));
|
|
409
|
+
// FormalizationEngine for formalize tool (always available, uses dummy LLM for template selection only)
|
|
410
|
+
const formalizeEngine = new FormalizationEngine(library, llm ?? {
|
|
411
|
+
async complete() { return ""; },
|
|
412
|
+
});
|
|
413
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
414
|
+
const { name, arguments: args } = request.params;
|
|
415
|
+
switch (name) {
|
|
416
|
+
case "chiasmus_verify":
|
|
417
|
+
return handleVerify(args ?? {});
|
|
418
|
+
case "chiasmus_skills":
|
|
419
|
+
return handleSkills(library, args ?? {});
|
|
420
|
+
case "chiasmus_formalize":
|
|
421
|
+
return handleFormalize(formalizeEngine, args ?? {});
|
|
422
|
+
case "chiasmus_solve":
|
|
423
|
+
return handleSolve(formalizer, library, args ?? {});
|
|
424
|
+
case "chiasmus_learn":
|
|
425
|
+
return handleLearn(learner, args ?? {});
|
|
426
|
+
default:
|
|
427
|
+
return {
|
|
428
|
+
content: [
|
|
429
|
+
{ type: "text", text: JSON.stringify({ status: "error", error: `Unknown tool: ${name}` }) },
|
|
430
|
+
],
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
return { server, library, formalizer };
|
|
435
|
+
}
|
|
436
|
+
// CLI entry point
|
|
437
|
+
const isMain = process.argv[1] &&
|
|
438
|
+
(process.argv[1].endsWith("mcp-server.ts") ||
|
|
439
|
+
process.argv[1].endsWith("mcp-server.js"));
|
|
440
|
+
if (isMain) {
|
|
441
|
+
const { server } = await createChiasmusServer();
|
|
442
|
+
const transport = new StdioServerTransport();
|
|
443
|
+
await server.connect(transport);
|
|
444
|
+
console.error("[Chiasmus] MCP server running on stdio");
|
|
445
|
+
}
|
|
446
|
+
//# sourceMappingURL=mcp-server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-server.js","sourceRoot":"","sources":["../src/mcp-server.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,OAAO,EAA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oBACL,OAAO,EAAE;wBACP,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,iBAAiB,IAAI,EAAE,EAAE,CAAC,EAAE;qBAC5F;iBACF,CAAC;QACN,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,CAAC;AACzC,CAAC;AAED,kBAAkB;AAClB,MAAM,MAAM,GACV,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;IACf,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,eAAe,CAAC;QACxC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAC,CAAC;AAE/C,IAAI,MAAM,EAAE,CAAC;IACX,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,oBAAoB,EAAE,CAAC;IAChD,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;IAC7C,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAChC,OAAO,CAAC,KAAK,CAAC,wCAAwC,CAAC,CAAC;AAC1D,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal BM25 search for template retrieval.
|
|
3
|
+
* Each "document" is a template's concatenated searchable text.
|
|
4
|
+
*/
|
|
5
|
+
export declare function tokenize(text: string): string[];
|
|
6
|
+
interface DocEntry {
|
|
7
|
+
index: number;
|
|
8
|
+
tokens: string[];
|
|
9
|
+
length: number;
|
|
10
|
+
}
|
|
11
|
+
export interface BM25Index {
|
|
12
|
+
docs: DocEntry[];
|
|
13
|
+
idf: Map<string, number>;
|
|
14
|
+
avgLength: number;
|
|
15
|
+
}
|
|
16
|
+
export declare function buildIndex(texts: string[]): BM25Index;
|
|
17
|
+
export declare function search(index: BM25Index, query: string, limit?: number, k1?: number, b?: number): Array<{
|
|
18
|
+
docIndex: number;
|
|
19
|
+
score: number;
|
|
20
|
+
}>;
|
|
21
|
+
export {};
|
|
22
|
+
//# sourceMappingURL=bm25.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bm25.d.ts","sourceRoot":"","sources":["../../src/skills/bm25.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAYH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAK/C;AAED,UAAU,QAAQ;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,QAAQ,EAAE,CAAC;IACjB,GAAG,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,SAAS,CAwBrD;AAED,wBAAgB,MAAM,CACpB,KAAK,EAAE,SAAS,EAChB,KAAK,EAAE,MAAM,EACb,KAAK,SAAK,EACV,EAAE,SAAM,EACR,CAAC,SAAO,GACP,KAAK,CAAC;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC,CAkC5C"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal BM25 search for template retrieval.
|
|
3
|
+
* Each "document" is a template's concatenated searchable text.
|
|
4
|
+
*/
|
|
5
|
+
const STOPWORDS = new Set([
|
|
6
|
+
"a", "an", "the", "is", "are", "was", "were", "be", "been", "being",
|
|
7
|
+
"have", "has", "had", "do", "does", "did", "will", "would", "could",
|
|
8
|
+
"should", "may", "might", "can", "shall", "to", "of", "in", "for",
|
|
9
|
+
"on", "with", "at", "by", "from", "as", "into", "through", "during",
|
|
10
|
+
"before", "after", "above", "below", "between", "and", "but", "or",
|
|
11
|
+
"not", "no", "nor", "so", "if", "then", "than", "that", "this",
|
|
12
|
+
"these", "those", "it", "its",
|
|
13
|
+
]);
|
|
14
|
+
export function tokenize(text) {
|
|
15
|
+
return text
|
|
16
|
+
.toLowerCase()
|
|
17
|
+
.split(/[^a-z0-9_]+/)
|
|
18
|
+
.filter((t) => t.length >= 2 && !STOPWORDS.has(t));
|
|
19
|
+
}
|
|
20
|
+
export function buildIndex(texts) {
|
|
21
|
+
const docs = texts.map((text, index) => {
|
|
22
|
+
const tokens = tokenize(text);
|
|
23
|
+
return { index, tokens, length: tokens.length };
|
|
24
|
+
});
|
|
25
|
+
const avgLength = docs.reduce((sum, d) => sum + d.length, 0) / (docs.length || 1);
|
|
26
|
+
// Compute IDF for each term
|
|
27
|
+
const docFreq = new Map();
|
|
28
|
+
for (const doc of docs) {
|
|
29
|
+
const seen = new Set(doc.tokens);
|
|
30
|
+
for (const term of seen) {
|
|
31
|
+
docFreq.set(term, (docFreq.get(term) ?? 0) + 1);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const N = docs.length;
|
|
35
|
+
const idf = new Map();
|
|
36
|
+
for (const [term, df] of docFreq) {
|
|
37
|
+
idf.set(term, Math.log((N - df + 0.5) / (df + 0.5) + 1));
|
|
38
|
+
}
|
|
39
|
+
return { docs, idf, avgLength };
|
|
40
|
+
}
|
|
41
|
+
export function search(index, query, limit = 10, k1 = 1.2, b = 0.75) {
|
|
42
|
+
const queryTokens = tokenize(query);
|
|
43
|
+
if (queryTokens.length === 0)
|
|
44
|
+
return [];
|
|
45
|
+
const scores = [];
|
|
46
|
+
for (const doc of index.docs) {
|
|
47
|
+
let score = 0;
|
|
48
|
+
// Count term frequencies in this doc
|
|
49
|
+
const tf = new Map();
|
|
50
|
+
for (const token of doc.tokens) {
|
|
51
|
+
tf.set(token, (tf.get(token) ?? 0) + 1);
|
|
52
|
+
}
|
|
53
|
+
for (const term of queryTokens) {
|
|
54
|
+
const termIdf = index.idf.get(term);
|
|
55
|
+
if (!termIdf)
|
|
56
|
+
continue;
|
|
57
|
+
const termTf = tf.get(term) ?? 0;
|
|
58
|
+
if (termTf === 0)
|
|
59
|
+
continue;
|
|
60
|
+
const numerator = termTf * (k1 + 1);
|
|
61
|
+
const denominator = termTf + k1 * (1 - b + b * (doc.length / index.avgLength));
|
|
62
|
+
score += termIdf * (numerator / denominator);
|
|
63
|
+
}
|
|
64
|
+
if (score > 0) {
|
|
65
|
+
scores.push({ docIndex: doc.index, score });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
scores.sort((a, b) => b.score - a.score);
|
|
69
|
+
return scores.slice(0, limit);
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=bm25.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bm25.js","sourceRoot":"","sources":["../../src/skills/bm25.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC;IACxB,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO;IACnE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO;IACnE,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK;IACjE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ;IACnE,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI;IAClE,KAAK,EAAE,IAAI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IAAI,CAAC,OAAO;gBAAE,SAAS;YAEvB,MAAM,MAAM,GAAG,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACjC,IAAI,MAAM,KAAK,CAAC;gBAAE,SAAS;YAE3B,MAAM,SAAS,GAAG,MAAM,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC;YACpC,MAAM,WAAW,GAAG,MAAM,GAAG,EAAE,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,GAAG,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC;YAC/E,KAAK,IAAI,OAAO,GAAG,CAAC,SAAS,GAAG,WAAW,CAAC,CAAC;QAC/C,CAAC;QAED,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;YACd,MAAM,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;QAC9C,CAAC;IACH,CAAC;IAED,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;IACzC,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;AAChC,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { LLMAdapter } from "../llm/types.js";
|
|
2
|
+
import type { SkillLibrary } from "./library.js";
|
|
3
|
+
import type { SkillTemplate } from "./types.js";
|
|
4
|
+
import type { SolverType } from "../solvers/types.js";
|
|
5
|
+
export declare class SkillLearner {
|
|
6
|
+
private library;
|
|
7
|
+
private llm;
|
|
8
|
+
constructor(library: SkillLibrary, llm: LLMAdapter);
|
|
9
|
+
/**
|
|
10
|
+
* Extract a reusable template from a verified solution.
|
|
11
|
+
* Returns the template if accepted, null if rejected (duplicate or invalid).
|
|
12
|
+
*/
|
|
13
|
+
extractTemplate(solver: SolverType, verifiedSpec: string, problemDescription: string): Promise<SkillTemplate | null>;
|
|
14
|
+
/** Check promotions: promote candidates with sufficient reuse and success rate */
|
|
15
|
+
checkPromotions(): void;
|
|
16
|
+
/** Check if a template is too similar to an existing one */
|
|
17
|
+
private isDuplicate;
|
|
18
|
+
/** Simple word-overlap similarity (Jaccard) for dedup */
|
|
19
|
+
private textSimilarity;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=learner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"learner.d.ts","sourceRoot":"","sources":["../../src/skills/learner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,KAAK,EAAE,aAAa,EAA0B,MAAM,YAAY,CAAC;AACxE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AA2BtD,qBAAa,YAAY;IAErB,OAAO,CAAC,OAAO;IACf,OAAO,CAAC,GAAG;gBADH,OAAO,EAAE,YAAY,EACrB,GAAG,EAAE,UAAU;IAGzB;;;OAGG;IACG,eAAe,CACnB,MAAM,EAAE,UAAU,EAClB,YAAY,EAAE,MAAM,EACpB,kBAAkB,EAAE,MAAM,GACzB,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IAkEhC,kFAAkF;IAClF,eAAe,IAAI,IAAI;IAiBvB,4DAA4D;IAC5D,OAAO,CAAC,WAAW;IAUnB,yDAAyD;IACzD,OAAO,CAAC,cAAc;CAOvB"}
|