@arbol-ai/maximus 7.4.0 → 8.0.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 +36 -38
- package/bin/maximus.js +4 -0
- package/config/oxfmt.mjs +1 -0
- package/config/oxlint.mjs +10 -0
- package/dist/analytics/analytics.d.ts +21 -9
- package/dist/analytics/analytics.js +19 -5
- package/dist/body.d.ts +1 -1
- package/dist/body.js +2 -2
- package/dist/chunks/agent-skills-Ef65jTf_.js +49 -0
- package/dist/chunks/agent-skills-m7y1EUch.js +3 -0
- package/dist/chunks/{body-6LichrQm.js → body-DOIHwIIl.js} +14 -7
- package/dist/chunks/{correlation-BEkcFFGT.js → correlation-4b23a-a4.js} +8 -5
- package/dist/chunks/define-app-CfVZBt7D.js +336 -0
- package/dist/chunks/{errors-D7zG9kx9.js → errors-WfyKhiyk.js} +9 -4
- package/dist/chunks/{execution-kVUJPy1A.js → execution-CfZEx_JP.js} +28 -15
- package/dist/chunks/generate-C-9FKW0o.js +197 -0
- package/dist/chunks/handler-DWTGoduQ.js +140 -0
- package/dist/chunks/http-layout-_h7xwgPC.js +128 -0
- package/dist/chunks/{http-paths-yKD6TSNJ.js → http-paths-DMBxwpp6.js} +50 -31
- package/dist/chunks/{json-Bu4BLT25.js → json-B14evE6-.js} +21 -5
- package/dist/chunks/names-ncD7A3DV.js +280 -0
- package/dist/chunks/openapi-CV0KmJpS.js +1588 -0
- package/dist/chunks/openapi-file-OjSs9mRv.js +83 -0
- package/dist/chunks/pages-DyC0m8Zb.js +14 -0
- package/dist/chunks/prepare-configured-BmRJLg6J.js +3 -0
- package/dist/chunks/prepare-configured-Cithjf80.js +52 -0
- package/dist/chunks/project-files-C0OX7MQQ.js +130 -0
- package/dist/chunks/query-kind-CWKAXEME.js +40 -0
- package/dist/chunks/record-DQUDTpEA.js +10 -0
- package/dist/chunks/result-CKgZHrnj.js +56 -0
- package/dist/chunks/{runtime-DlqA7J38.js → runtime-CVVf5hmp.js} +50 -26
- package/dist/chunks/scaffold-DGrcjR3h.js +3 -0
- package/dist/chunks/scaffold-DZD6dfoZ.js +138 -0
- package/dist/chunks/{schemas-CzTvbXb6.js → schemas-p_aRcMC1.js} +30 -14
- package/dist/chunks/server-CCc7fil4.js +410 -0
- package/dist/chunks/src--HZrH1U9.js +568 -0
- package/dist/chunks/{telemetry-BTwB68m5.js → telemetry-C3LfZcOB.js} +32 -11
- package/dist/chunks/{types-DQdfRF3f.d.ts → types-BQYB4Ads.d.ts} +993 -341
- package/dist/chunks/{validation-r-sgmsqb.js → validation-fTohBT6R.js} +69 -28
- package/dist/chunks/workers-types-iONp32T4.d.ts +143 -0
- package/dist/chunks/yaml-Dk2L24yQ.js +74 -0
- package/dist/diagnostics.d.ts +6 -4
- package/dist/diagnostics.js +2 -2
- package/dist/errors.d.ts +2 -2
- package/dist/errors.js +2 -2
- package/dist/http-paths.d.ts +1 -1
- package/dist/http-paths.js +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +12 -11
- package/dist/internals.d.ts +1 -1
- package/dist/internals.js +5 -4
- package/dist/mcp/index.d.ts +3 -2
- package/dist/mcp/index.js +5 -387
- package/dist/postgres/index.d.ts +25 -8
- package/dist/postgres/index.js +17 -8
- package/dist/project/config.d.ts +110 -72
- package/dist/project/config.js +161 -45
- package/dist/project/index.d.ts +17 -4
- package/dist/project/index.js +44 -9
- package/dist/projection.d.ts +14 -13
- package/dist/projection.js +8 -32
- package/dist/schemas.d.ts +1 -1
- package/dist/schemas.js +2 -2
- package/dist/standard-failures.d.ts +2 -89
- package/dist/tooling/cli.js +149 -105
- package/dist/tooling/testing.js +2 -2
- package/dist/vite/cloudflare-workers.js +25 -0
- package/dist/vite/index.d.ts +157 -0
- package/dist/vite/index.js +1477 -0
- package/dist/webhooks.d.ts +12 -10
- package/dist/webhooks.js +26 -12
- package/dist/workers/runtime.d.ts +1 -1
- package/dist/workers/runtime.js +1 -1
- package/package.json +26 -22
- package/skills/maximus/SKILL.md +165 -68
- package/skills/maximus/references/deploying.md +73 -0
- package/skills/maximus/references/errors-and-results.md +114 -55
- package/skills/maximus/references/project.md +202 -0
- package/skills/maximus/references/routes-and-tools.md +256 -0
- package/skills/maximus/references/testing.md +137 -50
- package/skills/maximus/references/transports.md +113 -100
- package/skills/maximus/references/writing-code.md +84 -0
- package/templates/mcp/mcp/instructions.md.template +2 -0
- package/templates/mcp/routes/v1/greeting.get.ts.template +12 -0
- package/templates/mcp/tests/mcp.test.ts.template +36 -0
- package/templates/project/.agents/skills/project/SKILL.md.template +21 -12
- package/templates/project/.github/workflows/ci.yml.template +4 -1
- package/templates/project/CLAUDE.md.template +41 -25
- package/templates/project/README.md.template +54 -48
- package/templates/project/middleware/10-response.ts.template +5 -2
- package/templates/project/package.json.template +13 -9
- package/templates/project/pages/index.ts.template +2 -4
- package/templates/project/routes/v1/greeting.get.ts.template +10 -0
- package/templates/project/routes/v1/greeting.post.ts.template +12 -0
- package/templates/project/tests/greeting.test.ts.template +45 -0
- package/templates/project/tsconfig.json.template +8 -6
- package/templates/project/vite.config.ts.template +15 -7
- package/templates/web/app/main.tsx.template +16 -0
- package/templates/web/app/root.tsx.template +17 -0
- package/templates/web/app/routes/index.tsx.template +9 -0
- package/templates/web/index.html.template +12 -0
- package/templates/web/package.json.template +12 -0
- package/templates/web/tsconfig.json.template +7 -0
- package/templates/web/vite.config.ts.template +22 -0
- package/dist/chunks/builder-BiXv7d2v.js +0 -100
- package/dist/chunks/define-app-DvD_1jUE.js +0 -625
- package/dist/chunks/prepare-project-CMyGSEtI.js +0 -236
- package/dist/chunks/prepare-project-MtS0-W_n.js +0 -3
- package/dist/chunks/record-DY-lX9w1.js +0 -34
- package/dist/chunks/src-N3oyx5QF.js +0 -1091
- package/dist/client/client.d.ts +0 -129
- package/dist/client/client.js +0 -321
- package/dist/tooling/vite.d.ts +0 -3
- package/dist/tooling/vite.js +0 -57
- package/skills/maximus/references/adding-an-operation.md +0 -179
- package/skills/maximus/references/building-a-service.md +0 -167
- package/skills/maximus/references/migration-from-v2.md +0 -62
- package/skills/maximus/references/migration-to-v5.md +0 -37
- package/skills/maximus/references/publishing-and-verification.md +0 -42
- package/templates/project/cloudflare.config.ts.template +0 -24
- package/templates/project/maximus.config.ts.template +0 -20
- package/templates/project/routes/v1/greeting.ts.template +0 -38
- package/templates/service/.agents/skills/project/SKILL.md.template +0 -10
- package/templates/service/.github/workflows/ci.yml.template +0 -21
- package/templates/service/.gitignore.template +0 -9
- package/templates/service/CLAUDE.md.template +0 -5
- package/templates/service/README.md.template +0 -45
- package/templates/service/cloudflare.config.ts.template +0 -30
- package/templates/service/package.json.template +0 -36
- package/templates/service/src/app.ts.template +0 -30
- package/templates/service/src/index.ts.template +0 -13
- package/templates/service/tests/contract.test.ts.template +0 -12
- package/templates/service/tsconfig.json.template +0 -16
- package/templates/service/vite.config.ts.template +0 -13
- package/templates/service/vitest.config.ts.template +0 -10
package/dist/mcp/index.js
CHANGED
|
@@ -1,270 +1,10 @@
|
|
|
1
|
-
import { i as standardErrors } from "../chunks/errors-D7zG9kx9.js";
|
|
2
|
-
import { S as operationMatchesScopes, h as MAX_BODY_BYTES_CEILING, p as sha256Hex, y as mcpOpenWorld } from "../chunks/define-app-DvD_1jUE.js";
|
|
3
|
-
import { a as canonical, o as standaloneSchema } from "../chunks/src-N3oyx5QF.js";
|
|
4
|
-
import { i as reportUnexpected } from "../chunks/telemetry-BTwB68m5.js";
|
|
5
|
-
import { Type } from "@sinclair/typebox";
|
|
6
|
-
import { Value } from "@sinclair/typebox/value";
|
|
7
|
-
import { stringify } from "yaml";
|
|
8
|
-
import { McpServer, ProtocolError, ProtocolErrorCode, createMcpHandler } from "@modelcontextprotocol/server";
|
|
9
|
-
|
|
10
|
-
// src/mcp/skills.ts
|
|
11
|
-
/**
|
|
12
|
-
* The MCP Skills extension (SEP-2640): `skills/list` and `skills/get` describe each skill by its
|
|
13
|
-
* frontmatter and the manifest of its one file, whose size and SHA-256 digest hosts verify, and
|
|
14
|
-
* `resources/read` serves that `SKILL.md`. An optional tool serves the same skills to clients that import
|
|
15
|
-
* only tools.
|
|
16
|
-
*/
|
|
17
|
-
/** The official Skills extension (SEP-2640) a server declares in its capabilities. */
|
|
18
|
-
const SKILLS_EXTENSION = "io.modelcontextprotocol/skills";
|
|
19
|
-
/** Skills change only with a deployment and are the same for every caller, so hosts may share them. */
|
|
20
|
-
const SKILL_CACHE = {
|
|
21
|
-
ttlMs: 3e5,
|
|
22
|
-
cacheScope: "public"
|
|
23
|
-
};
|
|
24
|
-
/** Adapts a TypeBox schema to the Standard Schema the SDK validates custom method params with. */
|
|
25
|
-
function standardSchema(schema) {
|
|
26
|
-
return { "~standard": {
|
|
27
|
-
version: 1,
|
|
28
|
-
vendor: "maximus",
|
|
29
|
-
validate: (value) => Value.Check(schema, value) ? { value } : { issues: [{ message: "Invalid params" }] }
|
|
30
|
-
} };
|
|
31
|
-
}
|
|
32
|
-
/** `skills/list` params: an optional page cursor, which one page of every skill never issues. */
|
|
33
|
-
const ListSkillsParams = standardSchema(Type.Object({ cursor: Type.Optional(Type.String()) }));
|
|
34
|
-
/** `skills/get` params: the URI of the skill's `SKILL.md`. */
|
|
35
|
-
const GetSkillParams = standardSchema(Type.Object({ uri: Type.String() }));
|
|
36
|
-
/**
|
|
37
|
-
* A skill's `SKILL.md` URI, `skill://<name>/SKILL.md`: the file's directory carries the skill's name, as
|
|
38
|
-
* SEP-2640 requires.
|
|
39
|
-
*/
|
|
40
|
-
function skillUri(skill) {
|
|
41
|
-
return `skill://${skill.name}/SKILL.md`;
|
|
42
|
-
}
|
|
43
|
-
/** The served `SKILL.md`: frontmatter generated from the name and description, then the instructions. */
|
|
44
|
-
function skillDocument(skill) {
|
|
45
|
-
return `---\n${stringify({
|
|
46
|
-
name: skill.name,
|
|
47
|
-
description: skill.description
|
|
48
|
-
}, { lineWidth: 0 })}---\n\n${skill.text.trim()}\n`;
|
|
49
|
-
}
|
|
50
|
-
/** A catalog entry. Hosts verify every file they read against its manifest's size and SHA-256 digest. */
|
|
51
|
-
async function skillEntry(skill) {
|
|
52
|
-
const uri = skillUri(skill);
|
|
53
|
-
const bytes = new TextEncoder().encode(skillDocument(skill));
|
|
54
|
-
return {
|
|
55
|
-
uri,
|
|
56
|
-
frontmatter: {
|
|
57
|
-
name: skill.name,
|
|
58
|
-
description: skill.description
|
|
59
|
-
},
|
|
60
|
-
resources: [{
|
|
61
|
-
uri,
|
|
62
|
-
digest: `sha256:${await sha256Hex(bytes)}`,
|
|
63
|
-
size: bytes.byteLength
|
|
64
|
-
}]
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Serve skills through the Skills extension: `skills/list`, `skills/get`, and `resources/read` of each
|
|
69
|
-
* `SKILL.md`, each request answered through `answer`, which counts it against the caller's rate limit.
|
|
70
|
-
* Clients without the extension still find the files among the server's resources.
|
|
71
|
-
*/
|
|
72
|
-
function registerSkills(server, skills, answer) {
|
|
73
|
-
for (const skill of skills) {
|
|
74
|
-
const uri = skillUri(skill);
|
|
75
|
-
server.registerResource(skill.name, uri, {
|
|
76
|
-
description: skill.description,
|
|
77
|
-
mimeType: "text/markdown",
|
|
78
|
-
cacheHint: SKILL_CACHE
|
|
79
|
-
}, () => answer(() => ({ contents: [{
|
|
80
|
-
uri,
|
|
81
|
-
mimeType: "text/markdown",
|
|
82
|
-
text: skillDocument(skill)
|
|
83
|
-
}] })));
|
|
84
|
-
}
|
|
85
|
-
server.server.setRequestHandler("skills/list", { params: ListSkillsParams }, (params) => answer(async () => {
|
|
86
|
-
if (params.cursor !== void 0) throw new ProtocolError(ProtocolErrorCode.InvalidParams, "Invalid skills cursor");
|
|
87
|
-
return {
|
|
88
|
-
resultType: "complete",
|
|
89
|
-
skills: await Promise.all(skills.map(skillEntry)),
|
|
90
|
-
...SKILL_CACHE
|
|
91
|
-
};
|
|
92
|
-
}));
|
|
93
|
-
server.server.setRequestHandler("skills/get", { params: GetSkillParams }, (params) => answer(async () => {
|
|
94
|
-
const skill = skills.find((candidate) => skillUri(candidate) === params.uri);
|
|
95
|
-
if (!skill) throw new ProtocolError(ProtocolErrorCode.InvalidParams, "Unknown skill");
|
|
96
|
-
return {
|
|
97
|
-
resultType: "complete",
|
|
98
|
-
skill: await skillEntry(skill),
|
|
99
|
-
...SKILL_CACHE
|
|
100
|
-
};
|
|
101
|
-
}));
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* The same skills as one read-only tool, for clients that import only tools. Its description names every
|
|
105
|
-
* skill, so a semantic tool search for one of those tasks can find it.
|
|
106
|
-
*/
|
|
107
|
-
function skillTool(name, skills) {
|
|
108
|
-
return {
|
|
109
|
-
name,
|
|
110
|
-
title: "Read a workflow skill",
|
|
111
|
-
description: [
|
|
112
|
-
"Read the instructions of one workflow skill before doing its task: the steps, tools and pitfalls of work that spans several tools. A skill grants no tools or permissions.",
|
|
113
|
-
"Skills:",
|
|
114
|
-
...skills.map((skill) => `- ${skill.name}: ${skill.description}`)
|
|
115
|
-
].join("\n"),
|
|
116
|
-
inputSchema: {
|
|
117
|
-
type: "object",
|
|
118
|
-
properties: { name: {
|
|
119
|
-
type: "string",
|
|
120
|
-
enum: skills.map((skill) => skill.name),
|
|
121
|
-
description: "The skill to read."
|
|
122
|
-
} },
|
|
123
|
-
required: ["name"],
|
|
124
|
-
additionalProperties: false
|
|
125
|
-
},
|
|
126
|
-
outputSchema: {
|
|
127
|
-
type: "object",
|
|
128
|
-
properties: {
|
|
129
|
-
name: { type: "string" },
|
|
130
|
-
description: { type: "string" },
|
|
131
|
-
instructions: {
|
|
132
|
-
type: "string",
|
|
133
|
-
description: "The Markdown instructions."
|
|
134
|
-
}
|
|
135
|
-
},
|
|
136
|
-
required: [
|
|
137
|
-
"name",
|
|
138
|
-
"description",
|
|
139
|
-
"instructions"
|
|
140
|
-
],
|
|
141
|
-
additionalProperties: false
|
|
142
|
-
},
|
|
143
|
-
annotations: {
|
|
144
|
-
readOnlyHint: true,
|
|
145
|
-
destructiveHint: false,
|
|
146
|
-
idempotentHint: true,
|
|
147
|
-
openWorldHint: false
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
/** Answer a skill tool call. An unknown name is a tool error, which the model can correct. */
|
|
152
|
-
function readSkill(skills, input) {
|
|
153
|
-
const skill = skills.find((candidate) => candidate.name === input.name);
|
|
154
|
-
if (!skill) return {
|
|
155
|
-
isError: true,
|
|
156
|
-
content: [{
|
|
157
|
-
type: "text",
|
|
158
|
-
text: `Unknown skill. Read one of: ${skills.map((entry) => entry.name).join(", ")}.`
|
|
159
|
-
}]
|
|
160
|
-
};
|
|
161
|
-
const value = {
|
|
162
|
-
name: skill.name,
|
|
163
|
-
description: skill.description,
|
|
164
|
-
instructions: skill.text.trim()
|
|
165
|
-
};
|
|
166
|
-
return {
|
|
167
|
-
content: [{
|
|
168
|
-
type: "text",
|
|
169
|
-
text: JSON.stringify(value)
|
|
170
|
-
}],
|
|
171
|
-
structuredContent: value
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
|
|
175
1
|
// src/mcp/index.ts
|
|
176
2
|
/**
|
|
177
|
-
* `@arbol-ai/maximus/mcp`: the MCP transport. One stateless server per request, built with the official SDK,
|
|
178
|
-
* lists the caller's tools and runs them through the shared execution path, and serves the OpenAPI document
|
|
179
|
-
* and the guides as resources, the guides as prompts and the skills through the Skills extension.
|
|
180
|
-
*/
|
|
181
|
-
/** Operation tools on one `tools/list` page; the skill tool, when served, comes before them on the first. */
|
|
182
|
-
const PAGE_SIZE = 50;
|
|
183
|
-
/** The JSON-RPC error of a request beyond the caller's rate limit: an implementation-defined server error. */
|
|
184
|
-
const RATE_LIMITED = -32e3;
|
|
185
|
-
/** Each operation's tool definition, built on first listing: an application's operations never change. */
|
|
186
|
-
const definitions = /* @__PURE__ */ new WeakMap();
|
|
187
|
-
/**
|
|
188
|
-
* The tool an operation lists as. Both schemas are self-contained, as MCP clients compile them: no `$id`,
|
|
189
|
-
* which one schema may not repeat. The hints follow the operation's effect and tool declaration; they are not
|
|
190
|
-
* authorization.
|
|
191
|
-
*/
|
|
192
|
-
function toolDefinition(op) {
|
|
193
|
-
let tool = definitions.get(op);
|
|
194
|
-
if (!tool) {
|
|
195
|
-
tool = {
|
|
196
|
-
name: op.mcp.name,
|
|
197
|
-
title: op.title,
|
|
198
|
-
description: op.description,
|
|
199
|
-
inputSchema: toolInputSchema(op.input),
|
|
200
|
-
outputSchema: {
|
|
201
|
-
...standaloneSchema(op.output),
|
|
202
|
-
type: "object"
|
|
203
|
-
},
|
|
204
|
-
annotations: {
|
|
205
|
-
readOnlyHint: op.effect === "read",
|
|
206
|
-
destructiveHint: op.effect === "delete",
|
|
207
|
-
idempotentHint: op.mcp.idempotent,
|
|
208
|
-
openWorldHint: mcpOpenWorld(op.mcp)
|
|
209
|
-
}
|
|
210
|
-
};
|
|
211
|
-
definitions.set(op, tool);
|
|
212
|
-
}
|
|
213
|
-
return tool;
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* An operation's input as a tool's `inputSchema`: an object at the root, as MCP requires and hosts enforce.
|
|
217
|
-
* A union input (`{ channel: 'email', … } | { channel: 'sms', … }`) lists as one object with every member's
|
|
218
|
-
* fields, required when every member requires them, and a field the members declare differently as their
|
|
219
|
-
* choices. Hosts that refuse a root `anyOf`, the Anthropic API among them, would fail every tool of the
|
|
220
|
-
* request; each call is still checked against the exact union.
|
|
221
|
-
*/
|
|
222
|
-
function toolInputSchema(input) {
|
|
223
|
-
const schema = standaloneSchema(input);
|
|
224
|
-
const { anyOf, ...root } = schema;
|
|
225
|
-
const bundled = root.$defs ?? {};
|
|
226
|
-
const members = Array.isArray(anyOf) ? anyOf.map((member) => localDefinition(member, bundled)) : [];
|
|
227
|
-
if (!members.length || members.some((member) => member.type !== "object")) return {
|
|
228
|
-
...schema,
|
|
229
|
-
type: "object"
|
|
230
|
-
};
|
|
231
|
-
const fields = (member) => member.properties ?? {};
|
|
232
|
-
const names = [...new Set(members.flatMap((member) => Object.keys(fields(member))))];
|
|
233
|
-
const required = names.filter((name) => members.every((member) => Array.isArray(member.required) && member.required.includes(name)));
|
|
234
|
-
return {
|
|
235
|
-
type: "object",
|
|
236
|
-
...root,
|
|
237
|
-
properties: Object.fromEntries(names.map((name) => [name, fieldChoices(members.flatMap((member) => name in fields(member) ? [fields(member)[name] ?? {}] : []))])),
|
|
238
|
-
...required.length ? { required } : {},
|
|
239
|
-
...members.every((member) => member.additionalProperties === false) ? { additionalProperties: false } : {},
|
|
240
|
-
type: "object"
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
/** A union member, or the definition under `$defs` its `$ref` names. */
|
|
244
|
-
function localDefinition(member, bundled) {
|
|
245
|
-
const reference = typeof member.$ref === "string" ? member.$ref : "";
|
|
246
|
-
return reference.startsWith("#/$defs/") ? bundled[reference.slice(8)] ?? member : member;
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* One field of a listed union: the one schema every member that declares it gives it, the members' literals
|
|
250
|
-
* as an `enum` (a discriminator), or else each distinct schema as a choice.
|
|
251
|
-
*/
|
|
252
|
-
function fieldChoices(schemas) {
|
|
253
|
-
const distinct = [...new Map(schemas.map((schema) => [canonical(schema), schema])).values()];
|
|
254
|
-
const [first] = distinct;
|
|
255
|
-
if (distinct.length === 1 && first) return first;
|
|
256
|
-
const literal = (schema) => "const" in schema && Object.keys(schema).every((key) => key === "const" || key === "type");
|
|
257
|
-
if (first && distinct.every(literal) && distinct.every((schema) => schema.type === first.type)) return {
|
|
258
|
-
...first.type === void 0 ? {} : { type: first.type },
|
|
259
|
-
enum: distinct.map((schema) => schema.const)
|
|
260
|
-
};
|
|
261
|
-
return { anyOf: distinct };
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
3
|
* The MCP transport, for `createWorker(app, { transports: [mcp()] })`: serves the operations that declare
|
|
265
4
|
* `mcp` as tools over Streamable HTTP at `/mcp` (or `path`), with the OpenAPI document and guides as
|
|
266
5
|
* resources, guides as prompts and skills through the Skills extension. A caller needs `<namespace>:mcp:use`
|
|
267
|
-
* and sees only the tools its scopes admit.
|
|
6
|
+
* and sees only the tools its scopes and actor kind admit. Each message of a JSON-RPC batch passes the rate
|
|
7
|
+
* limit as a request of its own, and each call is held to its operation's body limit.
|
|
268
8
|
*/
|
|
269
9
|
function mcp(options = {}) {
|
|
270
10
|
return {
|
|
@@ -272,131 +12,9 @@ function mcp(options = {}) {
|
|
|
272
12
|
path: options.path ?? "/mcp",
|
|
273
13
|
scope: (namespace) => `${namespace}:mcp:use`,
|
|
274
14
|
maxBodyBytes: (operations, fallback) => Math.max(fallback, ...operations.flatMap(([, op]) => op.mcp ? [op.maxBodyBytes ?? fallback] : [])),
|
|
275
|
-
async handle(
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* Counts one more request of this HTTP request against the caller's rate limit, as a request of its own
|
|
280
|
-
* (a JSON-RPC batch carries several), and refuses it once the limit does.
|
|
281
|
-
*/
|
|
282
|
-
const count = async () => {
|
|
283
|
-
if (!await admitRequest()) {
|
|
284
|
-
const failure = standardErrors.rateLimited.create();
|
|
285
|
-
throw new ProtocolError(RATE_LIMITED, failure.message, failure);
|
|
286
|
-
}
|
|
287
|
-
};
|
|
288
|
-
/**
|
|
289
|
-
* Runs one of the server's own handlers as a counted request, unless `invoke` counts it: a protocol
|
|
290
|
-
* error answers as thrown, and any other failure is reported by its safe description and answers a
|
|
291
|
-
* generic internal error, never its message.
|
|
292
|
-
*/
|
|
293
|
-
const answer = async (work, counted = true) => {
|
|
294
|
-
try {
|
|
295
|
-
if (counted) await count();
|
|
296
|
-
return await work();
|
|
297
|
-
} catch (error) {
|
|
298
|
-
if (error instanceof ProtocolError) throw error;
|
|
299
|
-
reportUnexpected(error, "mcp", requestId);
|
|
300
|
-
throw new ProtocolError(ProtocolErrorCode.InternalError, "Internal error");
|
|
301
|
-
}
|
|
302
|
-
};
|
|
303
|
-
const response = await createMcpHandler(() => {
|
|
304
|
-
const skills = application.mcp?.skills ?? [];
|
|
305
|
-
const skillToolName = skills.length ? application.mcp?.skillTool : void 0;
|
|
306
|
-
const server = new McpServer({
|
|
307
|
-
name: application.name,
|
|
308
|
-
title: application.title,
|
|
309
|
-
version: application.version
|
|
310
|
-
}, {
|
|
311
|
-
instructions: application.mcp?.instructions,
|
|
312
|
-
capabilities: {
|
|
313
|
-
tools: { listChanged: false },
|
|
314
|
-
resources: { listChanged: false },
|
|
315
|
-
prompts: { listChanged: false },
|
|
316
|
-
...skills.length ? { extensions: { [SKILLS_EXTENSION]: {} } } : {}
|
|
317
|
-
}
|
|
318
|
-
});
|
|
319
|
-
const visible = operations.filter((entry) => Boolean(entry[1].mcp) && operationMatchesScopes(entry[1], authenticated.scopes) && (!entry[1].actorKinds || entry[1].actorKinds.includes(authenticated.actorKind)));
|
|
320
|
-
server.server.setRequestHandler("tools/call", (call) => answer(async () => {
|
|
321
|
-
if (skillToolName && call.params.name === skillToolName) {
|
|
322
|
-
await count();
|
|
323
|
-
return readSkill(skills, call.params.arguments ?? {});
|
|
324
|
-
}
|
|
325
|
-
const selected = visible.find(([, op]) => op.mcp.name === call.params.name);
|
|
326
|
-
if (!selected) throw new ProtocolError(ProtocolErrorCode.InvalidParams, "Tool not found");
|
|
327
|
-
const [name, op] = selected;
|
|
328
|
-
if (bodyBytes > bodyLimit(op)) return {
|
|
329
|
-
isError: true,
|
|
330
|
-
content: [{
|
|
331
|
-
type: "text",
|
|
332
|
-
text: JSON.stringify(standardErrors.tooLarge.create())
|
|
333
|
-
}]
|
|
334
|
-
};
|
|
335
|
-
const result = await invoke(name, op, call.params.arguments ?? {});
|
|
336
|
-
return result.ok ? {
|
|
337
|
-
content: [{
|
|
338
|
-
type: "text",
|
|
339
|
-
text: JSON.stringify(result.value)
|
|
340
|
-
}],
|
|
341
|
-
structuredContent: result.value
|
|
342
|
-
} : {
|
|
343
|
-
isError: true,
|
|
344
|
-
content: [{
|
|
345
|
-
type: "text",
|
|
346
|
-
text: JSON.stringify(result.error)
|
|
347
|
-
}]
|
|
348
|
-
};
|
|
349
|
-
}, false));
|
|
350
|
-
server.server.setRequestHandler("tools/list", (listing) => answer(() => {
|
|
351
|
-
const cursor = listing.params?.cursor;
|
|
352
|
-
const previous = cursor === void 0 ? -1 : visible.findIndex(([name]) => pageCursor(name) === cursor);
|
|
353
|
-
if (cursor !== void 0 && previous < 0) throw new ProtocolError(ProtocolErrorCode.InvalidParams, "Invalid cursor");
|
|
354
|
-
const page = visible.slice(previous + 1, previous + 1 + PAGE_SIZE);
|
|
355
|
-
const last = page.at(-1);
|
|
356
|
-
return {
|
|
357
|
-
tools: [...skillToolName && cursor === void 0 ? [skillTool(skillToolName, skills)] : [], ...page.map(([, op]) => toolDefinition(op))],
|
|
358
|
-
...last && previous + 1 + PAGE_SIZE < visible.length ? { nextCursor: pageCursor(last[0]) } : {}
|
|
359
|
-
};
|
|
360
|
-
}));
|
|
361
|
-
for (const extension of ["json", "yaml"]) {
|
|
362
|
-
const uri = url(`/openapi.${extension}`);
|
|
363
|
-
const mimeType = extension === "json" ? "application/json" : "application/yaml";
|
|
364
|
-
server.registerResource(`openapi-${extension}`, uri, {
|
|
365
|
-
title: `${application.title} OpenAPI (${extension.toUpperCase()})`,
|
|
366
|
-
description: "The service's OpenAPI document. Reading it grants no authority to run its operations.",
|
|
367
|
-
mimeType
|
|
368
|
-
}, () => answer(() => ({ contents: [{
|
|
369
|
-
uri,
|
|
370
|
-
mimeType,
|
|
371
|
-
text: contractText(extension)
|
|
372
|
-
}] })));
|
|
373
|
-
}
|
|
374
|
-
for (const guide of application.mcp?.guides ?? []) {
|
|
375
|
-
const uri = `${application.origin}/guides/${guide.name}`;
|
|
376
|
-
server.registerResource(guide.name, uri, {
|
|
377
|
-
title: guide.title,
|
|
378
|
-
mimeType: "text/markdown"
|
|
379
|
-
}, () => answer(() => ({ contents: [{
|
|
380
|
-
uri,
|
|
381
|
-
mimeType: "text/markdown",
|
|
382
|
-
text: guide.text
|
|
383
|
-
}] })));
|
|
384
|
-
server.registerPrompt(guide.name, {
|
|
385
|
-
title: guide.title,
|
|
386
|
-
description: "Read the integration usage guide."
|
|
387
|
-
}, () => answer(() => ({ messages: [{
|
|
388
|
-
role: "user",
|
|
389
|
-
content: {
|
|
390
|
-
type: "text",
|
|
391
|
-
text: guide.text
|
|
392
|
-
}
|
|
393
|
-
}] })));
|
|
394
|
-
}
|
|
395
|
-
if (skills.length) registerSkills(server, skills, answer);
|
|
396
|
-
return server;
|
|
397
|
-
}, { maxRequestBodySize: MAX_BODY_BYTES_CEILING }).fetch(request);
|
|
398
|
-
if (response.status >= 500) reportUnexpected(/* @__PURE__ */ new Error("MCP transport failure"), "mcp", requestId);
|
|
399
|
-
return response;
|
|
15
|
+
async handle(context) {
|
|
16
|
+
const { handleMcp } = await import("../chunks/server-CCc7fil4.js");
|
|
17
|
+
return handleMcp(context);
|
|
400
18
|
}
|
|
401
19
|
};
|
|
402
20
|
}
|
package/dist/postgres/index.d.ts
CHANGED
|
@@ -13,7 +13,10 @@ interface TransactionFailure {
|
|
|
13
13
|
code: 'DATABASE_CONFLICT' | 'DATABASE_RETRY' | 'DATABASE_UNAVAILABLE' | 'COMMIT_UNCERTAIN' | 'DATABASE_FAILURE';
|
|
14
14
|
/** A fixed description of the failure, never the server's own message or the query text. */
|
|
15
15
|
message: string;
|
|
16
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* Whether repeating the transaction may succeed: true for a serialization or deadlock abort and an
|
|
18
|
+
* unavailable database. It never makes replaying an effect outside the database safe.
|
|
19
|
+
*/
|
|
17
20
|
retryable: boolean;
|
|
18
21
|
/** Where the transaction was when it failed, such as `work` while the callback ran, or `commit`. */
|
|
19
22
|
stage: TransactionStage;
|
|
@@ -23,7 +26,12 @@ interface TransactionFailure {
|
|
|
23
26
|
*/
|
|
24
27
|
sqlState?: string;
|
|
25
28
|
}
|
|
26
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* Classifies a failure by its stage and by the SQLSTATE of a `pg` `DatabaseError`, the server's own error
|
|
31
|
+
* response, never by a message: a commit whose outcome is unknown is `COMMIT_UNCERTAIN`, a serialization
|
|
32
|
+
* failure or deadlock `DATABASE_RETRY`, a constraint violation `DATABASE_CONFLICT`, a failed connection or an
|
|
33
|
+
* unavailable server `DATABASE_UNAVAILABLE`, and anything else `DATABASE_FAILURE`.
|
|
34
|
+
*/
|
|
27
35
|
export declare function classifyTransactionFailure(error: unknown, stage: TransactionStage): TransactionFailure;
|
|
28
36
|
/** The organization a transaction works for, taken from the verified principal. */
|
|
29
37
|
interface TenantContext {
|
|
@@ -52,7 +60,12 @@ interface TransactionOptions {
|
|
|
52
60
|
idleTimeoutMs: number;
|
|
53
61
|
/** The transaction's isolation level; `read committed` when absent. */
|
|
54
62
|
isolation?: 'read committed' | 'repeatable read' | 'serializable';
|
|
55
|
-
/**
|
|
63
|
+
/**
|
|
64
|
+
* Retries a transaction the database aborted (`DATABASE_RETRY`), checking out a client again, up to
|
|
65
|
+
* `maxAttempts` attempts in all (1 to 5), waiting `baseDelayMs` (1 to 1000) times the attempt number, plus
|
|
66
|
+
* jitter, between them. Set it only when the callback does nothing but database work; without it, a
|
|
67
|
+
* transaction runs once.
|
|
68
|
+
*/
|
|
56
69
|
databaseOnlyRetry?: {
|
|
57
70
|
maxAttempts: number;
|
|
58
71
|
baseDelayMs: number;
|
|
@@ -67,11 +80,15 @@ type TransactionResult<T> = {
|
|
|
67
80
|
error: TransactionFailure;
|
|
68
81
|
};
|
|
69
82
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* or
|
|
73
|
-
*
|
|
74
|
-
*
|
|
83
|
+
* Runs `work` in one short transaction on a client checked out of `pool`: it begins at the isolation level,
|
|
84
|
+
* sets the time budgets for this transaction alone, passes the tenant explicitly and commits. It answers the
|
|
85
|
+
* work's value, or a classified failure, rolling back while the client can still be trusted; an error without
|
|
86
|
+
* a SQLSTATE that the work throws is thrown on, as it was, after the rollback. A client whose state is
|
|
87
|
+
* unknown is discarded rather than returned to the pool. It installs no row-level security, and no session
|
|
88
|
+
* setting stands for isolation: every query must filter by `tenant.organizationId` and use composite keys
|
|
89
|
+
* itself, and the pool must connect as a restricted role. Keep the callback to database work, without
|
|
90
|
+
* provider, model or stream I/O. An uncertain commit is never retried; reconcile it by the work's own key
|
|
91
|
+
* before repeating anything. An invalid organization id or option throws a TypeError.
|
|
75
92
|
*/
|
|
76
93
|
export declare function withTenantTransaction<T>(pool: Pick<Pool, 'connect'>, tenant: TenantContext, options: TransactionOptions, work: (client: PoolClient, tenant: TenantContext) => Promise<T>): Promise<TransactionResult<T>>;
|
|
77
94
|
export type { TenantContext, TransactionFailure, TransactionOptions, TransactionResult, TransactionStage };
|
package/dist/postgres/index.js
CHANGED
|
@@ -6,7 +6,12 @@ import { DatabaseError } from "pg";
|
|
|
6
6
|
* Classifies a Postgres transaction failure by the server's SQLSTATE and the stage it happened in, never
|
|
7
7
|
* by its message.
|
|
8
8
|
*/
|
|
9
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* Classifies a failure by its stage and by the SQLSTATE of a `pg` `DatabaseError`, the server's own error
|
|
11
|
+
* response, never by a message: a commit whose outcome is unknown is `COMMIT_UNCERTAIN`, a serialization
|
|
12
|
+
* failure or deadlock `DATABASE_RETRY`, a constraint violation `DATABASE_CONFLICT`, a failed connection or an
|
|
13
|
+
* unavailable server `DATABASE_UNAVAILABLE`, and anything else `DATABASE_FAILURE`.
|
|
14
|
+
*/
|
|
10
15
|
function classifyTransactionFailure(error, stage) {
|
|
11
16
|
const sqlState = error instanceof DatabaseError && /^[0-9A-Z]{5}$/.test(error.code ?? "") ? error.code : void 0;
|
|
12
17
|
if (stage === "commit" && (!sqlState || sqlState.startsWith("08") || sqlState === "40003" || sqlState.startsWith("57"))) return {
|
|
@@ -57,13 +62,13 @@ const ORGANIZATION_ID = /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/;
|
|
|
57
62
|
* and unbounded retries.
|
|
58
63
|
*/
|
|
59
64
|
function validateOptions(tenant, options) {
|
|
60
|
-
if (!ORGANIZATION_ID.test(tenant.organizationId)) throw new TypeError("A trusted organization identifier is required.");
|
|
65
|
+
if (typeof tenant.organizationId !== "string" || !ORGANIZATION_ID.test(tenant.organizationId)) throw new TypeError("A trusted organization identifier is required.");
|
|
61
66
|
for (const value of [
|
|
62
67
|
options.statementTimeoutMs,
|
|
63
68
|
options.lockTimeoutMs,
|
|
64
69
|
options.idleTimeoutMs
|
|
65
70
|
]) if (!Number.isSafeInteger(value) || value < 1 || value > 6e4) throw new TypeError("Transaction time budgets must be between 1 and 60000 milliseconds.");
|
|
66
|
-
if (options.isolation && ![
|
|
71
|
+
if (options.isolation !== void 0 && ![
|
|
67
72
|
"read committed",
|
|
68
73
|
"repeatable read",
|
|
69
74
|
"serializable"
|
|
@@ -72,11 +77,15 @@ function validateOptions(tenant, options) {
|
|
|
72
77
|
if (retry && (!Number.isSafeInteger(retry.maxAttempts) || retry.maxAttempts < 1 || retry.maxAttempts > 5 || !Number.isSafeInteger(retry.baseDelayMs) || retry.baseDelayMs < 1 || retry.baseDelayMs > 1e3)) throw new TypeError("Database retries require bounded attempts and delay.");
|
|
73
78
|
}
|
|
74
79
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
* or
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
+
* Runs `work` in one short transaction on a client checked out of `pool`: it begins at the isolation level,
|
|
81
|
+
* sets the time budgets for this transaction alone, passes the tenant explicitly and commits. It answers the
|
|
82
|
+
* work's value, or a classified failure, rolling back while the client can still be trusted; an error without
|
|
83
|
+
* a SQLSTATE that the work throws is thrown on, as it was, after the rollback. A client whose state is
|
|
84
|
+
* unknown is discarded rather than returned to the pool. It installs no row-level security, and no session
|
|
85
|
+
* setting stands for isolation: every query must filter by `tenant.organizationId` and use composite keys
|
|
86
|
+
* itself, and the pool must connect as a restricted role. Keep the callback to database work, without
|
|
87
|
+
* provider, model or stream I/O. An uncertain commit is never retried; reconcile it by the work's own key
|
|
88
|
+
* before repeating anything. An invalid organization id or option throws a TypeError.
|
|
80
89
|
*/
|
|
81
90
|
async function withTenantTransaction(pool, tenant, options, work) {
|
|
82
91
|
validateOptions(tenant, options);
|