@abraca/mcp 2.88.0 → 2.91.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -12
- package/src/active-server.ts +0 -91
- package/src/converters/markdownToYjs.ts +0 -13
- package/src/converters/yjsToMarkdown.ts +0 -12
- package/src/hook-bridge.ts +0 -355
- package/src/index.ts +0 -305
- package/src/mentions.ts +0 -42
- package/src/resources/agent-guide.ts +0 -557
- package/src/resources/server-info.ts +0 -47
- package/src/resources/tree-resource.ts +0 -57
- package/src/schema/loader.ts +0 -139
- package/src/schema/validator.ts +0 -31
- package/src/server-manager.ts +0 -182
- package/src/server.ts +0 -1393
- package/src/tool-detail.ts +0 -147
- package/src/tools/awareness.ts +0 -159
- package/src/tools/channel.ts +0 -155
- package/src/tools/content.ts +0 -518
- package/src/tools/files.ts +0 -118
- package/src/tools/hooks.ts +0 -51
- package/src/tools/meta.ts +0 -98
- package/src/tools/servers.ts +0 -211
- package/src/tools/svg.ts +0 -80
- package/src/tools/tree.ts +0 -682
- package/src/utils/sanitizeSvg.ts +0 -71
- package/src/utils.ts +0 -6
package/src/schema/loader.ts
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Schema bundle loader for the MCP runtime hook.
|
|
3
|
-
*
|
|
4
|
-
* Reads a JSON Schema bundle produced by `@abraca/schema` codegen, compiles
|
|
5
|
-
* one Ajv validator per declared doc-type, and returns a
|
|
6
|
-
* {@link SchemaBundleValidator} that the MCP tool handlers consult before
|
|
7
|
-
* forwarding `meta` writes to the server.
|
|
8
|
-
*
|
|
9
|
-
* Bundle format (see abracadabra-ts/packages/schema/scripts/codegen-json-schema.ts):
|
|
10
|
-
*
|
|
11
|
-
* ```jsonc
|
|
12
|
-
* { "schemaVersion": 1,
|
|
13
|
-
* "label": "...",
|
|
14
|
-
* "types": {
|
|
15
|
-
* "<doc-type>": {
|
|
16
|
-
* "version": 1,
|
|
17
|
-
* "meta": { ...JSON Schema 2020-12... },
|
|
18
|
-
* "body": "ytext" | "yarray" | "ymap" | "none",
|
|
19
|
-
* "children": ["..."]
|
|
20
|
-
* } } }
|
|
21
|
-
* ```
|
|
22
|
-
*
|
|
23
|
-
* Loader errors at boot are *fatal* — caller is expected to abort startup
|
|
24
|
-
* with a clear message. We do not silently fall back to "no validation".
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
import { readFileSync } from 'node:fs'
|
|
28
|
-
import Ajv2020 from 'ajv/dist/2020.js'
|
|
29
|
-
import addFormats from 'ajv-formats'
|
|
30
|
-
import type { ValidateFunction } from 'ajv/dist/2020.js'
|
|
31
|
-
import type {
|
|
32
|
-
MetaValidationIssue,
|
|
33
|
-
MetaValidationResult,
|
|
34
|
-
SchemaBundleValidator,
|
|
35
|
-
} from './validator.ts'
|
|
36
|
-
|
|
37
|
-
interface BundleDoc {
|
|
38
|
-
version: number
|
|
39
|
-
meta: Record<string, unknown>
|
|
40
|
-
body: 'ytext' | 'yarray' | 'ymap' | 'none'
|
|
41
|
-
children: ReadonlyArray<string>
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
interface Bundle {
|
|
45
|
-
schemaVersion: number
|
|
46
|
-
label?: string
|
|
47
|
-
types: Record<string, BundleDoc>
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const SUPPORTED_BUNDLE_VERSION = 1
|
|
51
|
-
|
|
52
|
-
export class SchemaBundleLoadError extends Error {
|
|
53
|
-
constructor(message: string, readonly cause?: unknown) {
|
|
54
|
-
super(message)
|
|
55
|
-
this.name = 'SchemaBundleLoadError'
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Read, parse, and compile a JSON Schema bundle into a runtime validator.
|
|
61
|
-
*
|
|
62
|
-
* @throws {SchemaBundleLoadError} if the file is missing, malformed, has
|
|
63
|
-
* an unsupported `schemaVersion`, or any per-type schema fails to compile.
|
|
64
|
-
*/
|
|
65
|
-
export function loadSchemaBundle(path: string): SchemaBundleValidator {
|
|
66
|
-
let raw: string
|
|
67
|
-
try {
|
|
68
|
-
raw = readFileSync(path, 'utf8')
|
|
69
|
-
} catch (err) {
|
|
70
|
-
throw new SchemaBundleLoadError(
|
|
71
|
-
`Failed to read schema bundle from ${path}: ${(err as Error).message}`,
|
|
72
|
-
err,
|
|
73
|
-
)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
let bundle: Bundle
|
|
77
|
-
try {
|
|
78
|
-
bundle = JSON.parse(raw) as Bundle
|
|
79
|
-
} catch (err) {
|
|
80
|
-
throw new SchemaBundleLoadError(
|
|
81
|
-
`Schema bundle at ${path} is not valid JSON: ${(err as Error).message}`,
|
|
82
|
-
err,
|
|
83
|
-
)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (bundle.schemaVersion !== SUPPORTED_BUNDLE_VERSION) {
|
|
87
|
-
throw new SchemaBundleLoadError(
|
|
88
|
-
`Schema bundle at ${path} has schemaVersion ${bundle.schemaVersion}; this MCP server supports ${SUPPORTED_BUNDLE_VERSION}`,
|
|
89
|
-
)
|
|
90
|
-
}
|
|
91
|
-
if (!bundle.types || typeof bundle.types !== 'object') {
|
|
92
|
-
throw new SchemaBundleLoadError(
|
|
93
|
-
`Schema bundle at ${path} is missing the "types" object`,
|
|
94
|
-
)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const ajv = new Ajv2020({
|
|
98
|
-
strict: false,
|
|
99
|
-
allErrors: true,
|
|
100
|
-
// JSON Schema bundles include their own $schema URI; we don't need Ajv to add another.
|
|
101
|
-
validateSchema: false,
|
|
102
|
-
})
|
|
103
|
-
addFormats(ajv)
|
|
104
|
-
|
|
105
|
-
const validators = new Map<string, ValidateFunction>()
|
|
106
|
-
for (const [docType, decl] of Object.entries(bundle.types)) {
|
|
107
|
-
try {
|
|
108
|
-
const fn = ajv.compile(decl.meta)
|
|
109
|
-
validators.set(docType, fn)
|
|
110
|
-
} catch (err) {
|
|
111
|
-
throw new SchemaBundleLoadError(
|
|
112
|
-
`Schema bundle at ${path} has an invalid meta schema for "${docType}": ${(err as Error).message}`,
|
|
113
|
-
err,
|
|
114
|
-
)
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const knownTypes = Array.from(validators.keys()).sort()
|
|
119
|
-
|
|
120
|
-
return {
|
|
121
|
-
knownTypes,
|
|
122
|
-
validateMeta(docType: string, meta: unknown): MetaValidationResult {
|
|
123
|
-
const fn = validators.get(docType)
|
|
124
|
-
if (!fn) {
|
|
125
|
-
// Unknown doc-type → unconstrained (Rule 4). Existing-app traffic
|
|
126
|
-
// for non-canary types is never rejected by this validator.
|
|
127
|
-
return { ok: true }
|
|
128
|
-
}
|
|
129
|
-
const ok = fn(meta)
|
|
130
|
-
if (ok) return { ok: true }
|
|
131
|
-
const errors: MetaValidationIssue[] = (fn.errors ?? []).map((e) => ({
|
|
132
|
-
path: e.instancePath || '/',
|
|
133
|
-
message: e.message ?? 'validation failed',
|
|
134
|
-
keyword: e.keyword,
|
|
135
|
-
}))
|
|
136
|
-
return { ok: false, errors }
|
|
137
|
-
},
|
|
138
|
-
}
|
|
139
|
-
}
|
package/src/schema/validator.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Validator types for the MCP schema runtime hook.
|
|
3
|
-
*
|
|
4
|
-
* The MCP server stays schema-agnostic at the type level: it loads
|
|
5
|
-
* JSON Schema bundles (data) at boot, never imports a live Zod registry.
|
|
6
|
-
* This file declares only the shape of the validator, not how it's built.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
export interface MetaValidationIssue {
|
|
10
|
-
/** JSON-pointer-ish path to the offending field, e.g. "/priority". */
|
|
11
|
-
readonly path: string;
|
|
12
|
-
/** Human-readable error message. */
|
|
13
|
-
readonly message: string;
|
|
14
|
-
/** Ajv keyword that failed (e.g. "enum", "required", "pattern"). */
|
|
15
|
-
readonly keyword?: string;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export type MetaValidationResult =
|
|
19
|
-
| { ok: true }
|
|
20
|
-
| { ok: false; errors: ReadonlyArray<MetaValidationIssue> };
|
|
21
|
-
|
|
22
|
-
export interface SchemaBundleValidator {
|
|
23
|
-
/** Doc-types known to this bundle. */
|
|
24
|
-
readonly knownTypes: ReadonlyArray<string>;
|
|
25
|
-
/**
|
|
26
|
-
* Validate `meta` against the schema for `docType`. If `docType` is not
|
|
27
|
-
* in this bundle, returns `{ ok: true }` — unknown doc-types are
|
|
28
|
-
* unconstrained (Rule 4: never reject existing-app traffic).
|
|
29
|
-
*/
|
|
30
|
-
validateMeta(docType: string, meta: unknown): MetaValidationResult;
|
|
31
|
-
}
|
package/src/server-manager.ts
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ServerManager — owns the registry of known servers and the single *active*
|
|
3
|
-
* live connection (one-active model: switching tears down the previous one).
|
|
4
|
-
*
|
|
5
|
-
* The heavy per-server lifecycle (auth, space discovery, inbox observer,
|
|
6
|
-
* heartbeat, channel notifications, awareness) stays in {@link
|
|
7
|
-
* AbracadabraMCPServer}; the manager just decides *which* one is live and
|
|
8
|
-
* brokers connect/switch/disconnect. Tools reach the live connection through the
|
|
9
|
-
* forwarding proxy in `active-server.ts`, so they never hold a stale reference.
|
|
10
|
-
*
|
|
11
|
-
* The connect path is policy-aware: it probes the server first (see
|
|
12
|
-
* {@link loginOrRegisterWithPolicy}) so an un-registered key yields an
|
|
13
|
-
* actionable outcome (`needs_invite` / `registration_closed`) instead of an
|
|
14
|
-
* opaque error — and, crucially, surfaces the agent's public key so an admin can
|
|
15
|
-
* authorize it by hand when no invite token is available.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import {
|
|
19
|
-
AbracadabraClient,
|
|
20
|
-
type ConnectPolicyResult,
|
|
21
|
-
loadOrCreateKeyFile,
|
|
22
|
-
loginOrRegisterWithPolicy,
|
|
23
|
-
resolveServer,
|
|
24
|
-
type ServerProfile,
|
|
25
|
-
type ServerRegistry,
|
|
26
|
-
} from "@abraca/dabra";
|
|
27
|
-
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
28
|
-
import { AbracadabraMCPServer, type TriggerMode } from "./server.ts";
|
|
29
|
-
|
|
30
|
-
/** Defaults applied to every connection the manager builds (from env at boot). */
|
|
31
|
-
export interface ServerManagerDefaults {
|
|
32
|
-
agentName?: string;
|
|
33
|
-
agentColor?: string;
|
|
34
|
-
inviteCode?: string;
|
|
35
|
-
triggerMode?: TriggerMode;
|
|
36
|
-
mentionAliases?: string[];
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** Outcome of {@link ServerManager.connectServer}, formatted by the tool layer. */
|
|
40
|
-
export interface ConnectOutcome {
|
|
41
|
-
result: ConnectPolicyResult;
|
|
42
|
-
profile: ServerProfile;
|
|
43
|
-
/** True when the active connection actually changed (status === connected). */
|
|
44
|
-
switched: boolean;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export class ServerManager {
|
|
48
|
-
private _registry: ServerRegistry;
|
|
49
|
-
private readonly _defaults: ServerManagerDefaults;
|
|
50
|
-
private _active: AbracadabraMCPServer | null = null;
|
|
51
|
-
private _activeAlias: string | null = null;
|
|
52
|
-
private _mcp: McpServer | null = null;
|
|
53
|
-
|
|
54
|
-
constructor(registry: ServerRegistry, defaults: ServerManagerDefaults = {}) {
|
|
55
|
-
this._registry = registry;
|
|
56
|
-
this._defaults = defaults;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/** The currently live connection, or null when nothing is connected. */
|
|
60
|
-
get active(): AbracadabraMCPServer | null {
|
|
61
|
-
return this._active;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
get activeAlias(): string | null {
|
|
65
|
-
return this._activeAlias;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
get registry(): ServerRegistry {
|
|
69
|
-
return this._registry;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** Provide the MCP server so channel notifications can be (re)wired on switch. */
|
|
73
|
-
attachMcp(mcp: McpServer): void {
|
|
74
|
-
this._mcp = mcp;
|
|
75
|
-
// Wire notifications for an already-active connection (boot path).
|
|
76
|
-
if (this._active) this._active.startChannelNotifications(mcp);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Resolve, probe, and (if authorized) connect to a server. On success the
|
|
81
|
-
* previous active connection is torn down and the new one becomes active.
|
|
82
|
-
* On `needs_invite` / `registration_closed` nothing changes — the caller
|
|
83
|
-
* relays the actionable message (including this agent's public key).
|
|
84
|
-
*/
|
|
85
|
-
async connectServer(
|
|
86
|
-
target: string,
|
|
87
|
-
opts: { inviteCode?: string; name?: string } = {},
|
|
88
|
-
): Promise<ConnectOutcome> {
|
|
89
|
-
const profile = await resolveServer(this._registry, target);
|
|
90
|
-
if (!profile) {
|
|
91
|
-
throw new Error(
|
|
92
|
-
`Unknown server "${target}". Pass a full URL (http[s]://…) or add it to the registry first.`,
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// Already active? No-op switch.
|
|
97
|
-
if (this._active && this._activeAlias === profile.alias) {
|
|
98
|
-
return {
|
|
99
|
-
profile,
|
|
100
|
-
switched: false,
|
|
101
|
-
result: {
|
|
102
|
-
status: "connected",
|
|
103
|
-
token: "",
|
|
104
|
-
publicKey: this._active.userId ?? "",
|
|
105
|
-
serverName: profile.displayName,
|
|
106
|
-
needsPromotion: false,
|
|
107
|
-
},
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const agentName = opts.name ?? profile.displayName ?? this._defaults.agentName;
|
|
112
|
-
const inviteCode = opts.inviteCode ?? profile.inviteCode ?? this._defaults.inviteCode;
|
|
113
|
-
|
|
114
|
-
// Policy probe on a throwaway client — cheap, and lets us bail before
|
|
115
|
-
// building the heavy connection when the key isn't authorized.
|
|
116
|
-
const probeClient = new AbracadabraClient({
|
|
117
|
-
url: profile.url,
|
|
118
|
-
persistAuth: false,
|
|
119
|
-
});
|
|
120
|
-
const keypair = await loadOrCreateKeyFile(profile.keyFile, {
|
|
121
|
-
defaultFileName: "agent.key",
|
|
122
|
-
});
|
|
123
|
-
const result = await loginOrRegisterWithPolicy(probeClient, keypair, {
|
|
124
|
-
displayName: agentName,
|
|
125
|
-
deviceName: "MCP Agent",
|
|
126
|
-
inviteCode,
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
if (result.status !== "connected") {
|
|
130
|
-
return { profile, switched: false, result };
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Authorized — build and connect the full per-server connection. It
|
|
134
|
-
// re-authenticates with the same (now-registered) key.
|
|
135
|
-
const conn = new AbracadabraMCPServer({
|
|
136
|
-
url: profile.url,
|
|
137
|
-
agentName,
|
|
138
|
-
agentColor: profile.color ?? this._defaults.agentColor,
|
|
139
|
-
inviteCode,
|
|
140
|
-
keyFile: profile.keyFile,
|
|
141
|
-
triggerMode: this._defaults.triggerMode,
|
|
142
|
-
mentionAliases: this._defaults.mentionAliases,
|
|
143
|
-
});
|
|
144
|
-
await conn.connect();
|
|
145
|
-
|
|
146
|
-
const prev = this._active;
|
|
147
|
-
this._active = conn;
|
|
148
|
-
this._activeAlias = profile.alias;
|
|
149
|
-
if (this._mcp) conn.startChannelNotifications(this._mcp);
|
|
150
|
-
if (prev) await prev.destroy().catch(() => {});
|
|
151
|
-
|
|
152
|
-
return { profile, switched: true, result };
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/** Tear down the active connection (no-op when none). */
|
|
156
|
-
async disconnect(): Promise<void> {
|
|
157
|
-
const prev = this._active;
|
|
158
|
-
this._active = null;
|
|
159
|
-
this._activeAlias = null;
|
|
160
|
-
if (prev) await prev.destroy().catch(() => {});
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/** Snapshot of known servers + which is active, for `list_servers`. */
|
|
164
|
-
listServers(): Array<{
|
|
165
|
-
alias: string;
|
|
166
|
-
url: string;
|
|
167
|
-
active: boolean;
|
|
168
|
-
isDefault: boolean;
|
|
169
|
-
}> {
|
|
170
|
-
return Object.values(this._registry.servers).map((p) => ({
|
|
171
|
-
alias: p.alias,
|
|
172
|
-
url: p.url,
|
|
173
|
-
active: p.alias === this._activeAlias,
|
|
174
|
-
isDefault: p.alias === this._registry.defaultAlias,
|
|
175
|
-
}));
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/** Replace the in-memory registry (e.g. after a CLI edit reload). */
|
|
179
|
-
setRegistry(registry: ServerRegistry): void {
|
|
180
|
-
this._registry = registry;
|
|
181
|
-
}
|
|
182
|
-
}
|