@ultimat3/mcp 31.49.0 → 31.51.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/CLAUDE.md +5 -5
- package/README.md +132 -14
- package/package.json +11 -11
- package/src/app-tool.ts +10 -3
- package/src/app-tools.ts +29 -7
- package/src/audit.ts +2 -0
- package/src/catalog-errors.ts +11 -5
- package/src/errors.ts +6 -1
- package/src/from-action.ts +33 -3
- package/src/index.ts +11 -1
- package/src/meta-call.ts +2 -2
- package/src/meta-errors.ts +27 -3
- package/src/meta-split-call.ts +45 -6
- package/src/meta-split-options.ts +180 -0
- package/src/meta-split.ts +3 -2
- package/src/meta-surface.ts +17 -1
- package/src/protocol-era.ts +2 -1
- package/src/recipe-errors.ts +19 -7
- package/src/recipe-markdown.ts +64 -12
- package/src/recipe-replay.ts +12 -5
- package/src/recipe-search.ts +14 -7
- package/src/recipes-surface.ts +19 -6
- package/src/recipes-tool.ts +67 -14
- package/src/recipes.ts +99 -24
- package/src/registry.ts +30 -0
- package/src/remote-client.ts +21 -2
- package/src/remote-tools.ts +4 -0
- package/src/runtime-tool.ts +10 -4
- package/src/server-catalog.ts +6 -0
- package/src/server-input.ts +61 -0
- package/src/server-voice.ts +17 -0
- package/src/server.ts +40 -78
- package/src/tenant-discover.ts +167 -0
- package/src/tenant.ts +94 -21
- package/src/url-elicitation.ts +153 -0
- package/src/wire.ts +2 -0
package/src/recipe-search.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `docs({ q })`'s ranking, stated so it can be argued with: a recipe scores the query words it
|
|
3
|
-
* contains — 3
|
|
3
|
+
* contains — 3 in name, title or aliases, 2 in description, 1 in body — each word once.
|
|
4
4
|
* The best positive score is the hit. With none, the nearest names by edit distance are offered,
|
|
5
5
|
* so a miss is never an empty answer while any recipe exists.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import type { McpRecipe } from './recipes';
|
|
9
|
-
import { renderRecipe } from './recipes';
|
|
9
|
+
import { renderRecipe, uriOf } from './recipes';
|
|
10
10
|
|
|
11
11
|
const NEAREST = 3;
|
|
12
12
|
/** A recipe name is a few dozen characters; a longer probe is no nearer to one for its tail. */
|
|
@@ -35,19 +35,25 @@ export function rankRecipes(recipes: readonly McpRecipe[], q: string): RecipeRan
|
|
|
35
35
|
const scored = recipes
|
|
36
36
|
.map((recipe) => ({ recipe, score: scoreOf(recipe, asked) }))
|
|
37
37
|
.filter((entry) => entry.score > 0)
|
|
38
|
-
.sort((a, b) => b.score - a.score || a.recipe
|
|
38
|
+
.sort((a, b) => b.score - a.score || byName(a.recipe, b.recipe));
|
|
39
39
|
if (scored.length > 0) return { matches: scored.map((entry) => entry.recipe), nearest: [] };
|
|
40
40
|
const target = q.toLowerCase().trim();
|
|
41
41
|
const nearest = recipes
|
|
42
42
|
.map((recipe) => ({ recipe, distance: closeness(recipe, target, asked) }))
|
|
43
|
-
.sort((a, b) => a.distance - b.distance || a.recipe
|
|
43
|
+
.sort((a, b) => a.distance - b.distance || byName(a.recipe, b.recipe))
|
|
44
44
|
.slice(0, NEAREST)
|
|
45
45
|
.map((entry) => entry.recipe);
|
|
46
46
|
return { matches: [], nearest };
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
/** Ties by name, then by URI: one name may sit in two sections. */
|
|
50
|
+
const byName = (a: McpRecipe, b: McpRecipe): number =>
|
|
51
|
+
a.name.localeCompare(b.name) || uriOf(a).localeCompare(uriOf(b));
|
|
52
|
+
|
|
53
|
+
const aliasText = (recipe: McpRecipe): string => (recipe.aliases ?? []).join(' ');
|
|
54
|
+
|
|
49
55
|
function scoreOf(recipe: McpRecipe, asked: readonly string[]): number {
|
|
50
|
-
const named = words(`${recipe.name} ${recipe.title}`);
|
|
56
|
+
const named = words(`${recipe.name} ${recipe.title} ${aliasText(recipe)}`);
|
|
51
57
|
const described = words(recipe.description);
|
|
52
58
|
const body = words(renderRecipe(recipe));
|
|
53
59
|
let score = 0;
|
|
@@ -60,13 +66,14 @@ function scoreOf(recipe: McpRecipe, asked: readonly string[]): number {
|
|
|
60
66
|
}
|
|
61
67
|
|
|
62
68
|
/**
|
|
63
|
-
* The smallest edit distance from the query, or any of its words, to the name or a title
|
|
69
|
+
* The smallest edit distance from the query, or any of its words, to the name or a title or alias
|
|
70
|
+
* word.
|
|
64
71
|
* Bounded: the query is caller input and the distance is |probe| x |candidate|, so only the first
|
|
65
72
|
* `MAX_PROBES` words count, each cut to `MAX_PROBE_LENGTH`, and a pair whose lengths already differ
|
|
66
73
|
* by the best distance found is skipped — it cannot beat it.
|
|
67
74
|
*/
|
|
68
75
|
function closeness(recipe: McpRecipe, target: string, asked: readonly string[]): number {
|
|
69
|
-
const candidates = [recipe.name, ...words(recipe.title)].map(clamped);
|
|
76
|
+
const candidates = [recipe.name, ...words(`${recipe.title} ${aliasText(recipe)}`)].map(clamped);
|
|
70
77
|
const probes = [target, ...asked.slice(0, MAX_PROBES)].map(clamped);
|
|
71
78
|
let best = Number.POSITIVE_INFINITY;
|
|
72
79
|
for (const probe of probes) {
|
package/src/recipes-surface.ts
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* What `recipes:` and `extraRecipes:` add to a `defineAppMcp` server, after the boot checks: the
|
|
3
|
-
* `docs` tool, one `docs
|
|
3
|
+
* `docs` tool, one `docs://<section>/<name>` resource per recipe, and — for a caller's own — a
|
|
4
4
|
* per-caller resource source. Nothing at all without either option.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import { McpToolDuplicateError } from './errors';
|
|
8
8
|
import { noteServedRecipes } from './recipe-links';
|
|
9
9
|
import type { McpRecipe } from './recipes';
|
|
10
|
-
import {
|
|
11
|
-
|
|
10
|
+
import {
|
|
11
|
+
assertNoRecipeResources,
|
|
12
|
+
checkRecipes,
|
|
13
|
+
DEFAULT_SECTION,
|
|
14
|
+
recipeResources,
|
|
15
|
+
sectionOf,
|
|
16
|
+
} from './recipes';
|
|
17
|
+
import type { DocsToolOptions, ExtraRecipes } from './recipes-tool';
|
|
12
18
|
import { DOCS_TOOL, recipesFor, recipesTool } from './recipes-tool';
|
|
13
19
|
import type { AnyMcpTool } from './registry';
|
|
14
20
|
import type { McpResource, McpResourceSource } from './resources';
|
|
@@ -24,19 +30,26 @@ export function withRecipes(
|
|
|
24
30
|
resources: readonly McpResource[],
|
|
25
31
|
recipes: readonly McpRecipe[] | undefined,
|
|
26
32
|
extra: ExtraRecipes | undefined,
|
|
33
|
+
options: DocsToolOptions = {},
|
|
27
34
|
): RecipeSurface {
|
|
28
35
|
const declared = recipes ?? [];
|
|
29
36
|
if (declared.length === 0 && extra === undefined) {
|
|
30
37
|
return { tools, resources, resourceSources: [] };
|
|
31
38
|
}
|
|
32
|
-
assertNoRecipeResources(resources);
|
|
39
|
+
assertNoRecipeResources(resources, declared);
|
|
33
40
|
checkRecipes(declared, tools);
|
|
34
41
|
// The one name this adds; an app tool already called `docs` is a duplicate, never a shadow.
|
|
35
42
|
if (tools.some((tool) => tool.name === DOCS_TOOL)) {
|
|
36
43
|
throw new McpToolDuplicateError({ name: DOCS_TOOL, declaredBy: ['tools:', 'recipes:'] });
|
|
37
44
|
}
|
|
38
|
-
const final: readonly AnyMcpTool[] = [
|
|
39
|
-
|
|
45
|
+
const final: readonly AnyMcpTool[] = [
|
|
46
|
+
...tools,
|
|
47
|
+
recipesTool(declared, () => final, extra, options),
|
|
48
|
+
];
|
|
49
|
+
// Error links name `docs://recipes/<name>` only: the other sections are not theirs to judge.
|
|
50
|
+
noteServedRecipes(
|
|
51
|
+
declared.filter((recipe) => sectionOf(recipe) === DEFAULT_SECTION).map((recipe) => recipe.name),
|
|
52
|
+
);
|
|
40
53
|
const source: McpResourceSource | undefined =
|
|
41
54
|
extra === undefined
|
|
42
55
|
? undefined
|
package/src/recipes-tool.ts
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `docs` — recipes for clients that ignore resources: `{}` lists,
|
|
3
|
-
* `{ q }` inlines the best match or names the nearest.
|
|
4
|
-
* hidden and unknown are one not-found; a caller's own
|
|
2
|
+
* `docs` — recipes for clients that ignore resources: `{}` lists (JSON, or a text map grouped by
|
|
3
|
+
* section), `{ name }` / `{ uri }` reads one, `{ q }` inlines the best match or names the nearest.
|
|
4
|
+
* Gated per recipe by the tools it names, so hidden and unknown are one not-found; a caller's own
|
|
5
|
+
* recipes are marked `source: 'caller'`.
|
|
5
6
|
*/
|
|
6
7
|
|
|
7
8
|
import { renderCauseValue, UltimateError } from '@ultimat3/core';
|
|
8
9
|
import { MAX_QUERY_LENGTH, rankRecipes } from './recipe-search';
|
|
9
10
|
import type { McpRecipe } from './recipes';
|
|
10
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
assertSection,
|
|
13
|
+
recipeKey,
|
|
14
|
+
recipeVisible,
|
|
15
|
+
renderRecipe,
|
|
16
|
+
sectionOf,
|
|
17
|
+
uriOf,
|
|
18
|
+
uriOfKey,
|
|
19
|
+
} from './recipes';
|
|
11
20
|
import type { AnyMcpTool, McpCaller, McpToolResult } from './registry';
|
|
12
21
|
|
|
13
22
|
export const DOCS_TOOL = 'docs';
|
|
@@ -17,13 +26,25 @@ export type ExtraRecipes = (
|
|
|
17
26
|
caller: McpCaller,
|
|
18
27
|
) => Promise<readonly McpRecipe[]> | readonly McpRecipe[];
|
|
19
28
|
|
|
29
|
+
/** How `docs` presents itself: the app's own description, and the shape `docs({})` answers. */
|
|
30
|
+
export interface DocsToolOptions {
|
|
31
|
+
/** Replaces the default tool description. */
|
|
32
|
+
readonly description?: string;
|
|
33
|
+
/**
|
|
34
|
+
* `'json'` (default): `{ recipes: [{ name, title, description, uri, source }] }` — `name` is the
|
|
35
|
+
* bare name, so outside `recipes` read by `uri` or `<section>/<name>`. `'map'`: plain
|
|
36
|
+
* text, one `# <section>` heading per section, then one `<uri> <title>` line per recipe.
|
|
37
|
+
*/
|
|
38
|
+
readonly index?: 'json' | 'map';
|
|
39
|
+
}
|
|
40
|
+
|
|
20
41
|
/** One recipe this caller may follow, and where it came from. */
|
|
21
42
|
export interface SourcedRecipe {
|
|
22
43
|
readonly recipe: McpRecipe;
|
|
23
44
|
readonly source: 'app' | 'caller';
|
|
24
45
|
}
|
|
25
46
|
|
|
26
|
-
/** The app's recipes, then the caller's (a
|
|
47
|
+
/** The app's recipes, then the caller's (a URI the app holds is never shadowed), all gated. */
|
|
27
48
|
export async function recipesFor(
|
|
28
49
|
recipes: readonly McpRecipe[],
|
|
29
50
|
extra: ExtraRecipes | undefined,
|
|
@@ -31,12 +52,13 @@ export async function recipesFor(
|
|
|
31
52
|
caller: McpCaller,
|
|
32
53
|
): Promise<readonly SourcedRecipe[]> {
|
|
33
54
|
const tools = new Map(catalog.map((tool) => [tool.name, tool]));
|
|
34
|
-
const taken = new Set(recipes.map(
|
|
55
|
+
const taken = new Set(recipes.map(uriOf));
|
|
35
56
|
const own = extra === undefined ? [] : await extra(caller);
|
|
57
|
+
for (const recipe of own) assertSection(recipe);
|
|
36
58
|
return [
|
|
37
59
|
...recipes.map((recipe) => ({ recipe, source: 'app' as const })),
|
|
38
60
|
...own
|
|
39
|
-
.filter((recipe) => !taken.has(recipe
|
|
61
|
+
.filter((recipe) => !taken.has(uriOf(recipe)))
|
|
40
62
|
.map((recipe) => ({ recipe, source: 'caller' as const })),
|
|
41
63
|
].filter((entry) => recipeVisible(entry.recipe, tools, caller));
|
|
42
64
|
}
|
|
@@ -45,16 +67,25 @@ export function recipesTool(
|
|
|
45
67
|
recipes: readonly McpRecipe[],
|
|
46
68
|
catalog: () => readonly AnyMcpTool[],
|
|
47
69
|
extra?: ExtraRecipes,
|
|
70
|
+
options: DocsToolOptions = {},
|
|
48
71
|
): AnyMcpTool {
|
|
49
72
|
return {
|
|
50
73
|
name: DOCS_TOOL,
|
|
51
74
|
description:
|
|
75
|
+
options.description ??
|
|
52
76
|
'How-to recipes for this server. docs({}) lists the ones you can follow; docs({ q }) finds one by words and reads it; docs({ name }) or docs({ uri }) reads one: which tools to call, in order, with example arguments.',
|
|
53
77
|
inputSchema: {
|
|
54
78
|
type: 'object',
|
|
55
79
|
properties: {
|
|
56
|
-
name: {
|
|
57
|
-
|
|
80
|
+
name: {
|
|
81
|
+
type: 'string',
|
|
82
|
+
description:
|
|
83
|
+
'A recipe name: bare in the recipes section, <section>/<name> in any other. The uri docs({}) lists always works.',
|
|
84
|
+
},
|
|
85
|
+
uri: {
|
|
86
|
+
type: 'string',
|
|
87
|
+
description: 'A docs://<section>/<name> URI exactly as docs({}) or an error gives it.',
|
|
88
|
+
},
|
|
58
89
|
// A miss quotes `q` back and ranks every recipe against it: a megabyte of it is refused.
|
|
59
90
|
q: {
|
|
60
91
|
type: 'string',
|
|
@@ -72,16 +103,18 @@ export function recipesTool(
|
|
|
72
103
|
typeof args['name'] === 'string'
|
|
73
104
|
? args['name']
|
|
74
105
|
: typeof args['uri'] === 'string'
|
|
75
|
-
?
|
|
106
|
+
? args['uri']
|
|
76
107
|
: undefined;
|
|
77
108
|
if (named !== undefined) {
|
|
78
|
-
const
|
|
109
|
+
const uri = uriOfKey(named);
|
|
110
|
+
const found = visible.find((entry) => uriOf(entry.recipe) === uri);
|
|
79
111
|
if (found === undefined) throw notFound(named);
|
|
80
112
|
return text(renderRecipe(found.recipe));
|
|
81
113
|
}
|
|
82
114
|
if (typeof args['q'] === 'string' && args['q'].trim() !== '') {
|
|
83
115
|
return text(answerQuery(visible, args['q']));
|
|
84
116
|
}
|
|
117
|
+
if (options.index === 'map') return text(renderMap(visible));
|
|
85
118
|
return text(JSON.stringify({ recipes: visible.map(row) }));
|
|
86
119
|
},
|
|
87
120
|
};
|
|
@@ -91,19 +124,39 @@ const row = (entry: SourcedRecipe) => ({
|
|
|
91
124
|
name: entry.recipe.name,
|
|
92
125
|
title: entry.recipe.title,
|
|
93
126
|
description: entry.recipe.description,
|
|
94
|
-
uri:
|
|
127
|
+
uri: uriOf(entry.recipe),
|
|
95
128
|
source: entry.source,
|
|
96
129
|
});
|
|
97
130
|
|
|
131
|
+
/** A line-per-entry answer: a title's own line breaks would split its entry. */
|
|
132
|
+
const oneLine = (title: string): string =>
|
|
133
|
+
title.replace(/[\s\u0085]+/g, (space) =>
|
|
134
|
+
/[\r\n\v\f\u0085\u2028\u2029]/.test(space) ? ' ' : space,
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
const yours = (entry: SourcedRecipe): string => (entry.source === 'caller' ? ' (yours)' : '');
|
|
138
|
+
|
|
139
|
+
/** Sections in first-seen order, each its `<uri> <title>` lines: no tables, no nesting. */
|
|
140
|
+
function renderMap(visible: readonly SourcedRecipe[]): string {
|
|
141
|
+
if (visible.length === 0) return 'No recipes are available to this caller.\n';
|
|
142
|
+
const sections = new Map<string, string[]>();
|
|
143
|
+
for (const entry of visible) {
|
|
144
|
+
const lines = sections.get(sectionOf(entry.recipe)) ?? [];
|
|
145
|
+
lines.push(`${uriOf(entry.recipe)} ${oneLine(entry.recipe.title)}${yours(entry)}`);
|
|
146
|
+
sections.set(sectionOf(entry.recipe), lines);
|
|
147
|
+
}
|
|
148
|
+
return `${[...sections].map(([section, lines]) => `# ${section}\n${lines.join('\n')}`).join('\n\n')}\n`;
|
|
149
|
+
}
|
|
150
|
+
|
|
98
151
|
/** The best match inlined, with the runners-up named; a miss names the nearest instead. */
|
|
99
152
|
function answerQuery(visible: readonly SourcedRecipe[], q: string): string {
|
|
100
|
-
const sources = new Map(visible.map((entry) => [entry.recipe
|
|
153
|
+
const sources = new Map(visible.map((entry) => [uriOf(entry.recipe), entry.source]));
|
|
101
154
|
const { matches, nearest } = rankRecipes(
|
|
102
155
|
visible.map((entry) => entry.recipe),
|
|
103
156
|
q,
|
|
104
157
|
);
|
|
105
158
|
const line = (recipe: McpRecipe) =>
|
|
106
|
-
`- docs({ name: '${recipe
|
|
159
|
+
`- docs({ name: '${recipeKey(recipe)}' }) — ${oneLine(recipe.title)}${sources.get(uriOf(recipe)) === 'caller' ? ' (yours)' : ''}`;
|
|
107
160
|
const [best, ...rest] = matches;
|
|
108
161
|
if (best !== undefined) {
|
|
109
162
|
const related = rest.slice(0, 4);
|
package/src/recipes.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MCP recipes — "how do I do X here" as structure the boot checks: every step names a projected
|
|
3
|
-
* tool and its example must pass that tool's input. Served as `docs
|
|
4
|
-
* `docs`, only to a caller who can see and call every tool a
|
|
3
|
+
* tool and its example must pass that tool's input. Served as `docs://<section>/<name>` (section
|
|
4
|
+
* `recipes` unless set) and through `docs`, only to a caller who can see and call every tool a
|
|
5
|
+
* recipe names.
|
|
5
6
|
*/
|
|
6
7
|
|
|
7
8
|
import {
|
|
@@ -17,7 +18,30 @@ import type { McpResource } from './resources';
|
|
|
17
18
|
import { validateArgs } from './validate-args';
|
|
18
19
|
|
|
19
20
|
export const RECIPE_URI_PREFIX = 'docs://recipes/';
|
|
20
|
-
|
|
21
|
+
/** The section a recipe that names none is served under. */
|
|
22
|
+
export const DEFAULT_SECTION = 'recipes';
|
|
23
|
+
/**
|
|
24
|
+
* A name and a section alike: lowercase letters and digits in groups joined by single `-`
|
|
25
|
+
* (`ref-v2`, never `-ref`, `ref-` or `ref--v2`) — closed, so neither steps out of `docs://<section>/`.
|
|
26
|
+
*/
|
|
27
|
+
export const NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
28
|
+
/**
|
|
29
|
+
* A `docs://<section>/` link and its whole destination, up to whitespace or the Markdown that
|
|
30
|
+
* closes it (brackets, quotes, `*` emphasis, a table's `|`): `docs://ref/errors/old`, `.html`,
|
|
31
|
+
* `%65rrors` and `errors+old` are not `docs://ref/errors`. Less a sentence's end (`LINK_TAIL`).
|
|
32
|
+
*/
|
|
33
|
+
const SECTIONED_LINK = /(\]\()?docs:\/\/([a-z0-9]+(?:-[a-z0-9]+)*)\/([^\s()<>[\]{}"'`*|]+)/g;
|
|
34
|
+
/** Trimmed from a link in prose only: a `[text](…)` destination is taken whole. */
|
|
35
|
+
const LINK_TAIL = new Set(['.', ',', ';', ':', '!', '?', '…']);
|
|
36
|
+
|
|
37
|
+
/** `path` less its trailing sentence punctuation, in one backward pass (no regex backtracking). */
|
|
38
|
+
function withoutLinkTail(path: string): string {
|
|
39
|
+
let end = path.length;
|
|
40
|
+
while (end > 0 && LINK_TAIL.has(path[end - 1] as string)) end -= 1;
|
|
41
|
+
return path.slice(0, end);
|
|
42
|
+
}
|
|
43
|
+
/** The section a `docs://<section>/…` URI sits in. */
|
|
44
|
+
const SECTION_OF_URI = /^docs:\/\/([^/]*)\//;
|
|
21
45
|
|
|
22
46
|
export interface McpRecipeStep {
|
|
23
47
|
/** A tool THIS server projects, by name. Checked at boot. */
|
|
@@ -29,21 +53,48 @@ export interface McpRecipeStep {
|
|
|
29
53
|
}
|
|
30
54
|
|
|
31
55
|
export interface McpRecipe {
|
|
32
|
-
/** `
|
|
56
|
+
/** Lowercase letters and digits in `-`-joined groups (`NAME`); the URI is `docs://<section>/<name>`. */
|
|
33
57
|
readonly name: string;
|
|
58
|
+
/** Spelled like `name`, `recipes` when unset: one `docs({})` group and one `docs://<section>/` prefix. */
|
|
59
|
+
readonly section?: string;
|
|
60
|
+
/** Other words for it: `docs({ q })` scores each as it scores the title. */
|
|
61
|
+
readonly aliases?: readonly string[];
|
|
34
62
|
readonly title: string;
|
|
35
63
|
readonly description: string;
|
|
36
64
|
readonly steps: readonly McpRecipeStep[];
|
|
37
65
|
/** Free text, unchecked: where a tool is named, it is a step. */
|
|
38
66
|
readonly notes?: string;
|
|
39
67
|
/**
|
|
40
|
-
* Markdown served
|
|
41
|
-
* builds from a file
|
|
68
|
+
* Markdown served as given in place of the rendered steps — what `markdownRecipes({ dir })`
|
|
69
|
+
* builds from a file (LF line ends, outer blank lines dropped), its ```call fences becoming
|
|
70
|
+
* `steps`, so they are checked all the same.
|
|
42
71
|
*/
|
|
43
72
|
readonly body?: string;
|
|
44
73
|
}
|
|
45
74
|
|
|
46
|
-
export const recipeUri = (name: string): string =>
|
|
75
|
+
export const recipeUri = (name: string, section: string = DEFAULT_SECTION): string =>
|
|
76
|
+
`docs://${section}/${name}`;
|
|
77
|
+
|
|
78
|
+
/** The URI one recipe is served at. */
|
|
79
|
+
export const uriOf = (recipe: McpRecipe): string => recipeUri(recipe.name, recipe.section);
|
|
80
|
+
|
|
81
|
+
/** The section one recipe is listed under. */
|
|
82
|
+
export const sectionOf = (recipe: McpRecipe): string => recipe.section ?? DEFAULT_SECTION;
|
|
83
|
+
|
|
84
|
+
/** `docs({ name })`'s spelling: the bare name in `recipes`, `<section>/<name>` elsewhere. */
|
|
85
|
+
export const recipeKey = (recipe: McpRecipe): string =>
|
|
86
|
+
sectionOf(recipe) === DEFAULT_SECTION ? recipe.name : `${sectionOf(recipe)}/${recipe.name}`;
|
|
87
|
+
|
|
88
|
+
/** The URI a `docs({ name })` / `docs({ uri })` argument names: a URI, `<section>/<name>` or a name. */
|
|
89
|
+
export const uriOfKey = (key: string): string =>
|
|
90
|
+
key.startsWith('docs://') ? key : key.includes('/') ? `docs://${key}` : recipeUri(key);
|
|
91
|
+
|
|
92
|
+
/** A section outside the closed pattern is refused before it becomes part of a URI. */
|
|
93
|
+
export function assertSection(recipe: McpRecipe): void {
|
|
94
|
+
if (recipe.section !== undefined && !NAME.test(recipe.section)) {
|
|
95
|
+
throw new McpRecipeUriReservedError({ uri: uriOf(recipe), reason: 'section' });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
47
98
|
|
|
48
99
|
/** The recipe a `docs://recipes/<name>` names, or `undefined` for any other URI. */
|
|
49
100
|
export const recipeNameOf = (uri: string): string | undefined =>
|
|
@@ -56,10 +107,11 @@ export const recipeNameOf = (uri: string): string | undefined =>
|
|
|
56
107
|
export function checkRecipes(recipes: readonly McpRecipe[], catalog: readonly AnyMcpTool[]): void {
|
|
57
108
|
const seen = new Set<string>();
|
|
58
109
|
for (const recipe of recipes) {
|
|
59
|
-
const uri =
|
|
110
|
+
const uri = uriOf(recipe);
|
|
60
111
|
if (!NAME.test(recipe.name)) throw new McpRecipeUriReservedError({ uri, reason: 'name' });
|
|
61
|
-
|
|
62
|
-
seen.
|
|
112
|
+
assertSection(recipe);
|
|
113
|
+
if (seen.has(uri)) throw new McpRecipeUriReservedError({ uri, reason: 'duplicate' });
|
|
114
|
+
seen.add(uri);
|
|
63
115
|
}
|
|
64
116
|
const tools = new Map(catalog.map((tool) => [tool.name, tool]));
|
|
65
117
|
const unknown: string[] = [];
|
|
@@ -90,30 +142,53 @@ export function checkRecipes(recipes: readonly McpRecipe[], catalog: readonly An
|
|
|
90
142
|
assertRecipeBodyLinks(recipes);
|
|
91
143
|
}
|
|
92
144
|
|
|
93
|
-
/**
|
|
94
|
-
export
|
|
145
|
+
/** `recipes`, then every section `recipes` holds: the `docs://` prefixes this set owns. */
|
|
146
|
+
export const sectionsOf = (recipes: readonly McpRecipe[]): ReadonlySet<string> =>
|
|
147
|
+
new Set([DEFAULT_SECTION, ...recipes.map(sectionOf)]);
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Every recipe URI a recipe's text names, as written — in a section `sections` holds. A link into
|
|
151
|
+
* any other `docs://` prefix is somebody else's resource, and not judged here.
|
|
152
|
+
*/
|
|
153
|
+
export function recipeLinks(recipe: McpRecipe, sections: ReadonlySet<string>): readonly string[] {
|
|
95
154
|
const text = `${recipe.body ?? ''}\n${recipe.notes ?? ''}`;
|
|
96
|
-
return [...text.matchAll(
|
|
97
|
-
(match) => match[
|
|
98
|
-
|
|
155
|
+
return [...text.matchAll(SECTIONED_LINK)]
|
|
156
|
+
.filter((match) => sections.has(match[2] as string))
|
|
157
|
+
.map((match) => ({
|
|
158
|
+
section: match[2] as string,
|
|
159
|
+
path: match[1] === undefined ? withoutLinkTail(match[3] as string) : (match[3] as string),
|
|
160
|
+
}))
|
|
161
|
+
.filter((link) => link.path !== '')
|
|
162
|
+
.map((link) => recipeUri(link.path, link.section));
|
|
99
163
|
}
|
|
100
164
|
|
|
101
165
|
/** A recipe pointing at one that does not exist is the stale link this whole file refuses. */
|
|
102
166
|
function assertRecipeBodyLinks(recipes: readonly McpRecipe[]): void {
|
|
103
|
-
const
|
|
167
|
+
const uris = new Set(recipes.map(uriOf));
|
|
168
|
+
const sections = sectionsOf(recipes);
|
|
104
169
|
const offenders = recipes.flatMap((recipe) =>
|
|
105
|
-
recipeLinks(recipe)
|
|
106
|
-
.filter((
|
|
107
|
-
.map((
|
|
170
|
+
recipeLinks(recipe, sections)
|
|
171
|
+
.filter((uri) => !uris.has(uri))
|
|
172
|
+
.map((uri) => `recipe "${recipe.name}" links to ${uri}`),
|
|
108
173
|
);
|
|
109
174
|
if (offenders.length > 0) {
|
|
110
|
-
throw new McpRecipeLinkDanglingError({
|
|
175
|
+
throw new McpRecipeLinkDanglingError({
|
|
176
|
+
offenders,
|
|
177
|
+
recipes: recipes.map(recipeKey).sort(),
|
|
178
|
+
});
|
|
111
179
|
}
|
|
112
180
|
}
|
|
113
181
|
|
|
114
|
-
/** A hand-written resource may not take
|
|
115
|
-
export function assertNoRecipeResources(
|
|
116
|
-
|
|
182
|
+
/** A hand-written resource may not take a `docs://<section>/` prefix `recipes:` owns. */
|
|
183
|
+
export function assertNoRecipeResources(
|
|
184
|
+
resources: readonly McpResource[],
|
|
185
|
+
recipes: readonly McpRecipe[],
|
|
186
|
+
): void {
|
|
187
|
+
const sections = sectionsOf(recipes);
|
|
188
|
+
const taken = resources.find((resource) => {
|
|
189
|
+
const section = SECTION_OF_URI.exec(resource.uri)?.[1];
|
|
190
|
+
return section !== undefined && sections.has(section);
|
|
191
|
+
});
|
|
117
192
|
if (taken !== undefined)
|
|
118
193
|
throw new McpRecipeUriReservedError({ uri: taken.uri, reason: 'resource' });
|
|
119
194
|
}
|
|
@@ -157,7 +232,7 @@ export function recipeResources(
|
|
|
157
232
|
): readonly McpResource[] {
|
|
158
233
|
const tools = new Map(catalog.map((tool) => [tool.name, tool]));
|
|
159
234
|
return recipes.map((recipe) => ({
|
|
160
|
-
uri:
|
|
235
|
+
uri: uriOf(recipe),
|
|
161
236
|
name: recipe.title,
|
|
162
237
|
description: recipe.description,
|
|
163
238
|
mimeType: 'text/markdown',
|
package/src/registry.ts
CHANGED
|
@@ -47,6 +47,19 @@ export interface McpCaller {
|
|
|
47
47
|
* belongs to the server, and `ToolRegistry` is where it lives.
|
|
48
48
|
*/
|
|
49
49
|
readonly tenant?: string;
|
|
50
|
+
/**
|
|
51
|
+
* The `'meta-split'` dispatcher THIS call came through — set by `meta-split-call.ts` for one
|
|
52
|
+
* call. Absent on every other path. The audit line carries its `tool`, never its `fields`.
|
|
53
|
+
*/
|
|
54
|
+
readonly door?: McpDoor;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The dispatcher one call came through, and the app-declared envelope fields it carried. */
|
|
58
|
+
export interface McpDoor {
|
|
59
|
+
/** `read_resource`, `write_resource` or `delete_resource`. */
|
|
60
|
+
readonly tool: string;
|
|
61
|
+
/** `split.fields` as this call sent them: a `reason`, a ticket. Never part of the action input. */
|
|
62
|
+
readonly fields: Readonly<Record<string, string>>;
|
|
50
63
|
}
|
|
51
64
|
|
|
52
65
|
/**
|
|
@@ -67,6 +80,17 @@ export type ContentBlock =
|
|
|
67
80
|
| { readonly type: 'text'; readonly text: string }
|
|
68
81
|
| { readonly type: 'resource'; readonly uri: string; readonly mimeType?: string };
|
|
69
82
|
|
|
83
|
+
/**
|
|
84
|
+
* A URL-mode elicitation (MCP 2026-07-28, `client/elicitation`): a page the person opens in their
|
|
85
|
+
* own browser, for what must not pass through the client or the model — a credential, a consent.
|
|
86
|
+
*/
|
|
87
|
+
export interface McpUrlElicitation {
|
|
88
|
+
/** Absolute `https:` (`http:` on this machine only), no `user:password@`, nothing secret in it. */
|
|
89
|
+
readonly url: string;
|
|
90
|
+
/** Why the page is needed, shown to the person beside the URL. */
|
|
91
|
+
readonly message: string;
|
|
92
|
+
}
|
|
93
|
+
|
|
70
94
|
/**
|
|
71
95
|
* `tools/call` result. `isError` flags an EXPECTED tool-level failure (a policy denied the
|
|
72
96
|
* action, a queue was unreachable) so the model sees it as an outcome to reason about.
|
|
@@ -91,6 +115,12 @@ export interface McpToolResult {
|
|
|
91
115
|
* through. Absent keeps the conservative reading.
|
|
92
116
|
*/
|
|
93
117
|
readonly code?: string;
|
|
118
|
+
/**
|
|
119
|
+
* A page the person is asked to open (`url-elicitation.ts`). NEVER a wire field of this result:
|
|
120
|
+
* a modern client that declared `elicitation.url` is answered an `InputRequiredResult` carrying
|
|
121
|
+
* it instead of `content`; every other caller gets `content`, which says the same in text.
|
|
122
|
+
*/
|
|
123
|
+
readonly urlElicitation?: McpUrlElicitation;
|
|
94
124
|
}
|
|
95
125
|
|
|
96
126
|
export type ToolArgs = Record<string, unknown>;
|
package/src/remote-client.ts
CHANGED
|
@@ -33,7 +33,17 @@ export interface McpClientOptions {
|
|
|
33
33
|
readonly auth?: () =>
|
|
34
34
|
| Readonly<Record<string, string>>
|
|
35
35
|
| Promise<Readonly<Record<string, string>>>;
|
|
36
|
-
/**
|
|
36
|
+
/**
|
|
37
|
+
* The largest answer one call reads, in bytes; past it the read stops, `X_MCP_REMOTE_REFUSED`
|
|
38
|
+
* (`meta.failure: 'transport'`, source `X_OUTBOUND_TOO_LARGE`). Default 16 MiB, `outbound()`'s.
|
|
39
|
+
*/
|
|
40
|
+
readonly maxBytes?: number;
|
|
41
|
+
/**
|
|
42
|
+
* The transport: a test double, or a production one. Default `fetch`. It sits under `outbound()`,
|
|
43
|
+
* so the dial screen, deadline, `maxBytes` and redirect refusal stay this client's whatever it
|
|
44
|
+
* does; one that dials anywhere but the url it is handed takes the screen over
|
|
45
|
+
* (`OutboundFetch`).
|
|
46
|
+
*/
|
|
37
47
|
readonly fetch?: OutboundFetch;
|
|
38
48
|
/**
|
|
39
49
|
* The server said `notifications/tools/list_changed` — on a response stream, or on the `GET`
|
|
@@ -81,6 +91,10 @@ const MAX_TOOL_PAGES = 50;
|
|
|
81
91
|
export function mcpClient(options: McpClientOptions): McpClient {
|
|
82
92
|
const state: { era?: 'modern' | 'legacy'; session?: string | null; next: number } = { next: 1 };
|
|
83
93
|
const listenMs = finiteCount('mcpClient', 'listenMs', options.listenMs ?? DEFAULT_LISTEN_MS, 1);
|
|
94
|
+
const maxBytes =
|
|
95
|
+
options.maxBytes === undefined
|
|
96
|
+
? undefined
|
|
97
|
+
: finiteCount('mcpClient', 'maxBytes', options.maxBytes, 1);
|
|
84
98
|
/** The open `GET` stream; `'unsupported'` once the server answered it 405, `'closed'` after close(). */
|
|
85
99
|
let listener: Listener | 'unsupported' | 'closed' | undefined;
|
|
86
100
|
/** The one handshake in flight: concurrent calls that need a session share it, never race two. */
|
|
@@ -127,6 +141,7 @@ export function mcpClient(options: McpClientOptions): McpClient {
|
|
|
127
141
|
...headers,
|
|
128
142
|
},
|
|
129
143
|
body: JSON.stringify(body),
|
|
144
|
+
...(maxBytes === undefined ? {} : { maxBytes }),
|
|
130
145
|
...(signal === undefined ? {} : { signal }),
|
|
131
146
|
},
|
|
132
147
|
options.fetch === undefined ? {} : { fetch: options.fetch },
|
|
@@ -135,7 +150,11 @@ export function mcpClient(options: McpClientOptions): McpClient {
|
|
|
135
150
|
if (!isUltimateError(error)) throw error;
|
|
136
151
|
// An abort is the caller's decision, never the server's fault: it passes through.
|
|
137
152
|
if (error.code === 'X_ABORTED') throw error;
|
|
138
|
-
|
|
153
|
+
const reason =
|
|
154
|
+
error.code === 'X_OUTBOUND_TOO_LARGE'
|
|
155
|
+
? `answered past the client's byte cap (${error.code})`
|
|
156
|
+
: `was not reachable (${error.code})`;
|
|
157
|
+
throw refused(reason, 'transport', undefined, error);
|
|
139
158
|
}
|
|
140
159
|
// A token is wanted, or the one sent is no longer good: never an era to fall back from.
|
|
141
160
|
if (answer.status === 401) {
|
package/src/remote-tools.ts
CHANGED
|
@@ -40,6 +40,9 @@ export interface McpToolsDef {
|
|
|
40
40
|
readonly deadlineMs?: number;
|
|
41
41
|
/** The longest answer handed to a model; the middle of a longer one is cut. Default 20,000. */
|
|
42
42
|
readonly maxResultChars?: number;
|
|
43
|
+
/** The largest answer one call reads off the wire, in bytes (`mcpClient`'s). Default 16 MiB. */
|
|
44
|
+
readonly maxBytes?: number;
|
|
45
|
+
/** The transport under `outbound()` (`mcpClient`'s `fetch`). Default `fetch`. */
|
|
43
46
|
readonly fetch?: OutboundFetch;
|
|
44
47
|
/**
|
|
45
48
|
* Whether this app's OWN MCP surface (`include: 'exposed'`) and its in-app `agent()`s are offered
|
|
@@ -113,6 +116,7 @@ export function mcpTools(def: McpToolsDef): McpTools {
|
|
|
113
116
|
...(def.dial === undefined ? {} : { dial: def.dial }),
|
|
114
117
|
...(def.deadlineMs === undefined ? {} : { deadlineMs: def.deadlineMs }),
|
|
115
118
|
...(def.auth === undefined ? {} : { auth: def.auth }),
|
|
119
|
+
...(def.maxBytes === undefined ? {} : { maxBytes: def.maxBytes }),
|
|
116
120
|
...(def.fetch === undefined ? {} : { fetch: def.fetch }),
|
|
117
121
|
// The server's list moved: forget the verdict, so the next call re-lists and is held to the
|
|
118
122
|
// lock again — a drift there is `X_MCP_REMOTE_TOOL_CHANGED`, never a quietly new tool.
|
package/src/runtime-tool.ts
CHANGED
|
@@ -11,8 +11,8 @@ import type { KnownPermission } from '@ultimat3/policy';
|
|
|
11
11
|
import { can } from '@ultimat3/policy';
|
|
12
12
|
import { McpCatalogUnresolvedError } from './catalog-errors';
|
|
13
13
|
import { McpToolUnsafeError } from './errors';
|
|
14
|
-
import { toolFrom } from './from-action';
|
|
15
|
-
import type { AnyMcpTool, McpToolAnnotations } from './registry';
|
|
14
|
+
import { throughDoor, toolFrom } from './from-action';
|
|
15
|
+
import type { AnyMcpTool, McpDoor, McpToolAnnotations } from './registry';
|
|
16
16
|
import { runtimeInputSchema } from './runtime-schema';
|
|
17
17
|
import type { JsonSchema } from './wire';
|
|
18
18
|
|
|
@@ -21,6 +21,11 @@ export interface RuntimeToolArgs {
|
|
|
21
21
|
readonly input: Readonly<Record<string, unknown>>;
|
|
22
22
|
/** The ambient request context, `actor` set to the MCP caller — the tenant's, on a `tenant` server. */
|
|
23
23
|
readonly ctx: Ctx;
|
|
24
|
+
/**
|
|
25
|
+
* The `'meta-split'` dispatcher this call came through, and the `split.fields` its envelope
|
|
26
|
+
* carried. Absent on every other path.
|
|
27
|
+
*/
|
|
28
|
+
readonly door?: McpDoor;
|
|
24
29
|
}
|
|
25
30
|
|
|
26
31
|
export interface RuntimeToolDefinition {
|
|
@@ -92,15 +97,16 @@ export function runtimeTool(def: RuntimeToolDefinition): McpRuntimeTool {
|
|
|
92
97
|
const ctx = useContext();
|
|
93
98
|
guardActionBeforeInput(policy, { actor: actorOf(ctx), ctx, action: name }, 'mcp');
|
|
94
99
|
}),
|
|
95
|
-
|
|
100
|
+
...throughDoor(({ input, actor, door }) =>
|
|
96
101
|
// The caller is the actor for the whole call, exactly as `app-tool.ts` runs one.
|
|
97
102
|
asCallerContext(actor, async () => {
|
|
98
103
|
const ctx = useContext();
|
|
99
104
|
guardActionBeforeInput(policy, { actor: actorOf(ctx), ctx, action: name }, 'mcp');
|
|
100
105
|
const args = input as Readonly<Record<string, unknown>>;
|
|
101
106
|
guardAction(policy, { actor: actorOf(ctx), input: args, ctx, action: name }, 'mcp');
|
|
102
|
-
return def.run({ input: args, ctx });
|
|
107
|
+
return def.run({ input: args, ctx, ...(door === undefined ? {} : { door }) });
|
|
103
108
|
}),
|
|
109
|
+
),
|
|
104
110
|
});
|
|
105
111
|
const handle: McpRuntimeTool = Object.freeze({ name, scope: def.scope });
|
|
106
112
|
BUILT.set(handle, tool);
|