agent-trellis 0.7.0 → 0.8.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.
Files changed (104) hide show
  1. package/README.md +97 -38
  2. package/dist/adapters/claude-code.js +3 -2
  3. package/dist/adapters/codex.js +3 -2
  4. package/dist/adapters/jsonMcp.d.ts +3 -1
  5. package/dist/adapters/jsonMcp.js +18 -4
  6. package/dist/adapters/kimi-code.d.ts +21 -0
  7. package/dist/adapters/kimi-code.js +107 -0
  8. package/dist/adapters/kiro.js +3 -2
  9. package/dist/adapters/mcpPlan.d.ts +6 -1
  10. package/dist/adapters/mcpPlan.js +106 -8
  11. package/dist/adapters/pi.js +22 -3
  12. package/dist/adapters/symlinkPlan.d.ts +7 -0
  13. package/dist/adapters/symlinkPlan.js +16 -7
  14. package/dist/cli.js +121 -21
  15. package/dist/commands/doctor.d.ts +7 -2
  16. package/dist/commands/doctor.js +38 -2
  17. package/dist/commands/init.d.ts +4 -2
  18. package/dist/commands/init.js +26 -13
  19. package/dist/commands/kimi.d.ts +16 -0
  20. package/dist/commands/kimi.js +64 -0
  21. package/dist/commands/manage.d.ts +39 -0
  22. package/dist/commands/manage.js +126 -0
  23. package/dist/commands/mcp.d.ts +22 -1
  24. package/dist/commands/mcp.js +65 -2
  25. package/dist/commands/mcpGateway.d.ts +6 -0
  26. package/dist/commands/mcpGateway.js +34 -14
  27. package/dist/commands/mcpImport.d.ts +27 -0
  28. package/dist/commands/mcpImport.js +336 -0
  29. package/dist/commands/memory.d.ts +5 -3
  30. package/dist/commands/memory.js +12 -8
  31. package/dist/commands/migrate.d.ts +16 -5
  32. package/dist/commands/migrate.js +90 -17
  33. package/dist/commands/onboard.d.ts +74 -2
  34. package/dist/commands/onboard.js +458 -71
  35. package/dist/commands/rollback.js +4 -1
  36. package/dist/commands/skill.d.ts +17 -0
  37. package/dist/commands/skill.js +47 -2
  38. package/dist/commands/sync.js +2 -0
  39. package/dist/core/canonical.d.ts +15 -7
  40. package/dist/core/canonical.js +101 -13
  41. package/dist/core/capabilitySelection.d.ts +45 -0
  42. package/dist/core/capabilitySelection.js +124 -0
  43. package/dist/core/types.d.ts +25 -2
  44. package/dist/core/types.js +7 -0
  45. package/dist/lib/backup.d.ts +2 -0
  46. package/dist/lib/backup.js +4 -0
  47. package/dist/lib/builtinSkills.d.ts +2 -0
  48. package/dist/lib/builtinSkills.js +4 -0
  49. package/dist/lib/cliMetadata.d.ts +1 -0
  50. package/dist/lib/cliMetadata.js +14 -0
  51. package/dist/lib/gatewayBackend.d.ts +28 -0
  52. package/dist/lib/gatewayBackend.js +72 -1
  53. package/dist/lib/installAgent.d.ts +7 -5
  54. package/dist/lib/installAgent.js +4 -3
  55. package/dist/lib/mcpConnect.js +5 -1
  56. package/dist/lib/mcpMigrateRead.d.ts +1 -0
  57. package/dist/lib/mcpMigrateRead.js +3 -0
  58. package/dist/lib/mcpRuntime.d.ts +46 -0
  59. package/dist/lib/mcpRuntime.js +185 -0
  60. package/dist/lib/mcpStatusProvider.d.ts +10 -0
  61. package/dist/lib/mcpStatusProvider.js +25 -0
  62. package/dist/lib/mcpToolRegistry.d.ts +18 -0
  63. package/dist/lib/mcpToolRegistry.js +107 -14
  64. package/dist/lib/memoryGraph.d.ts +1 -1
  65. package/dist/lib/memoryGraph.js +9 -3
  66. package/dist/lib/memoryProvider.d.ts +58 -0
  67. package/dist/lib/memoryProvider.js +175 -0
  68. package/dist/lib/memoryStatus.d.ts +17 -0
  69. package/dist/lib/memoryStatus.js +88 -0
  70. package/dist/lib/nativeMemory.d.ts +19 -0
  71. package/dist/lib/nativeMemory.js +129 -0
  72. package/dist/lib/runtimeControlProvider.d.ts +16 -0
  73. package/dist/lib/runtimeControlProvider.js +168 -0
  74. package/dist/lib/secretEnv.d.ts +2 -1
  75. package/dist/lib/secretEnv.js +6 -2
  76. package/dist/lib/skillProvider.d.ts +13 -0
  77. package/dist/lib/skillProvider.js +210 -0
  78. package/dist/lib/taskProvider.d.ts +7 -0
  79. package/dist/lib/taskProvider.js +43 -0
  80. package/dist/lib/taskStore.d.ts +46 -0
  81. package/dist/lib/taskStore.js +156 -0
  82. package/dist/lib/terminalPicker.d.ts +23 -49
  83. package/dist/lib/terminalPicker.js +53 -218
  84. package/dist/pi-bridge/bundle.js +475 -37
  85. package/dist/pi-bridge/index.js +42 -21
  86. package/dist/pi-bridge/schemaTranslate.d.ts +4 -8
  87. package/dist/pi-bridge/schemaTranslate.js +4 -8
  88. package/dist/probes/kimi-code.d.ts +12 -0
  89. package/dist/probes/kimi-code.js +55 -0
  90. package/dist/sdk.d.ts +1 -1
  91. package/docs/architecture.md +224 -26
  92. package/docs/assets/agent-trellis-architecture-horizontal.svg +80 -0
  93. package/docs/assets/agent-trellis-architecture-vertical.svg +82 -0
  94. package/docs/assets/trellis-mark.svg +12 -0
  95. package/docs/getting-started.md +328 -35
  96. package/docs/overview.md +131 -0
  97. package/docs/overview.zh-CN.md +100 -0
  98. package/docs/research.md +59 -0
  99. package/docs/roadmap.md +9 -5
  100. package/package.json +3 -2
  101. package/schema/builtin-skills/trellis-runtime/SKILL.md +117 -0
  102. package/schema/capability-selection.example.yaml +23 -0
  103. package/schema/scope.example.yaml +1 -1
  104. package/schema/servers.example.yaml +15 -1
@@ -67,6 +67,7 @@ export function openBackupSession(homeDir, command) {
67
67
  dirCreated = true;
68
68
  }
69
69
  return {
70
+ runId,
70
71
  writeFile(path, content) {
71
72
  ensureDir();
72
73
  const afterHash = sha256(content);
@@ -105,6 +106,9 @@ export function openBackupSession(homeDir, command) {
105
106
  await rm(path, { force: true });
106
107
  operations.push({ kind: "symlink-remove", path, beforeLinkTarget: oldLinkTarget });
107
108
  },
109
+ hasOperations() {
110
+ return operations.length > 0;
111
+ },
108
112
  finalize() {
109
113
  if (!dirCreated)
110
114
  return;
@@ -0,0 +1,2 @@
1
+ export declare const TRELLIS_RUNTIME_SKILL_NAME = "trellis-runtime";
2
+ export declare function isBuiltinSkillName(name: string): boolean;
@@ -0,0 +1,4 @@
1
+ export const TRELLIS_RUNTIME_SKILL_NAME = "trellis-runtime";
2
+ export function isBuiltinSkillName(name) {
3
+ return name === TRELLIS_RUNTIME_SKILL_NAME;
4
+ }
@@ -0,0 +1 @@
1
+ export declare const TRELLIS_VERSION: string;
@@ -0,0 +1,14 @@
1
+ import { readFileSync } from "node:fs";
2
+ /**
3
+ * Read the package version relative to the compiled/source CLI entrypoint.
4
+ * `src/lib` and `dist/lib` are both exactly two levels below package.json,
5
+ * so the same URL works under tsx and from an installed package.
6
+ */
7
+ function loadPackageVersion() {
8
+ const manifest = JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8"));
9
+ if (typeof manifest.version !== "string" || manifest.version.length === 0) {
10
+ throw new Error("agent-trellis package.json does not declare a version");
11
+ }
12
+ return manifest.version;
13
+ }
14
+ export const TRELLIS_VERSION = loadPackageVersion();
@@ -23,6 +23,8 @@ export interface GatewayBackend {
23
23
  listTools(): Promise<AggregatedTool[]>;
24
24
  /** Invoke one aggregated tool; implementations route it to its owner. */
25
25
  callTool(name: string, args?: Record<string, unknown>): Promise<unknown>;
26
+ /** Sanitized connection state for user/Agent remediation. */
27
+ listStatus?(): readonly UpstreamStatus[];
26
28
  /** Release everything this backend holds. Must be safe to call twice. */
27
29
  close(): Promise<void>;
28
30
  }
@@ -32,6 +34,17 @@ export interface UpstreamSpec {
32
34
  name: string;
33
35
  def: McpServerDef;
34
36
  }
37
+ export type UpstreamStatusKind = "ready" | "auth-required" | "unavailable" | "timeout" | "degraded";
38
+ export interface UpstreamStatus {
39
+ name: string;
40
+ transport: "stdio" | "http" | "sse";
41
+ status: UpstreamStatusKind;
42
+ detail?: string;
43
+ remediation?: string;
44
+ remediationKind?: "oauth" | "environment" | "install" | "endpoint" | "router";
45
+ requiresHuman?: boolean;
46
+ requiresSessionRestart?: boolean;
47
+ }
35
48
  export interface LocalBackendOptions {
36
49
  secretsPolicy: SecretsPolicy;
37
50
  clientInfo: McpClientInfo;
@@ -64,12 +77,27 @@ export declare class LocalBackend implements GatewayBackend {
64
77
  private readonly warn;
65
78
  private readonly registry;
66
79
  private readonly connected;
80
+ private readonly statuses;
67
81
  private closed;
68
82
  private constructor();
69
83
  static connect(upstreams: readonly UpstreamSpec[], options: LocalBackendOptions): Promise<LocalBackend>;
70
84
  private addUpstream;
71
85
  listTools(): Promise<AggregatedTool[]>;
72
86
  callTool(name: string, args?: Record<string, unknown>): Promise<unknown>;
87
+ listStatus(): readonly UpstreamStatus[];
73
88
  close(): Promise<void>;
74
89
  private closeUpstream;
75
90
  }
91
+ /**
92
+ * Attaches a stored OAuth token, refreshing it first if it has expired.
93
+ *
94
+ * Returns the definition unchanged when nothing is stored for this
95
+ * server: the overwhelmingly common case is a remote server that uses no
96
+ * OAuth at all, or one whose credential lives in `headers` already, and
97
+ * neither should be disturbed.
98
+ *
99
+ * An explicit `Authorization` header in canonical wins. Someone who wrote
100
+ * one meant it, and silently overriding it with a stored token would be
101
+ * the kind of invisible precedence that is impossible to debug.
102
+ */
103
+ export declare function withOAuthHeader(name: string, def: McpServerDef, homeDir: string): Promise<McpServerDef>;
@@ -32,6 +32,7 @@ export class LocalBackend {
32
32
  warn;
33
33
  registry = new McpToolRegistry();
34
34
  connected = new Set();
35
+ statuses = new Map();
35
36
  closed = false;
36
37
  constructor(warn) {
37
38
  this.warn = warn;
@@ -43,6 +44,8 @@ export class LocalBackend {
43
44
  return backend;
44
45
  }
45
46
  async addUpstream({ name, def }, options) {
47
+ const transport = def.transport === "http" || def.transport === "sse" ? def.transport : "stdio";
48
+ this.statuses.set(name, { name, transport, status: "unavailable", detail: "connecting" });
46
49
  let effectiveDef = def;
47
50
  if (options.homeDir && def.url) {
48
51
  try {
@@ -53,6 +56,7 @@ export class LocalBackend {
53
56
  // dropped — the same isolation a failed connection gets, never a
54
57
  // browser and never a blocked startup.
55
58
  this.warn(`trellis-mcp-gateway: skipping MCP server "${name}": ${errorText(err)}`);
59
+ this.statuses.set(name, failureStatus(name, def, err));
56
60
  return;
57
61
  }
58
62
  }
@@ -62,6 +66,7 @@ export class LocalBackend {
62
66
  }
63
67
  catch (err) {
64
68
  this.warn(`trellis-mcp-gateway: failed to connect to MCP server "${name}": ${errorText(err)}`);
69
+ this.statuses.set(name, failureStatus(name, def, err));
65
70
  return;
66
71
  }
67
72
  this.connected.add(client);
@@ -73,12 +78,28 @@ export class LocalBackend {
73
78
  catch (err) {
74
79
  this.warn(`trellis-mcp-gateway: failed to list tools for MCP server "${name}": ${errorText(err)}`);
75
80
  await this.closeUpstream(client);
81
+ this.statuses.set(name, failureStatus(name, def, err));
76
82
  return;
77
83
  }
78
84
  const { skipped } = this.registry.add(name, client, tools);
79
85
  for (const collided of skipped) {
80
86
  this.warn(`trellis-mcp-gateway: tool "${collided}" from server "${name}" collides with an already-registered tool and was skipped`);
81
87
  }
88
+ if (tools.length === 0 && name === "mcp-router") {
89
+ this.statuses.set(name, {
90
+ name,
91
+ transport,
92
+ status: "degraded",
93
+ detail: "router connected but exposed no downstream tools",
94
+ remediationKind: "router",
95
+ requiresHuman: true,
96
+ remediation: "check the MCP router's provider login/configuration and reconnect it, then restart the Agent session",
97
+ requiresSessionRestart: true,
98
+ });
99
+ }
100
+ else {
101
+ this.statuses.set(name, { name, transport, status: "ready", detail: `connected; ${tools.length} tool(s) available` });
102
+ }
82
103
  }
83
104
  async listTools() {
84
105
  return this.registry.listTools();
@@ -86,6 +107,9 @@ export class LocalBackend {
86
107
  async callTool(name, args) {
87
108
  return this.registry.callTool(name, args);
88
109
  }
110
+ listStatus() {
111
+ return [...this.statuses.values()];
112
+ }
89
113
  async close() {
90
114
  if (this.closed)
91
115
  return;
@@ -105,6 +129,53 @@ export class LocalBackend {
105
129
  }
106
130
  }
107
131
  }
132
+ function failureStatus(name, def, error) {
133
+ const detail = errorText(error).slice(0, 300);
134
+ const auth = def.url !== undefined && /401|403|unauthorized|forbidden|authentication|required|login/i.test(detail);
135
+ const timeout = /timed out|timeout/i.test(detail);
136
+ if (auth) {
137
+ return {
138
+ name,
139
+ transport: def.transport === "sse" ? "sse" : "http",
140
+ status: "auth-required",
141
+ detail,
142
+ remediationKind: "oauth",
143
+ requiresHuman: true,
144
+ requiresSessionRestart: true,
145
+ remediation: `run \`trellis mcp auth ${name}\` in a human terminal, then restart the Agent session`,
146
+ };
147
+ }
148
+ if (timeout) {
149
+ return {
150
+ name,
151
+ transport: def.transport === "sse" ? "sse" : def.url ? "http" : "stdio",
152
+ status: "timeout",
153
+ detail,
154
+ remediationKind: def.url ? "endpoint" : "environment",
155
+ requiresHuman: true,
156
+ requiresSessionRestart: true,
157
+ remediation: def.url
158
+ ? `check the endpoint/network; if it requires OAuth, run \`trellis mcp auth ${name}\``
159
+ : `check that \`${def.command ?? "the MCP command"}\` is installed and responsive`,
160
+ };
161
+ }
162
+ const envNames = [...(def.env ?? []), ...Object.keys(def.envAliases ?? {})];
163
+ const missingExecutable = /ENOENT|not found|no such file/i.test(detail);
164
+ return {
165
+ name,
166
+ transport: def.transport === "sse" ? "sse" : def.url ? "http" : "stdio",
167
+ status: "unavailable",
168
+ detail,
169
+ remediationKind: def.url ? "endpoint" : (missingExecutable ? "install" : "environment"),
170
+ requiresHuman: true,
171
+ requiresSessionRestart: true,
172
+ remediation: def.url
173
+ ? `check the MCP endpoint and credentials; run \`trellis mcp auth ${name}\` if it uses OAuth`
174
+ : envNames.length > 0
175
+ ? `check the configured environment variables (${envNames.join(", ")}) and rerun \`trellis mcp sync\``
176
+ : `check that \`${def.command ?? "the MCP command"}\` is installed and responsive`,
177
+ };
178
+ }
108
179
  /**
109
180
  * Attaches a stored OAuth token, refreshing it first if it has expired.
110
181
  *
@@ -117,7 +188,7 @@ export class LocalBackend {
117
188
  * one meant it, and silently overriding it with a stored token would be
118
189
  * the kind of invisible precedence that is impossible to debug.
119
190
  */
120
- async function withOAuthHeader(name, def, homeDir) {
191
+ export async function withOAuthHeader(name, def, homeDir) {
121
192
  const hasExplicitAuth = Object.keys(def.headers ?? {}).some((key) => key.toLowerCase() === "authorization");
122
193
  if (hasExplicitAuth)
123
194
  return def;
@@ -2,14 +2,15 @@
2
2
  * Install-then-manage (trellis-managed-agents design.md D5): selecting a
3
3
  * not-yet-present agent into the managed set is itself the authorization
4
4
  * to install it, but never silently — one confirmation, then a real
5
- * `npm install -g <package>` child process. Kiro has no npm package (a
6
- * desktop download) and is refused before this module is ever reached.
5
+ * `npm install -g <package>` child process. Kiro and Kimi Code use official
6
+ * install scripts rather than an npm package and are refused before this
7
+ * module is ever reached.
7
8
  */
8
9
  import type { AgentId } from "../core/types.js";
9
10
  /** Only agents with a real `npm install -g <pkg>` command — Kiro's own
10
11
  * `INSTALL_HINTS` entry is a download URL, not a package, and is never
11
12
  * looked up here. */
12
- export declare const NPM_INSTALLABLE: Record<Exclude<AgentId, "kiro">, string>;
13
+ export declare const NPM_INSTALLABLE: Record<Exclude<AgentId, "kiro" | "kimi-code">, string>;
13
14
  export interface ConfirmAndInstallOptions {
14
15
  /** Test/real seam, same pattern as onboard's `promptForAgent` — a real
15
16
  * terminal confirmation by default. */
@@ -19,8 +20,9 @@ export interface ConfirmAndInstallOptions {
19
20
  }
20
21
  export interface ConfirmAndInstallResult {
21
22
  installed: boolean;
22
- /** False only when the agent has no npm package at all (Kiro) — the
23
- * caller refuses this agent with its download URL instead of prompting. */
23
+ /** False when the agent has no supported npm installation path — the
24
+ * caller refuses it with its official installation hint instead of
25
+ * prompting. */
24
26
  installable: boolean;
25
27
  }
26
28
  export declare function confirmAndInstall(agent: AgentId, opts?: ConfirmAndInstallOptions): Promise<ConfirmAndInstallResult>;
@@ -2,8 +2,9 @@
2
2
  * Install-then-manage (trellis-managed-agents design.md D5): selecting a
3
3
  * not-yet-present agent into the managed set is itself the authorization
4
4
  * to install it, but never silently — one confirmation, then a real
5
- * `npm install -g <package>` child process. Kiro has no npm package (a
6
- * desktop download) and is refused before this module is ever reached.
5
+ * `npm install -g <package>` child process. Kiro and Kimi Code use official
6
+ * install scripts rather than an npm package and are refused before this
7
+ * module is ever reached.
7
8
  */
8
9
  import { execFileSync } from "node:child_process";
9
10
  import { createInterface } from "node:readline/promises";
@@ -31,7 +32,7 @@ function runInstallReal(pkg) {
31
32
  execFileSync("npm", ["install", "-g", pkg], { stdio: "inherit" });
32
33
  }
33
34
  export async function confirmAndInstall(agent, opts = {}) {
34
- if (agent === "kiro") {
35
+ if (agent === "kiro" || agent === "kimi-code") {
35
36
  return { installed: false, installable: false };
36
37
  }
37
38
  const pkg = NPM_INSTALLABLE[agent];
@@ -19,6 +19,7 @@ import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
19
19
  import { resolveSecretEnv } from "./secretEnv.js";
20
20
  import { extractTemplateVarNames } from "./envVarNames.js";
21
21
  export const DEFAULT_CONNECT_TIMEOUT_MS = 10_000;
22
+ const FAILED_TRANSPORT_CLEANUP_TIMEOUT_MS = 1_000;
22
23
  /**
23
24
  * A hanging server (process alive, protocol response never sent) leaves
24
25
  * `promise` permanently unsettled — indistinguishable from "still
@@ -55,7 +56,10 @@ export async function connectWithCleanup(client, transport, timeoutMs, message)
55
56
  }
56
57
  catch (err) {
57
58
  try {
58
- await transport.close();
59
+ // A failed HTTP transport can itself hang while closing. Cleanup is
60
+ // best-effort and must not turn one bad upstream into a Gateway-wide
61
+ // startup timeout; preserve the original connect error below.
62
+ await withTimeout(Promise.resolve().then(() => transport.close()), FAILED_TRANSPORT_CLEANUP_TIMEOUT_MS, "transport cleanup timed out");
59
63
  }
60
64
  catch {
61
65
  // best-effort; the original connect failure is what matters
@@ -37,6 +37,7 @@ export interface McpMigrateReadResult {
37
37
  export declare function resolvedEnvTextMap(def: Pick<McpServerDef, "env" | "envAliases" | "staticEnv">): Record<string, string>;
38
38
  export declare function readClaudeCodeMcpDefs(homeDir: string): McpMigrateReadResult;
39
39
  export declare function readKiroMcpDefs(homeDir: string): McpMigrateReadResult;
40
+ export declare function readKimiCodeMcpDefs(homeDir: string): McpMigrateReadResult;
40
41
  export interface CodexMcpEntryRich {
41
42
  name: string;
42
43
  enabled: boolean;
@@ -108,6 +108,9 @@ export function readClaudeCodeMcpDefs(homeDir) {
108
108
  export function readKiroMcpDefs(homeDir) {
109
109
  return readRichJsonMcpDefs(join(homeDir, ".kiro", "settings", "mcp.json"));
110
110
  }
111
+ export function readKimiCodeMcpDefs(homeDir) {
112
+ return readRichJsonMcpDefs(join(homeDir, ".kimi-code", "mcp.json"));
113
+ }
111
114
  /**
112
115
  * Pure conversion half — separated from the subprocess/file-read effects
113
116
  * below so the stdio/non-stdio/malformed decision logic is unit-testable
@@ -0,0 +1,46 @@
1
+ import { Server } from "@modelcontextprotocol/sdk/server/index.js";
2
+ import type { CallToolResult, GetPromptResult, Implementation, Prompt, ReadResourceResult, Resource, Tool } from "@modelcontextprotocol/sdk/types.js";
3
+ import type { AgentId } from "../core/types.js";
4
+ import type { GatewayBackend } from "./gatewayBackend.js";
5
+ export interface RuntimeContext {
6
+ agentId: AgentId;
7
+ homeDir: string;
8
+ }
9
+ export interface TrellisProvider {
10
+ readonly id: string;
11
+ listTools?(context: RuntimeContext): Promise<Tool[]> | Tool[];
12
+ callTool(name: string, args: unknown, context: RuntimeContext): Promise<CallToolResult>;
13
+ listResources?(context: RuntimeContext): Promise<Resource[]> | Resource[];
14
+ readResource?(uri: URL, context: RuntimeContext): Promise<ReadResourceResult> | ReadResourceResult;
15
+ listPrompts?(context: RuntimeContext): Promise<Prompt[]> | Prompt[];
16
+ getPrompt?(name: string, args: Record<string, string> | undefined, context: RuntimeContext): Promise<GetPromptResult> | GetPromptResult;
17
+ close?(): Promise<void>;
18
+ }
19
+ export declare class BuiltinRegistry {
20
+ private readonly providers;
21
+ private readonly warn;
22
+ private readonly toolOwners;
23
+ private readonly originalToolOwners;
24
+ private readonly exposedToOriginal;
25
+ private readonly resourceOwners;
26
+ private readonly promptOwners;
27
+ constructor(providers: readonly TrellisProvider[], warn?: (message: string) => void);
28
+ listTools(context: RuntimeContext): Promise<Tool[]>;
29
+ callTool(name: string, args: unknown, context: RuntimeContext): Promise<CallToolResult>;
30
+ listResources(context: RuntimeContext): Promise<Resource[]>;
31
+ readResource(uri: URL, context: RuntimeContext): Promise<ReadResourceResult>;
32
+ listPrompts(context: RuntimeContext): Promise<Prompt[]>;
33
+ getPrompt(name: string, args: Record<string, string> | undefined, context: RuntimeContext): Promise<GetPromptResult>;
34
+ close(): Promise<void>;
35
+ }
36
+ /** Adapts the existing gateway tool backend without duplicating its routing
37
+ * or upstream connection logic. */
38
+ export declare class UpstreamProvider implements TrellisProvider {
39
+ private readonly backend;
40
+ readonly id = "upstream";
41
+ constructor(backend: GatewayBackend);
42
+ listTools(): Promise<Tool[]>;
43
+ callTool(name: string, args: unknown): Promise<CallToolResult>;
44
+ close(): Promise<void>;
45
+ }
46
+ export declare function createRuntimeServer(serverInfo: Implementation, context: RuntimeContext, registry: BuiltinRegistry): Server;
@@ -0,0 +1,185 @@
1
+ import { Server } from "@modelcontextprotocol/sdk/server/index.js";
2
+ import { CallToolRequestSchema, GetPromptRequestSchema, ListPromptsRequestSchema, ListResourcesRequestSchema, ListToolsRequestSchema, ReadResourceRequestSchema } from "@modelcontextprotocol/sdk/types.js";
3
+ import { allocateExposedToolNames } from "./mcpToolRegistry.js";
4
+ function compactBuiltinToolName(name) {
5
+ return name.startsWith("trellis.") ? name.slice("trellis.".length).replaceAll(".", "_") : name;
6
+ }
7
+ function providerError(provider, error) {
8
+ return `trellis-mcp-runtime: provider "${provider}" failed: ${error instanceof Error ? error.message : String(error)}`;
9
+ }
10
+ export class BuiltinRegistry {
11
+ providers;
12
+ warn;
13
+ toolOwners = new Map();
14
+ originalToolOwners = new Map();
15
+ exposedToOriginal = new Map();
16
+ resourceOwners = new Map();
17
+ promptOwners = new Map();
18
+ constructor(providers, warn = console.error) {
19
+ this.providers = providers;
20
+ this.warn = warn;
21
+ }
22
+ async listTools(context) {
23
+ const candidates = [];
24
+ this.toolOwners.clear();
25
+ this.originalToolOwners.clear();
26
+ this.exposedToOriginal.clear();
27
+ for (const provider of this.providers) {
28
+ if (!provider.listTools)
29
+ continue;
30
+ try {
31
+ for (const tool of await provider.listTools(context)) {
32
+ candidates.push({ provider, tool });
33
+ }
34
+ }
35
+ catch (err) {
36
+ this.warn(providerError(provider.id, err));
37
+ }
38
+ }
39
+ const exposedNames = allocateExposedToolNames(candidates.map(({ provider, tool }) => ({
40
+ serverName: provider.id,
41
+ toolName: provider.id === "upstream" ? tool.name : compactBuiltinToolName(tool.name),
42
+ })));
43
+ return candidates.map(({ provider, tool }, index) => {
44
+ const exposedName = exposedNames[index];
45
+ this.toolOwners.set(exposedName, provider);
46
+ this.exposedToOriginal.set(exposedName, tool.name);
47
+ if (!this.originalToolOwners.has(tool.name))
48
+ this.originalToolOwners.set(tool.name, provider);
49
+ const title = exposedName === tool.name || tool.title !== undefined
50
+ ? tool.title
51
+ : `${provider.id} / ${tool.name}`;
52
+ return { ...tool, name: exposedName, ...(title === undefined ? {} : { title }) };
53
+ });
54
+ }
55
+ async callTool(name, args, context) {
56
+ let provider = this.toolOwners.get(name);
57
+ let originalName = this.exposedToOriginal.get(name) ?? name;
58
+ if (!provider) {
59
+ await this.listTools(context);
60
+ provider = this.toolOwners.get(name);
61
+ originalName = this.exposedToOriginal.get(name) ?? name;
62
+ // Keep direct programmatic calls using the logical pre-normalization
63
+ // name working, while never advertising an invalid alias to an Agent.
64
+ if (!provider)
65
+ provider = this.originalToolOwners.get(name);
66
+ }
67
+ if (!provider)
68
+ return { content: [{ type: "text", text: `unknown Trellis tool "${name}"` }], isError: true };
69
+ try {
70
+ return await provider.callTool(originalName, args, context);
71
+ }
72
+ catch (err) {
73
+ return { content: [{ type: "text", text: providerError(provider.id, err) }], isError: true };
74
+ }
75
+ }
76
+ async listResources(context) {
77
+ const resources = [];
78
+ this.resourceOwners.clear();
79
+ for (const provider of this.providers) {
80
+ if (!provider.listResources)
81
+ continue;
82
+ try {
83
+ for (const resource of await provider.listResources(context)) {
84
+ if (this.resourceOwners.has(resource.uri)) {
85
+ this.warn(`trellis-mcp-runtime: duplicate resource "${resource.uri}" from provider "${provider.id}"`);
86
+ continue;
87
+ }
88
+ this.resourceOwners.set(resource.uri, provider);
89
+ resources.push(resource);
90
+ }
91
+ }
92
+ catch (err) {
93
+ this.warn(providerError(provider.id, err));
94
+ }
95
+ }
96
+ return resources;
97
+ }
98
+ async readResource(uri, context) {
99
+ const key = uri.toString();
100
+ let provider = this.resourceOwners.get(key);
101
+ if (!provider) {
102
+ await this.listResources(context);
103
+ provider = this.resourceOwners.get(key);
104
+ }
105
+ if (!provider?.readResource)
106
+ throw new Error(`unknown Trellis resource "${key}"`);
107
+ return provider.readResource(uri, context);
108
+ }
109
+ async listPrompts(context) {
110
+ const prompts = [];
111
+ this.promptOwners.clear();
112
+ for (const provider of this.providers) {
113
+ if (!provider.listPrompts)
114
+ continue;
115
+ try {
116
+ for (const prompt of await provider.listPrompts(context)) {
117
+ if (this.promptOwners.has(prompt.name)) {
118
+ this.warn(`trellis-mcp-runtime: duplicate prompt "${prompt.name}" from provider "${provider.id}"`);
119
+ continue;
120
+ }
121
+ this.promptOwners.set(prompt.name, provider);
122
+ prompts.push(prompt);
123
+ }
124
+ }
125
+ catch (err) {
126
+ this.warn(providerError(provider.id, err));
127
+ }
128
+ }
129
+ return prompts;
130
+ }
131
+ async getPrompt(name, args, context) {
132
+ let provider = this.promptOwners.get(name);
133
+ if (!provider) {
134
+ await this.listPrompts(context);
135
+ provider = this.promptOwners.get(name);
136
+ }
137
+ if (!provider?.getPrompt)
138
+ throw new Error(`unknown Trellis prompt "${name}"`);
139
+ return provider.getPrompt(name, args, context);
140
+ }
141
+ async close() {
142
+ await Promise.all(this.providers.map(async (provider) => {
143
+ try {
144
+ await provider.close?.();
145
+ }
146
+ catch (err) {
147
+ this.warn(providerError(provider.id, err));
148
+ }
149
+ }));
150
+ }
151
+ }
152
+ /** Adapts the existing gateway tool backend without duplicating its routing
153
+ * or upstream connection logic. */
154
+ export class UpstreamProvider {
155
+ backend;
156
+ id = "upstream";
157
+ constructor(backend) {
158
+ this.backend = backend;
159
+ }
160
+ async listTools() {
161
+ return (await this.backend.listTools());
162
+ }
163
+ async callTool(name, args) {
164
+ return (await this.backend.callTool(name, args));
165
+ }
166
+ async close() {
167
+ await this.backend.close();
168
+ }
169
+ }
170
+ export function createRuntimeServer(serverInfo, context, registry) {
171
+ const server = new Server(serverInfo, {
172
+ capabilities: {
173
+ tools: { listChanged: true },
174
+ resources: { listChanged: true },
175
+ prompts: { listChanged: true },
176
+ },
177
+ });
178
+ server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: await registry.listTools(context) }));
179
+ server.setRequestHandler(CallToolRequestSchema, async (request) => registry.callTool(request.params.name, request.params.arguments, context));
180
+ server.setRequestHandler(ListResourcesRequestSchema, async () => ({ resources: await registry.listResources(context) }));
181
+ server.setRequestHandler(ReadResourceRequestSchema, async (request) => registry.readResource(new URL(request.params.uri), context));
182
+ server.setRequestHandler(ListPromptsRequestSchema, async () => ({ prompts: await registry.listPrompts(context) }));
183
+ server.setRequestHandler(GetPromptRequestSchema, async (request) => registry.getPrompt(request.params.name, request.params.arguments, context));
184
+ return server;
185
+ }
@@ -0,0 +1,10 @@
1
+ import type { CallToolResult, Tool } from "@modelcontextprotocol/sdk/types.js";
2
+ import type { GatewayBackend } from "./gatewayBackend.js";
3
+ import type { RuntimeContext, TrellisProvider } from "./mcpRuntime.js";
4
+ export declare class McpStatusProvider implements TrellisProvider {
5
+ private readonly backend;
6
+ readonly id = "mcp";
7
+ constructor(backend: GatewayBackend);
8
+ listTools(): Tool[];
9
+ callTool(name: string, _args: unknown, _context: RuntimeContext): Promise<CallToolResult>;
10
+ }
@@ -0,0 +1,25 @@
1
+ const STATUS_TOOL = {
2
+ name: "trellis.mcp.status",
3
+ description: "Show MCP upstream availability and the safe next action for authentication or configuration failures.",
4
+ inputSchema: { type: "object", properties: {}, additionalProperties: false },
5
+ annotations: { readOnlyHint: true, destructiveHint: false, openWorldHint: false },
6
+ };
7
+ export class McpStatusProvider {
8
+ backend;
9
+ id = "mcp";
10
+ constructor(backend) {
11
+ this.backend = backend;
12
+ }
13
+ listTools() {
14
+ return [STATUS_TOOL];
15
+ }
16
+ async callTool(name, _args, _context) {
17
+ if (name !== STATUS_TOOL.name)
18
+ return { content: [{ type: "text", text: `unknown MCP status tool "${name}"` }], isError: true };
19
+ const statuses = this.backend.listStatus?.() ?? [];
20
+ return {
21
+ content: [{ type: "text", text: JSON.stringify({ servers: statuses }, null, 2) }],
22
+ structuredContent: { servers: statuses },
23
+ };
24
+ }
25
+ }
@@ -33,6 +33,11 @@ export interface UpstreamTool {
33
33
  [key: string]: unknown;
34
34
  }
35
35
  export type AggregatedTool = UpstreamTool;
36
+ /** The strictest Agent-facing tool-name contract currently exercised by the
37
+ * local stack (notably the Codex-compatible model tool surface). */
38
+ export declare const MAX_EXPOSED_TOOL_NAME_LENGTH = 64;
39
+ export declare const SHORT_EXPOSED_TOOL_NAME_LENGTH = 12;
40
+ export declare const EXPOSED_TOOL_NAME_PATTERN: RegExp;
36
41
  /**
37
42
  * `${serverName}__${toolName}`, the separator every consumer of this
38
43
  * registry shares. Two underscores rather than one because single
@@ -41,7 +46,17 @@ export type AggregatedTool = UpstreamTool;
41
46
  * gateway matching it keeps one naming convention across both consumers.
42
47
  */
43
48
  export declare const TOOL_NAME_SEPARATOR = "__";
49
+ /** Normalize one raw tool name without adding a server prefix. */
50
+ export declare function normalizedToolName(toolName: string): string;
44
51
  export declare function prefixedToolName(serverName: string, toolName: string): string;
52
+ export interface ToolNameCandidate {
53
+ serverName: string;
54
+ toolName: string;
55
+ }
56
+ /** Allocate all exposed names together so normalization collisions are
57
+ * deterministic and no upstream tool is silently lost. The returned array
58
+ * is indexed like `candidates`. */
59
+ export declare function allocateExposedToolNames(candidates: readonly ToolNameCandidate[]): string[];
45
60
  export interface AddResult {
46
61
  /** Prefixed names actually registered by this call. */
47
62
  added: string[];
@@ -58,6 +73,8 @@ export interface AddResult {
58
73
  export declare class McpToolRegistry {
59
74
  private readonly entries;
60
75
  private readonly order;
76
+ private readonly candidates;
77
+ private rebuild;
61
78
  /**
62
79
  * Register one connected upstream's tools. Callers add each upstream
63
80
  * independently, so an upstream that failed to connect or to list its
@@ -65,6 +82,7 @@ export declare class McpToolRegistry {
65
82
  * aggregated, with no special-casing here.
66
83
  */
67
84
  add(serverName: string, upstream: ToolUpstream, tools: readonly AggregatedTool[]): AddResult;
85
+ private exposedNameFor;
68
86
  /** Every registered tool, under its prefixed name, in registration
69
87
  * order. Each tool's own `description`/`inputSchema` and any additional
70
88
  * fields the upstream sent are preserved verbatim; only `name` changes. */