@livx.cc/agentx 0.94.38
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 +164 -0
- package/dist/Agent-1DRfsYaK.d.ts +408 -0
- package/dist/cli.d.ts +293 -0
- package/dist/cli.js +12796 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +1319 -0
- package/dist/index.js +5998 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp-CnzmQ8JE.d.ts +101 -0
- package/dist/mcp.client.d.ts +228 -0
- package/dist/mcp.client.js +617 -0
- package/dist/mcp.client.js.map +1 -0
- package/dist/tools-DtpN8Agv.d.ts +274 -0
- package/dist/tools.shell.d.ts +115 -0
- package/dist/tools.shell.js +539 -0
- package/dist/tools.shell.js.map +1 -0
- package/package.json +81 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { A as AgentTool } from './tools-DtpN8Agv.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* MCP bridge — adapt a Model Context Protocol tool list into the agent's AgentTool[],
|
|
5
|
+
* so any MCP server's tools become first-class agent tools (edge-safe: you supply the
|
|
6
|
+
* transport via `callTool`; this module has no node/network dependency of its own).
|
|
7
|
+
*
|
|
8
|
+
* Pass the server's advertised tools + a `callTool(name, args)` that performs the call
|
|
9
|
+
* (over stdio/HTTP/whatever you wire up); each becomes an AgentTool the Agent can use.
|
|
10
|
+
*/
|
|
11
|
+
interface McpToolSpec {
|
|
12
|
+
name: string;
|
|
13
|
+
description?: string;
|
|
14
|
+
/** JSON Schema for the tool's arguments (MCP's `inputSchema`). */
|
|
15
|
+
inputSchema?: object;
|
|
16
|
+
}
|
|
17
|
+
/** Perform an MCP tool call; return its textual result. Throw to surface an error to the model. */
|
|
18
|
+
type McpCall = (name: string, args: any) => Promise<unknown>;
|
|
19
|
+
interface McpImage {
|
|
20
|
+
mimeType: string;
|
|
21
|
+
data: string;
|
|
22
|
+
}
|
|
23
|
+
interface McpToolResult {
|
|
24
|
+
text: string;
|
|
25
|
+
images?: McpImage[];
|
|
26
|
+
}
|
|
27
|
+
/** Adapt one MCP tool spec into an AgentTool backed by `callTool`. */
|
|
28
|
+
declare function mcpToolToAgentTool(spec: McpToolSpec, callTool: McpCall, prefix?: string): AgentTool;
|
|
29
|
+
/** Adapt a whole MCP tool list into AgentTool[] (names prefixed to avoid collisions).
|
|
30
|
+
* Optional `filter` pre-narrows the set at mount time (host allowlist), so a server with
|
|
31
|
+
* hundreds of tools needn't mount them all eagerly. */
|
|
32
|
+
declare function mcpToolsToAgentTools(specs: McpToolSpec[], callTool: McpCall, prefix?: string, filter?: (spec: McpToolSpec) => boolean): AgentTool[];
|
|
33
|
+
interface McpToolSearchOptions {
|
|
34
|
+
/** Prefix stripped from / shown on tool names (cosmetic; mirrors the adapter prefix). Default 'mcp__'. */
|
|
35
|
+
prefix?: string;
|
|
36
|
+
/** Max tools returned per `ToolSearch` query. Default 10. */
|
|
37
|
+
maxResults?: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Deferred-mount mode (ToolSearch-equivalent) for large MCP tool sets. Instead of mounting N
|
|
41
|
+
* tools into the wire schema (cost + latency + model confusion past a few dozen), mount exactly
|
|
42
|
+
* TWO bounded tools regardless of N:
|
|
43
|
+
* - `ToolSearch({ query })` — ranks the catalog by relevance and returns the top matches with
|
|
44
|
+
* their argument schemas, so the model discovers what's available on demand.
|
|
45
|
+
* - `McpCall({ name, args })` — invokes any catalog tool by name through the same transport.
|
|
46
|
+
* Keep `mcpToolsToAgentTools` (eager) as the default for small sets.
|
|
47
|
+
*/
|
|
48
|
+
declare function makeMcpToolSearch(specs: McpToolSpec[], callTool: McpCall, options?: McpToolSearchOptions): AgentTool[];
|
|
49
|
+
/** Minimal shape of a mounted MCP server this module needs — structurally satisfied by
|
|
50
|
+
* `MountedMcp` from `mcp.client.ts`, kept local so `mcp.ts` stays node-free/edge-safe. */
|
|
51
|
+
interface MountedMcpLike {
|
|
52
|
+
name: string;
|
|
53
|
+
specs: McpToolSpec[];
|
|
54
|
+
client: {
|
|
55
|
+
callTool(name: string, args: unknown): Promise<unknown>;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/** A flattened catalog entry's origin: which server owns it + the un-prefixed raw tool name. */
|
|
59
|
+
interface McpRoute {
|
|
60
|
+
server: string;
|
|
61
|
+
rawName: string;
|
|
62
|
+
}
|
|
63
|
+
/** Flatten servers' specs into one `mcp__<server>__<tool>` namespace + a display→origin map.
|
|
64
|
+
* Sanitizing/truncating to provider name rules can MERGE distinct names (`a/b` & `a:b` → `a_b`),
|
|
65
|
+
* so dedupe with a numeric suffix — a silent overwrite would orphan a tool. */
|
|
66
|
+
declare function buildMcpCatalog(servers: {
|
|
67
|
+
name: string;
|
|
68
|
+
specs: McpToolSpec[];
|
|
69
|
+
}[]): {
|
|
70
|
+
specs: McpToolSpec[];
|
|
71
|
+
routes: Map<string, McpRoute>;
|
|
72
|
+
};
|
|
73
|
+
/** Resolve a routed MCP call to its result. Throw to surface an error to the model. */
|
|
74
|
+
type McpRouteResolver = (server: string, rawName: string, args: any) => Promise<unknown>;
|
|
75
|
+
/**
|
|
76
|
+
* Ergonomic deferred-mount over already-mounted MCP servers: flattens their specs into one
|
|
77
|
+
* `mcp__<server>__<tool>` namespace and wires a single `ToolSearch`/`McpCall` pair that routes
|
|
78
|
+
* each call to the owning server's RAW `callTool` (so result normalization happens exactly once —
|
|
79
|
+
* double-normalizing corrupts image blocks).
|
|
80
|
+
*/
|
|
81
|
+
declare function makeMcpToolSearchFromMounted(mounted: MountedMcpLike[], options?: McpToolSearchOptions): {
|
|
82
|
+
tools: AgentTool[];
|
|
83
|
+
serverNames: string[];
|
|
84
|
+
toolCount: number;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Lazy variant: same `ToolSearch`/`McpCall` surface, but the server isn't required to be connected.
|
|
88
|
+
* Each `McpCall` resolves the owning server from the catalog and `resolve(server, rawName, args)`
|
|
89
|
+
* connects-on-demand — so a turn that calls no MCP tool opens ZERO connections. Edge-safe: the
|
|
90
|
+
* caller (`mountMcpCatalog` in `mcp.client.ts`) supplies the node-side connect/pool logic.
|
|
91
|
+
*/
|
|
92
|
+
declare function makeLazyMcpToolSearch(servers: {
|
|
93
|
+
name: string;
|
|
94
|
+
specs: McpToolSpec[];
|
|
95
|
+
}[], resolve: McpRouteResolver, options?: McpToolSearchOptions): {
|
|
96
|
+
tools: AgentTool[];
|
|
97
|
+
serverNames: string[];
|
|
98
|
+
toolCount: number;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export { type McpCall as M, type McpImage as a, type McpRoute as b, type McpRouteResolver as c, type McpToolResult as d, type McpToolSearchOptions as e, type McpToolSpec as f, type MountedMcpLike as g, buildMcpCatalog as h, makeMcpToolSearch as i, makeMcpToolSearchFromMounted as j, mcpToolToAgentTool as k, mcpToolsToAgentTools as l, makeLazyMcpToolSearch as m };
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { e as McpToolSearchOptions, f as McpToolSpec } from './mcp-CnzmQ8JE.js';
|
|
2
|
+
import { A as AgentTool } from './tools-DtpN8Agv.js';
|
|
3
|
+
import '@livx.cc/wcli/core';
|
|
4
|
+
|
|
5
|
+
/** A JSON-RPC 2.0 transport: request (awaits a result), notify (fire-and-forget), close. */
|
|
6
|
+
interface McpTransport {
|
|
7
|
+
start(): Promise<void>;
|
|
8
|
+
request(method: string, params?: unknown): Promise<any>;
|
|
9
|
+
notify(method: string, params?: unknown): Promise<void>;
|
|
10
|
+
close(): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
interface StdioServerSpec {
|
|
13
|
+
command: string;
|
|
14
|
+
args?: string[];
|
|
15
|
+
env?: Record<string, string>;
|
|
16
|
+
cwd?: string;
|
|
17
|
+
timeoutMs?: number;
|
|
18
|
+
}
|
|
19
|
+
declare class StdioTransport implements McpTransport {
|
|
20
|
+
private spec;
|
|
21
|
+
private proc?;
|
|
22
|
+
private buf;
|
|
23
|
+
private nextId;
|
|
24
|
+
private pending;
|
|
25
|
+
constructor(spec: StdioServerSpec);
|
|
26
|
+
start(): Promise<void>;
|
|
27
|
+
private onData;
|
|
28
|
+
private dispatch;
|
|
29
|
+
private failAll;
|
|
30
|
+
private write;
|
|
31
|
+
request(method: string, params?: unknown): Promise<any>;
|
|
32
|
+
notify(method: string, params?: unknown): Promise<void>;
|
|
33
|
+
close(): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
interface HttpServerSpec {
|
|
36
|
+
url: string;
|
|
37
|
+
headers?: Record<string, string>;
|
|
38
|
+
/** Sugar over `headers` → `Authorization: Bearer <token>`. The headless-friendly path (no OAuth loopback);
|
|
39
|
+
* inject a static token (from env / VFS secret) per our CLAUDE.md rule preferring API keys for cron/edge. */
|
|
40
|
+
bearerToken?: string;
|
|
41
|
+
timeoutMs?: number;
|
|
42
|
+
}
|
|
43
|
+
/** Thrown when an HTTP MCP server rejects auth (401/403). Carries `needsAuth` so callers can surface a hint
|
|
44
|
+
* ("set bearerToken / headers") instead of a generic mount failure — without attempting an interactive flow. */
|
|
45
|
+
declare class McpAuthError extends Error {
|
|
46
|
+
status: number;
|
|
47
|
+
serverName?: string | undefined;
|
|
48
|
+
needsAuth: boolean;
|
|
49
|
+
constructor(status: number, serverName?: string | undefined);
|
|
50
|
+
}
|
|
51
|
+
declare class HttpTransport implements McpTransport {
|
|
52
|
+
private spec;
|
|
53
|
+
private nextId;
|
|
54
|
+
private sessionId?;
|
|
55
|
+
private fetchImpl;
|
|
56
|
+
constructor(spec: HttpServerSpec, fetchImpl?: typeof fetch);
|
|
57
|
+
start(): Promise<void>;
|
|
58
|
+
request(method: string, params?: unknown): Promise<any>;
|
|
59
|
+
notify(method: string, params?: unknown): Promise<void>;
|
|
60
|
+
private post;
|
|
61
|
+
close(): Promise<void>;
|
|
62
|
+
}
|
|
63
|
+
declare class McpClient {
|
|
64
|
+
transport: McpTransport;
|
|
65
|
+
private clientInfo;
|
|
66
|
+
constructor(transport: McpTransport, clientInfo?: {
|
|
67
|
+
name: string;
|
|
68
|
+
version: string;
|
|
69
|
+
});
|
|
70
|
+
/** `initialize` handshake → `notifications/initialized`. Returns the server's init result. */
|
|
71
|
+
connect(): Promise<{
|
|
72
|
+
serverInfo?: {
|
|
73
|
+
name?: string;
|
|
74
|
+
version?: string;
|
|
75
|
+
};
|
|
76
|
+
capabilities?: any;
|
|
77
|
+
}>;
|
|
78
|
+
listTools(): Promise<McpToolSpec[]>;
|
|
79
|
+
callTool(name: string, args: unknown): Promise<unknown>;
|
|
80
|
+
listResources(): Promise<Array<{
|
|
81
|
+
uri: string;
|
|
82
|
+
name?: string;
|
|
83
|
+
mimeType?: string;
|
|
84
|
+
}>>;
|
|
85
|
+
readResource(uri: string): Promise<any>;
|
|
86
|
+
close(): Promise<void>;
|
|
87
|
+
}
|
|
88
|
+
/** One server: stdio (`command`+`args`+`env`) OR http (`url`+`headers`). `disabled` skips it. */
|
|
89
|
+
interface McpServerConfig {
|
|
90
|
+
command?: string;
|
|
91
|
+
args?: string[];
|
|
92
|
+
env?: Record<string, string>;
|
|
93
|
+
cwd?: string;
|
|
94
|
+
url?: string;
|
|
95
|
+
headers?: Record<string, string>;
|
|
96
|
+
/** `Authorization: Bearer <token>` sugar for http servers (headless-friendly; no OAuth flow). */
|
|
97
|
+
bearerToken?: string;
|
|
98
|
+
/** Auth strategy for http servers. `'oauth'` → the CLI resolves a token via the attended OAuth flow
|
|
99
|
+
* (cli/mcpOAuth.ts) and injects it as `bearerToken` at mount; the core never runs the flow. Default none. */
|
|
100
|
+
auth?: 'oauth' | 'none';
|
|
101
|
+
timeoutMs?: number;
|
|
102
|
+
disabled?: boolean;
|
|
103
|
+
}
|
|
104
|
+
interface MountedMcp {
|
|
105
|
+
name: string;
|
|
106
|
+
client: McpClient;
|
|
107
|
+
tools: AgentTool[];
|
|
108
|
+
/** Raw tool specs discovered at mount (already fetched internally) — exposed so deferred-mount
|
|
109
|
+
* glue (`makeMcpToolSearchFromMounted`) needn't re-call `tools/list`. */
|
|
110
|
+
specs: McpToolSpec[];
|
|
111
|
+
serverInfo?: {
|
|
112
|
+
name?: string;
|
|
113
|
+
version?: string;
|
|
114
|
+
};
|
|
115
|
+
/** The config this server was mounted with — lets a host remount it (`/mcp reconnect`). */
|
|
116
|
+
config: McpServerConfig;
|
|
117
|
+
}
|
|
118
|
+
/** Connect one server, discover its tools, and adapt them to prefixed AgentTools (`mcp__<name>__`). */
|
|
119
|
+
declare function mountMcpServer(name: string, cfg: McpServerConfig): Promise<MountedMcp>;
|
|
120
|
+
/**
|
|
121
|
+
* Mount every configured server in PARALLEL (one slow/dead server no longer serializes the rest);
|
|
122
|
+
* each may carry a `mountTimeoutMs` deadline. A server that fails is logged and skipped.
|
|
123
|
+
*/
|
|
124
|
+
declare function mountMcpServers(servers?: Record<string, McpServerConfig>, opts?: {
|
|
125
|
+
mountTimeoutMs?: number;
|
|
126
|
+
}): Promise<MountedMcp[]>;
|
|
127
|
+
/**
|
|
128
|
+
* One-shot deferred MCP mount: connect every entry (failures dropped, never block startup) and
|
|
129
|
+
* fold the survivors into a single `ToolSearch`/`McpCall` pair via `makeMcpToolSearchFromMounted`.
|
|
130
|
+
* The node-only counterpart to that edge-safe helper — returns `mounted` too so callers can close
|
|
131
|
+
* the clients on shutdown. Eager: it connects all servers up front. For lazy connect + a cached
|
|
132
|
+
* catalog (zero connections on a turn that uses no MCP tool), use `mountMcpCatalog`.
|
|
133
|
+
*/
|
|
134
|
+
declare function mountMcpDeferred(servers?: Record<string, McpServerConfig>, options?: McpToolSearchOptions & {
|
|
135
|
+
mountTimeoutMs?: number;
|
|
136
|
+
}): Promise<{
|
|
137
|
+
tools: AgentTool[];
|
|
138
|
+
serverNames: string[];
|
|
139
|
+
toolCount: number;
|
|
140
|
+
mounted: MountedMcp[];
|
|
141
|
+
}>;
|
|
142
|
+
/** A persistent tool catalog: lets `mountMcpCatalog` build `ToolSearch` WITHOUT connecting when a
|
|
143
|
+
* fresh entry exists. The lib defines the interface; the consumer supplies the store (RAM/disk).
|
|
144
|
+
* Key = a hash of the server's resolved config (see `mcpConfigKey`). TTL/versioning is the store's. */
|
|
145
|
+
interface McpCatalogStore {
|
|
146
|
+
get(key: string): McpToolSpec[] | null;
|
|
147
|
+
set(key: string, specs: McpToolSpec[]): void;
|
|
148
|
+
}
|
|
149
|
+
/** Stable cache key for a server config — command/args/cwd + env NAMES (not secret values), or
|
|
150
|
+
* url + header names. Changing any of these invalidates the cached catalog; rotating a secret does not. */
|
|
151
|
+
declare function mcpConfigKey(cfg: McpServerConfig): string;
|
|
152
|
+
/** Default in-memory catalog: process-lifetime, hash-keyed, TTL-expiring (so a server that gains
|
|
153
|
+
* tools is picked up after the TTL without a restart). Shared module singleton → cross-turn reuse. */
|
|
154
|
+
declare class MemMcpCatalog implements McpCatalogStore {
|
|
155
|
+
private ttlMs;
|
|
156
|
+
private m;
|
|
157
|
+
constructor(ttlMs?: number);
|
|
158
|
+
get(key: string): McpToolSpec[] | null;
|
|
159
|
+
set(key: string, specs: McpToolSpec[]): void;
|
|
160
|
+
}
|
|
161
|
+
/** Opt-in warm pool: keeps stdio MCP clients connected across turns, reaping each after `ttlMs`
|
|
162
|
+
* idle. Most stdio MCPs are per-turn by design, so this is off by default; HTTP servers are
|
|
163
|
+
* stateless and never pooled. */
|
|
164
|
+
declare class McpPool {
|
|
165
|
+
private ttlMs;
|
|
166
|
+
private warm;
|
|
167
|
+
constructor(ttlMs?: number);
|
|
168
|
+
get(key: string): McpClient | null;
|
|
169
|
+
put(key: string, client: McpClient): void;
|
|
170
|
+
private arm;
|
|
171
|
+
private evict;
|
|
172
|
+
closeAll(): Promise<void>;
|
|
173
|
+
}
|
|
174
|
+
interface McpCatalogOptions extends McpToolSearchOptions {
|
|
175
|
+
/** Where to read/write discovered specs. Default: a shared process-lifetime `MemMcpCatalog`. */
|
|
176
|
+
catalog?: McpCatalogStore;
|
|
177
|
+
/** Per-server deadline for the connect+list on a cache MISS. A hung server is skipped, not blocking. */
|
|
178
|
+
mountTimeoutMs?: number;
|
|
179
|
+
/** Opt-in: keep stdio clients warm across turns (see `McpPool`). HTTP servers are never pooled. */
|
|
180
|
+
keepWarm?: boolean;
|
|
181
|
+
/** Warm-client pool. Default: a shared process-lifetime `McpPool` (only used when `keepWarm`). */
|
|
182
|
+
pool?: McpPool;
|
|
183
|
+
/** Negative cache: after a server fails/times out discovery, skip it for this long so it never
|
|
184
|
+
* re-floors a turn at the deadline. Default 60s; the server is re-probed once the cooldown lapses. */
|
|
185
|
+
failureCooldownMs?: number;
|
|
186
|
+
/** Shared failure-cooldown map (configKey → until-epoch-ms). Default: a process-lifetime singleton. */
|
|
187
|
+
failures?: Map<string, number>;
|
|
188
|
+
/** Discovery policy on a cache MISS:
|
|
189
|
+
* - `'connect'` (default): synchronously connect+list (deadline-bounded) — blocks until ready.
|
|
190
|
+
* - `'cache-only'`: NEVER block the turn — serve only cached servers, and kick uncached discovery
|
|
191
|
+
* to the background (so it's warm next turn). Pair with a boot/timer `warmMcpCatalog` so the
|
|
192
|
+
* first turn is already a cache hit. */
|
|
193
|
+
discover?: 'connect' | 'cache-only';
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Lazy + cached MCP mount. Builds the `ToolSearch`/`McpCall` pair from the CACHED catalog when one
|
|
197
|
+
* exists — connecting NOTHING. On a cache miss it (by default) connects once (parallel, deadline-
|
|
198
|
+
* bounded), lists, caches, then disconnects (or keeps warm if `keepWarm`). A server is connected only
|
|
199
|
+
* when one of its tools is actually invoked via `McpCall` (memoized per turn; reused from the warm
|
|
200
|
+
* pool if enabled). A server that fails/times out is negative-cached (`failureCooldownMs`) so it
|
|
201
|
+
* never re-floors a later turn.
|
|
202
|
+
*
|
|
203
|
+
* For zero turn-path latency even on a cold process, set `discover: 'cache-only'` and call
|
|
204
|
+
* `warmMcpCatalog` at boot + on a timer: the turn then NEVER synchronously connects — it serves
|
|
205
|
+
* cached servers and warms the rest in the background.
|
|
206
|
+
*
|
|
207
|
+
* Per-turn cost: a turn using NO MCP tool → 0 connections; a turn using one → exactly one.
|
|
208
|
+
*/
|
|
209
|
+
declare function mountMcpCatalog(servers?: Record<string, McpServerConfig>, opts?: McpCatalogOptions): Promise<{
|
|
210
|
+
tools: AgentTool[];
|
|
211
|
+
serverNames: string[];
|
|
212
|
+
toolCount: number;
|
|
213
|
+
connect(name: string): Promise<McpClient>;
|
|
214
|
+
close(): Promise<void>;
|
|
215
|
+
}>;
|
|
216
|
+
/**
|
|
217
|
+
* Off-turn catalog warm-up: do the one cold discovery pass (connect + list, parallel, deadline-
|
|
218
|
+
* bounded) so a later `mountMcpCatalog(..., { discover: 'cache-only' })` is a cache HIT. Call at
|
|
219
|
+
* server boot and on a timer (cadence < the catalog TTL). Cache hits cost nothing; down servers are
|
|
220
|
+
* negative-cached so turns never touch them. Returns which servers warmed vs failed.
|
|
221
|
+
*/
|
|
222
|
+
declare function warmMcpCatalog(servers?: Record<string, McpServerConfig>, opts?: McpCatalogOptions): Promise<{
|
|
223
|
+
warmed: string[];
|
|
224
|
+
failed: string[];
|
|
225
|
+
toolCount: number;
|
|
226
|
+
}>;
|
|
227
|
+
|
|
228
|
+
export { type HttpServerSpec, HttpTransport, McpAuthError, type McpCatalogOptions, type McpCatalogStore, McpClient, McpPool, type McpServerConfig, type McpTransport, MemMcpCatalog, type MountedMcp, type StdioServerSpec, StdioTransport, mcpConfigKey, mountMcpCatalog, mountMcpDeferred, mountMcpServer, mountMcpServers, warmMcpCatalog };
|