@klura/mcp 0.1.0 → 0.3.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/README.md +2 -2
- package/index.js +14 -260
- package/package.json +2 -3
- package/tools.js +0 -1220
package/README.md
CHANGED
|
@@ -34,7 +34,7 @@ Restart the client. The agent picks up the klura toolset automatically.
|
|
|
34
34
|
Two surfaces land in the agent's context:
|
|
35
35
|
|
|
36
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#
|
|
37
|
+
- **Resource** `klura://reference` — the detailed reference doc, served section-by-section via URL fragments (`klura://reference#reverse-engineer-playbook`, `klura://reference#recorded-path-schema`, etc.) so each response fits inside the MCP output budget. Fetch `klura://reference` with no fragment for a table of contents.
|
|
38
38
|
|
|
39
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
40
|
|
|
@@ -46,7 +46,7 @@ Thin wrapper — each MCP `tools/call` dispatches to the corresponding klura run
|
|
|
46
46
|
MCP client (Claude Desktop, Cursor, …)
|
|
47
47
|
│ stdio transport, JSON-RPC
|
|
48
48
|
▼
|
|
49
|
-
klura
|
|
49
|
+
@klura/mcp (this package)
|
|
50
50
|
│ Node require('@klura/runtime')
|
|
51
51
|
▼
|
|
52
52
|
klura runtime → local daemon → Playwright
|
package/index.js
CHANGED
|
@@ -15,269 +15,23 @@
|
|
|
15
15
|
// }
|
|
16
16
|
// }
|
|
17
17
|
// }
|
|
18
|
+
//
|
|
19
|
+
// This package is a thin stdio wrapper. The server factory itself —
|
|
20
|
+
// `createKluraMcpServer()` — lives in `@klura/runtime` (runtime/mcp-server.js),
|
|
21
|
+
// so the runtime's optional CLI agent can build the same server without a
|
|
22
|
+
// dependency cycle (`@klura/mcp` depends on `@klura/runtime`, never the
|
|
23
|
+
// reverse). The factory is re-exported here for back-compatibility.
|
|
18
24
|
|
|
19
|
-
|
|
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
|
-
}
|
|
25
|
+
const { createKluraMcpServer } = require('@klura/runtime/mcp-server');
|
|
279
26
|
|
|
280
27
|
async function main() {
|
|
28
|
+
// Latch this process as driven by an external MCP host BEFORE anything else.
|
|
29
|
+
// This is the load-bearing layer of the agent guardrail: with the flag set,
|
|
30
|
+
// the optional klura CLI LLM agent refuses to run, so it can never start a
|
|
31
|
+
// second LLM underneath the host that is already driving klura. Stdio is the
|
|
32
|
+
// external-host transport; the in-memory transport the CLI agent and the
|
|
33
|
+
// test harnesses use never reaches this path.
|
|
34
|
+
require('@klura/runtime').markExternalMcpHost();
|
|
281
35
|
const { StdioServerTransport } = await import('@modelcontextprotocol/sdk/server/stdio.js');
|
|
282
36
|
const server = await createKluraMcpServer();
|
|
283
37
|
const transport = new StdioServerTransport();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@klura/mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -11,7 +11,6 @@
|
|
|
11
11
|
},
|
|
12
12
|
"files": [
|
|
13
13
|
"index.js",
|
|
14
|
-
"tools.js",
|
|
15
14
|
"LICENSE",
|
|
16
15
|
"README.md"
|
|
17
16
|
],
|
|
@@ -31,7 +30,7 @@
|
|
|
31
30
|
},
|
|
32
31
|
"dependencies": {
|
|
33
32
|
"@modelcontextprotocol/sdk": "^1.0.0",
|
|
34
|
-
"@klura/runtime": "^0.
|
|
33
|
+
"@klura/runtime": "^0.3.0"
|
|
35
34
|
},
|
|
36
35
|
"devDependencies": {
|
|
37
36
|
"@eslint/js": "^10.0.1",
|