ovr 7.0.0-next.2 → 7.0.0-next.21
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/CHANGELOG.md +1157 -0
- package/dist/auth/client-source.d.ts +2 -2
- package/dist/auth/client-source.d.ts.map +1 -1
- package/dist/auth/client-source.js +2 -2
- package/dist/auth/client.d.ts.map +1 -1
- package/dist/auth/client.js +7 -4
- package/dist/auth/configure.d.ts +21 -8
- package/dist/auth/configure.d.ts.map +1 -1
- package/dist/auth/configure.js +172 -6
- package/dist/auth/index.d.ts +48 -0
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/index.js +19 -0
- package/dist/auth/passkey.d.ts +14 -2
- package/dist/auth/passkey.d.ts.map +1 -1
- package/dist/auth/passkey.js +18 -5
- package/dist/auth/state.d.ts +6 -0
- package/dist/auth/state.d.ts.map +1 -1
- package/dist/auth/state.js +28 -5
- package/dist/auth/storage.d.ts +38 -0
- package/dist/auth/storage.d.ts.map +1 -1
- package/dist/auth/storage.js +60 -3
- package/dist/context/index.d.ts +4 -4
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js +6 -3
- package/dist/form/index.d.ts +239 -31
- package/dist/form/index.d.ts.map +1 -1
- package/dist/form/index.js +657 -66
- package/dist/markdown/index.d.ts +6 -1
- package/dist/markdown/index.d.ts.map +1 -1
- package/dist/markdown/index.js +180 -9
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +154 -57
- package/dist/schema/index.d.ts +88 -19
- package/dist/schema/index.d.ts.map +1 -1
- package/dist/schema/index.js +32 -11
- package/dist/util/index.d.ts +12 -0
- package/dist/util/index.d.ts.map +1 -1
- package/dist/util/index.js +19 -0
- package/package.json +2 -1
- package/src/auth/client-source.ts +2 -2
- package/src/auth/client.ts +7 -4
- package/src/auth/configure.ts +189 -13
- package/src/auth/index.ts +44 -0
- package/src/auth/passkey.ts +25 -7
- package/src/auth/state.ts +32 -7
- package/src/auth/storage.ts +66 -3
- package/src/context/index.ts +14 -6
- package/src/form/index.ts +1174 -195
- package/src/markdown/index.ts +203 -12
- package/src/mcp/index.ts +205 -72
- package/src/schema/index.ts +195 -52
- package/src/util/index.ts +23 -0
package/dist/markdown/index.d.ts
CHANGED
|
@@ -15,11 +15,16 @@ export declare namespace Markdown {
|
|
|
15
15
|
*
|
|
16
16
|
* @param method Form method
|
|
17
17
|
* @param action Resolved form action
|
|
18
|
-
* @returns The tool's name,
|
|
18
|
+
* @returns The tool's name, the arguments from the address's parameters,
|
|
19
|
+
* the fields whose choices are given as the form renders, which its
|
|
20
|
+
* input schema can't list, and the fields it takes as an array, which
|
|
21
|
+
* the form shows as rows or lines
|
|
19
22
|
*/
|
|
20
23
|
readonly [tools]?: ((method: string, action: URL) => {
|
|
21
24
|
name: string;
|
|
22
25
|
args: Record<string, string>;
|
|
26
|
+
choices: ReadonlySet<string>;
|
|
27
|
+
lists: ReadonlySet<string>;
|
|
23
28
|
} | undefined) | undefined;
|
|
24
29
|
};
|
|
25
30
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/markdown/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAEN,KAAK,MAAM,EAKX,MAAM,oBAAoB,CAAC;AAyC5B,kFAAkF;AAClF,eAAO,MAAM,KAAK,eAAkB,CAAC;AAErC,yBAAiB,QAAQ,CAAC;IACzB,kCAAkC;IAClC,KAAY,OAAO,GAAG;QACrB,wEAAwE;QACxE,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS,CAAC;QAEzC
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/markdown/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAC3C,OAAO,EAEN,KAAK,MAAM,EAKX,MAAM,oBAAoB,CAAC;AAyC5B,kFAAkF;AAClF,eAAO,MAAM,KAAK,eAAkB,CAAC;AAErC,yBAAiB,QAAQ,CAAC;IACzB,kCAAkC;IAClC,KAAY,OAAO,GAAG;QACrB,wEAAwE;QACxE,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,GAAG,GAAG,SAAS,CAAC;QAEzC;;;;;;;;;;;;WAYG;QACH,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC,EACd,CAAC,CACD,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,GAAG,KAET;YACA,IAAI,EAAE,MAAM,CAAC;YACb,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;YAC7B,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;YAC7B,KAAK,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;SAC1B,GACD,SAAS,CAAC,GACZ,SAAS,CAAC;KACb,CAAC;CACF;AAq1CD;;;;;;;;;GASG;AACH,qBAAa,QAAQ;;IAOpB;;;;;OAKG;IACH,YAAY,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,QAAQ,CAAC,OAAO,EAG3D;IAED,+CAA+C;IACxC,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,cAAc,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,CAWrE;IAED;;;;OAIG;IACH,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,QAAQ,CAAC,OAAO,+CAE7D;IAED;;;;;;OAMG;IACH,OAAa,MAAM,CAAC,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,QAAQ,CAAC,OAAO,mBAcnE;IAED;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,GAAG,CAAC,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,GAAG,MAAM,CAAC,KAAK,CAEzD;CACD"}
|
package/dist/markdown/index.js
CHANGED
|
@@ -82,6 +82,8 @@ class Writer {
|
|
|
82
82
|
#skipping = 0;
|
|
83
83
|
/** Writing a form's table, where a control in a cell is named */
|
|
84
84
|
#controls = false;
|
|
85
|
+
/** The array fields of the tool whose form's table is being written */
|
|
86
|
+
#arrays;
|
|
85
87
|
/** Reader mode: before, inside, or after `<main>` */
|
|
86
88
|
#main = "before";
|
|
87
89
|
/** `<main>` elements open, so a nested one does not end the outer one */
|
|
@@ -595,6 +597,9 @@ class Writer {
|
|
|
595
597
|
if (child.tag === "option") {
|
|
596
598
|
const text = Writer.#plain(child);
|
|
597
599
|
const value = child.attrs.value ?? text;
|
|
600
|
+
// the empty choice of a select that may be left empty
|
|
601
|
+
if (!value)
|
|
602
|
+
continue;
|
|
598
603
|
entry.options.push(`${value}${text && text !== value ? ` (${text})` : ""}`);
|
|
599
604
|
if ("selected" in child.attrs)
|
|
600
605
|
entry.value.push(value);
|
|
@@ -664,11 +669,21 @@ class Writer {
|
|
|
664
669
|
// its controls are fields, and the table is kept, with each
|
|
665
670
|
// control named in its cell, a row's button included
|
|
666
671
|
const before = submits.length;
|
|
672
|
+
const from = fields.length;
|
|
667
673
|
visit(child, label, true);
|
|
668
674
|
submits.length = before;
|
|
675
|
+
// a table of a list's rows alone is left out: the tool's
|
|
676
|
+
// form lists its items as the array an agent sends
|
|
677
|
+
const within = fields.slice(from);
|
|
678
|
+
if (tool &&
|
|
679
|
+
within.length &&
|
|
680
|
+
within.every((f) => Writer.#cell(f.name, tool.lists)))
|
|
681
|
+
break;
|
|
669
682
|
this.#controls = true;
|
|
683
|
+
this.#arrays = tool?.lists;
|
|
670
684
|
block(this.#table(child));
|
|
671
685
|
this.#controls = false;
|
|
686
|
+
this.#arrays = undefined;
|
|
672
687
|
break;
|
|
673
688
|
}
|
|
674
689
|
case "pre":
|
|
@@ -728,16 +743,21 @@ class Writer {
|
|
|
728
743
|
notes.pop();
|
|
729
744
|
const lines = tool
|
|
730
745
|
? // the tool's input schema describes the fields, so only what the
|
|
731
|
-
// page adds: current values, such as an edit form's,
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
746
|
+
// page adds: current values, such as an edit form's, choices
|
|
747
|
+
// known only as it renders, such as a record's own IDs, and messages
|
|
748
|
+
Writer.#gather(fields, tool.lists)
|
|
749
|
+
.map((f) => ({
|
|
750
|
+
// a single checkbox is a boolean argument, unchecked included
|
|
751
|
+
...(f.lone ? { ...f, value: [String(f.value.length > 0)] } : f),
|
|
752
|
+
options: tool.choices.has(f.name) ? f.options : [],
|
|
753
|
+
}))
|
|
754
|
+
.filter((f) => f.value.length || f.options.length || f.notes.length)
|
|
737
755
|
.map((f) => {
|
|
738
756
|
let line = `- ${Writer.#code(f.name)}`;
|
|
739
757
|
if (f.value.length)
|
|
740
758
|
line += `: ${Writer.#codes(f.value)}`;
|
|
759
|
+
if (f.options.length)
|
|
760
|
+
line += `, options: ${Writer.#codes(f.options)}`;
|
|
741
761
|
return Writer.#notes(f, line);
|
|
742
762
|
})
|
|
743
763
|
: fields.map((f) => {
|
|
@@ -773,6 +793,143 @@ class Writer {
|
|
|
773
793
|
...(submits.length ? ["", `Submit: ${submit}`] : []),
|
|
774
794
|
].join("\n");
|
|
775
795
|
}
|
|
796
|
+
/**
|
|
797
|
+
* @param name A control's name
|
|
798
|
+
* @param lists The fields a tool takes as an array
|
|
799
|
+
* @returns The list, place, and field of the row the control is in, named
|
|
800
|
+
* `steps.0` or `ingredients.0.amount`, `undefined` for any other control
|
|
801
|
+
*/
|
|
802
|
+
static #cell(name, lists) {
|
|
803
|
+
for (const list of lists) {
|
|
804
|
+
if (!name.startsWith(`${list}.`))
|
|
805
|
+
continue;
|
|
806
|
+
const [, place, key] = /^(\d+)(?:\.(.+))?$/s.exec(name.slice(list.length + 1)) ?? [];
|
|
807
|
+
if (place !== undefined)
|
|
808
|
+
return [list, Number(place), key];
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
/**
|
|
812
|
+
* @param select `<select>` element
|
|
813
|
+
* @returns The value of its selected option, `undefined` without one
|
|
814
|
+
*/
|
|
815
|
+
static #chosen(select) {
|
|
816
|
+
for (const child of select.children) {
|
|
817
|
+
if (typeof child === "string")
|
|
818
|
+
continue;
|
|
819
|
+
if (child.tag !== "option") {
|
|
820
|
+
const chosen = Writer.#chosen(child);
|
|
821
|
+
if (chosen !== undefined)
|
|
822
|
+
return chosen;
|
|
823
|
+
}
|
|
824
|
+
else if ("selected" in child.attrs)
|
|
825
|
+
return child.attrs.value ?? Writer.#plain(child);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
/**
|
|
829
|
+
* @param fields A tool's form's fields, one for each control
|
|
830
|
+
* @param lists The fields the tool takes as an array
|
|
831
|
+
* @returns The fields as the tool takes them: a list's rows together, and
|
|
832
|
+
* lines apart, each as the array an agent sends
|
|
833
|
+
*/
|
|
834
|
+
static #gather(fields, lists) {
|
|
835
|
+
if (!lists.size)
|
|
836
|
+
return fields;
|
|
837
|
+
const gathered = [];
|
|
838
|
+
const found = new Map();
|
|
839
|
+
/** A control's value, as its argument's type */
|
|
840
|
+
const typed = (f) => {
|
|
841
|
+
if (f.lone)
|
|
842
|
+
return f.value.length > 0 || undefined;
|
|
843
|
+
if (f.type === "checkbox" || f.type === "select multiple")
|
|
844
|
+
return f.value.length ? f.value : undefined;
|
|
845
|
+
const [value] = f.value;
|
|
846
|
+
if (!value)
|
|
847
|
+
return;
|
|
848
|
+
return (f.type === "number" || f.type === "range") &&
|
|
849
|
+
Number.isFinite(Number(value))
|
|
850
|
+
? Number(value)
|
|
851
|
+
: value;
|
|
852
|
+
};
|
|
853
|
+
for (const f of fields) {
|
|
854
|
+
if (lists.has(f.name)) {
|
|
855
|
+
// lines, one on each line of a textarea
|
|
856
|
+
const lines = (f.value[0] ?? "")
|
|
857
|
+
.split(/\r?\n/)
|
|
858
|
+
.map((line) => line.trim())
|
|
859
|
+
.filter(Boolean);
|
|
860
|
+
gathered.push({
|
|
861
|
+
...f,
|
|
862
|
+
value: lines.length ? [JSON.stringify(lines)] : [],
|
|
863
|
+
});
|
|
864
|
+
continue;
|
|
865
|
+
}
|
|
866
|
+
const cell = Writer.#cell(f.name, lists);
|
|
867
|
+
if (!cell) {
|
|
868
|
+
gathered.push(f);
|
|
869
|
+
continue;
|
|
870
|
+
}
|
|
871
|
+
const [list, place, key] = cell;
|
|
872
|
+
let group = found.get(list);
|
|
873
|
+
if (!group) {
|
|
874
|
+
group = {
|
|
875
|
+
entry: {
|
|
876
|
+
...f,
|
|
877
|
+
label: list,
|
|
878
|
+
name: list,
|
|
879
|
+
value: [],
|
|
880
|
+
notes: [],
|
|
881
|
+
lone: false,
|
|
882
|
+
},
|
|
883
|
+
rows: new Map(),
|
|
884
|
+
};
|
|
885
|
+
// one row's choices are every row's
|
|
886
|
+
if (key !== undefined)
|
|
887
|
+
group.entry.options = [];
|
|
888
|
+
found.set(list, group);
|
|
889
|
+
gathered.push(group.entry);
|
|
890
|
+
}
|
|
891
|
+
group.entry.notes.push(...f.notes);
|
|
892
|
+
let row = group.rows.get(place);
|
|
893
|
+
if (!row) {
|
|
894
|
+
row = {
|
|
895
|
+
// no prototype, a control's name is its key
|
|
896
|
+
value: key === undefined ? undefined : Object.create(null),
|
|
897
|
+
filled: false,
|
|
898
|
+
};
|
|
899
|
+
group.rows.set(place, row);
|
|
900
|
+
}
|
|
901
|
+
const value = typed(f);
|
|
902
|
+
if (key === undefined)
|
|
903
|
+
row.value = value;
|
|
904
|
+
else if (value !== undefined)
|
|
905
|
+
row.value[key] = value;
|
|
906
|
+
// a hidden value, such as a row's ID, is in an empty row too
|
|
907
|
+
if (value !== undefined && f.type !== "hidden")
|
|
908
|
+
row.filled = true;
|
|
909
|
+
// a column's choices, given once
|
|
910
|
+
const column = `${list}.${key}`;
|
|
911
|
+
if (key !== undefined && f.options.length && !found.has(column)) {
|
|
912
|
+
const entry = {
|
|
913
|
+
...f,
|
|
914
|
+
label: column,
|
|
915
|
+
name: column,
|
|
916
|
+
value: [],
|
|
917
|
+
notes: [],
|
|
918
|
+
lone: false,
|
|
919
|
+
};
|
|
920
|
+
found.set(column, { entry, rows: new Map() });
|
|
921
|
+
gathered.push(entry);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
for (const { entry, rows } of found.values()) {
|
|
925
|
+
const items = [...rows]
|
|
926
|
+
.sort(([a], [b]) => a - b)
|
|
927
|
+
.flatMap(([, row]) => (row.filled ? [row.value] : []));
|
|
928
|
+
if (items.length)
|
|
929
|
+
entry.value = [JSON.stringify(items)];
|
|
930
|
+
}
|
|
931
|
+
return gathered;
|
|
932
|
+
}
|
|
776
933
|
/**
|
|
777
934
|
* @param tool Tool and the arguments an address gave
|
|
778
935
|
* @returns The call, as an agent makes it
|
|
@@ -870,10 +1027,21 @@ class Writer {
|
|
|
870
1027
|
const { name, value, type } = child.attrs;
|
|
871
1028
|
if (child.tag === "button")
|
|
872
1029
|
out += inner();
|
|
1030
|
+
// a list's row, whose items its tool's form lists
|
|
1031
|
+
if (name && this.#arrays && Writer.#cell(name, this.#arrays))
|
|
1032
|
+
break;
|
|
873
1033
|
if (name && type !== "hidden") {
|
|
874
|
-
|
|
875
|
-
const
|
|
876
|
-
|
|
1034
|
+
const box = type === "checkbox" || type === "radio";
|
|
1035
|
+
const checked = "checked" in child.attrs;
|
|
1036
|
+
// what it holds now: a box with a value names its row and
|
|
1037
|
+
// says if it is checked, one without is "on" when it is, and
|
|
1038
|
+
// a select is the option chosen
|
|
1039
|
+
const sent = box
|
|
1040
|
+
? (value ?? (checked ? "on" : ""))
|
|
1041
|
+
: child.tag === "select"
|
|
1042
|
+
? (Writer.#chosen(child) ?? "")
|
|
1043
|
+
: (value ?? "");
|
|
1044
|
+
out += ` ${Writer.#code(sent ? `${name}=${sent}` : name)}${box && value !== undefined && checked ? " (checked)" : ""} `;
|
|
877
1045
|
}
|
|
878
1046
|
break;
|
|
879
1047
|
}
|
|
@@ -976,6 +1144,9 @@ class Writer {
|
|
|
976
1144
|
for (const child of node.children) {
|
|
977
1145
|
if (typeof child === "string")
|
|
978
1146
|
text += child;
|
|
1147
|
+
// an image is read as its alternative text, such as a label's
|
|
1148
|
+
else if (child.tag === "img")
|
|
1149
|
+
text += ` ${child.attrs.alt ?? ""} `;
|
|
979
1150
|
else if (!omitControls || !["select", "textarea"].includes(child.tag))
|
|
980
1151
|
text += " " + Writer.#plain(child, omitControls) + " ";
|
|
981
1152
|
}
|
package/dist/mcp/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/mcp/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAE,MAAM,EAAU,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAAE,KAAK,OAAO,EAAS,MAAM,qBAAqB,CAAC;AAG1D,OAAO,EAAE,KAAK,EAAuB,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/mcp/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAE,MAAM,EAAU,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAAE,KAAK,OAAO,EAAS,MAAM,qBAAqB,CAAC;AAG1D,OAAO,EAAE,KAAK,EAAuB,MAAM,mBAAmB,CAAC;AA2C/D,yBAAiB,GAAG,CAAC;IACpB;;;;OAIG;IACH,KAAY,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI;QAChD;;;;WAIG;QACH,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAEvB,gDAAgD;QAChD,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;QAE/B;;;;WAIG;QACH,QAAQ,CAAC,IAAI,CAAC,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAE9B;;;;WAIG;QACH,QAAQ,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC,IAAI,CAAC;KACzB,CAAC;IAEF,gEAAgE;IAChE,UAAiB,IAAI;QACpB,qEAAqE;QACrE,QAAQ,CAAC,CAAC,MAAM,CAAC,EAAE;YAClB,yCAAyC;YACzC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;YAExB;;;;eAIG;YACH,MAAM,CAAC,CAAC,EAAE,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;YAEpE;;;;eAIG;YACH,KAAK,CACJ,GAAG,EAAE,GAAG,CAAC,KAAK,EACd,QAAQ,EAAE,MAAM,GACd,UAAU,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,KAAK,CAAC,CAAC,CAAC;SACzC,CAAC;KACF;CACD;AAED;;;;;;;GAOG;AACH,qBAAa,GAAG,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM;;IA8CzC,wCAAwC;IACxC,YAAY,OAAO,GAAE,GAAG,CAAC,OAAO,CAAC,CAAC,CAAM,EAEvC;IAED;;;;;OAKG;IACH,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,KAAK,GAAG,UAAU,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,KAAK,CAAC,CAAC,CAgD7D;CAqtCD"}
|
package/dist/mcp/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { agents, direct, mount } from "../app/index.js";
|
|
2
2
|
import { agent } from "../context/index.js";
|
|
3
|
-
import { forms } from "../form/index.js";
|
|
3
|
+
import { forms, field as isField } from "../form/index.js";
|
|
4
4
|
import { Route, parameters, schemas } from "../route/index.js";
|
|
5
5
|
import { Schema } from "../schema/index.js";
|
|
6
|
-
import { Codec, Header, Method, Mime, Size, noStore } from "../util/index.js";
|
|
6
|
+
import { Codec, Header, Method, Mime, Prose, Size, noStore, } from "../util/index.js";
|
|
7
7
|
/** JSON-RPC error */
|
|
8
8
|
class RpcError extends Error {
|
|
9
9
|
/** JSON-RPC error code */
|
|
@@ -488,31 +488,8 @@ export class Mcp {
|
|
|
488
488
|
Object.assign(properties, schema.properties);
|
|
489
489
|
for (const field of (schema.required ?? []))
|
|
490
490
|
required.add(field);
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
for (const name in shape) {
|
|
494
|
-
const field = shape[name];
|
|
495
|
-
const property = properties[name];
|
|
496
|
-
if (field.type === "file")
|
|
497
|
-
properties[name] =
|
|
498
|
-
field.parts > 1
|
|
499
|
-
? { ...property, items: Mcp.#file }
|
|
500
|
-
: {
|
|
501
|
-
...property,
|
|
502
|
-
...Mcp.#file,
|
|
503
|
-
// the field's own, such as a size limit, then how to send it
|
|
504
|
-
description: [
|
|
505
|
-
property.description?.replace(/\.$/, ""),
|
|
506
|
-
Mcp.#file.description,
|
|
507
|
-
]
|
|
508
|
-
.filter(Boolean)
|
|
509
|
-
.join(". "),
|
|
510
|
-
};
|
|
511
|
-
else if (Mcp.#whole(field)) {
|
|
512
|
-
const { type: _, contentMediaType: __, contentSchema, ...keywords } = properties[name];
|
|
513
|
-
properties[name] = { ...contentSchema, ...keywords };
|
|
514
|
-
}
|
|
515
|
-
}
|
|
491
|
+
for (const name in shape)
|
|
492
|
+
properties[name] = Mcp.#property(shape[name], properties[name]);
|
|
516
493
|
}
|
|
517
494
|
for (const [param, key] of Mcp.#params(route)) {
|
|
518
495
|
const schema = route[schemas]?.[key];
|
|
@@ -537,6 +514,43 @@ export class Mcp {
|
|
|
537
514
|
}
|
|
538
515
|
return tools;
|
|
539
516
|
}
|
|
517
|
+
/**
|
|
518
|
+
* A tool's arguments are JSON, so a file is sent as its contents, and a
|
|
519
|
+
* JSON field as the value it parses, not a string of it.
|
|
520
|
+
*
|
|
521
|
+
* @param field Form field
|
|
522
|
+
* @param property The field's JSON Schema
|
|
523
|
+
* @returns The JSON Schema of the argument an agent sends for it
|
|
524
|
+
*/
|
|
525
|
+
static #property(field, property) {
|
|
526
|
+
const { item } = field;
|
|
527
|
+
if (item) {
|
|
528
|
+
// each item of a list, as its own fields are sent
|
|
529
|
+
const items = property.items;
|
|
530
|
+
if (!items)
|
|
531
|
+
return property;
|
|
532
|
+
if (isField(item))
|
|
533
|
+
return { ...property, items: Mcp.#property(item, items) };
|
|
534
|
+
const cells = { ...items.properties };
|
|
535
|
+
for (const key of Object.keys(item))
|
|
536
|
+
cells[key] = Mcp.#property(item[key], cells[key]);
|
|
537
|
+
return { ...property, items: { ...items, properties: cells } };
|
|
538
|
+
}
|
|
539
|
+
if (field.type === "file")
|
|
540
|
+
return field.parts > 1
|
|
541
|
+
? { ...property, items: Mcp.#file }
|
|
542
|
+
: {
|
|
543
|
+
...property,
|
|
544
|
+
...Mcp.#file,
|
|
545
|
+
// the field's own, such as a size limit, then how to send it
|
|
546
|
+
description: Prose.join(property.description, Mcp.#file.description),
|
|
547
|
+
};
|
|
548
|
+
if (Mcp.#whole(field)) {
|
|
549
|
+
const { type: _, contentMediaType: __, contentSchema, ...keywords } = property;
|
|
550
|
+
return { ...contentSchema, ...keywords };
|
|
551
|
+
}
|
|
552
|
+
return property;
|
|
553
|
+
}
|
|
540
554
|
/**
|
|
541
555
|
* @param route Route a tool calls
|
|
542
556
|
* @returns Pattern parameters by the argument that fills them, `wildcard`
|
|
@@ -577,6 +591,19 @@ export class Mcp {
|
|
|
577
591
|
def = "inner" in def ? def.inner.def : undefined;
|
|
578
592
|
return Boolean(def);
|
|
579
593
|
}
|
|
594
|
+
/**
|
|
595
|
+
* @param field Form field
|
|
596
|
+
* @returns `true` for a field of lines, `Field.textarea().lines()`, also when made
|
|
597
|
+
* optional
|
|
598
|
+
*/
|
|
599
|
+
static #lines(field) {
|
|
600
|
+
if (field.tag !== "textarea")
|
|
601
|
+
return false;
|
|
602
|
+
let def = field.def;
|
|
603
|
+
while ("inner" in def)
|
|
604
|
+
def = def.inner.def;
|
|
605
|
+
return def.type === "array";
|
|
606
|
+
}
|
|
580
607
|
/**
|
|
581
608
|
* @param value Tool argument for a file field
|
|
582
609
|
* @returns The file, `undefined` when the argument does not describe one
|
|
@@ -681,22 +708,44 @@ export class Mcp {
|
|
|
681
708
|
message: "Unexpected argument",
|
|
682
709
|
}))), true);
|
|
683
710
|
}
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
711
|
+
/**
|
|
712
|
+
* @param field The field the argument is for, if there is one
|
|
713
|
+
* @param name Form data name
|
|
714
|
+
* @param value The argument
|
|
715
|
+
* @returns The name of a file field whose argument is not a file
|
|
716
|
+
*/
|
|
717
|
+
const send = (field, name, value) => {
|
|
718
|
+
const item = field?.item;
|
|
719
|
+
if (item) {
|
|
720
|
+
// each item's fields are named by its place, as a row's are
|
|
721
|
+
let place = 0;
|
|
722
|
+
for (const row of Array.isArray(value) ? value : []) {
|
|
723
|
+
const at = `${name}.${place++}`;
|
|
724
|
+
if (isField(item)) {
|
|
725
|
+
const failed = send(item, at, row);
|
|
726
|
+
if (failed)
|
|
727
|
+
return failed;
|
|
728
|
+
}
|
|
729
|
+
else if (row && typeof row === "object") {
|
|
730
|
+
for (const key of Object.keys(item)) {
|
|
731
|
+
const failed = send(item[key], `${at}.${key}`, row[key]);
|
|
732
|
+
if (failed)
|
|
733
|
+
return failed;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
689
739
|
const upload = !read && field?.type === "file";
|
|
690
740
|
// a JSON field reads one value, so an array is sent whole, as its
|
|
691
741
|
// input schema describes it
|
|
692
|
-
|
|
693
|
-
if (whole && value != null) {
|
|
742
|
+
if (field && Mcp.#whole(field) && value != null) {
|
|
694
743
|
const text = JSON.stringify(value);
|
|
695
744
|
if (read)
|
|
696
745
|
url.searchParams.append(name, text);
|
|
697
746
|
else
|
|
698
747
|
body.append(name, text);
|
|
699
|
-
|
|
748
|
+
return;
|
|
700
749
|
}
|
|
701
750
|
for (const item of Array.isArray(value) ? value : [value]) {
|
|
702
751
|
if (item == null || item === false)
|
|
@@ -704,12 +753,7 @@ export class Mcp {
|
|
|
704
753
|
if (upload) {
|
|
705
754
|
const file = Mcp.#upload(item);
|
|
706
755
|
if (!file)
|
|
707
|
-
return
|
|
708
|
-
{
|
|
709
|
-
path: name,
|
|
710
|
-
message: "Expected a file: its name, and its contents as text or base64",
|
|
711
|
-
},
|
|
712
|
-
]), true);
|
|
756
|
+
return name;
|
|
713
757
|
body.append(name, file);
|
|
714
758
|
continue;
|
|
715
759
|
}
|
|
@@ -723,8 +767,24 @@ export class Mcp {
|
|
|
723
767
|
else
|
|
724
768
|
body.append(name, text);
|
|
725
769
|
}
|
|
770
|
+
};
|
|
771
|
+
for (const [name, value] of Object.entries(args)) {
|
|
772
|
+
const field = shape && Object.hasOwn(shape, name) ? shape[name] : undefined;
|
|
773
|
+
// a parameter is in the path, and also sent when a field has its name
|
|
774
|
+
if (pathParams.has(name) && !field)
|
|
775
|
+
continue;
|
|
776
|
+
const path = send(field, name, value);
|
|
777
|
+
if (path)
|
|
778
|
+
return Mcp.#result(Mcp.#issues([
|
|
779
|
+
{
|
|
780
|
+
path,
|
|
781
|
+
message: "Expected a file: its name, and its contents as text or base64",
|
|
782
|
+
},
|
|
783
|
+
]), true);
|
|
726
784
|
}
|
|
727
|
-
return this.#fetch(c, session, url, read ? undefined : { method: route.method, body }
|
|
785
|
+
return this.#fetch(c, session, url, read ? undefined : { method: route.method, body },
|
|
786
|
+
// the form's fields among the arguments, which are all a write updates
|
|
787
|
+
new Set(Object.keys(args).filter((name) => shape && Object.hasOwn(shape, name))));
|
|
728
788
|
}
|
|
729
789
|
/**
|
|
730
790
|
* Reads a page as Markdown.
|
|
@@ -811,7 +871,8 @@ export class Mcp {
|
|
|
811
871
|
/**
|
|
812
872
|
* @param c Request context
|
|
813
873
|
* @returns The name of the tool a form submits to, so the page an agent
|
|
814
|
-
* reads points to the tool instead of repeating its fields
|
|
874
|
+
* reads points to the tool instead of repeating its fields, and the fields
|
|
875
|
+
* whose choices only the page lists
|
|
815
876
|
*/
|
|
816
877
|
#tool(c) {
|
|
817
878
|
return (method, action) => {
|
|
@@ -823,7 +884,34 @@ export class Mcp {
|
|
|
823
884
|
const args = {};
|
|
824
885
|
for (const param in match.params)
|
|
825
886
|
args[param === "*" ? "wildcard" : param] = match.params[param];
|
|
826
|
-
|
|
887
|
+
// choices from a schema, given as the form renders, and the fields
|
|
888
|
+
// an agent sends as an array, which the page shows as rows or lines
|
|
889
|
+
const choices = new Set();
|
|
890
|
+
const lists = new Set();
|
|
891
|
+
const shape = forms.get(match.route)?.shape;
|
|
892
|
+
const rendered = (field) => field.options?.length === 0;
|
|
893
|
+
for (const name in shape) {
|
|
894
|
+
const field = shape[name];
|
|
895
|
+
const { item } = field;
|
|
896
|
+
if (!item) {
|
|
897
|
+
if (rendered(field))
|
|
898
|
+
choices.add(name);
|
|
899
|
+
if (Mcp.#lines(field))
|
|
900
|
+
lists.add(name);
|
|
901
|
+
continue;
|
|
902
|
+
}
|
|
903
|
+
lists.add(name);
|
|
904
|
+
if (isField(item)) {
|
|
905
|
+
if (rendered(item))
|
|
906
|
+
choices.add(name);
|
|
907
|
+
}
|
|
908
|
+
else {
|
|
909
|
+
for (const key of Object.keys(item))
|
|
910
|
+
if (rendered(item[key]))
|
|
911
|
+
choices.add(`${name}.${key}`);
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
return { name, args, choices, lists };
|
|
827
915
|
};
|
|
828
916
|
}
|
|
829
917
|
/**
|
|
@@ -884,7 +972,7 @@ export class Mcp {
|
|
|
884
972
|
* @param init Method and body
|
|
885
973
|
* @returns The response, or the form's issues instead of the redirect back to it
|
|
886
974
|
*/
|
|
887
|
-
async #run(c, session, url, init) {
|
|
975
|
+
async #run(c, session, url, init, sent) {
|
|
888
976
|
const language = this.#languages.get(c.req);
|
|
889
977
|
const req = new Request(url, {
|
|
890
978
|
...init,
|
|
@@ -899,6 +987,7 @@ export class Mcp {
|
|
|
899
987
|
const call = {
|
|
900
988
|
session,
|
|
901
989
|
tool: this.#tool(c),
|
|
990
|
+
...(sent && { sent }),
|
|
902
991
|
};
|
|
903
992
|
agent.set(req, call);
|
|
904
993
|
const res = await this.#app.fetch(req, { ip: c.ip });
|
|
@@ -912,7 +1001,8 @@ export class Mcp {
|
|
|
912
1001
|
return Mcp.#issues(call.issues);
|
|
913
1002
|
}
|
|
914
1003
|
/**
|
|
915
|
-
* Calls the app as the agent
|
|
1004
|
+
* Calls the app as the agent. A read follows redirects to pages it can
|
|
1005
|
+
* read, and a write that redirects is answered with where.
|
|
916
1006
|
*
|
|
917
1007
|
* @param c Request context
|
|
918
1008
|
* @param session Session the token authorizes
|
|
@@ -920,14 +1010,14 @@ export class Mcp {
|
|
|
920
1010
|
* @param init Method and body
|
|
921
1011
|
* @returns Tool result
|
|
922
1012
|
*/
|
|
923
|
-
async #fetch(c, session, url, init) {
|
|
1013
|
+
async #fetch(c, session, url, init, sent) {
|
|
924
1014
|
let redirected;
|
|
925
1015
|
/** The call writes, so a redirect from it means it was done */
|
|
926
1016
|
const wrote = init !== undefined;
|
|
927
1017
|
/** A page of this app, by its path */
|
|
928
1018
|
const redirect = (page) => `Redirected to ${page.pathname}${page.search}`;
|
|
929
1019
|
for (let i = 0;; i++) {
|
|
930
|
-
const res = await this.#run(c, session, url, init);
|
|
1020
|
+
const res = await this.#run(c, session, url, init, sent);
|
|
931
1021
|
if (typeof res === "string")
|
|
932
1022
|
return Mcp.#result(res, true);
|
|
933
1023
|
const location = res.headers.get(Header.name.loc);
|
|
@@ -935,9 +1025,11 @@ export class Mcp {
|
|
|
935
1025
|
await res.body?.cancel();
|
|
936
1026
|
const next = new URL(location, url);
|
|
937
1027
|
const readable = this.#readable(c, next);
|
|
938
|
-
|
|
1028
|
+
// a read follows it, as a browser does. A write that redirects is
|
|
1029
|
+
// done, and the agent is told where: the page it goes to, such as a
|
|
1030
|
+
// whole list after deleting one item, is its to read if it wants
|
|
1031
|
+
if (readable && i < Mcp.#redirects && !wrote) {
|
|
939
1032
|
url = redirected = next;
|
|
940
|
-
init = undefined;
|
|
941
1033
|
continue;
|
|
942
1034
|
}
|
|
943
1035
|
// another site is where the app sends the user, such as a checkout,
|
|
@@ -949,13 +1041,6 @@ export class Mcp {
|
|
|
949
1041
|
? redirect(next)
|
|
950
1042
|
: "Redirected");
|
|
951
1043
|
}
|
|
952
|
-
// the route answered a write by redirecting, so the write is done. A
|
|
953
|
-
// page it sends to that then fails, such as on a rate limit, must not
|
|
954
|
-
// read as the write failing, or an agent would make it again
|
|
955
|
-
if (redirected && wrote && !res.ok) {
|
|
956
|
-
await res.body?.cancel();
|
|
957
|
-
return Mcp.#result(`${redirect(redirected)}, which could not be read: ${Mcp.#status(res)}`);
|
|
958
|
-
}
|
|
959
1044
|
if (res.status >= 500)
|
|
960
1045
|
throw await Mcp.#failed(res);
|
|
961
1046
|
const { type, text, blob, live } = await Mcp.#body(res);
|
|
@@ -971,6 +1056,18 @@ export class Mcp {
|
|
|
971
1056
|
mimeType: type,
|
|
972
1057
|
});
|
|
973
1058
|
const [kind] = type?.split("/") ?? [];
|
|
1059
|
+
// a page the route sends as a file to download, such as one to
|
|
1060
|
+
// print: the agent gets the file, not its text to read
|
|
1061
|
+
const disposition = res.headers.get("content-disposition") ?? "";
|
|
1062
|
+
if (text !== undefined && res.ok && /^\s*attachment/i.test(disposition))
|
|
1063
|
+
return Mcp.#result(
|
|
1064
|
+
// the file's name, without what could read as more than a name
|
|
1065
|
+
(/filename="?([^";]+)"?/i.exec(disposition)?.[1] ?? "")
|
|
1066
|
+
.replace(/[^\w .-]+/g, "_")
|
|
1067
|
+
.slice(0, 100), false, {
|
|
1068
|
+
type: "resource",
|
|
1069
|
+
resource: { uri: this.#name(url), mimeType: type, text },
|
|
1070
|
+
});
|
|
974
1071
|
return Mcp.#result([redirected && redirect(redirected), !res.ok && Mcp.#status(res), text]
|
|
975
1072
|
.filter(Boolean)
|
|
976
1073
|
.join("\n\n"), !res.ok,
|