@ant0/mcp 0.0.0-stage → 0.3.22-beta.8

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 (54) hide show
  1. package/LICENSE +28 -0
  2. package/README.md +95 -2
  3. package/THIRD_PARTY_NOTICES +311 -0
  4. package/dist/api/generated/schema.d.ts +7340 -0
  5. package/dist/api/generated/schema.js +6 -0
  6. package/dist/api/index.d.ts +171 -0
  7. package/dist/api/index.js +332 -0
  8. package/dist/bin.d.ts +3 -0
  9. package/dist/bin.js +59 -0
  10. package/dist/call-permits.d.ts +10 -0
  11. package/dist/call-permits.js +67 -0
  12. package/dist/connection.d.ts +20 -0
  13. package/dist/connection.js +157 -0
  14. package/dist/credential-file.d.ts +33 -0
  15. package/dist/credential-file.js +773 -0
  16. package/dist/daemon.d.ts +7 -0
  17. package/dist/daemon.js +306 -0
  18. package/dist/errors.d.ts +92 -0
  19. package/dist/errors.js +101 -0
  20. package/dist/export-files.d.ts +37 -0
  21. package/dist/export-files.js +167 -0
  22. package/dist/grant.d.ts +5 -0
  23. package/dist/grant.js +11 -0
  24. package/dist/http-local.d.ts +38 -0
  25. package/dist/http-local.js +324 -0
  26. package/dist/http.d.ts +66 -0
  27. package/dist/http.js +556 -0
  28. package/dist/index.d.ts +21 -0
  29. package/dist/index.js +86 -0
  30. package/dist/key-contract.d.ts +325 -0
  31. package/dist/key-contract.js +241 -0
  32. package/dist/limits.d.ts +22 -0
  33. package/dist/limits.js +22 -0
  34. package/dist/local-routes.d.ts +30 -0
  35. package/dist/local-routes.js +231 -0
  36. package/dist/output-descriptor.d.ts +3 -0
  37. package/dist/output-descriptor.js +19 -0
  38. package/dist/output-worker-body.d.ts +3 -0
  39. package/dist/output-worker-body.js +77 -0
  40. package/dist/output-worker.d.ts +2 -0
  41. package/dist/output-worker.js +4 -0
  42. package/dist/output-writer.d.ts +39 -0
  43. package/dist/output-writer.js +199 -0
  44. package/dist/peer-watchdog.d.ts +4 -0
  45. package/dist/peer-watchdog.js +55 -0
  46. package/dist/server.d.ts +28 -0
  47. package/dist/server.js +33 -0
  48. package/dist/text-budget.d.ts +13 -0
  49. package/dist/text-budget.js +42 -0
  50. package/dist/tools.d.ts +39 -0
  51. package/dist/tools.js +1893 -0
  52. package/dist/transport.d.ts +45 -0
  53. package/dist/transport.js +313 -0
  54. package/package.json +66 -4
@@ -0,0 +1,157 @@
1
+ import { SHARED_TOKEN_FILE_ENV, TokenFileNotPrivateError, privateTokenProblem, resolveHome, } from "./api/index.js";
2
+ import { closeSync, constants, fstatSync, lstatSync, openSync, readSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ import { McpFailure } from "./errors.js";
5
+ import { LIMITS } from "./limits.js";
6
+ import { readCredentialFile, assertCredentialUnchanged, } from "./credential-file.js";
7
+ import { API_KEY_ENV, apiKeyId } from "./key-contract.js";
8
+ export function keyMode(connection) {
9
+ return connection.credential !== undefined || connection.apiKey === true;
10
+ }
11
+ function portNumber(value) {
12
+ if (typeof value === "string" && /^[1-9]\d{0,4}$/.test(value))
13
+ value = Number(value);
14
+ if (typeof value !== "number" || !Number.isInteger(value) || value < 1 || value > 65535) {
15
+ throw new McpFailure("configuration");
16
+ }
17
+ return value;
18
+ }
19
+ /** Exact syntax first: URL parsing alone accepts short/octal/hex IPv4 and strips URL parts. */
20
+ export function canonicalOrigin(value) {
21
+ const match = /^http:\/\/(localhost|127\.\d{1,3}\.\d{1,3}\.\d{1,3}|\[::1\]):([1-9]\d{0,4})\/?$/.exec(value);
22
+ if (!match)
23
+ throw new McpFailure("configuration");
24
+ let host = match[1];
25
+ const port = portNumber(match[2]);
26
+ if (host.startsWith("127.")) {
27
+ if (host.split(".").some((part) => String(Number(part)) !== part || Number(part) > 255)) {
28
+ throw new McpFailure("configuration");
29
+ }
30
+ }
31
+ // Avoid DNS (including an altered hosts file) even for the accepted localhost spelling.
32
+ if (host === "localhost")
33
+ host = "127.0.0.1";
34
+ return new URL(`http://${host}:${port}`).origin;
35
+ }
36
+ /** Fixed discovery filenames only, bounded regular files; never follow a file symlink/FIFO. */
37
+ function localFile(file, mustBePrivate = false, env = process.env) {
38
+ let fd;
39
+ try {
40
+ let stat;
41
+ try {
42
+ stat = lstatSync(file);
43
+ }
44
+ catch (error) {
45
+ if (error.code === "ENOENT")
46
+ return undefined;
47
+ throw error;
48
+ }
49
+ // The token file only: owned by this user, no group or other access (not checked on Windows,
50
+ // as in api-client). The ino/dev comparison below ties the opened file to this lstat.
51
+ const problem = mustBePrivate ? privateTokenProblem(stat) : undefined;
52
+ if (problem !== undefined) {
53
+ // The path and the fix go to stderr (never the protocol stream); the token is never read here.
54
+ const optedOut = env[SHARED_TOKEN_FILE_ENV] === "1";
55
+ const refusal = new TokenFileNotPrivateError(file, problem, { optOutHint: !optedOut });
56
+ const text = refusal.message;
57
+ if (optedOut && refusal.waivable) {
58
+ process.stderr.write(`ant0-mcp: warning: ${text} (continuing: ${SHARED_TOKEN_FILE_ENV}=1)\n`);
59
+ }
60
+ else {
61
+ process.stderr.write(`ant0-mcp: ${text}\n`);
62
+ throw new McpFailure("token_file");
63
+ }
64
+ }
65
+ if (!stat.isFile() || stat.size > LIMITS.discoveryBytes)
66
+ throw new McpFailure("configuration");
67
+ fd = openSync(file, constants.O_RDONLY | constants.O_NONBLOCK | (constants.O_NOFOLLOW ?? 0));
68
+ const opened = fstatSync(fd);
69
+ if (!opened.isFile() || opened.ino !== stat.ino || opened.dev !== stat.dev) {
70
+ throw new McpFailure("configuration");
71
+ }
72
+ const buffer = Buffer.alloc(LIMITS.discoveryBytes + 1);
73
+ let size = 0;
74
+ while (size < buffer.length) {
75
+ const count = readSync(fd, buffer, size, buffer.length - size, null);
76
+ if (count === 0)
77
+ break;
78
+ size += count;
79
+ }
80
+ if (size > LIMITS.discoveryBytes)
81
+ throw new McpFailure("configuration");
82
+ return new TextDecoder("utf-8", { fatal: true }).decode(buffer.subarray(0, size));
83
+ }
84
+ catch (error) {
85
+ if (error instanceof McpFailure && error.code === "token_file")
86
+ throw error;
87
+ throw new McpFailure("configuration");
88
+ }
89
+ finally {
90
+ if (fd !== undefined)
91
+ closeSync(fd);
92
+ }
93
+ }
94
+ /** Same home and flag > pid > env > default precedence as the CLI, with fail-closed validation. */
95
+ export function discoverConnection(options) {
96
+ try {
97
+ const env = options.env ?? process.env;
98
+ const home = resolveHome({
99
+ ...(options.home !== undefined ? { home: options.home } : {}),
100
+ env,
101
+ });
102
+ const raw = localFile(join(home, "ant0d.pid"));
103
+ let pid = {};
104
+ if (raw !== undefined) {
105
+ const parsed = JSON.parse(raw);
106
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
107
+ throw new McpFailure("configuration");
108
+ const p = parsed;
109
+ if (!Number.isSafeInteger(p.pid) || p.pid < 1)
110
+ throw new McpFailure("configuration");
111
+ if (typeof p.port !== "number")
112
+ throw new McpFailure("configuration");
113
+ const port = portNumber(p.port);
114
+ const host = p.host ?? "127.0.0.1";
115
+ if (typeof host !== "string")
116
+ throw new McpFailure("configuration");
117
+ canonicalOrigin(`http://${host === "::1" ? "[::1]" : host}:${port}`);
118
+ pid = { host, port };
119
+ }
120
+ const host = pid.host ?? env.ANT0D_HOST ?? "127.0.0.1";
121
+ const port = portNumber(options.port ?? pid.port ?? env.ANT0D_PORT ?? 27100);
122
+ const origin = canonicalOrigin(`http://${host === "::1" ? "[::1]" : host}:${port}`);
123
+ const envKey = env[API_KEY_ENV];
124
+ if ([options.credentialFile, options.apiKey, envKey].filter((value) => value !== undefined)
125
+ .length > 1)
126
+ throw new McpFailure("configuration");
127
+ const key = options.apiKey ?? envKey;
128
+ if (key !== undefined) {
129
+ if (apiKeyId(key) === undefined)
130
+ throw new McpFailure("configuration");
131
+ return { origin, token: key, apiKey: true };
132
+ }
133
+ // Validate destination BEFORE opening the only credential file this adapter ever reads.
134
+ if (options.credentialFile !== undefined) {
135
+ const credential = readCredentialFile(options.credentialFile);
136
+ if (credential.value.phase !== "ready")
137
+ throw new McpFailure("configuration");
138
+ assertCredentialUnchanged(credential);
139
+ return { origin, token: credential.value.create.credential, credential };
140
+ }
141
+ const token = localFile(join(home, "ant0d.token"), true, env)?.trim();
142
+ if (!token)
143
+ throw new McpFailure("unavailable");
144
+ if (!/^[A-Za-z0-9._~+\/-]+=*$/.test(token))
145
+ throw new McpFailure("configuration");
146
+ // An API key in the local token file would silently turn the local bearer into a key.
147
+ if (token.startsWith("ant0_sk_"))
148
+ throw new McpFailure("configuration");
149
+ return { origin, token };
150
+ }
151
+ catch (error) {
152
+ if (error instanceof McpFailure)
153
+ throw error;
154
+ throw new McpFailure("configuration");
155
+ }
156
+ }
157
+ //# sourceMappingURL=connection.js.map
@@ -0,0 +1,33 @@
1
+ import { type BigIntStats as Stats } from "node:fs";
2
+ import { CredentialFailure, type CredentialFile } from "./key-contract.js";
3
+ export * from "./key-contract.js";
4
+ interface PublishedSnapshot {
5
+ path: string;
6
+ stat: Stats;
7
+ parent: Stats;
8
+ text: string;
9
+ bytes: Buffer;
10
+ }
11
+ export interface CredentialSnapshot extends PublishedSnapshot {
12
+ /** Original bytes and decoded document never change when a receipt supplies a ready view. */
13
+ rawValue: CredentialFile;
14
+ value: CredentialFile;
15
+ receipt?: PublishedSnapshot;
16
+ }
17
+ export declare function readCredentialFile(file: string): CredentialSnapshot;
18
+ export declare function assertCredentialUnchanged(snapshot: CredentialSnapshot): void;
19
+ /** Re-establish durability even after a failed fsync or killed writer. Pin the file through
20
+ * dispatch (also its parent on POSIX); syncing the lock is not credential durability. */
21
+ export declare function flushCredentialBeforeSubmission(snapshot: CredentialSnapshot): {
22
+ close(): void;
23
+ };
24
+ /** ACL checks must not block MCP's ping/EOF/cancellation loop. Keep admission until the child
25
+ * has closed, including on abort; no secret is passed in argv, environment or child output. */
26
+ export declare function assertCredentialUnchangedAsync(snapshot: CredentialSnapshot, signal: AbortSignal): Promise<void>;
27
+ export declare class CredentialCollision extends CredentialFailure {
28
+ }
29
+ export declare function saveCredentialFile(file: string, value: CredentialFile, previous?: CredentialSnapshot): CredentialSnapshot;
30
+ export declare function newCredential(id: string): string;
31
+ /** Optional in-process convenience only. Correctness rests on create-only publication and replay. */
32
+ export declare function withCredentialLock<T>(file: string, operation: () => Promise<T>): Promise<T>;
33
+ //# sourceMappingURL=credential-file.d.ts.map