@klura/mcp 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.
Files changed (5) hide show
  1. package/LICENSE +113 -0
  2. package/README.md +57 -0
  3. package/index.js +294 -0
  4. package/package.json +43 -0
  5. package/tools.js +1220 -0
package/LICENSE ADDED
@@ -0,0 +1,113 @@
1
+ Business Source License 1.1
2
+
3
+ Parameters
4
+
5
+ Licensor: Narek Mailian, a natural person resident in Sweden,
6
+ his successors and assigns
7
+
8
+ Licensed Work: klura-mcp
9
+ Copyright (c) 2026 Narek Mailian and Klura
10
+ contributors
11
+
12
+ Additional Use Grant: You may make production use of the Licensed Work,
13
+ provided that your production use does not include any
14
+ of the following:
15
+
16
+ (a) offering the Licensed Work, in whole or in part,
17
+ to third parties as a hosted or managed service;
18
+
19
+ (b) exposing the Licensed Work's functionality to
20
+ third parties via an API, SDK, or other interface;
21
+
22
+ (c) using the Licensed Work to build, offer, or
23
+ operate a product or service that competes with
24
+ any commercial product or service offered by
25
+ Licensor (a "Competing Service"); or
26
+
27
+ (d) sublicensing, selling, or reselling access to the
28
+ Licensed Work or its functionality.
29
+
30
+ Change Date: 2030-04-23
31
+
32
+ Change License: Apache License, Version 2.0
33
+
34
+ For information about alternative licensing arrangements for the Licensed
35
+ Work, please contact hello@klura.ai.
36
+
37
+ -----------------------------------------------------------------------------
38
+
39
+ Notice
40
+
41
+ Business Source License 1.1
42
+
43
+ License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved.
44
+ "Business Source License" is a trademark of MariaDB Corporation Ab.
45
+
46
+ -----------------------------------------------------------------------------
47
+
48
+ Terms
49
+
50
+ The Licensor hereby grants you the right to copy, modify, create derivative
51
+ works, redistribute, and make non-production use of the Licensed Work. The
52
+ Licensor may make an Additional Use Grant, above, permitting limited
53
+ production use.
54
+
55
+ Effective on the Change Date, or the fourth anniversary of the first publicly
56
+ available distribution of a specific version of the Licensed Work under this
57
+ License, whichever comes first, the Licensor hereby grants you rights under
58
+ the terms of the Change License, and the rights granted in the paragraph
59
+ above terminate.
60
+
61
+ If your use of the Licensed Work does not comply with the requirements
62
+ currently in effect as described in this License, you must purchase a
63
+ commercial license from the Licensor, its affiliated entities, or authorized
64
+ resellers, or you must refrain from using the Licensed Work.
65
+
66
+ All copies of the original and modified Licensed Work, and derivative works
67
+ of the Licensed Work, are subject to this License. This License applies
68
+ separately for each version of the Licensed Work and the Change Date may vary
69
+ for each version of the Licensed Work released by Licensor.
70
+
71
+ You must conspicuously display this License on each original or modified copy
72
+ of the Licensed Work. If you receive the Licensed Work in original or
73
+ modified form from a third party, the terms and conditions set forth in this
74
+ License apply to your use of that work.
75
+
76
+ Any use of the Licensed Work in violation of this License will automatically
77
+ terminate your rights under this License for the current and all other
78
+ versions of the Licensed Work.
79
+
80
+ This License does not grant you any right in any trademark or logo of
81
+ Licensor or its affiliates (provided that you may use a trademark or logo of
82
+ Licensor as expressly required by this License).
83
+
84
+ TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
85
+ AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
86
+ EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
87
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
88
+ TITLE.
89
+
90
+ MariaDB hereby grants you permission to use this License's text to license
91
+ your works, and to refer to it using the trademark "Business Source License",
92
+ as long as you comply with the Covenants of Licensor below.
93
+
94
+ Covenants of Licensor
95
+
96
+ In consideration of the right to use this License's text and the "Business
97
+ Source License" name and trademark, Licensor covenants to MariaDB, and to all
98
+ other recipients of the licensed work to be provided by Licensor:
99
+
100
+ 1. To specify as the Change License the GPL Version 2.0 or any later version,
101
+ or a license that is compatible with GPL Version 2.0 or a later version,
102
+ where "compatible" means that software provided under the Change License
103
+ can be included in a program with software provided under GPL Version 2.0
104
+ or a later version. Licensor may specify additional Change Licenses
105
+ without limitation.
106
+
107
+ 2. To either: (a) specify an additional grant of rights to use that does not
108
+ impose any additional restriction on the right granted in this License,
109
+ as the Additional Use Grant; or (b) insert the text "None".
110
+
111
+ 3. To specify a Change Date.
112
+
113
+ 4. Not to modify this License in any other way.
package/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # @klura/mcp
2
+
3
+ MCP (Model Context Protocol) server for [@klura/runtime](https://www.npmjs.com/package/@klura/runtime). Exposes the klura runtime's browser-automation + skill-discovery toolset to any MCP client — Claude Desktop, Claude Code, Cursor, Windsurf, and others.
4
+
5
+ `@klura/runtime` is the runtime that turns websites into reusable skills: drive the site once, save the recipe, skip the browser on every subsequent run. `@klura/mcp` is the thin wrapper that speaks MCP on top of it. See the [runtime README](https://www.npmjs.com/package/@klura/runtime) for what the skills actually do and how they get saved.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install -g @klura/mcp
11
+ ```
12
+
13
+ The `@klura/runtime` package is declared as a dependency and installed alongside. The runtime auto-starts a local daemon on first use and stores everything it learns under `~/.klura/`.
14
+
15
+ ## Wire it up
16
+
17
+ Add the server to your MCP client's config. The exact file path depends on the client — the server definition is the same.
18
+
19
+ ```json
20
+ {
21
+ "mcpServers": {
22
+ "klura": {
23
+ "command": "npx",
24
+ "args": ["-y", "@klura/mcp"]
25
+ }
26
+ }
27
+ }
28
+ ```
29
+
30
+ Restart the client. The agent picks up the klura toolset automatically.
31
+
32
+ ## What it exposes
33
+
34
+ Two surfaces land in the agent's context:
35
+
36
+ - **Tools** — browser automation (`start_session`, `perform_action`, `get_screenshot`, `get_a11y_tree`), discovery + persistence (`save_strategy`, `execute`, `list_platform_skills`, `get_strategy`), network-log inspection (`get_network_log`, `find_in_page`), and the reverse-engineering escape hatches (`inspect_ws_frame`, `try_generator`, `js_eval`, `get_js_source`, `search_js_source`, `read_js_function`, `set_breakpoint`, `wait_for_pause`, and more). The runtime owns the canonical list; this server mirrors it one-for-one.
37
+ - **Resource** `klura://reference` — the detailed reference doc, served section-by-section via URL fragments (`klura://reference#reverse-engineer-playbook`, `klura://reference#strategy-schemas-overview`, etc.) so each response fits inside the MCP output budget. Fetch `klura://reference` with no fragment for a table of contents.
38
+
39
+ The always-loaded orientation is SKILL.md, passed as the server's `instructions` capability. Agents read SKILL.md on every conversation and pull detail on demand via the `klura://reference` fragments.
40
+
41
+ ## How it works
42
+
43
+ Thin wrapper — each MCP `tools/call` dispatches to the corresponding klura runtime function. The runtime handles daemon lifecycle, browser sessions, strategy persistence under `~/.klura/skills/`, and execution.
44
+
45
+ ```
46
+ MCP client (Claude Desktop, Cursor, …)
47
+ │ stdio transport, JSON-RPC
48
+
49
+ klura-mcp (this package)
50
+ │ Node require('@klura/runtime')
51
+
52
+ klura runtime → local daemon → Playwright
53
+ ```
54
+
55
+ ## License
56
+
57
+ BUSL-1.1. See [LICENSE](LICENSE). Same terms as the underlying `@klura/runtime`; see the runtime README for the commercial-use terms.
package/index.js ADDED
@@ -0,0 +1,294 @@
1
+ #!/usr/bin/env node
2
+
3
+ // Klura MCP Server — exposes browser automation tools via Model Context Protocol.
4
+ // Works with Claude Desktop, Cursor, Windsurf, and any MCP client.
5
+ //
6
+ // Usage:
7
+ // npx @klura/mcp (stdio transport)
8
+ //
9
+ // MCP config:
10
+ // {
11
+ // "mcpServers": {
12
+ // "klura": {
13
+ // "command": "npx",
14
+ // "args": ["-y", "@klura/mcp"]
15
+ // }
16
+ // }
17
+ // }
18
+
19
+ // Build the klura MCP server — wires every tool + resource handler onto a
20
+ // fresh Server instance and returns it unconnected. Callers pick a transport:
21
+ // `main()` below attaches stdio for the CLI path; the field-reports harness
22
+ // imports this directly and passes the instance to the Agent SDK via
23
+ // `{type:'sdk', instance}` so the browser pool survives across SDK `resume`
24
+ // queries (each resume would otherwise spawn a fresh stdio child and orphan
25
+ // every in-memory session).
26
+ async function createKluraMcpServer() {
27
+ const { Server } = await import('@modelcontextprotocol/sdk/server/index.js');
28
+ const { ListToolsRequestSchema, CallToolRequestSchema, ListResourcesRequestSchema, ReadResourceRequestSchema } = await import('@modelcontextprotocol/sdk/types.js');
29
+ // Load klura runtime
30
+ const klura = require('@klura/runtime');
31
+
32
+ // SKILL.md (compact) is the always-loaded orientation.
33
+ // REFERENCE.md (detailed schemas, examples) is served as an on-demand
34
+ // resource via klura.resolveReferenceResource — see the ReadResource
35
+ // handler below for the fragment-based section addressing.
36
+ const skillMd = klura.getSkillMd()
37
+ .replace(/^---[\s\S]*?---\s*/, ''); // strip frontmatter
38
+
39
+ const server = new Server(
40
+ { name: '@klura/mcp', version: '0.1.0' },
41
+ { capabilities: { tools: {}, resources: {} }, instructions: skillMd }
42
+ );
43
+
44
+ // -- Resources (on-demand reference docs) --
45
+ //
46
+ // REFERENCE.md is served section-by-section via URL fragments so each
47
+ // response fits inside the MCP output budget. Fetching `klura://reference`
48
+ // with no fragment returns a short table of contents listing every
49
+ // addressable `#<slug>`; fetching `klura://reference#<slug>` returns only
50
+ // that section. The section parser + budget logic lives in the runtime
51
+ // module (`runtime/src/reference-sections.ts`) so a pre-commit test can
52
+ // assert every section fits before a regression lands.
53
+
54
+ server.setRequestHandler(ListResourcesRequestSchema, async () => {
55
+ const sections = klura.listReferenceSections();
56
+ return {
57
+ resources: [
58
+ {
59
+ uri: 'klura://reference',
60
+ name: 'Klura Reference — Table of Contents',
61
+ description:
62
+ 'Table of contents for the detailed reference. Fetch individual sections by appending a URL fragment, e.g. klura://reference#fetch-schema. ' +
63
+ `Available sections: ${sections.map((s) => '#' + s.slug).join(', ')}.`,
64
+ mimeType: 'text/markdown',
65
+ },
66
+ ],
67
+ };
68
+ });
69
+
70
+ server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
71
+ const uri = request.params.uri;
72
+ try {
73
+ const { text } = klura.resolveReferenceResource(uri);
74
+ return {
75
+ contents: [{ uri, mimeType: 'text/markdown', text }],
76
+ };
77
+ } catch (err) {
78
+ // Surface the runtime's helpful error (which lists available slugs)
79
+ // directly to the MCP client instead of swallowing it.
80
+ throw new Error(err instanceof Error ? err.message : String(err));
81
+ }
82
+ });
83
+
84
+ // -- Tool registry --
85
+ //
86
+ // Every tool's schema and handler live colocated in mcp/tools.js. The
87
+ // ListTools handler reads the registry; the CallTool dispatcher looks up
88
+ // the entry by name and invokes its handler. Adding a tool means adding
89
+ // exactly one entry there — no separate switch case to keep in sync.
90
+ const tools = require('./tools.js')(klura);
91
+ const toolByName = new Map(tools.map((t) => [t.name, t]));
92
+
93
+ server.setRequestHandler(ListToolsRequestSchema, async () => ({
94
+ tools: tools.map(({ name, description, inputSchema }) => ({ name, description, inputSchema })),
95
+ }));
96
+
97
+ // OpenAI-style tool_calls deliver `arguments` as a JSON string parsed
98
+ // once by the client. Many non-Anthropic models then JSON-encode nested
99
+ // object/array fields a SECOND time, so we receive `args.foo === "{...}"`
100
+ // where the schema declares `foo: {type: 'object'}`. Walk the inputSchema
101
+ // and parse strings that the schema says shouldn't be strings. Only
102
+ // strict JSON ('{', '['); leave anything else alone so we don't disturb
103
+ // legitimately stringy fields. Best-effort: if parse fails or the value
104
+ // doesn't match the declared type after parsing, leave it for the
105
+ // runtime's own validators to reject with a useful error.
106
+ function coerceArgs(toolName, args) {
107
+ const tool = toolByName.get(toolName);
108
+ const schema = tool && tool.inputSchema;
109
+ if (!schema || !schema.properties || !args || typeof args !== 'object') return args;
110
+ for (const [key, propSchema] of Object.entries(schema.properties)) {
111
+ const v = args[key];
112
+ if (typeof v !== 'string') continue;
113
+ const expected = propSchema?.type;
114
+ const wantsContainer =
115
+ expected === 'object' ||
116
+ expected === 'array' ||
117
+ (Array.isArray(expected) && (expected.includes('object') || expected.includes('array')));
118
+ if (!wantsContainer) continue;
119
+ const trimmed = v.trim();
120
+ if (!trimmed.startsWith('{') && !trimmed.startsWith('[')) continue;
121
+ try {
122
+ const parsed = JSON.parse(trimmed);
123
+ const parsedType = Array.isArray(parsed) ? 'array' : typeof parsed;
124
+ const matches = Array.isArray(expected)
125
+ ? expected.includes(parsedType)
126
+ : expected === parsedType;
127
+ if (matches) args[key] = parsed;
128
+ } catch { /* leave for downstream validator */ }
129
+ }
130
+ return args;
131
+ }
132
+
133
+ // -- Tool execution --
134
+
135
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
136
+ const { name, arguments: rawArgs } = request.params;
137
+ const tool = toolByName.get(name);
138
+ if (!tool) {
139
+ return {
140
+ content: [{ type: 'text', text: `Unknown tool: ${name}` }],
141
+ isError: true,
142
+ };
143
+ }
144
+ const args = coerceArgs(name, rawArgs);
145
+
146
+ try {
147
+ // Phase admissibility — hard tool blocking per the session-phase
148
+ // state machine. Tools not in the current phase's allowedTools
149
+ // (or, when budget is exhausted, not in allowedToolsWhenExhausted)
150
+ // are rejected here without running. Universal tools (control
151
+ // plane, memory reads, escape valve) bypass; tools called without
152
+ // a session (start_session, etc.) bypass too. After admission,
153
+ // tickPhaseCounter increments the per-phase round counter and
154
+ // engages the soft-block flag when the budget is hit.
155
+ if (args && args.session_id) {
156
+ try {
157
+ klura.assertToolAdmissibleBySessionId(args.session_id, name);
158
+ } catch (err) {
159
+ if (err instanceof klura.ToolNotAdmissibleError) {
160
+ return {
161
+ content: [
162
+ {
163
+ type: 'text',
164
+ text: JSON.stringify({
165
+ ok: false,
166
+ error: 'tool_not_admissible',
167
+ phase: err.phase,
168
+ tool: err.toolName,
169
+ message: err.reason,
170
+ }, null, 2),
171
+ },
172
+ ],
173
+ isError: true,
174
+ };
175
+ }
176
+ throw err;
177
+ }
178
+ }
179
+
180
+ // Pending-interruption / pending-checkpoint gates. A prior tool
181
+ // call returned a handover resolution; every subsequent tool call
182
+ // on the same session must echo the relevant token + an ack
183
+ // (user_response / viewer_result) or cancel with {cancelled: true,
184
+ // reason}. Tools that deliberately resolve the matching pending
185
+ // state opt out via `skipInterruptionGate` / `skipCheckpointGate`
186
+ // on their registry entry.
187
+ if (args && args.session_id && !tool.skipInterruptionGate) {
188
+ klura.assertNoPendingInterruption(args.session_id, {
189
+ interruption_token: args.interruption_token,
190
+ user_response: args.user_response,
191
+ viewer_result: args.viewer_result,
192
+ cancelled: args.cancelled,
193
+ reason: args.reason,
194
+ });
195
+ }
196
+ if (args && args.session_id && !tool.skipCheckpointGate) {
197
+ klura.assertNoPendingCheckpoint(args.session_id, {
198
+ checkpoint_token: args.checkpoint_token,
199
+ user_response: args.user_response,
200
+ viewer_result: args.viewer_result,
201
+ cancelled: args.cancelled,
202
+ reason: args.reason,
203
+ });
204
+ }
205
+
206
+ let result = await tool.handler(args);
207
+
208
+ // Inject sticky LIFT obligation reminder. Fires on every tool
209
+ // response between the first mutating perform_action and either a
210
+ // successful save_strategy or close_session ok:true. Once-per-session
211
+ // semantics → no token-binding needed (see runtime/docs/gates.md
212
+ // §once-vs-many). klura.formatToolResult hoists the obligation
213
+ // message into a leading [klura obligation]: <message> text block
214
+ // so the model reads it as a top-level directive rather than buried
215
+ // inside the JSON-stringified payload — that hoist + the imperative
216
+ // wording in session-obligations.ts is the primary mechanism. If a
217
+ // model still ends_turn with an open obligation despite reading the
218
+ // hoisted block, treat that as a runtime weakness worth surfacing,
219
+ // not a harness gap to paper over.
220
+ if (args && args.session_id) {
221
+ try {
222
+ const obligation = klura.getSessionObligation(args.session_id);
223
+ if (obligation && result && typeof result === 'object' && !Array.isArray(result)) {
224
+ result = { ...result, _session_obligation: obligation };
225
+ }
226
+ } catch {
227
+ /* non-fatal */
228
+ }
229
+ }
230
+
231
+ // Convert to MCP content blocks (screenshots become image blocks)
232
+ const blocks = klura.formatToolResult(name, result);
233
+ return {
234
+ content: blocks.map(b =>
235
+ b.type === 'image'
236
+ ? { type: 'image', data: b.data, mimeType: b.mediaType }
237
+ : { type: 'text', text: b.text }
238
+ ),
239
+ };
240
+ } catch (err) {
241
+ // Attach the LIFT obligation to error responses too. Without this,
242
+ // every save_strategy / close_session rejection drops the "MUST be
243
+ // close_session" anchor exactly when the agent most needs it — agents
244
+ // reading just the bare error treat the failure as a one-off shape
245
+ // complaint and end the turn after the user-facing goal looks done.
246
+ let obligationLine = '';
247
+ if (args && args.session_id) {
248
+ try {
249
+ const obligation = klura.getSessionObligation(args.session_id);
250
+ if (obligation && obligation.message) {
251
+ obligationLine = `[klura obligation]: ${obligation.message}\n\n`;
252
+ }
253
+ } catch { /* non-fatal */ }
254
+ }
255
+
256
+ // klura's audit-style rejections (`invalid_<kind>: ...` and
257
+ // `invalid_<kind>_rejected (<reason>)`) are iteration steps, not tool
258
+ // errors — the agent's expected next move is to re-call the same tool
259
+ // with audit_token + audit_answers. Returning these with
260
+ // `isError: true` makes the SDK surface them as tool errors, which
261
+ // Claude reads as "task failed, here's why" and reflexively wraps up
262
+ // with text (end_turn) instead of continuing the iteration loop. Send
263
+ // them as normal text results so the model treats them as data.
264
+ const msg = typeof err.message === 'string' ? err.message : String(err);
265
+ if (/^invalid_[a-z_]+:/.test(msg)) {
266
+ return {
267
+ content: [{ type: 'text', text: `${obligationLine}${msg}` }],
268
+ };
269
+ }
270
+ return {
271
+ content: [{ type: 'text', text: `${obligationLine}Error: ${msg}` }],
272
+ isError: true,
273
+ };
274
+ }
275
+ });
276
+
277
+ return server;
278
+ }
279
+
280
+ async function main() {
281
+ const { StdioServerTransport } = await import('@modelcontextprotocol/sdk/server/stdio.js');
282
+ const server = await createKluraMcpServer();
283
+ const transport = new StdioServerTransport();
284
+ await server.connect(transport);
285
+ }
286
+
287
+ module.exports = { createKluraMcpServer };
288
+
289
+ if (require.main === module) {
290
+ main().catch((err) => {
291
+ console.error('Klura MCP server failed:', err);
292
+ process.exit(1);
293
+ });
294
+ }
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@klura/mcp",
3
+ "version": "0.1.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "MCP server for klura — exposes browser automation tools via Model Context Protocol",
8
+ "main": "index.js",
9
+ "bin": {
10
+ "klura-mcp": "./index.js"
11
+ },
12
+ "files": [
13
+ "index.js",
14
+ "tools.js",
15
+ "LICENSE",
16
+ "README.md"
17
+ ],
18
+ "scripts": {
19
+ "start": "node index.js",
20
+ "test": "node --test test/*.test.js"
21
+ },
22
+ "keywords": [
23
+ "mcp",
24
+ "klura",
25
+ "web-automation",
26
+ "browser"
27
+ ],
28
+ "license": "BUSL-1.1",
29
+ "engines": {
30
+ "node": ">=20.0.0"
31
+ },
32
+ "dependencies": {
33
+ "@modelcontextprotocol/sdk": "^1.0.0",
34
+ "@klura/runtime": "^0.1.0"
35
+ },
36
+ "devDependencies": {
37
+ "@eslint/js": "^10.0.1",
38
+ "eslint": "^10.2.0",
39
+ "eslint-config-prettier": "^10.1.8",
40
+ "prettier": "^3.8.1",
41
+ "typescript-eslint": "^8.58.0"
42
+ }
43
+ }