@casys/mcp-server 0.26.1 → 0.27.1

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.
Files changed (62) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/LICENSE +21 -0
  3. package/README.md +10 -627
  4. package/esm/mod.d.ts +1 -0
  5. package/esm/mod.js +1 -0
  6. package/package.json +28 -25
  7. package/mod.ts +0 -302
  8. package/src/auth/as-metadata-proxy.ts +0 -288
  9. package/src/auth/config.ts +0 -368
  10. package/src/auth/jwt-provider.ts +0 -327
  11. package/src/auth/middleware.ts +0 -257
  12. package/src/auth/mod.ts +0 -89
  13. package/src/auth/multi-tenant-middleware.ts +0 -236
  14. package/src/auth/presets.ts +0 -351
  15. package/src/auth/provider.ts +0 -49
  16. package/src/auth/scope-middleware.ts +0 -59
  17. package/src/auth/static-token-provider.ts +0 -197
  18. package/src/auth/types.ts +0 -207
  19. package/src/client-auth/callback-server.ts +0 -166
  20. package/src/client-auth/client-id-metadata.ts +0 -260
  21. package/src/client-auth/connect.ts +0 -72
  22. package/src/client-auth/mod.ts +0 -44
  23. package/src/client-auth/provider.ts +0 -300
  24. package/src/client-auth/token-store/file-store.ts +0 -73
  25. package/src/client-auth/token-store/memory-store.ts +0 -32
  26. package/src/client-auth/types.ts +0 -95
  27. package/src/concurrency/rate-limiter.ts +0 -190
  28. package/src/concurrency/request-queue.ts +0 -140
  29. package/src/http/body.ts +0 -72
  30. package/src/http/request-guards.ts +0 -137
  31. package/src/http/wire.ts +0 -41
  32. package/src/inspector/launcher.ts +0 -131
  33. package/src/mcp-app.ts +0 -4595
  34. package/src/middleware/backpressure.ts +0 -36
  35. package/src/middleware/mod.ts +0 -21
  36. package/src/middleware/rate-limit.ts +0 -45
  37. package/src/middleware/runner.ts +0 -63
  38. package/src/middleware/types.ts +0 -104
  39. package/src/middleware/validation.ts +0 -28
  40. package/src/mrtr/capability-check.ts +0 -303
  41. package/src/mrtr/mod.ts +0 -54
  42. package/src/mrtr/replay-store.ts +0 -103
  43. package/src/mrtr/request-state.ts +0 -435
  44. package/src/mrtr/retry-guard.ts +0 -110
  45. package/src/mrtr/types.ts +0 -107
  46. package/src/observability/metrics.ts +0 -336
  47. package/src/observability/mod.ts +0 -21
  48. package/src/observability/otel.ts +0 -330
  49. package/src/runtime/runtime.deno.ts +0 -149
  50. package/src/runtime/runtime.node.ts +0 -324
  51. package/src/runtime/runtime.ts +0 -50
  52. package/src/runtime/types.ts +0 -130
  53. package/src/security/channel-hmac.ts +0 -140
  54. package/src/security/csp.ts +0 -89
  55. package/src/security/message-signer.ts +0 -223
  56. package/src/subscriptions/subscription-registry.ts +0 -558
  57. package/src/tasks/mod.ts +0 -33
  58. package/src/tasks/task-store.ts +0 -1014
  59. package/src/transport/request-headers.ts +0 -661
  60. package/src/types.ts +0 -908
  61. package/src/ui/viewer-utils.ts +0 -117
  62. package/src/validation/schema-validator.ts +0 -307
@@ -1,117 +0,0 @@
1
- /**
2
- * MCP Apps Viewer Utilities
3
- *
4
- * Shared functions for resolving viewer dist paths and discovering
5
- * viewer directories. Used by registerViewers() and the build pipeline.
6
- *
7
- * @module lib/server/src/ui/viewer-utils
8
- */
9
-
10
- /** Directories to skip during auto-discovery */
11
- const SKIP_DIRS = new Set([
12
- "shared",
13
- "dist",
14
- "node_modules",
15
- ".cache",
16
- ".vite",
17
- ]);
18
-
19
- /**
20
- * Convert a file:// URL to a filesystem path.
21
- * Handles Windows drive letters and UNC paths.
22
- */
23
- function fileUrlToPath(url: URL): string {
24
- const decoded = decodeURIComponent(url.pathname);
25
- // Windows: /C:/path → C:/path
26
- if (/^\/[A-Za-z]:\//.test(decoded)) return decoded.slice(1);
27
- // UNC: //host/share
28
- if (url.host.length > 0) return `//${url.host}${decoded}`;
29
- return decoded;
30
- }
31
-
32
- /**
33
- * Resolve the dist path for a viewer's built index.html.
34
- *
35
- * Checks two candidate locations relative to the module URL:
36
- * 1. ./src/ui/dist/{viewerName}/index.html (Deno dev)
37
- * 2. ./ui-dist/{viewerName}/index.html (npm package)
38
- *
39
- * @param moduleUrl - import.meta.url of the consumer's server.ts
40
- * @param viewerName - Viewer directory name (e.g. "invoice-viewer")
41
- * @param exists - Function to check if a path exists (injectable for tests)
42
- * @returns Absolute path to index.html, or null if not found
43
- */
44
- export function resolveViewerDistPath(
45
- moduleUrl: string,
46
- viewerName: string,
47
- exists: (path: string) => boolean,
48
- ): string | null {
49
- // When consumed from JSR or any remote registry, import.meta.url is an
50
- // HTTPS URL. fileUrlToPath() only handles file:// — passing an HTTPS URL
51
- // produces a broken UNC-like path ("//jsr.io/..."). Return the raw resolved
52
- // URL string instead; the caller's exists/readFile callbacks are responsible
53
- // for handling remote URLs (e.g., via fetch()).
54
- if (moduleUrl.startsWith("https://") || moduleUrl.startsWith("http://")) {
55
- const candidates = [
56
- new URL(`./src/ui/dist/${viewerName}/index.html`, moduleUrl).href,
57
- new URL(`./ui-dist/${viewerName}/index.html`, moduleUrl).href,
58
- ];
59
- for (const candidate of candidates) {
60
- if (exists(candidate)) return candidate;
61
- }
62
- return null;
63
- }
64
-
65
- const candidates = [
66
- fileUrlToPath(new URL(`./src/ui/dist/${viewerName}/index.html`, moduleUrl)),
67
- fileUrlToPath(new URL(`./ui-dist/${viewerName}/index.html`, moduleUrl)),
68
- ];
69
-
70
- for (const candidate of candidates) {
71
- if (exists(candidate)) return candidate;
72
- }
73
-
74
- return null;
75
- }
76
-
77
- /** Minimal entry returned by a directory reader */
78
- export interface DirEntry {
79
- name: string;
80
- isDirectory: boolean;
81
- }
82
-
83
- /** Injectable filesystem operations for discoverViewers */
84
- export interface DiscoverViewersFS {
85
- readDir: (path: string) => DirEntry[];
86
- hasIndexHtml: (uiDir: string, viewerName: string) => boolean;
87
- }
88
-
89
- /**
90
- * Auto-discover viewer directories inside a UI root folder.
91
- *
92
- * A viewer is a directory that:
93
- * - Is not in the skip list (shared, dist, node_modules, .cache, .vite)
94
- * - Does not start with "."
95
- * - Contains an index.html file
96
- *
97
- * @param uiDir - Absolute path to the UI root directory
98
- * @param fs - Injectable filesystem operations
99
- * @returns Sorted array of viewer names
100
- */
101
- export function discoverViewers(
102
- uiDir: string,
103
- fs: DiscoverViewersFS,
104
- ): string[] {
105
- const entries = fs.readDir(uiDir);
106
- const viewers: string[] = [];
107
-
108
- for (const entry of entries) {
109
- if (!entry.isDirectory) continue;
110
- if (entry.name.startsWith(".")) continue;
111
- if (SKIP_DIRS.has(entry.name)) continue;
112
- if (!fs.hasIndexHtml(uiDir, entry.name)) continue;
113
- viewers.push(entry.name);
114
- }
115
-
116
- return viewers.sort();
117
- }
@@ -1,307 +0,0 @@
1
- /**
2
- * Schema Validator
3
- *
4
- * JSON Schema validation using ajv for MCP tool arguments.
5
- * Compiles schemas once for optimal performance.
6
- *
7
- * @module lib/server/schema-validator
8
- */
9
-
10
- // deno-lint-ignore-file no-explicit-any
11
- // Draft 2020-12, not ajv's default export (draft-07). Spec 2026-07-28
12
- // (SEP-2106) allows any JSON Schema 2020-12 keyword in `inputSchema` /
13
- // `outputSchema`, and the draft-07 build treats the keywords added in 2020-12 —
14
- // `prefixItems`, `unevaluatedProperties`, `unevaluatedItems`, `$dynamicRef` —
15
- // as unknown. Combined with `strict: false` that failure is silent: the schema
16
- // compiles, the constraint is never applied, and a violating payload validates
17
- // clean. A tool author's declared boundary has to actually hold.
18
- //
19
- // Spelled as the full `dist/` path, not `ajv/2020`: `build-node.sh` copies
20
- // import specifiers verbatim into the npm distribution, and ajv 8 ships no
21
- // `exports` map and no root-level `2020.js`, so `ajv/2020` resolves under
22
- // Deno's import map and then fails at runtime in Node with ERR_MODULE_NOT_FOUND.
23
- // This path resolves under both.
24
- import AjvDefault from "ajv/dist/2020.js";
25
-
26
- // Get the Ajv constructor (handles ESM/CJS differences)
27
- const Ajv = (AjvDefault as any).default ?? AjvDefault;
28
-
29
- // Type definitions for ajv
30
- interface AjvErrorObject {
31
- keyword: string;
32
- instancePath: string;
33
- schemaPath: string;
34
- params: Record<string, any>;
35
- message?: string;
36
- data?: unknown;
37
- }
38
-
39
- interface AjvValidateFunction {
40
- (data: unknown): boolean;
41
- errors?: AjvErrorObject[] | null;
42
- }
43
-
44
- /**
45
- * Count with its noun, agreeing in number.
46
- *
47
- * A bound of 1 is a legitimate schema (`minItems: 1`), and "must have at least
48
- * 1 items" reads as a bug in the server rather than a problem with the input.
49
- */
50
- function plural(n: number, singular: string, plural = `${singular}s`): string {
51
- return `${n} ${n === 1 ? singular : plural}`;
52
- }
53
-
54
- /**
55
- * Validation error with formatted message
56
- */
57
- export interface ValidationError {
58
- /** Error message */
59
- message: string;
60
- /** Path to invalid property */
61
- path: string;
62
- /** Invalid value */
63
- value?: unknown;
64
- /** Expected type or constraint */
65
- expected?: string;
66
- }
67
-
68
- /**
69
- * Validation result
70
- */
71
- export interface ValidationResult {
72
- valid: boolean;
73
- errors: ValidationError[];
74
- }
75
-
76
- /**
77
- * Schema validator with compiled schema caching
78
- *
79
- * @example
80
- * ```typescript
81
- * const validator = new SchemaValidator();
82
- *
83
- * // Register tool schema
84
- * validator.addSchema("my_tool", {
85
- * type: "object",
86
- * properties: { count: { type: "number" } },
87
- * required: ["count"]
88
- * });
89
- *
90
- * // Validate arguments
91
- * const result = validator.validate("my_tool", { count: 5 });
92
- * if (!result.valid) {
93
- * console.error(result.errors);
94
- * }
95
- * ```
96
- */
97
- export class SchemaValidator {
98
- private ajv: any;
99
- private validators = new Map<string, AjvValidateFunction>();
100
-
101
- constructor() {
102
- this.ajv = new Ajv({
103
- allErrors: true, // Report all errors, not just first
104
- strict: false, // Allow additional keywords
105
- useDefaults: true, // Apply default values
106
- coerceTypes: false, // Don't coerce types (strict validation)
107
- });
108
- }
109
-
110
- /**
111
- * Add a schema for a tool
112
- *
113
- * @param toolName - Name of the tool
114
- * @param schema - JSON Schema for tool arguments
115
- */
116
- addSchema(toolName: string, schema: Record<string, unknown>): void {
117
- // Compile and cache the validator
118
- const validate = this.ajv.compile(schema);
119
- this.validators.set(toolName, validate);
120
- }
121
-
122
- /**
123
- * Remove a schema
124
- */
125
- removeSchema(toolName: string): void {
126
- this.validators.delete(toolName);
127
- }
128
-
129
- /**
130
- * Check if a schema exists
131
- */
132
- hasSchema(toolName: string): boolean {
133
- return this.validators.has(toolName);
134
- }
135
-
136
- /**
137
- * Validate arguments against a tool's schema
138
- *
139
- * @param toolName - Name of the tool
140
- * @param args - Arguments to validate
141
- * @returns Validation result with errors if invalid
142
- */
143
- validate(toolName: string, args: Record<string, unknown>): ValidationResult {
144
- const validate = this.validators.get(toolName);
145
-
146
- if (!validate) {
147
- // No schema registered - pass through
148
- return { valid: true, errors: [] };
149
- }
150
-
151
- const valid = validate(args);
152
-
153
- if (valid) {
154
- return { valid: true, errors: [] };
155
- }
156
-
157
- // Format errors
158
- const errors = this.formatErrors(validate.errors || []);
159
- return { valid: false, errors };
160
- }
161
-
162
- /**
163
- * Validate and throw if invalid
164
- *
165
- * @throws Error with formatted validation message
166
- */
167
- validateOrThrow(toolName: string, args: Record<string, unknown>): void {
168
- const result = this.validate(toolName, args);
169
-
170
- if (!result.valid) {
171
- const messages = result.errors.map((e) => e.message).join("; ");
172
- throw new Error(`Invalid arguments for ${toolName}: ${messages}`);
173
- }
174
- }
175
-
176
- /**
177
- * Format ajv errors into readable messages
178
- */
179
- private formatErrors(errors: AjvErrorObject[]): ValidationError[] {
180
- return errors.map((error) => {
181
- const path = error.instancePath || "/";
182
- const param = error.params;
183
-
184
- let message: string;
185
- let expected: string | undefined;
186
-
187
- switch (error.keyword) {
188
- case "required":
189
- message = `Missing required property: ${param.missingProperty}`;
190
- break;
191
-
192
- case "type":
193
- message = `Property ${path} must be ${param.type}`;
194
- expected = param.type;
195
- break;
196
-
197
- case "enum":
198
- message = `Property ${path} must be one of: ${
199
- param.allowedValues?.join(", ")
200
- }`;
201
- expected = param.allowedValues?.join(" | ");
202
- break;
203
-
204
- case "minimum":
205
- message = `Property ${path} must be >= ${param.limit}`;
206
- expected = `>= ${param.limit}`;
207
- break;
208
-
209
- case "maximum":
210
- message = `Property ${path} must be <= ${param.limit}`;
211
- expected = `<= ${param.limit}`;
212
- break;
213
-
214
- case "minLength":
215
- message =
216
- `Property ${path} must have at least ${param.limit} characters`;
217
- expected = `length >= ${param.limit}`;
218
- break;
219
-
220
- case "maxLength":
221
- message =
222
- `Property ${path} must have at most ${param.limit} characters`;
223
- expected = `length <= ${param.limit}`;
224
- break;
225
-
226
- case "pattern":
227
- message = `Property ${path} must match pattern: ${param.pattern}`;
228
- expected = param.pattern;
229
- break;
230
-
231
- // Collection bounds. ajv enforces these already — what was missing is a
232
- // message shaped like the others, with a structured `expected`. Without
233
- // it these fell through to `default` and surfaced ajv's bare prose
234
- // ("must NOT have more than 8 items"), which states the limit but names
235
- // neither the property nor an `expected` a caller can act on.
236
- //
237
- // `expected` says "items"/"properties" rather than "length": it is read
238
- // on its own as a recovery hint, where "length <= 8" is indistinguishable
239
- // from the character bound that minLength/maxLength above produce.
240
- case "minItems":
241
- message = `Property ${path} must have at least ${
242
- plural(param.limit, "item")
243
- }`;
244
- expected = `items >= ${param.limit}`;
245
- break;
246
-
247
- case "maxItems":
248
- message = `Property ${path} must have at most ${
249
- plural(param.limit, "item")
250
- }`;
251
- expected = `items <= ${param.limit}`;
252
- break;
253
-
254
- case "uniqueItems":
255
- // ajv reports the two colliding array indices, zero-based, and in
256
- // either order depending on how it optimised the comparison. The
257
- // indices are not what is equal — the items at them are.
258
- message =
259
- `Property ${path} must not contain duplicate items (items at indices ${param.j} and ${param.i} are equal)`;
260
- expected = "unique items";
261
- break;
262
-
263
- case "minProperties":
264
- message = `Property ${path} must have at least ${
265
- plural(param.limit, "property", "properties")
266
- }`;
267
- expected = `properties >= ${param.limit}`;
268
- break;
269
-
270
- case "maxProperties":
271
- message = `Property ${path} must have at most ${
272
- plural(param.limit, "property", "properties")
273
- }`;
274
- expected = `properties <= ${param.limit}`;
275
- break;
276
-
277
- case "additionalProperties":
278
- message = `Unknown property: ${param.additionalProperty}`;
279
- break;
280
-
281
- default:
282
- message = error.message || `Validation failed at ${path}`;
283
- }
284
-
285
- return {
286
- message,
287
- path,
288
- value: error.data,
289
- expected,
290
- };
291
- });
292
- }
293
-
294
- /**
295
- * Get number of registered schemas
296
- */
297
- get count(): number {
298
- return this.validators.size;
299
- }
300
-
301
- /**
302
- * Clear all schemas
303
- */
304
- clear(): void {
305
- this.validators.clear();
306
- }
307
- }