@jangmanj/mcp 0.0.0-stage → 0.12.3
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/LICENSE +21 -0
- package/README.md +337 -2
- package/adapters/pi/HOST-NOTES.md +65 -0
- package/adapters/pi/bridge.ts +721 -0
- package/adapters/pi/config.ts +364 -0
- package/adapters/pi/content-offloaded.ts +53 -0
- package/adapters/pi/content.ts +78 -0
- package/adapters/pi/diagnostics.ts +26 -0
- package/adapters/pi/elicitation.ts +133 -0
- package/adapters/pi/index.ts +210 -0
- package/adapters/pi/oauth.ts +219 -0
- package/adapters/pi/pi-api.ts +133 -0
- package/adapters/pi/resources.ts +177 -0
- package/adapters/pi/result-files.ts +32 -0
- package/adapters/pi/tasks.ts +49 -0
- package/dist/auth/client-credentials.d.ts +65 -0
- package/dist/auth/client-credentials.js +388 -0
- package/dist/auth/discovery.d.ts +28 -0
- package/dist/auth/discovery.js +125 -0
- package/dist/auth/index.d.ts +9 -0
- package/dist/auth/index.js +5 -0
- package/dist/auth/node-ports.d.ts +8 -0
- package/dist/auth/node-ports.js +67 -0
- package/dist/auth/protocol.d.ts +12 -0
- package/dist/auth/protocol.js +39 -0
- package/dist/auth/provider.d.ts +114 -0
- package/dist/auth/provider.js +407 -0
- package/dist/auth/safe-fetch.d.ts +14 -0
- package/dist/auth/safe-fetch.js +130 -0
- package/dist/client/events.d.ts +33 -0
- package/dist/client/events.js +84 -0
- package/dist/client/index.d.ts +65 -0
- package/dist/client/index.js +128 -0
- package/dist/core/auth-types.d.ts +36 -0
- package/dist/core/auth-types.js +11 -0
- package/dist/core/bearer-challenge.d.ts +3 -0
- package/dist/core/bearer-challenge.js +63 -0
- package/dist/core/cancel.d.ts +70 -0
- package/dist/core/cancel.js +177 -0
- package/dist/core/extension.d.ts +33 -0
- package/dist/core/extension.js +145 -0
- package/dist/core/index.d.ts +73 -0
- package/dist/core/index.js +129 -0
- package/dist/core/jsonrpc.d.ts +49 -0
- package/dist/core/jsonrpc.js +80 -0
- package/dist/core/lifecycle.d.ts +128 -0
- package/dist/core/lifecycle.js +251 -0
- package/dist/core/outcome.d.ts +98 -0
- package/dist/core/outcome.js +32 -0
- package/dist/core/ports.d.ts +144 -0
- package/dist/core/ports.js +67 -0
- package/dist/core/progress.d.ts +81 -0
- package/dist/core/progress.js +205 -0
- package/dist/core/request.d.ts +108 -0
- package/dist/core/request.js +578 -0
- package/dist/core/schema/index.d.ts +1 -0
- package/dist/core/schema/index.js +1 -0
- package/dist/core/schema/types.d.ts +4154 -0
- package/dist/core/schema/types.js +6 -0
- package/dist/core/telemetry.d.ts +25 -0
- package/dist/core/telemetry.js +37 -0
- package/dist/ext-events/index.d.ts +79 -0
- package/dist/ext-events/index.js +218 -0
- package/dist/ext-events/model.d.ts +10 -0
- package/dist/ext-events/model.js +6 -0
- package/dist/ext-events/push.d.ts +18 -0
- package/dist/ext-events/push.js +101 -0
- package/dist/ext-events/stream.d.ts +6 -0
- package/dist/ext-events/stream.js +60 -0
- package/dist/ext-server-cards/index.d.ts +107 -0
- package/dist/ext-server-cards/index.js +249 -0
- package/dist/ext-skills/cache.d.ts +20 -0
- package/dist/ext-skills/cache.js +101 -0
- package/dist/ext-skills/client.d.ts +82 -0
- package/dist/ext-skills/client.js +292 -0
- package/dist/ext-skills/extension.d.ts +16 -0
- package/dist/ext-skills/extension.js +29 -0
- package/dist/ext-skills/index.d.ts +4 -0
- package/dist/ext-skills/index.js +4 -0
- package/dist/ext-skills/verify.d.ts +57 -0
- package/dist/ext-skills/verify.js +168 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/mrtr-cache/cache.d.ts +51 -0
- package/dist/mrtr-cache/cache.js +295 -0
- package/dist/mrtr-cache/client.d.ts +24 -0
- package/dist/mrtr-cache/client.js +44 -0
- package/dist/mrtr-cache/common.d.ts +10 -0
- package/dist/mrtr-cache/common.js +9 -0
- package/dist/mrtr-cache/elicitation.d.ts +42 -0
- package/dist/mrtr-cache/elicitation.js +221 -0
- package/dist/mrtr-cache/index.d.ts +9 -0
- package/dist/mrtr-cache/index.js +8 -0
- package/dist/mrtr-cache/mrtr.d.ts +37 -0
- package/dist/mrtr-cache/mrtr.js +250 -0
- package/dist/mrtr-cache/refresh.d.ts +8 -0
- package/dist/mrtr-cache/refresh.js +30 -0
- package/dist/mrtr-cache/stream.d.ts +5 -0
- package/dist/mrtr-cache/stream.js +18 -0
- package/dist/mrtr-cache/subscriptions.d.ts +96 -0
- package/dist/mrtr-cache/subscriptions.js +288 -0
- package/dist/mrtr-cache/tasks.d.ts +103 -0
- package/dist/mrtr-cache/tasks.js +444 -0
- package/dist/ops/common.d.ts +18 -0
- package/dist/ops/common.js +39 -0
- package/dist/ops/completion.d.ts +44 -0
- package/dist/ops/completion.js +65 -0
- package/dist/ops/content.d.ts +40 -0
- package/dist/ops/content.js +117 -0
- package/dist/ops/icons.d.ts +29 -0
- package/dist/ops/icons.js +82 -0
- package/dist/ops/index.d.ts +30 -0
- package/dist/ops/index.js +28 -0
- package/dist/ops/pagination.d.ts +32 -0
- package/dist/ops/pagination.js +65 -0
- package/dist/ops/prompts.d.ts +32 -0
- package/dist/ops/prompts.js +68 -0
- package/dist/ops/resources.d.ts +42 -0
- package/dist/ops/resources.js +104 -0
- package/dist/ops/schema.d.ts +39 -0
- package/dist/ops/schema.js +163 -0
- package/dist/ops/tools.d.ts +88 -0
- package/dist/ops/tools.js +169 -0
- package/dist/ops/uri-template.d.ts +3 -0
- package/dist/ops/uri-template.js +101 -0
- package/dist/server/index.d.ts +5 -0
- package/dist/server/index.js +5 -0
- package/dist/server/mrtr.d.ts +38 -0
- package/dist/server/mrtr.js +99 -0
- package/dist/server/protocol.d.ts +21 -0
- package/dist/server/protocol.js +49 -0
- package/dist/server/server.d.ts +105 -0
- package/dist/server/server.js +383 -0
- package/dist/server/skills.d.ts +45 -0
- package/dist/server/skills.js +73 -0
- package/dist/server/uri.d.ts +2 -0
- package/dist/server/uri.js +27 -0
- package/dist/server-events/bus.d.ts +40 -0
- package/dist/server-events/bus.js +84 -0
- package/dist/server-events/events.d.ts +36 -0
- package/dist/server-events/events.js +108 -0
- package/dist/server-events/index.d.ts +6 -0
- package/dist/server-events/index.js +6 -0
- package/dist/server-events/notices.d.ts +24 -0
- package/dist/server-events/notices.js +31 -0
- package/dist/server-events/subscriptions.d.ts +33 -0
- package/dist/server-events/subscriptions.js +77 -0
- package/dist/server-events/tasks.d.ts +43 -0
- package/dist/server-events/tasks.js +101 -0
- package/dist/server-events/types.d.ts +25 -0
- package/dist/server-events/types.js +34 -0
- package/dist/server-transport/auth.d.ts +20 -0
- package/dist/server-transport/auth.js +29 -0
- package/dist/server-transport/channel.d.ts +17 -0
- package/dist/server-transport/channel.js +47 -0
- package/dist/server-transport/http.d.ts +19 -0
- package/dist/server-transport/http.js +209 -0
- package/dist/server-transport/index.d.ts +4 -0
- package/dist/server-transport/index.js +4 -0
- package/dist/server-transport/stdio.d.ts +16 -0
- package/dist/server-transport/stdio.js +83 -0
- package/dist/telemetry-otel/index.d.ts +64 -0
- package/dist/telemetry-otel/index.js +196 -0
- package/dist/testing/index.d.ts +36 -0
- package/dist/testing/index.js +140 -0
- package/dist/transport-http/headers.d.ts +24 -0
- package/dist/transport-http/headers.js +140 -0
- package/dist/transport-http/index.d.ts +3 -0
- package/dist/transport-http/index.js +3 -0
- package/dist/transport-http/sse.d.ts +33 -0
- package/dist/transport-http/sse.js +152 -0
- package/dist/transport-http/transport.d.ts +66 -0
- package/dist/transport-http/transport.js +854 -0
- package/dist/transport-stdio/framing.d.ts +50 -0
- package/dist/transport-stdio/framing.js +154 -0
- package/dist/transport-stdio/index.d.ts +4 -0
- package/dist/transport-stdio/index.js +4 -0
- package/dist/transport-stdio/stderr-ring.d.ts +27 -0
- package/dist/transport-stdio/stderr-ring.js +105 -0
- package/dist/transport-stdio/transport.d.ts +142 -0
- package/dist/transport-stdio/transport.js +743 -0
- package/dist/transport-stdio/write-queue.d.ts +48 -0
- package/dist/transport-stdio/write-queue.js +147 -0
- package/package.json +134 -4
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SchemaValidator port and the built-in JSON Schema 2020-12 validator
|
|
3
|
+
* (OPS-SCHEMA-1..7), backed by Ajv 2020 (`ajv/dist/2020`). No network:
|
|
4
|
+
* Ajv `loadSchema` is never configured, so only same-document `$ref` resolves.
|
|
5
|
+
*/
|
|
6
|
+
export declare const DIALECT_2020_12 = "https://json-schema.org/draft/2020-12/schema";
|
|
7
|
+
export interface SchemaIssue {
|
|
8
|
+
readonly path: string;
|
|
9
|
+
readonly keyword: string;
|
|
10
|
+
readonly message: string;
|
|
11
|
+
}
|
|
12
|
+
export interface ValidationResult {
|
|
13
|
+
readonly valid: boolean;
|
|
14
|
+
readonly errors: readonly SchemaIssue[];
|
|
15
|
+
}
|
|
16
|
+
export type CompileResult = {
|
|
17
|
+
readonly ok: true;
|
|
18
|
+
readonly validate: (value: unknown) => ValidationResult;
|
|
19
|
+
} | {
|
|
20
|
+
readonly ok: false;
|
|
21
|
+
readonly reason: "unsupported-dialect" | "invalid-schema";
|
|
22
|
+
readonly message: string;
|
|
23
|
+
readonly dialect?: string;
|
|
24
|
+
};
|
|
25
|
+
/** Dialect-agnostic port; another validator may replace the built-in one. */
|
|
26
|
+
export interface SchemaValidator {
|
|
27
|
+
compile(schema: unknown): CompileResult;
|
|
28
|
+
}
|
|
29
|
+
export interface SchemaBounds {
|
|
30
|
+
readonly maxDepth: number;
|
|
31
|
+
readonly maxSubschemas: number;
|
|
32
|
+
readonly maxBytes: number;
|
|
33
|
+
}
|
|
34
|
+
export declare const DEFAULT_SCHEMA_BOUNDS: SchemaBounds;
|
|
35
|
+
export interface BuiltinValidatorOptions {
|
|
36
|
+
readonly bounds?: Partial<SchemaBounds>;
|
|
37
|
+
}
|
|
38
|
+
/** Built-in Draft 2020-12 validator (Ajv). Other `$schema` dialects fail as unsupported-dialect. */
|
|
39
|
+
export declare function createBuiltinValidator(opts?: BuiltinValidatorOptions): SchemaValidator;
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SchemaValidator port and the built-in JSON Schema 2020-12 validator
|
|
3
|
+
* (OPS-SCHEMA-1..7), backed by Ajv 2020 (`ajv/dist/2020`). No network:
|
|
4
|
+
* Ajv `loadSchema` is never configured, so only same-document `$ref` resolves.
|
|
5
|
+
*/
|
|
6
|
+
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
7
|
+
export const DIALECT_2020_12 = "https://json-schema.org/draft/2020-12/schema";
|
|
8
|
+
export const DEFAULT_SCHEMA_BOUNDS = {
|
|
9
|
+
maxDepth: 64,
|
|
10
|
+
maxSubschemas: 10_000,
|
|
11
|
+
maxBytes: 1024 * 1024,
|
|
12
|
+
};
|
|
13
|
+
const isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
14
|
+
/**
|
|
15
|
+
* Options shared by every Ajv instance. `strict:false` lets unknown annotations
|
|
16
|
+
* (`x-*`, `x-mcp-header`, vendor keywords) through; formats are annotations only;
|
|
17
|
+
* `ownProperties` keeps inherited properties from satisfying `required`.
|
|
18
|
+
* `loadSchema` is deliberately absent: external `$ref` fails compilation.
|
|
19
|
+
*/
|
|
20
|
+
const AJV_BASE = {
|
|
21
|
+
strict: false,
|
|
22
|
+
validateFormats: false,
|
|
23
|
+
ownProperties: true,
|
|
24
|
+
multipleOfPrecision: 9,
|
|
25
|
+
unicodeRegExp: true,
|
|
26
|
+
};
|
|
27
|
+
/** Meta-schema validation only; user schemas are never added to this instance. */
|
|
28
|
+
let metaAjv;
|
|
29
|
+
const meta = () => (metaAjv ??= new Ajv2020({ ...AJV_BASE }));
|
|
30
|
+
/**
|
|
31
|
+
* One Ajv instance per compiled schema: user `$id`s never collide across schemas
|
|
32
|
+
* or shadow the meta-schema, and nothing accumulates in a shared Ajv cache.
|
|
33
|
+
*/
|
|
34
|
+
const freshAjv = () => new Ajv2020({ ...AJV_BASE, meta: false, validateSchema: false, allErrors: true, verbose: true });
|
|
35
|
+
class SchemaError extends Error {
|
|
36
|
+
reason;
|
|
37
|
+
dialect;
|
|
38
|
+
constructor(reason, message, dialect) {
|
|
39
|
+
super(message);
|
|
40
|
+
this.reason = reason;
|
|
41
|
+
this.dialect = dialect;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** Pre-compile scan (OPS-SCHEMA-6): runs before Ajv sees the schema. */
|
|
45
|
+
function bound(schema, b) {
|
|
46
|
+
const bytes = new TextEncoder().encode(JSON.stringify(schema) ?? "").length;
|
|
47
|
+
if (bytes > b.maxBytes)
|
|
48
|
+
throw new SchemaError("invalid-schema", `schema exceeds ${b.maxBytes} bytes`);
|
|
49
|
+
let count = 0;
|
|
50
|
+
// Depth counts nested objects and arrays only; scalar leaves do not add a level.
|
|
51
|
+
const walk = (v, depth) => {
|
|
52
|
+
if (typeof v !== "object" || v === null)
|
|
53
|
+
return;
|
|
54
|
+
if (depth > b.maxDepth)
|
|
55
|
+
throw new SchemaError("invalid-schema", `schema depth exceeds ${b.maxDepth}`);
|
|
56
|
+
if (Array.isArray(v)) {
|
|
57
|
+
for (const x of v)
|
|
58
|
+
walk(x, depth + 1);
|
|
59
|
+
}
|
|
60
|
+
else if (isObj(v)) {
|
|
61
|
+
count += 1;
|
|
62
|
+
if (count > b.maxSubschemas)
|
|
63
|
+
throw new SchemaError("invalid-schema", `schema exceeds ${b.maxSubschemas} subschemas`);
|
|
64
|
+
for (const x of Object.values(v))
|
|
65
|
+
walk(x, depth + 1);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
walk(schema, 1);
|
|
69
|
+
}
|
|
70
|
+
function typeOf(v) {
|
|
71
|
+
if (v === null)
|
|
72
|
+
return "null";
|
|
73
|
+
if (Array.isArray(v))
|
|
74
|
+
return "array";
|
|
75
|
+
if (typeof v === "number")
|
|
76
|
+
return Number.isInteger(v) ? "integer" : "number";
|
|
77
|
+
return typeof v;
|
|
78
|
+
}
|
|
79
|
+
function toIssue(e) {
|
|
80
|
+
const path = e.instancePath || "/";
|
|
81
|
+
if (e.keyword === "type") {
|
|
82
|
+
const t = e.params.type;
|
|
83
|
+
return { path, keyword: "type", message: `expected ${Array.isArray(t) ? t.join("|") : String(t)}, got ${typeOf(e.data)}` };
|
|
84
|
+
}
|
|
85
|
+
if (e.keyword === "false schema")
|
|
86
|
+
return { path, keyword: "false", message: "schema is false" };
|
|
87
|
+
return { path, keyword: e.keyword, message: e.message ?? e.keyword };
|
|
88
|
+
}
|
|
89
|
+
const DEPTH_ISSUE = { path: "/", keyword: "$ref", message: "validation depth limit exceeded" };
|
|
90
|
+
/** Built-in Draft 2020-12 validator (Ajv). Other `$schema` dialects fail as unsupported-dialect. */
|
|
91
|
+
export function createBuiltinValidator(opts = {}) {
|
|
92
|
+
const bounds = { ...DEFAULT_SCHEMA_BOUNDS, ...opts.bounds };
|
|
93
|
+
return {
|
|
94
|
+
compile(schema) {
|
|
95
|
+
try {
|
|
96
|
+
if (typeof schema !== "boolean" && !isObj(schema)) {
|
|
97
|
+
throw new SchemaError("invalid-schema", "schema must be an object or boolean");
|
|
98
|
+
}
|
|
99
|
+
if (isObj(schema) && "$schema" in schema) {
|
|
100
|
+
const d = schema["$schema"];
|
|
101
|
+
if (typeof d !== "string" || d.replace(/#$/, "") !== DIALECT_2020_12) {
|
|
102
|
+
throw new SchemaError("unsupported-dialect", `unsupported JSON Schema dialect '${String(d)}'; only 2020-12 is supported`, typeof d === "string" ? d : String(d));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
bound(schema, bounds);
|
|
106
|
+
const m = meta();
|
|
107
|
+
if (!m.validateSchema(schema)) {
|
|
108
|
+
throw new SchemaError("invalid-schema", `invalid schema: ${m.errorsText(m.errors)}`);
|
|
109
|
+
}
|
|
110
|
+
let fn;
|
|
111
|
+
try {
|
|
112
|
+
fn = freshAjv().compile(schema);
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
if (e instanceof RangeError) {
|
|
116
|
+
throw new SchemaError("invalid-schema", "schema reference depth exceeded ($ref cycle without progress)");
|
|
117
|
+
}
|
|
118
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
119
|
+
if (/resolve reference|only supports hash fragment/.test(msg)) {
|
|
120
|
+
throw new SchemaError("invalid-schema", `${msg} (network and external references are never resolved)`);
|
|
121
|
+
}
|
|
122
|
+
throw new SchemaError("invalid-schema", msg);
|
|
123
|
+
}
|
|
124
|
+
// The SchemaValidator port is synchronous; Ajv's `$async` extension would return a Promise.
|
|
125
|
+
if (fn.$async === true) {
|
|
126
|
+
throw new SchemaError("invalid-schema", "'$async' schemas are not supported (validation is synchronous)");
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
ok: true,
|
|
130
|
+
validate(value) {
|
|
131
|
+
try {
|
|
132
|
+
const r = fn(value);
|
|
133
|
+
if (r !== true && r !== false) {
|
|
134
|
+
return { valid: false, errors: [{ path: "/", keyword: "$async", message: "non-boolean validation result" }] };
|
|
135
|
+
}
|
|
136
|
+
if (r)
|
|
137
|
+
return { valid: true, errors: [] };
|
|
138
|
+
const errors = (fn.errors ?? []).map(toIssue);
|
|
139
|
+
return { valid: false, errors: errors.length ? errors : [{ path: "/", keyword: "schema", message: "invalid" }] };
|
|
140
|
+
}
|
|
141
|
+
catch (e) {
|
|
142
|
+
// Recursive $ref without progress, or instance nesting beyond the stack.
|
|
143
|
+
if (e instanceof RangeError)
|
|
144
|
+
return { valid: false, errors: [DEPTH_ISSUE] };
|
|
145
|
+
throw e;
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
catch (e) {
|
|
151
|
+
if (e instanceof SchemaError) {
|
|
152
|
+
return {
|
|
153
|
+
ok: false,
|
|
154
|
+
reason: e.reason,
|
|
155
|
+
message: e.message,
|
|
156
|
+
...(e.dialect !== undefined ? { dialect: e.dialect } : {}),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
return { ok: false, reason: "invalid-schema", message: e instanceof Error ? e.message : String(e) };
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { Outcome, RequestOptions } from "../core/index.js";
|
|
2
|
+
import { type CapabilitySource, type OpsClient } from "./common.js";
|
|
3
|
+
import { type ContentBlock } from "./content.js";
|
|
4
|
+
import { type ListAllOptions, type ListAllResult } from "./pagination.js";
|
|
5
|
+
import { type SchemaIssue, type SchemaValidator } from "./schema.js";
|
|
6
|
+
export type SchemaState = "valid" | "unsupported-dialect" | "invalid-schema";
|
|
7
|
+
export interface ToolEntry {
|
|
8
|
+
readonly name: string;
|
|
9
|
+
/** The tool exactly as received. */
|
|
10
|
+
readonly tool: Record<string, unknown>;
|
|
11
|
+
readonly nameWarnings: readonly string[];
|
|
12
|
+
/** Annotations are untrusted unless ServerTrust says otherwise (OPS-TOOLS-6). Never used for retry. */
|
|
13
|
+
readonly annotations?: {
|
|
14
|
+
readonly value: unknown;
|
|
15
|
+
readonly trust: "untrusted" | "trusted";
|
|
16
|
+
};
|
|
17
|
+
readonly inputValidation: SchemaState;
|
|
18
|
+
readonly outputValidation?: SchemaState;
|
|
19
|
+
}
|
|
20
|
+
export interface ToolsListResult {
|
|
21
|
+
readonly tools: ToolEntry[];
|
|
22
|
+
readonly nextCursor?: string;
|
|
23
|
+
readonly ttlMs?: unknown;
|
|
24
|
+
readonly cacheScope?: unknown;
|
|
25
|
+
readonly warnings: string[];
|
|
26
|
+
readonly excluded: {
|
|
27
|
+
readonly name: string | undefined;
|
|
28
|
+
readonly reason: string;
|
|
29
|
+
readonly detail?: string;
|
|
30
|
+
}[];
|
|
31
|
+
}
|
|
32
|
+
export interface CallContext {
|
|
33
|
+
readonly name: string;
|
|
34
|
+
readonly arguments: unknown;
|
|
35
|
+
readonly opts: RequestOptions | undefined;
|
|
36
|
+
}
|
|
37
|
+
export interface ToolHooks {
|
|
38
|
+
/** Return `false` or `{ veto: reason }` to prevent the send. Host policy (OPS-TOOLS-17). */
|
|
39
|
+
beforeCall?(ctx: CallContext): void | boolean | {
|
|
40
|
+
readonly veto: string;
|
|
41
|
+
} | Promise<void | boolean | {
|
|
42
|
+
readonly veto: string;
|
|
43
|
+
}>;
|
|
44
|
+
afterCall?(ctx: CallContext, outcome: Outcome<unknown>): void | Promise<void>;
|
|
45
|
+
}
|
|
46
|
+
export interface ToolsOptions {
|
|
47
|
+
readonly capabilities?: CapabilitySource;
|
|
48
|
+
readonly validator?: SchemaValidator;
|
|
49
|
+
/** Host trust for annotations. Default: untrusted. */
|
|
50
|
+
readonly serverTrusted?: boolean;
|
|
51
|
+
readonly hooks?: ToolHooks;
|
|
52
|
+
}
|
|
53
|
+
export interface ToolCallOptions extends RequestOptions {
|
|
54
|
+
/** Default true (OPS-TOOLS-12). */
|
|
55
|
+
readonly validateInput?: boolean;
|
|
56
|
+
/** Call a tool whose schema has an unsupported dialect (OPS-SCHEMA-2). Default false. */
|
|
57
|
+
readonly allowUnvalidated?: boolean;
|
|
58
|
+
/** Turn an outputSchema mismatch into protocol-violation (OPS-TOOLS-11). Default false. */
|
|
59
|
+
readonly strictOutput?: boolean;
|
|
60
|
+
}
|
|
61
|
+
export interface OutputValidation {
|
|
62
|
+
readonly status: "valid" | "invalid" | "skipped" | "unsupported-dialect" | "invalid-schema" | "no-schema";
|
|
63
|
+
readonly errors?: readonly SchemaIssue[];
|
|
64
|
+
}
|
|
65
|
+
export interface ToolCallResult {
|
|
66
|
+
/** Raw complete result, untouched (OPS-TOOLS-15). */
|
|
67
|
+
readonly raw: Record<string, unknown>;
|
|
68
|
+
readonly content: ContentBlock[];
|
|
69
|
+
/** Any JSON value; do not assume an object (OPS-SCHEMA-7). */
|
|
70
|
+
readonly structuredContent?: unknown;
|
|
71
|
+
readonly validation: OutputValidation;
|
|
72
|
+
}
|
|
73
|
+
export declare class ToolsApi {
|
|
74
|
+
private readonly client;
|
|
75
|
+
private readonly opts;
|
|
76
|
+
private readonly compiled;
|
|
77
|
+
private readonly validator;
|
|
78
|
+
constructor(client: OpsClient, opts?: ToolsOptions);
|
|
79
|
+
list(opts?: RequestOptions & {
|
|
80
|
+
readonly cursor?: string;
|
|
81
|
+
}): Promise<Outcome<ToolsListResult>>;
|
|
82
|
+
listAll(opts?: RequestOptions & ListAllOptions): Promise<Outcome<ListAllResult<ToolEntry> & {
|
|
83
|
+
warnings: string[];
|
|
84
|
+
excluded: ToolsListResult["excluded"];
|
|
85
|
+
}>>;
|
|
86
|
+
private parsePage;
|
|
87
|
+
call(name: string, args?: unknown, opts?: ToolCallOptions): Promise<Outcome<ToolCallResult>>;
|
|
88
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { gate, isObj, local, mapMethodNotFound, mapOutcome, } from "./common.js";
|
|
2
|
+
import { normalizeContent } from "./content.js";
|
|
3
|
+
import { listAll, cursorParams, nextCursorOf } from "./pagination.js";
|
|
4
|
+
import { createBuiltinValidator } from "./schema.js";
|
|
5
|
+
const NAME_RE = /^[A-Za-z0-9_.-]{1,128}$/;
|
|
6
|
+
export class ToolsApi {
|
|
7
|
+
client;
|
|
8
|
+
opts;
|
|
9
|
+
compiled = new Map();
|
|
10
|
+
validator;
|
|
11
|
+
constructor(client, opts = {}) {
|
|
12
|
+
this.client = client;
|
|
13
|
+
this.opts = opts;
|
|
14
|
+
this.validator = opts.validator ?? createBuiltinValidator();
|
|
15
|
+
}
|
|
16
|
+
async list(opts) {
|
|
17
|
+
const blocked = gate(this.opts.capabilities, "tools", "tools/list");
|
|
18
|
+
if (blocked)
|
|
19
|
+
return blocked;
|
|
20
|
+
const { cursor, ...ro } = opts ?? {};
|
|
21
|
+
const o = await this.client.request("tools/list", cursorParams(cursor), { ...ro, idempotent: true });
|
|
22
|
+
return mapOutcome(mapMethodNotFound(o, "tools"), (v, meta) => {
|
|
23
|
+
const page = this.parsePage(v);
|
|
24
|
+
if (!page)
|
|
25
|
+
return { kind: "protocol-violation", detail: { reason: "tools-not-array" }, meta };
|
|
26
|
+
return { kind: "ok", value: page, meta };
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
async listAll(opts) {
|
|
30
|
+
const warnings = [];
|
|
31
|
+
const excluded = [];
|
|
32
|
+
const { maxPages, maxItems, restartOnInvalidCursor, ...ro } = opts ?? {};
|
|
33
|
+
const o = await listAll("tools/list", async (cursor) => {
|
|
34
|
+
const r = await this.list({ ...ro, ...(cursor !== undefined ? { cursor } : {}) });
|
|
35
|
+
return mapOutcome(r, (v, meta) => {
|
|
36
|
+
warnings.push(...v.warnings);
|
|
37
|
+
excluded.push(...v.excluded);
|
|
38
|
+
return { kind: "ok", value: { items: v.tools, ...(v.nextCursor !== undefined ? { nextCursor: v.nextCursor } : {}) }, meta };
|
|
39
|
+
});
|
|
40
|
+
}, {
|
|
41
|
+
...(maxPages !== undefined ? { maxPages } : {}),
|
|
42
|
+
...(maxItems !== undefined ? { maxItems } : {}),
|
|
43
|
+
...(restartOnInvalidCursor !== undefined ? { restartOnInvalidCursor } : {}),
|
|
44
|
+
});
|
|
45
|
+
return mapOutcome(o, (v, meta) => ({ kind: "ok", value: { ...v, warnings, excluded }, meta }));
|
|
46
|
+
}
|
|
47
|
+
parsePage(v) {
|
|
48
|
+
if (!Array.isArray(v["tools"]))
|
|
49
|
+
return undefined;
|
|
50
|
+
const next = nextCursorOf(v);
|
|
51
|
+
if (!next.ok)
|
|
52
|
+
return undefined;
|
|
53
|
+
const tools = [];
|
|
54
|
+
const warnings = [];
|
|
55
|
+
const excluded = [];
|
|
56
|
+
for (const raw of v["tools"]) {
|
|
57
|
+
const name = isObj(raw) && typeof raw["name"] === "string" ? raw["name"] : undefined;
|
|
58
|
+
if (!isObj(raw) || name === undefined) {
|
|
59
|
+
warnings.push("excluded tool without a string name");
|
|
60
|
+
excluded.push({ name, reason: "malformed-tool" });
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const inSchema = raw["inputSchema"];
|
|
64
|
+
if (!isObj(inSchema)) {
|
|
65
|
+
warnings.push(`excluded tool '${name}': inputSchema must be a JSON Schema object`);
|
|
66
|
+
excluded.push({ name, reason: "invalid-inputSchema" });
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const input = this.validator.compile(inSchema);
|
|
70
|
+
if (!input.ok && input.reason === "invalid-schema") {
|
|
71
|
+
warnings.push(`excluded tool '${name}': ${input.message}`);
|
|
72
|
+
excluded.push({ name, reason: "invalid-schema", detail: input.message });
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
const c = { input };
|
|
76
|
+
let outputValidation;
|
|
77
|
+
if (raw["outputSchema"] !== undefined) {
|
|
78
|
+
const out = this.validator.compile(raw["outputSchema"]);
|
|
79
|
+
c.output = out;
|
|
80
|
+
outputValidation = out.ok ? "valid" : out.reason;
|
|
81
|
+
if (!out.ok)
|
|
82
|
+
warnings.push(`tool '${name}' outputSchema: ${out.message}`);
|
|
83
|
+
}
|
|
84
|
+
const nameWarnings = [];
|
|
85
|
+
if (!NAME_RE.test(name))
|
|
86
|
+
nameWarnings.push("name-not-1-128-of-[A-Za-z0-9_.-]");
|
|
87
|
+
// Duplicates are kept and flagged (OPS-TOOLS-4); the first entry's schemas stay in force.
|
|
88
|
+
if (tools.some((t) => t.name === name))
|
|
89
|
+
nameWarnings.push("duplicate-name");
|
|
90
|
+
else
|
|
91
|
+
this.compiled.set(name, c);
|
|
92
|
+
tools.push({
|
|
93
|
+
name,
|
|
94
|
+
tool: raw,
|
|
95
|
+
nameWarnings,
|
|
96
|
+
...(raw["annotations"] !== undefined
|
|
97
|
+
? { annotations: { value: raw["annotations"], trust: this.opts.serverTrusted === true ? "trusted" : "untrusted" } }
|
|
98
|
+
: {}),
|
|
99
|
+
inputValidation: input.ok ? "valid" : input.reason,
|
|
100
|
+
...(outputValidation ? { outputValidation } : {}),
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
tools,
|
|
105
|
+
...(next.cursor !== undefined ? { nextCursor: next.cursor } : {}),
|
|
106
|
+
...("ttlMs" in v ? { ttlMs: v["ttlMs"] } : {}),
|
|
107
|
+
...("cacheScope" in v ? { cacheScope: v["cacheScope"] } : {}),
|
|
108
|
+
warnings,
|
|
109
|
+
excluded,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
async call(name, args, opts) {
|
|
113
|
+
const { validateInput = true, allowUnvalidated = false, strictOutput = false, ...ro } = opts ?? {};
|
|
114
|
+
const blocked = gate(this.opts.capabilities, "tools", "tools/call");
|
|
115
|
+
if (blocked)
|
|
116
|
+
return blocked;
|
|
117
|
+
const comp = this.compiled.get(name);
|
|
118
|
+
if (validateInput && comp?.input) {
|
|
119
|
+
if (!comp.input.ok) {
|
|
120
|
+
if (!allowUnvalidated) {
|
|
121
|
+
return local("invalid-schema", "tools/call", { reason: comp.input.reason, dialect: comp.input.dialect, message: comp.input.message });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
const r = comp.input.validate(args ?? {});
|
|
126
|
+
if (!r.valid)
|
|
127
|
+
return local("invalid-arguments", "tools/call", { errors: r.errors });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const ctx = { name, arguments: args, opts };
|
|
131
|
+
const verdict = await this.opts.hooks?.beforeCall?.(ctx);
|
|
132
|
+
if (verdict === false || (isObj(verdict) && "veto" in verdict)) {
|
|
133
|
+
return local("local-error", "tools/call", { reason: "vetoed", veto: isObj(verdict) ? verdict["veto"] : undefined });
|
|
134
|
+
}
|
|
135
|
+
const params = { name, ...(args !== undefined ? { arguments: args } : {}) };
|
|
136
|
+
// Annotations never make a call idempotent (OPS-TOOLS-6); idempotent is the caller's choice only.
|
|
137
|
+
const o = await this.client.request("tools/call", params, ro);
|
|
138
|
+
const mapped = mapOutcome(mapMethodNotFound(o, "tools"), (v, meta) => {
|
|
139
|
+
const content = normalizeContent(v["content"]);
|
|
140
|
+
let validation = { status: "no-schema" };
|
|
141
|
+
if ("structuredContent" in v && comp?.output) {
|
|
142
|
+
if (!comp.output.ok)
|
|
143
|
+
validation = { status: comp.output.reason };
|
|
144
|
+
else {
|
|
145
|
+
const r = comp.output.validate(v["structuredContent"]);
|
|
146
|
+
validation = r.valid ? { status: "valid" } : { status: "invalid", errors: r.errors };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
else if (comp?.output && !("structuredContent" in v)) {
|
|
150
|
+
validation = { status: "skipped" };
|
|
151
|
+
}
|
|
152
|
+
if (strictOutput && validation.status === "invalid") {
|
|
153
|
+
return { kind: "protocol-violation", detail: { reason: "output-schema-mismatch", errors: validation.errors }, meta };
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
kind: "ok",
|
|
157
|
+
value: {
|
|
158
|
+
raw: v,
|
|
159
|
+
content,
|
|
160
|
+
...("structuredContent" in v ? { structuredContent: v["structuredContent"] } : {}),
|
|
161
|
+
validation,
|
|
162
|
+
},
|
|
163
|
+
meta,
|
|
164
|
+
};
|
|
165
|
+
});
|
|
166
|
+
await this.opts.hooks?.afterCall?.(ctx, mapped);
|
|
167
|
+
return mapped;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/** In-tree RFC 6570 level 1-4 expander (OPS-RES-2, Q27). */
|
|
2
|
+
export type TemplateValue = string | number | boolean | readonly string[] | Readonly<Record<string, string>> | null | undefined;
|
|
3
|
+
export declare function expandUriTemplate(template: string, vars: Readonly<Record<string, TemplateValue>>): string;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/** In-tree RFC 6570 level 1-4 expander (OPS-RES-2, Q27). */
|
|
2
|
+
const OPS = {
|
|
3
|
+
"": { first: "", sep: ",", named: false, ifemp: "", allowReserved: false },
|
|
4
|
+
"+": { first: "", sep: ",", named: false, ifemp: "", allowReserved: true },
|
|
5
|
+
"#": { first: "#", sep: ",", named: false, ifemp: "", allowReserved: true },
|
|
6
|
+
".": { first: ".", sep: ".", named: false, ifemp: "", allowReserved: false },
|
|
7
|
+
"/": { first: "/", sep: "/", named: false, ifemp: "", allowReserved: false },
|
|
8
|
+
";": { first: ";", sep: ";", named: true, ifemp: "", allowReserved: false },
|
|
9
|
+
"?": { first: "?", sep: "&", named: true, ifemp: "=", allowReserved: false },
|
|
10
|
+
"&": { first: "&", sep: "&", named: true, ifemp: "=", allowReserved: false },
|
|
11
|
+
};
|
|
12
|
+
function encode(s, reserved) {
|
|
13
|
+
if (!reserved) {
|
|
14
|
+
return encodeURIComponent(s).replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
15
|
+
}
|
|
16
|
+
// keep reserved characters and existing pct-encoded triplets
|
|
17
|
+
return s
|
|
18
|
+
.split(/(%[0-9A-Fa-f]{2})/)
|
|
19
|
+
.map((part, i) => i % 2 === 1
|
|
20
|
+
? part
|
|
21
|
+
: encodeURI(part).replace(/%5B/gi, "[").replace(/%5D/gi, "]"))
|
|
22
|
+
.join("");
|
|
23
|
+
}
|
|
24
|
+
const VARSPEC = /^((?:[A-Za-z0-9_]|%[0-9A-Fa-f]{2})(?:[A-Za-z0-9_.]|%[0-9A-Fa-f]{2})*)(\*|:[1-9][0-9]{0,3})?$/;
|
|
25
|
+
export function expandUriTemplate(template, vars) {
|
|
26
|
+
let out = "";
|
|
27
|
+
let i = 0;
|
|
28
|
+
while (i < template.length) {
|
|
29
|
+
const open = template.indexOf("{", i);
|
|
30
|
+
const stray = template.indexOf("}", i);
|
|
31
|
+
if (open < 0) {
|
|
32
|
+
if (stray >= 0)
|
|
33
|
+
throw new TypeError("unmatched '}' in URI template");
|
|
34
|
+
out += template.slice(i);
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
if (stray >= 0 && stray < open)
|
|
38
|
+
throw new TypeError("unmatched '}' in URI template");
|
|
39
|
+
out += template.slice(i, open);
|
|
40
|
+
const close = template.indexOf("}", open);
|
|
41
|
+
if (close < 0)
|
|
42
|
+
throw new TypeError("unclosed '{' in URI template");
|
|
43
|
+
let expr = template.slice(open + 1, close);
|
|
44
|
+
i = close + 1;
|
|
45
|
+
let opch = "";
|
|
46
|
+
if (expr !== "" && "+#./;?&".includes(expr[0])) {
|
|
47
|
+
opch = expr[0];
|
|
48
|
+
expr = expr.slice(1);
|
|
49
|
+
}
|
|
50
|
+
else if (expr !== "" && "=,!@|".includes(expr[0])) {
|
|
51
|
+
throw new TypeError(`reserved operator '${expr[0]}' in URI template`);
|
|
52
|
+
}
|
|
53
|
+
const op = OPS[opch];
|
|
54
|
+
const parts = [];
|
|
55
|
+
for (const spec of expr.split(",")) {
|
|
56
|
+
const m = VARSPEC.exec(spec);
|
|
57
|
+
if (!m)
|
|
58
|
+
throw new TypeError(`invalid variable specifier '${spec}'`);
|
|
59
|
+
const name = m[1];
|
|
60
|
+
const mod = m[2];
|
|
61
|
+
const v = vars[name];
|
|
62
|
+
if (v === undefined || v === null)
|
|
63
|
+
continue;
|
|
64
|
+
const enc = (s) => encode(s, op.allowReserved);
|
|
65
|
+
if (Array.isArray(v) || (typeof v === "object" && v !== null)) {
|
|
66
|
+
const isList = Array.isArray(v);
|
|
67
|
+
const entries = isList
|
|
68
|
+
? v.map((x) => ["", x])
|
|
69
|
+
: Object.entries(v);
|
|
70
|
+
if (entries.length === 0)
|
|
71
|
+
continue;
|
|
72
|
+
if (mod === "*") {
|
|
73
|
+
const items = entries.map(([k, x]) => isList ? (op.named ? `${name}=${enc(x)}` : enc(x)) : `${enc(k)}=${enc(x)}`);
|
|
74
|
+
if (isList && op.named) {
|
|
75
|
+
parts.push(...entries.map(([, x]) => (x === "" ? `${name}${op.ifemp}` : `${name}=${enc(x)}`)));
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
parts.push(...items);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
const flat = entries.flatMap(([k, x]) => (isList ? [enc(x)] : [enc(k), enc(x)])).join(",");
|
|
83
|
+
parts.push(op.named ? `${name}=${flat}` : flat);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
let s = String(v);
|
|
88
|
+
if (mod?.startsWith(":"))
|
|
89
|
+
s = [...s].slice(0, Number(mod.slice(1))).join("");
|
|
90
|
+
const e = enc(s);
|
|
91
|
+
if (op.named)
|
|
92
|
+
parts.push(s === "" ? `${name}${op.ifemp}` : `${name}=${e}`);
|
|
93
|
+
else
|
|
94
|
+
parts.push(e);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (parts.length > 0)
|
|
98
|
+
out += op.first + parts.join(op.sep);
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type Obj } from "../server-events/index.js";
|
|
2
|
+
/** Multi Round-Trip Request interim result. At least one field is required. */
|
|
3
|
+
export declare function inputRequired(input: {
|
|
4
|
+
readonly inputRequests?: Record<string, {
|
|
5
|
+
readonly method: string;
|
|
6
|
+
readonly params: Obj;
|
|
7
|
+
}>;
|
|
8
|
+
readonly requestState?: string;
|
|
9
|
+
}): Obj;
|
|
10
|
+
/** Opaque, integrity-protected requestState (HMAC-SHA256 over method + payload). */
|
|
11
|
+
export declare function issueRequestState(secret: string, method: string, payload?: Obj): string;
|
|
12
|
+
/** Verify and decode requestState; tampered or wrong-method state is rejected. */
|
|
13
|
+
export declare function verifyRequestState(secret: string, state: unknown, method: string): Obj;
|
|
14
|
+
/** Validate a retry's inputResponses shape; unknown keys are ignored by design. */
|
|
15
|
+
export declare function readInputResponses(params: Obj): Obj | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Server-side MRTR correlation: issue an interim result, then validate the
|
|
18
|
+
* retry's requestState and inputResponses. States are single-use to prevent
|
|
19
|
+
* replay of one-time inputs.
|
|
20
|
+
*/
|
|
21
|
+
export declare class MrtrRegistry {
|
|
22
|
+
#private;
|
|
23
|
+
private readonly secret;
|
|
24
|
+
constructor(secret: string);
|
|
25
|
+
begin(method: string, input: {
|
|
26
|
+
readonly inputRequests?: Record<string, {
|
|
27
|
+
readonly method: string;
|
|
28
|
+
readonly params: Obj;
|
|
29
|
+
}>;
|
|
30
|
+
readonly payload?: Obj;
|
|
31
|
+
}): Obj;
|
|
32
|
+
/** Validate a retry and consume its state. Missing inputs are re-requested. */
|
|
33
|
+
resume(method: string, params: Obj): {
|
|
34
|
+
readonly inputResponses?: Obj;
|
|
35
|
+
readonly payload: Obj;
|
|
36
|
+
readonly retry?: Obj;
|
|
37
|
+
};
|
|
38
|
+
}
|