@opencraw/mcp 0.1.45 → 0.1.47

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.
Files changed (60) hide show
  1. package/README.md +27 -1
  2. package/dist/diff-tool/diff-tool.handler.d.ts +42 -0
  3. package/dist/diff-tool/index.d.ts +3 -0
  4. package/dist/docs-tool/docs-folder.repository.d.ts +24 -0
  5. package/dist/docs-tool/docs-root.client.d.ts +8 -0
  6. package/dist/docs-tool/docs-tool.handler.d.ts +21 -0
  7. package/dist/docs-tool/docs-topics.algorithm.d.ts +39 -0
  8. package/dist/docs-tool/index.d.ts +3 -0
  9. package/dist/index.d.ts +12 -1
  10. package/dist/index.esm.js +430 -1
  11. package/dist/list-tool/index.d.ts +3 -0
  12. package/dist/list-tool/list-recipes-tool.handler.d.ts +32 -0
  13. package/dist/main.d.ts +7 -0
  14. package/dist/probe-tool/index.d.ts +2 -0
  15. package/dist/probe-tool/probe-tool.handler.d.ts +27 -0
  16. package/dist/recipe-source/index.d.ts +3 -0
  17. package/dist/recipe-source/recipe-source.mapper.d.ts +21 -0
  18. package/dist/run-tool/index.d.ts +3 -0
  19. package/dist/run-tool/run-tool.handler.d.ts +51 -0
  20. package/dist/server/create-server.use-case.d.ts +11 -0
  21. package/dist/server/index.d.ts +2 -0
  22. package/dist/src/docs-tool/docs-folder.repository.d.ts +24 -0
  23. package/dist/src/docs-tool/docs-root.client.d.ts +8 -0
  24. package/dist/src/docs-tool/docs-tool.handler.d.ts +21 -0
  25. package/dist/src/docs-tool/docs-topics.algorithm.d.ts +39 -0
  26. package/dist/src/docs-tool/index.d.ts +3 -0
  27. package/dist/src/server/create-server.use-case.d.ts +1 -1
  28. package/dist/src/studio-tool/index.d.ts +2 -0
  29. package/dist/src/studio-tool/studio-command.client.d.ts +11 -0
  30. package/dist/src/studio-tool/studio-instance.repository.d.ts +22 -0
  31. package/dist/src/studio-tool/studio-tools.handler.d.ts +47 -0
  32. package/dist/studio-tool/index.d.ts +2 -0
  33. package/dist/studio-tool/studio-command.client.d.ts +11 -0
  34. package/dist/studio-tool/studio-instance.repository.d.ts +22 -0
  35. package/dist/studio-tool/studio-tools.handler.d.ts +47 -0
  36. package/dist/validate-tool/index.d.ts +3 -0
  37. package/dist/validate-tool/validate-tool.handler.d.ts +31 -0
  38. package/docs/how-it-works/01-recipes.md +698 -0
  39. package/docs/how-it-works/02-page-steps.md +672 -0
  40. package/docs/how-it-works/03-data-steps.md +813 -0
  41. package/docs/how-it-works/04-flow-steps.md +738 -0
  42. package/docs/how-it-works/05-templates-and-scope.md +428 -0
  43. package/docs/how-it-works/06-documents.md +1413 -0
  44. package/docs/how-it-works/07-mapping-and-transforms.md +699 -0
  45. package/docs/how-it-works/08-policies.md +405 -0
  46. package/docs/how-it-works/09-running.md +804 -0
  47. package/docs/how-it-works/README.md +225 -0
  48. package/docs/recipes/README.md +22 -0
  49. package/docs/recipes/access.md +440 -0
  50. package/docs/recipes/authoring.md +1503 -0
  51. package/docs/recipes/azure-durable.md +180 -0
  52. package/docs/recipes/captcha.md +204 -0
  53. package/docs/recipes/movies-example.md +12 -0
  54. package/docs/recipes/programmatic-authoring.md +101 -0
  55. package/docs/recipes/quick-guide.md +157 -0
  56. package/docs/recipes/vehicles-example.md +9 -0
  57. package/docs/recipes/worker-mode.md +221 -0
  58. package/docs/studio/manual.md +312 -0
  59. package/docs/studio/pdf-agreements.md +150 -0
  60. package/package.json +29 -4
package/README.md CHANGED
@@ -11,7 +11,7 @@ directly: probe a page, validate recipes, run a crawl, list what's already autho
11
11
  For a shared, remote endpoint that authors recipes where they will run (and publishes them to a crawl host), see
12
12
  [`@opencraw/azure-durable`](https://github.com/Open-Craw/OpenCraw/blob/main/packages/azure-durable/README.md#write-recipes-over-mcp)'s `/mcp`.
13
13
 
14
- This does **not** auto-author recipes from a sentence. The five tools are, `list_recipes` aside, the same
14
+ This does **not** auto-author recipes from a sentence. The crawl tools are, `list_recipes` and `docs` aside, the same
15
15
  primitives the `opencraw` cli gives a terminal; the calling agent still writes the JSON recipes, using `probe` and
16
16
  `validate` to iterate, the same way this project's own example recipes were built by hand. An agent that
17
17
  can write files passes their `paths`; one that can't (a chat-only host) passes the `recipes` inline.
@@ -116,6 +116,19 @@ call, for checking what already exists before writing a new recipe.
116
116
 
117
117
  Returns `{ outputs: [{ path, id }], inputs: [{ path, id, output, mode }], others: string[] }`.
118
118
 
119
+ ### `docs`
120
+
121
+ OpenCraw's own documentation, for an agent that has to write recipes right: the authoring guide (every step, selector, transform and policy), the
122
+ how-it-works chapters, the examples and the Studio manual. They ship inside the package.
123
+
124
+ | Input | Meaning |
125
+ |---|---|
126
+ | `topic?` | A document id (`recipes/authoring`, `how-it-works/06-documents`, `studio/manual`…). Leave out to list every document, with its title and size. |
127
+ | `section?` | A heading (or part of one) of that document: only that section comes back, subsections included. A long document answers with its outline until a section is asked for. |
128
+
129
+ Returns `{ topics, hint }` without a topic, `{ topic, text }` (or `{ topic, outline, note }` for a long document) with one, and `{ topic, section, text }`
130
+ with a section.
131
+
119
132
  ### `diff`
120
133
 
121
134
  Compares two runs' JSON Lines files by record key: what was added, what was removed, and which fields changed,
@@ -131,6 +144,19 @@ Returns `{ added, removed, changed, unchanged, shrunk?, summary, changes }`: `su
131
144
  reads, `changes` the first 200 changes (`removed`, `changed` with `fields: [{ field, before, after }]`, `added`),
132
145
  and `shrunk` is set when the current run holds under half the records the previous one did.
133
146
 
147
+ ### `studio_status`, `studio_open_recipe`, `studio_run_sample`
148
+
149
+ Drive the OpenCraw Studio the person has open, so they watch the recipe being built. A running Studio announces itself in `~/.opencraw/studio/<pid>-<port>.json`
150
+ (its address and its access token, readable by its owner only); these tools find it there, so nothing needs configuring. If several run, the newest is used.
151
+
152
+ | Tool | Input | Returns |
153
+ |---|---|---|
154
+ | `studio_status` | none | `{ running, url, folder?, pages, running? }`: whether Studio is up, the folder it has open, how many connections are listening (an open page holds a couple), the recipe being sampled. Without a Studio: `{ running: false, hint }`. |
155
+ | `studio_open_recipe` | `path` (a recipe file, a folder or a document), `workspace?` | `{ opened: true, pages }`, or `{ opened: false, hint }` when Studio runs but no page is open. The open page does it the way a Studio link does. |
156
+ | `studio_run_sample` | `recipeId`, `maxRecords?` (10), `waitSeconds?` (60) | `{ recipeId, state, result? }`: the counts, the first 20 records and rejections once finished. Studio must have the recipe's folder open (`studio_open_recipe` first). |
157
+
158
+ Picking a value on a page (`studio_pick`) is not here yet: it is still the person's click.
159
+
134
160
  ## Building
135
161
 
136
162
  `nx build @opencraw/mcp`, `nx test @opencraw/mcp` (unit tests per tool), `nx run @opencraw/mcp:e2e`
@@ -0,0 +1,42 @@
1
+ import type { CallToolResult } from '@modelcontextprotocol/sdk/types.js';
2
+ import type { RecordChange } from '@opencraw/core';
3
+ import { z } from 'zod';
4
+ /** Input schema for the `diff` tool. */
5
+ export declare const diffInputShape: {
6
+ previous: z.ZodString;
7
+ current: z.ZodString;
8
+ key: z.ZodOptional<z.ZodArray<z.ZodString>>;
9
+ ignore: z.ZodOptional<z.ZodArray<z.ZodString>>;
10
+ };
11
+ interface DiffArgs {
12
+ previous: string;
13
+ current: string;
14
+ key?: string[];
15
+ ignore?: string[];
16
+ }
17
+ /** What the `diff` tool returns. */
18
+ export interface DiffResult {
19
+ added: number;
20
+ removed: number;
21
+ changed: number;
22
+ unchanged: number;
23
+ /** Set when the current run lost most of the previous run's records. */
24
+ shrunk?: {
25
+ previous: number;
26
+ current: number;
27
+ };
28
+ /** The report a person reads, one line per change. */
29
+ summary: string[];
30
+ /** The changes, up to 200: removed, changed (with each field's before and after), added. */
31
+ changes: RecordChange[];
32
+ }
33
+ /**
34
+ * The `diff` tool: compares two runs' records by key, so an agent watching a
35
+ * source can say what changed since last time instead of re-reading it all.
36
+ *
37
+ * @param args - The tool's parsed input.
38
+ * @returns The MCP tool result.
39
+ */
40
+ export declare function diffTool(args: DiffArgs): Promise<CallToolResult>;
41
+ export {};
42
+ //# sourceMappingURL=diff-tool.handler.d.ts.map
@@ -0,0 +1,3 @@
1
+ export { diffTool, diffInputShape } from './diff-tool.handler';
2
+ export type { DiffResult } from './diff-tool.handler';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,24 @@
1
+ /** One document the server can show. */
2
+ export interface DocTopic {
3
+ id: string;
4
+ title: string;
5
+ /** Its size in characters, so an agent knows to ask for a section of a long one. */
6
+ size: number;
7
+ }
8
+ /**
9
+ * The documents under a docs folder: the Markdown files directly inside `recipes`, `how-it-works` and `studio` (their pictures and capture scripts are
10
+ * not documentation to read).
11
+ *
12
+ * @param root - The docs folder.
13
+ * @returns The topics, ordered by id.
14
+ */
15
+ export declare function listTopics(root: string): Promise<DocTopic[]>;
16
+ /**
17
+ * One document's text.
18
+ *
19
+ * @param root - The docs folder.
20
+ * @param id - The topic id from `listTopics` (never a path: nothing outside the listed folders is read).
21
+ * @returns The Markdown, or `undefined` when there is no such topic.
22
+ */
23
+ export declare function readTopic(root: string, id: string): Promise<string | undefined>;
24
+ //# sourceMappingURL=docs-folder.repository.d.ts.map
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Where the documentation is: a `docs` folder beside `dist` in the installed package (copied there at build), or the repository's own `docs` when the server runs
3
+ * from a checkout.
4
+ *
5
+ * @returns The docs folder.
6
+ */
7
+ export declare function docsRoot(): string;
8
+ //# sourceMappingURL=docs-root.client.d.ts.map
@@ -0,0 +1,21 @@
1
+ import type { CallToolResult } from '@modelcontextprotocol/sdk/types.js';
2
+ import { z } from 'zod';
3
+ /** Input schema for the `docs` tool. */
4
+ export declare const docsInputShape: {
5
+ topic: z.ZodOptional<z.ZodString>;
6
+ section: z.ZodOptional<z.ZodString>;
7
+ };
8
+ /**
9
+ * The `docs` tool: OpenCraw's own documentation, for an agent that has to write recipes right: the authoring guide (every step, selector, transform
10
+ * and policy), how it all works, the examples and the Studio manual. Without a topic it lists what there is; with one it gives the document, or just
11
+ * the section asked for, so a long guide costs only what is read.
12
+ *
13
+ * @param args - The tool's parsed input.
14
+ * @param root - The docs folder (the packaged copy, or the repository's).
15
+ * @returns The MCP tool result.
16
+ */
17
+ export declare function docsTool(args: {
18
+ topic?: string;
19
+ section?: string;
20
+ }, root: string): Promise<CallToolResult>;
21
+ //# sourceMappingURL=docs-tool.handler.d.ts.map
@@ -0,0 +1,39 @@
1
+ /** One heading of a document: how deep it is, its text and where its line is. */
2
+ export interface DocHeading {
3
+ level: number;
4
+ text: string;
5
+ line: number;
6
+ }
7
+ /**
8
+ * The topic id of a documentation file: its path under the docs folder, without the extension and with forward slashes, so `recipes/authoring.md` is
9
+ * `recipes/authoring`.
10
+ *
11
+ * @param relativePath - The file's path under the docs folder.
12
+ * @returns The id.
13
+ */
14
+ export declare function topicIdOf(relativePath: string): string;
15
+ /**
16
+ * The title of a document: its first top-level heading, else its id.
17
+ *
18
+ * @param markdown - The document.
19
+ * @param fallback - The id, when there is no heading.
20
+ * @returns The title.
21
+ */
22
+ export declare function titleOf(markdown: string, fallback: string): string;
23
+ /**
24
+ * Every heading of a Markdown document, in order. A line inside a fenced code block is not a heading (a shell comment starts with `#` too).
25
+ *
26
+ * @param markdown - The document.
27
+ * @returns The headings.
28
+ */
29
+ export declare function outlineOf(markdown: string): DocHeading[];
30
+ /**
31
+ * One section of a document: from the heading whose text contains `heading` (case-insensitive; an exact match wins over a partial one) to the next
32
+ * heading of the same or a higher level, so its subsections come with it.
33
+ *
34
+ * @param markdown - The document.
35
+ * @param heading - What the heading says, or part of it.
36
+ * @returns The section's text, or `undefined` when no heading matches.
37
+ */
38
+ export declare function sectionOf(markdown: string, heading: string): string | undefined;
39
+ //# sourceMappingURL=docs-topics.algorithm.d.ts.map
@@ -0,0 +1,3 @@
1
+ export { docsTool, docsInputShape } from './docs-tool.handler';
2
+ export { docsRoot } from './docs-root.client';
3
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.d.ts CHANGED
@@ -1 +1,12 @@
1
- export * from "./src/index.js";
1
+ export { main } from './main';
2
+ export { createServer } from './server';
3
+ export { probeTool, probeInputShape } from './probe-tool';
4
+ export { validateTool, validateInputShape } from './validate-tool';
5
+ export { diffTool, diffInputShape } from './diff-tool';
6
+ export type { DiffResult } from './diff-tool';
7
+ export type { ValidateResult } from './validate-tool';
8
+ export { runTool, runInputShape } from './run-tool';
9
+ export type { RunResult } from './run-tool';
10
+ export { listRecipesTool, listRecipesInputShape } from './list-tool';
11
+ export type { ListRecipesResult } from './list-tool';
12
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.esm.js CHANGED
@@ -3,6 +3,11 @@ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
3
3
  import { readRecordsFile, diffRecords, memorySink, fileSink, createCrawler, RecipeSet, RecipeValidationError, RecipeBindingError, parseOutputRecipe, parseInputRecipe, bindRecipeSet } from '@opencraw/core';
4
4
  import { diffReport, readRecipeFiles, probeUrl, loadForRun, loadPlugins, resolveAccess } from '@opencraw/cli';
5
5
  import { z } from 'zod';
6
+ import { readdir, readFile } from 'node:fs/promises';
7
+ import { join, dirname } from 'node:path';
8
+ import { existsSync } from 'node:fs';
9
+ import { fileURLToPath } from 'node:url';
10
+ import { homedir } from 'node:os';
6
11
 
7
12
  /** Changes returned at most; the counts cover all of them. */
8
13
  const CHANGE_LIMIT = 200;
@@ -56,6 +61,191 @@ async function diffTool(args) {
56
61
  }
57
62
  }
58
63
 
64
+ /**
65
+ * The topic id of a documentation file: its path under the docs folder, without the extension and with forward slashes, so `recipes/authoring.md` is
66
+ * `recipes/authoring`.
67
+ *
68
+ * @param relativePath - The file's path under the docs folder.
69
+ * @returns The id.
70
+ */
71
+ function topicIdOf(relativePath) {
72
+ return relativePath.replaceAll('\\', '/').replace(/\.md$/i, '');
73
+ }
74
+ /**
75
+ * The title of a document: its first top-level heading, else its id.
76
+ *
77
+ * @param markdown - The document.
78
+ * @param fallback - The id, when there is no heading.
79
+ * @returns The title.
80
+ */
81
+ function titleOf(markdown, fallback) {
82
+ const heading = outlineOf(markdown).find(candidate => candidate.level === 1);
83
+ return heading?.text ?? fallback;
84
+ }
85
+ /**
86
+ * Every heading of a Markdown document, in order. A line inside a fenced code block is not a heading (a shell comment starts with `#` too).
87
+ *
88
+ * @param markdown - The document.
89
+ * @returns The headings.
90
+ */
91
+ function outlineOf(markdown) {
92
+ const headings = [];
93
+ let fence;
94
+ for (const [line, text] of markdown.split(/\r?\n/).entries()) {
95
+ const marker = /^\s*(`{3,}|~{3,})/.exec(text)?.[1];
96
+ if (marker !== undefined) {
97
+ if (fence === undefined) fence = marker[0];else if (marker[0] === fence) fence = undefined;
98
+ continue;
99
+ }
100
+ if (fence !== undefined) continue;
101
+ const match = /^(#{1,6}) (.+)$/.exec(text);
102
+ if (match !== null) headings.push({
103
+ level: match[1].length,
104
+ text: match[2].trim().replace(/ #+$/, ''),
105
+ line
106
+ });
107
+ }
108
+ return headings;
109
+ }
110
+ /**
111
+ * One section of a document: from the heading whose text contains `heading` (case-insensitive; an exact match wins over a partial one) to the next
112
+ * heading of the same or a higher level, so its subsections come with it.
113
+ *
114
+ * @param markdown - The document.
115
+ * @param heading - What the heading says, or part of it.
116
+ * @returns The section's text, or `undefined` when no heading matches.
117
+ */
118
+ function sectionOf(markdown, heading) {
119
+ const wanted = heading.trim().toLowerCase();
120
+ const headings = outlineOf(markdown);
121
+ const start = headings.find(candidate => candidate.text.toLowerCase() === wanted) ?? headings.find(candidate => candidate.text.toLowerCase().includes(wanted));
122
+ if (start === undefined) return undefined;
123
+ const end = headings.find(candidate => candidate.line > start.line && candidate.level <= start.level);
124
+ const lines = markdown.split(/\r?\n/);
125
+ return lines.slice(start.line, end?.line ?? lines.length).join('\n').trim();
126
+ }
127
+
128
+ /** The folders under the docs folder whose Markdown files are documentation for a person (or an agent) using OpenCraw. */
129
+ const FOLDERS = ['recipes', 'how-it-works', 'studio'];
130
+ /**
131
+ * The documents under a docs folder: the Markdown files directly inside `recipes`, `how-it-works` and `studio` (their pictures and capture scripts are
132
+ * not documentation to read).
133
+ *
134
+ * @param root - The docs folder.
135
+ * @returns The topics, ordered by id.
136
+ */
137
+ async function listTopics(root) {
138
+ const topics = [];
139
+ for (const folder of FOLDERS) {
140
+ let names;
141
+ try {
142
+ names = await readdir(join(root, folder));
143
+ } catch {
144
+ continue;
145
+ }
146
+ const documents = names.filter(file => file.toLowerCase().endsWith('.md'));
147
+ for (const name of documents) {
148
+ const id = topicIdOf(`${folder}/${name}`);
149
+ const text = await readFile(join(root, folder, name), 'utf8');
150
+ topics.push({
151
+ id,
152
+ title: titleOf(text, id),
153
+ size: text.length
154
+ });
155
+ }
156
+ }
157
+ return topics.sort((a, b) => a.id.localeCompare(b.id));
158
+ }
159
+ /**
160
+ * One document's text.
161
+ *
162
+ * @param root - The docs folder.
163
+ * @param id - The topic id from `listTopics` (never a path: nothing outside the listed folders is read).
164
+ * @returns The Markdown, or `undefined` when there is no such topic.
165
+ */
166
+ async function readTopic(root, id) {
167
+ const known = await listTopics(root);
168
+ if (known.every(topic => topic.id !== id)) return undefined;
169
+ return await readFile(join(root, `${id}.md`), 'utf8');
170
+ }
171
+
172
+ /** Input schema for the `docs` tool. */
173
+ const docsInputShape = {
174
+ topic: z.string().optional().describe('A document id from the list this tool returns without a topic (`recipes/authoring`, `how-it-works/06-documents`, `studio/manual`…). Leave out to list every document.'),
175
+ section: z.string().optional().describe('A heading (or part of one) of that document: only that section comes back, subsections included. A long document answers with its outline until you ask for a section.')
176
+ };
177
+ /** A document longer than this many characters comes back as its outline, and the section asked for. */
178
+ const WHOLE_DOCUMENT_LIMIT = 30_000;
179
+ function text$1(value, isError = false) {
180
+ return {
181
+ content: [{
182
+ type: 'text',
183
+ text: typeof value === 'string' ? value : JSON.stringify(value)
184
+ }],
185
+ ...(isError && {
186
+ isError
187
+ }),
188
+ ...(typeof value === 'object' && value !== null && {
189
+ structuredContent: value
190
+ })
191
+ };
192
+ }
193
+ /**
194
+ * The `docs` tool: OpenCraw's own documentation, for an agent that has to write recipes right: the authoring guide (every step, selector, transform
195
+ * and policy), how it all works, the examples and the Studio manual. Without a topic it lists what there is; with one it gives the document, or just
196
+ * the section asked for, so a long guide costs only what is read.
197
+ *
198
+ * @param args - The tool's parsed input.
199
+ * @param root - The docs folder (the packaged copy, or the repository's).
200
+ * @returns The MCP tool result.
201
+ */
202
+ async function docsTool(args, root) {
203
+ const topics = await listTopics(root);
204
+ if (topics.length === 0) return text$1('The documentation is not installed with this server.', true);
205
+ if (args.topic === undefined) return text$1({
206
+ topics,
207
+ hint: 'Ask for one with { topic }, and for part of a long one with { topic, section }.'
208
+ });
209
+ const document = await readTopic(root, args.topic);
210
+ if (document === undefined) {
211
+ const close = topics.filter(topic => topic.id.includes(args.topic?.toLowerCase() ?? '') || args.topic?.includes(topic.id) === true).map(topic => topic.id);
212
+ return text$1(`There is no document "${args.topic}". ${close.length > 0 ? `Did you mean: ${close.join(', ')}?` : `The documents are: ${topics.map(topic => topic.id).join(', ')}.`}`, true);
213
+ }
214
+ if (args.section !== undefined) {
215
+ const section = sectionOf(document, args.section);
216
+ return section === undefined ? text$1({
217
+ topic: args.topic,
218
+ error: `no heading contains "${args.section}"`,
219
+ outline: outlineOf(document).map(heading => `${'#'.repeat(heading.level)} ${heading.text}`)
220
+ }, true) : text$1({
221
+ topic: args.topic,
222
+ section: args.section,
223
+ text: section
224
+ });
225
+ }
226
+ if (document.length <= WHOLE_DOCUMENT_LIMIT) return text$1({
227
+ topic: args.topic,
228
+ text: document
229
+ });
230
+ return text$1({
231
+ topic: args.topic,
232
+ outline: outlineOf(document).map(heading => `${'#'.repeat(heading.level)} ${heading.text}`),
233
+ note: `This document is ${String(document.length)} characters: ask for a section with { topic, section } using a heading from the outline.`
234
+ });
235
+ }
236
+
237
+ /**
238
+ * Where the documentation is: a `docs` folder beside `dist` in the installed package (copied there at build), or the repository's own `docs` when the server runs
239
+ * from a checkout.
240
+ *
241
+ * @returns The docs folder.
242
+ */
243
+ function docsRoot() {
244
+ const here = dirname(fileURLToPath(import.meta.url));
245
+ const candidates = [join(here, '..', 'docs'), join(here, '..', '..', '..', 'docs')];
246
+ return candidates.find(candidate => existsSync(join(candidate, 'recipes'))) ?? candidates[0];
247
+ }
248
+
59
249
  /** Input schema for the `list_recipes` tool. */
60
250
  const listRecipesInputShape = {
61
251
  dir: z.string().describe('A directory of recipe files.')
@@ -308,6 +498,229 @@ function loadErrorText(error) {
308
498
  return error instanceof Error ? error.message : String(error);
309
499
  }
310
500
 
501
+ /**
502
+ * Sends one command to a running Studio (`POST /api/command`, the same door its own page uses) and reads the answer.
503
+ *
504
+ * @param instance - The Studio to talk to.
505
+ * @param command - A command `@opencraw/studio`'s `studioCommandSchema` accepts.
506
+ * @returns The JSON the Studio answered.
507
+ * @throws Error with the Studio's own message when it refuses.
508
+ */
509
+ async function sendStudioCommand(instance, command) {
510
+ const response = await fetch(`${instance.url}/api/command`, {
511
+ method: 'POST',
512
+ headers: {
513
+ 'content-type': 'application/json',
514
+ 'x-opencraw-token': instance.token
515
+ },
516
+ body: JSON.stringify(command)
517
+ });
518
+ const body = await response.json();
519
+ if (!response.ok) throw new Error(body.error ?? `Studio answered ${String(response.status)}`);
520
+ return body;
521
+ }
522
+
523
+ /** Where running Studios announce themselves. */
524
+ function instancesDirectory() {
525
+ return join(homedir(), '.opencraw', 'studio');
526
+ }
527
+ function isAlive(pid) {
528
+ try {
529
+ process.kill(pid, 0);
530
+ return true;
531
+ } catch (error) {
532
+ // EPERM: the process exists but is not ours to signal.
533
+ return error.code === 'EPERM';
534
+ }
535
+ }
536
+ function instanceOf(text) {
537
+ try {
538
+ const value = JSON.parse(text);
539
+ return typeof value.pid === 'number' && typeof value.url === 'string' && typeof value.token === 'string' && typeof value.startedAt === 'string' ? {
540
+ pid: value.pid,
541
+ url: value.url,
542
+ token: value.token,
543
+ folder: value.folder,
544
+ startedAt: value.startedAt
545
+ } : undefined;
546
+ } catch {
547
+ return undefined;
548
+ }
549
+ }
550
+ /** The announcement files in a directory; none when it does not exist yet. */
551
+ async function namesIn(directory) {
552
+ try {
553
+ const names = await readdir(directory);
554
+ return names.filter(file => file.endsWith('.json'));
555
+ } catch {
556
+ return [];
557
+ }
558
+ }
559
+ /** A file's text; empty when it vanished (its Studio just stopped). */
560
+ async function textOf(file) {
561
+ try {
562
+ return await readFile(file, 'utf8');
563
+ } catch {
564
+ return '';
565
+ }
566
+ }
567
+ /**
568
+ * The Studios running on this machine, the newest first. A file whose process is gone (Studio was killed
569
+ * and could not withdraw it) is left out.
570
+ *
571
+ * @param directory - Where Studios announce themselves.
572
+ * @returns The live Studios.
573
+ */
574
+ async function runningStudios(directory = instancesDirectory()) {
575
+ const found = [];
576
+ const names = await namesIn(directory);
577
+ for (const name of names) {
578
+ const instance = instanceOf(await textOf(join(directory, name)));
579
+ if (instance !== undefined && isAlive(instance.pid)) found.push(instance);
580
+ }
581
+ return found.sort((a, b) => b.startedAt.localeCompare(a.startedAt));
582
+ }
583
+
584
+ /** Input schema for `studio_status`: nothing. */
585
+ const studioStatusInputShape = {};
586
+ /** Input schema for `studio_open_recipe`. */
587
+ const studioOpenRecipeInputShape = {
588
+ path: z.string().min(1).describe('A recipe file (`*.input.json` / `*.ocw-in.json`), a folder of recipes, or a document, as an absolute path.'),
589
+ workspace: z.string().optional().describe('The folder a document belongs to, when `path` is a document.')
590
+ };
591
+ /** Input schema for `studio_run_sample`. */
592
+ const studioRunSampleInputShape = {
593
+ recipeId: z.string().min(1).describe('The id of an input recipe in the folder Studio has open.'),
594
+ maxRecords: z.number().int().positive().optional().describe('Stop after this many records. Default 10.'),
595
+ waitSeconds: z.number().int().positive().max(300).optional().describe('How long to wait for the run to finish before answering that it is still running. Default 60.')
596
+ };
597
+ const NO_STUDIO = 'No Studio is running. Start one with `opencraw studio <folder>`, or open a recipe from the OpenCraw view in VS Code ("Open in Studio"), then ask again.';
598
+ const POLL_MS = 500;
599
+ function text(value, isError = false) {
600
+ return {
601
+ content: [{
602
+ type: 'text',
603
+ text: typeof value === 'string' ? value : JSON.stringify(value)
604
+ }],
605
+ ...(isError && {
606
+ isError
607
+ }),
608
+ ...(typeof value === 'object' && value !== null && {
609
+ structuredContent: value
610
+ })
611
+ };
612
+ }
613
+ function messageOf(error) {
614
+ return error instanceof Error ? error.message : String(error);
615
+ }
616
+ async function studioOf(directory) {
617
+ const [newest] = await runningStudios(directory);
618
+ return newest;
619
+ }
620
+ /**
621
+ * `studio_status`: whether a Studio is running, which folder it has open, how many pages are watching and what it is running.
622
+ *
623
+ * @param directory - Where Studios announce themselves; the default is the user's own.
624
+ * @returns The MCP tool result.
625
+ */
626
+ async function studioStatusTool(directory) {
627
+ const studio = await studioOf(directory);
628
+ if (studio === undefined) return text({
629
+ running: false,
630
+ hint: NO_STUDIO
631
+ });
632
+ try {
633
+ const status = await sendStudioCommand(studio, {
634
+ type: 'studio-status'
635
+ });
636
+ return text({
637
+ running: true,
638
+ url: studio.url,
639
+ ...status
640
+ });
641
+ } catch (error) {
642
+ return text(`Studio did not answer: ${messageOf(error)}`, true);
643
+ }
644
+ }
645
+ /**
646
+ * `studio_open_recipe`: opens a recipe, a folder or a document in the Studio the person is looking at, so they watch the work happen.
647
+ *
648
+ * @param args - What to open.
649
+ * @param directory - Where Studios announce themselves.
650
+ * @returns The MCP tool result.
651
+ */
652
+ async function studioOpenRecipeTool(args, directory) {
653
+ const studio = await studioOf(directory);
654
+ if (studio === undefined) return text(NO_STUDIO, true);
655
+ try {
656
+ const {
657
+ pages
658
+ } = await sendStudioCommand(studio, {
659
+ type: 'ask-ui',
660
+ request: {
661
+ kind: 'open',
662
+ path: args.path,
663
+ workspace: args.workspace
664
+ }
665
+ });
666
+ if (pages === 0) return text({
667
+ opened: false,
668
+ hint: 'Studio is running but no page is open on it. Ask the person to open Studio in their browser.',
669
+ url: studio.url
670
+ });
671
+ return text({
672
+ opened: true,
673
+ pages
674
+ });
675
+ } catch (error) {
676
+ return text(`Studio refused: ${messageOf(error)}`, true);
677
+ }
678
+ }
679
+ /**
680
+ * `studio_run_sample`: runs an input recipe as a sample in Studio (the person sees the records arrive), waits for it, and returns the
681
+ * counts and the first records.
682
+ *
683
+ * @param args - The recipe, how many records to stop at and how long to wait.
684
+ * @param directory - Where Studios announce themselves.
685
+ * @returns The MCP tool result.
686
+ */
687
+ async function studioRunSampleTool(args, directory) {
688
+ const studio = await studioOf(directory);
689
+ if (studio === undefined) return text(NO_STUDIO, true);
690
+ try {
691
+ const {
692
+ folder
693
+ } = await sendStudioCommand(studio, {
694
+ type: 'studio-status'
695
+ });
696
+ if (folder === undefined) return text('Studio has no folder open yet. Call studio_open_recipe with a recipe file or a folder first.', true);
697
+ await sendStudioCommand(studio, {
698
+ type: 'run-sample',
699
+ recipeId: args.recipeId,
700
+ budget: {
701
+ maxRecords: args.maxRecords ?? 10
702
+ }
703
+ });
704
+ const deadline = Date.now() + (args.waitSeconds ?? 60) * 1000;
705
+ for (;;) {
706
+ const status = await sendStudioCommand(studio, {
707
+ type: 'run-status',
708
+ recipeId: args.recipeId
709
+ });
710
+ if (status.state !== 'running') return text(status);
711
+ if (Date.now() >= deadline) return text({
712
+ ...status,
713
+ hint: 'Still running; ask again with studio_run_sample or look in Studio.'
714
+ });
715
+ await new Promise(resolve => {
716
+ setTimeout(resolve, POLL_MS);
717
+ });
718
+ }
719
+ } catch (error) {
720
+ return text(`Studio could not run it: ${messageOf(error)}`, true);
721
+ }
722
+ }
723
+
311
724
  /** Input schema for the `validate` tool. */
312
725
  const validateInputShape = recipeSourceShape;
313
726
  /**
@@ -408,7 +821,7 @@ const SERVER_INFO = {
408
821
  };
409
822
  /**
410
823
  * Builds the MCP server: one tool per crawl primitive (`probe`, `validate`,
411
- * `run`, `list_recipes`, `diff`), each a thin wrapper over `@opencraw/core` and
824
+ * `run`, `list_recipes`, `diff`), `docs` and the `studio_` tools that drive a running Studio, each a thin wrapper over `@opencraw/core` and
412
825
  * `@opencraw/cli`'s structured helpers. No transport is attached; `main`
413
826
  * connects it over stdio.
414
827
  *
@@ -436,6 +849,22 @@ function createServer() {
436
849
  description: 'Compare two runs\' JSON Lines files by record key: what was added, removed, and changed (each field before and after). Use to report what changed in a source since the last crawl.',
437
850
  inputSchema: diffInputShape
438
851
  }, args => diffTool(args));
852
+ server.registerTool('docs', {
853
+ description: 'OpenCraw\'s own documentation: the authoring guide (every step, selector, transform and policy), how it works, the examples and the Studio manual. Without a topic it lists the documents; with one it gives the document, or just a section of it. Read the guide before writing a recipe you are not sure of, instead of guessing.',
854
+ inputSchema: docsInputShape
855
+ }, args => docsTool(args, docsRoot()));
856
+ server.registerTool('studio_status', {
857
+ description: 'Whether OpenCraw Studio is running on this machine, which folder it has open, how many pages are watching and which recipe it is sampling. Call it before the other studio_ tools.',
858
+ inputSchema: studioStatusInputShape
859
+ }, () => studioStatusTool());
860
+ server.registerTool('studio_open_recipe', {
861
+ description: 'Open a recipe, a folder of recipes or a document in the Studio the person has open, so they watch the recipe being built. Does nothing to the files.',
862
+ inputSchema: studioOpenRecipeInputShape
863
+ }, args => studioOpenRecipeTool(args));
864
+ server.registerTool('studio_run_sample', {
865
+ description: 'Run an input recipe as a sample in Studio (the person sees the records arrive) and get back the counts, the first records and what was rejected. Studio must have the folder of that recipe open: call studio_open_recipe first.',
866
+ inputSchema: studioRunSampleInputShape
867
+ }, args => studioRunSampleTool(args));
439
868
  return server;
440
869
  }
441
870
 
@@ -0,0 +1,3 @@
1
+ export { listRecipesTool, listRecipesInputShape } from './list-recipes-tool.handler';
2
+ export type { ListRecipesResult } from './list-recipes-tool.handler';
3
+ //# sourceMappingURL=index.d.ts.map