operon-agents-core 0.1.0-alpha.33 → 0.1.0-alpha.34

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.
@@ -69,6 +69,8 @@ export interface ServerFromConfigOptions {
69
69
  readonly transport?: MCPTransport;
70
70
  /** A provider's answer for this connect. Takes the place of the OAuth service. */
71
71
  readonly credential?: McpCredential;
72
+ /** The workspace directories the server is told about (MCP `roots`). See {@link McpServersCapabilityOptions.roots}. */
73
+ readonly roots?: readonly string[];
72
74
  }
73
75
  export declare function serverFromConfig(name: string, rawConfig: McpServerConfig, opts?: ServerFromConfigOptions): TransportMCPServer;
74
76
  /** The OAuth client/scopes a server config pins (Codex `.mcp.json` `oauth` / `scopes`). */
@@ -85,6 +87,14 @@ export interface McpServersCapabilityOptions {
85
87
  readonly allowedTransports?: readonly McpTransportKind[];
86
88
  readonly cacheToolsList?: boolean;
87
89
  readonly transportFactory?: McpTransportFactory;
90
+ /**
91
+ * The workspace these servers serve, as absolute directories. Each server is told about them
92
+ * through MCP `roots` (the client declares the capability and answers `roots/list`), and an
93
+ * stdio server with no `cwd` of its own is started in the first one. Omitted: no `roots`
94
+ * capability, and an stdio server inherits the host's cwd — right for a process-wide set that
95
+ * serves every directory, wrong for one bound to a workspace.
96
+ */
97
+ readonly roots?: readonly string[];
88
98
  readonly reconnectPolicy?: ReconnectPolicy;
89
99
  readonly keepAlive?: KeepAlivePolicy;
90
100
  readonly timer?: McpTimer;
@@ -88,7 +88,7 @@ export function serverFromConfig(name, rawConfig, opts = {}) {
88
88
  const { config, error } = normalizeServerConfig(name, rawConfig);
89
89
  if (error !== undefined)
90
90
  throw new Error(error);
91
- const transport = opts.transport ?? transportFromConfig(name, config, opts.oauthService, opts.credential);
91
+ const transport = opts.transport ?? transportFromConfig(name, config, opts.oauthService, opts.credential, opts.roots);
92
92
  const filter = staticFilterFromConfig(config);
93
93
  return new TransportMCPServer({
94
94
  name,
@@ -108,17 +108,22 @@ function staticFilterFromConfig(config) {
108
108
  ...(blocked !== undefined ? { blockedToolNames: blocked } : {}),
109
109
  };
110
110
  }
111
- function transportFromConfig(name, config, oauthService, credential) {
111
+ function transportFromConfig(name, config, oauthService, credential, roots) {
112
112
  if (config.transport === "stdio") {
113
113
  if (config.command === undefined)
114
114
  throw new Error(`MCP server "${name}": stdio transport requires "command".`);
115
+ // A server with no cwd of its own runs in the workspace: it resolves a relative path the
116
+ // model hands it against its process cwd, and the host's cwd (`/` for an app launched from
117
+ // the Finder) is no place to write `test-results/shot.png` to.
118
+ const cwd = config.cwd ?? roots?.[0];
115
119
  return mcpStdioTransport({
116
120
  name,
117
121
  command: config.command,
118
122
  ...(config.args !== undefined ? { args: config.args } : {}),
119
123
  ...(config.env !== undefined ? { env: config.env } : {}),
120
- ...(config.cwd !== undefined ? { cwd: config.cwd } : {}),
124
+ ...(cwd !== undefined ? { cwd } : {}),
121
125
  ...(config.toolTimeoutMs !== undefined ? { toolTimeoutMs: config.toolTimeoutMs } : {}),
126
+ ...(roots !== undefined ? { roots } : {}),
122
127
  });
123
128
  }
124
129
  if (config.url === undefined)
@@ -132,6 +137,7 @@ function transportFromConfig(name, config, oauthService, credential) {
132
137
  ...(config.bearerTokenEnvVar !== undefined ? { bearerTokenEnvVar: config.bearerTokenEnvVar } : {}),
133
138
  ...(config.toolTimeoutMs !== undefined ? { toolTimeoutMs: config.toolTimeoutMs } : {}),
134
139
  ...(oauthProvider !== undefined ? { oauthProvider } : {}),
140
+ ...(roots !== undefined ? { roots } : {}),
135
141
  });
136
142
  }
137
143
  function resolveOAuthProvider(name, config, oauthService) {
@@ -176,6 +182,7 @@ class McpServerController {
176
182
  credentials;
177
183
  cacheToolsList;
178
184
  transportFactory;
185
+ roots;
179
186
  onStatusChange;
180
187
  reconnectPolicy;
181
188
  keepAlivePolicy;
@@ -197,6 +204,7 @@ class McpServerController {
197
204
  this.credentials = opts.credentials;
198
205
  this.cacheToolsList = opts.cacheToolsList;
199
206
  this.transportFactory = opts.transportFactory;
207
+ this.roots = opts.roots;
200
208
  this.onStatusChange = opts.onStatusChange;
201
209
  this.reconnectPolicy = opts.reconnectPolicy;
202
210
  this.keepAlivePolicy = opts.keepAlivePolicy;
@@ -259,6 +267,7 @@ class McpServerController {
259
267
  ...(this.oauthService !== undefined ? { oauthService: this.oauthService } : {}),
260
268
  cacheToolsList: this.cacheToolsList,
261
269
  ...(credential !== undefined ? { credential } : {}),
270
+ ...(this.roots !== undefined ? { roots: this.roots } : {}),
262
271
  ...(this.transportFactory !== undefined ? { transport: this.transportFactory(this.name, this.config, credential) } : {}),
263
272
  });
264
273
  const mine = server;
@@ -534,6 +543,7 @@ export function createMcpServers(configs, options = {}) {
534
543
  ...(options.credentials !== undefined ? { credentials: options.credentials } : {}),
535
544
  cacheToolsList: options.cacheToolsList ?? DEFAULT_CACHE_TOOLS,
536
545
  ...(options.transportFactory !== undefined ? { transportFactory: options.transportFactory } : {}),
546
+ ...(options.roots !== undefined ? { roots: options.roots } : {}),
537
547
  ...(options.reconnectPolicy !== undefined ? { reconnectPolicy: options.reconnectPolicy } : {}),
538
548
  ...keepAliveOptions(config, options.keepAlive),
539
549
  onStatusChange: notify,
@@ -7,6 +7,8 @@ export interface MCPStdioConfig {
7
7
  readonly env?: Record<string, string>;
8
8
  readonly cwd?: string;
9
9
  readonly toolTimeoutMs?: number;
10
+ /** Absolute directories the server may treat as the workspace (MCP `roots`). */
11
+ readonly roots?: readonly string[];
10
12
  }
11
13
  export declare function mcpStdioTransport(config: MCPStdioConfig): MCPTransport;
12
14
  export declare function mergeStdioEnv(configEnv?: Record<string, string>): Record<string, string>;
@@ -18,6 +20,8 @@ export interface MCPHttpConfig {
18
20
  readonly toolTimeoutMs?: number;
19
21
  readonly oauthProvider?: OAuthClientProvider;
20
22
  readonly envLookup?: (name: string) => string | undefined;
23
+ /** Absolute directories the server may treat as the workspace (MCP `roots`). */
24
+ readonly roots?: readonly string[];
21
25
  }
22
26
  export declare function mcpStreamableHttpTransport(config: MCPHttpConfig): MCPTransport;
23
27
  export declare function isTerminalTransportError(error: Error): boolean;
@@ -1,8 +1,21 @@
1
1
  import { Client } from "@modelcontextprotocol/sdk/client/index.js";
2
2
  import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
3
3
  import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
4
+ import { ListRootsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
5
+ import { basename } from "node:path";
6
+ import { pathToFileURL } from "node:url";
4
7
  const CLIENT_INFO = { name: "operon", version: "0.0.0" };
5
8
  const STDERR_BUFFER_CAPACITY = 4 * 1024;
9
+ /**
10
+ * The client side of MCP `roots`: the directories a server may treat as the workspace. A server
11
+ * that checks paths against roots (chrome-devtools-mcp writing a screenshot, a filesystem server)
12
+ * otherwise falls back to its own default — usually the OS temp directory only — so a client that
13
+ * never declares the capability gets "path is not within any of the configured workspace roots"
14
+ * for every path in the project it is working on.
15
+ */
16
+ function rootsResult(roots) {
17
+ return { roots: roots.map((dir) => ({ uri: pathToFileURL(dir).href, name: basename(dir) || dir })) };
18
+ }
6
19
  class SdkMcpTransport {
7
20
  name;
8
21
  cfg;
@@ -33,7 +46,12 @@ class SdkMcpTransport {
33
46
  const sdkTransport = this.cfg.makeSdkTransport();
34
47
  if (this.cfg.captureStderr)
35
48
  this.attachStderr(sdkTransport);
36
- const client = new Client(CLIENT_INFO, { capabilities: {} });
49
+ const roots = this.cfg.roots;
50
+ // The roots are fixed for the transport's lifetime, so `listChanged` is never sent.
51
+ const capabilities = roots !== undefined ? { roots: { listChanged: false } } : {};
52
+ const client = new Client(CLIENT_INFO, { capabilities });
53
+ if (roots !== undefined)
54
+ client.setRequestHandler(ListRootsRequestSchema, () => rootsResult(roots));
37
55
  this.client = client;
38
56
  // Hooks BEFORE the handshake so an onclose firing between handshake completion and our
39
57
  // wiring is never lost; the hooks themselves gate on `ready`.
@@ -177,6 +195,7 @@ export function mcpStdioTransport(config) {
177
195
  return new SdkMcpTransport({
178
196
  name: config.name,
179
197
  captureStderr: true,
198
+ ...(config.roots !== undefined ? { roots: config.roots } : {}),
180
199
  ...(config.toolTimeoutMs !== undefined ? { toolTimeoutMs: config.toolTimeoutMs } : {}),
181
200
  makeSdkTransport: () => new StdioClientTransport({
182
201
  command: config.command,
@@ -204,6 +223,7 @@ export function mcpStreamableHttpTransport(config) {
204
223
  name: config.name,
205
224
  captureStderr: false,
206
225
  isTerminalError: isTerminalTransportError,
226
+ ...(config.roots !== undefined ? { roots: config.roots } : {}),
207
227
  ...(config.toolTimeoutMs !== undefined ? { toolTimeoutMs: config.toolTimeoutMs } : {}),
208
228
  makeSdkTransport: () => new StreamableHTTPClientTransport(new URL(config.url), {
209
229
  ...(headers !== undefined ? { requestInit: { headers } } : {}),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "operon-agents-core",
3
- "version": "0.1.0-alpha.33",
3
+ "version": "0.1.0-alpha.34",
4
4
  "description": "Internal engine for operon-agents. Prefer operon-agents for application code.",
5
5
  "type": "module",
6
6
  "engines": {