@letta-ai/letta-agent-sdk 0.5.6 → 0.5.7

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/README.md CHANGED
@@ -123,27 +123,44 @@ Local execution (embedded Letta Code harness / app server) requires Node.js 22.1
123
123
 
124
124
  ### MCP tools
125
125
 
126
- Pass stdio MCP servers as session options. The SDK starts them in the session
127
- cwd, namespaces their tools as `mcp__<server>__<tool>`, and exposes them through
128
- Letta Code's external-tool protocol. Servers are closed with the session.
126
+ Pass MCP servers by name in session options. The SDK supports local stdio,
127
+ Streamable HTTP, and legacy SSE transports. It namespaces discovered tools as
128
+ `mcp__<server>__<tool>` and exposes them through Letta Code's external-tool
129
+ protocol.
129
130
 
130
131
  ```ts
131
132
  await using session = client.createSession(agentId, {
132
133
  cwd: process.cwd(),
133
- mcpServers: [
134
- {
135
- name: "filesystem",
134
+ mcpServers: {
135
+ filesystem: {
136
136
  command: "npx",
137
137
  args: ["-y", "@modelcontextprotocol/server-filesystem", process.cwd()],
138
138
  },
139
- ],
139
+ exa: {
140
+ type: "http",
141
+ url: "https://mcp.exa.ai/mcp",
142
+ },
143
+ github: {
144
+ type: "http",
145
+ url: "https://api.githubcopilot.com/mcp/",
146
+ headers: {
147
+ Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
148
+ },
149
+ },
150
+ },
151
+ allowedTools: ["mcp__filesystem__*", "mcp__exa__*", "mcp__github__list_issues"],
140
152
  });
141
153
  ```
142
154
 
143
- MCP stdio processes require the Node package entry; they are not available from
144
- `@letta-ai/letta-agent-sdk/client` in browser or React Native environments. For
145
- a simple smoke test, the official `@modelcontextprotocol/server-everything`
146
- package runs over stdio with `npx -y @modelcontextprotocol/server-everything`.
155
+ Connections start concurrently during session initialization. A failed server
156
+ is skipped without dropping healthy servers. OAuth is host-managed, matching
157
+ the Claude Agent SDK: complete OAuth in your application and provide the access
158
+ token through `headers`. The SDK does not open an interactive browser flow.
159
+
160
+ MCP connections run in the Node SDK process and close with the session. This
161
+ includes MCP used by remote and Cloud sessions: stdio servers see the SDK host
162
+ filesystem, not a managed sandbox. MCP is unavailable from the portable
163
+ `@letta-ai/letta-agent-sdk/client` browser and React Native entry point.
147
164
 
148
165
  ### Managed Cloud sandbox repositories
149
166
 
@@ -67,12 +67,15 @@ export declare class AppServerSession extends RemoteClientSessionCore {
67
67
  private ownedConnection;
68
68
  private externalTools;
69
69
  private mcpBridge;
70
+ private mcpCleanup;
70
71
  private removeExternalToolHandler;
71
72
  private removeControlRequestHandler;
72
73
  constructor(remoteOptions: AppServerSessionOptions, mode: AppServerSessionMode);
73
74
  protected shouldEnableMemfs(options: LettaCodeClientSessionOptions | CreateAgentOptions): boolean;
74
75
  protected initializeRuntimeController(): Promise<RuntimeSessionInit>;
75
76
  protected onCoreClose(): void;
77
+ protected onCoreDisposed(): Promise<void>;
78
+ private closeMcpBridge;
76
79
  private resolveAppServerUrl;
77
80
  private startRuntime;
78
81
  private buildRuntimeStartCommand;
@@ -1 +1 @@
1
- {"version":3,"file":"app-server-session.d.ts","sourceRoot":"","sources":["../src/app-server-session.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,eAAe,EACpB,KAAK,gCAAgC,EAEtC,MAAM,wCAAwC,CAAC;AAsBhD,OAAO,EACL,uBAAuB,EAKvB,KAAK,eAAe,EACpB,KAAK,6BAA6B,EAClC,KAAK,sBAAsB,EAC3B,KAAK,YAAY,EACjB,KAAK,kBAAkB,EACvB,KAAK,kBAAkB,EACxB,MAAM,iCAAiC,CAAC;AACzC,OAAO,KAAK,EACV,YAAY,EACZ,iBAAiB,EACjB,kBAAkB,EAClB,kBAAkB,EAClB,4BAA4B,EAC5B,6BAA6B,EAC7B,gBAAgB,EAChB,mBAAmB,EACnB,kBAAkB,EAElB,8BAA8B,EAC9B,6BAA6B,EAC7B,WAAW,EACX,iBAAiB,EAClB,MAAM,YAAY,CAAC;AAWpB,KAAK,kBAAkB,GAAG;IACxB,QAAQ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B,SAAQ,uBAAuB;IASzD,OAAO,CAAC,QAAQ,CAAC,aAAa;IARhC,OAAO,CAAC,eAAe,CAAkC;IACzD,OAAO,CAAC,aAAa,CAAmC;IACxD,OAAO,CAAC,SAAS,CAA8B;IAC/C,OAAO,CAAC,UAAU,CAAoC;IACtD,OAAO,CAAC,yBAAyB,CAA6B;IAC9D,OAAO,CAAC,2BAA2B,CAA6B;gBAG7C,aAAa,EAAE,uBAAuB,EACvD,IAAI,EAAE,oBAAoB;cAYT,iBAAiB,CAAC,OAAO,EAAE,6BAA6B,GAAG,kBAAkB,GAAG,OAAO;cAKjF,2BAA2B,IAAI,OAAO,CAAC,kBAAkB,CAAC;cAiFhE,WAAW,IAAI,IAAI;cAUb,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC;IAIxD,OAAO,CAAC,cAAc;YAOR,mBAAmB;YAenB,YAAY;YAOZ,wBAAwB;YAiExB,0BAA0B;CAqBzC"}
@@ -8618,16 +8618,31 @@ function buildCanUseToolContext(request, requestId) {
8618
8618
  // src/mcp-runtime.ts
8619
8619
  var connector = null;
8620
8620
  async function connectMcpServers(servers, options = {}) {
8621
- if (!servers || servers.length === 0) {
8621
+ if (!servers || Object.keys(servers).length === 0) {
8622
8622
  return { tools: [], close: async () => {
8623
8623
  return;
8624
8624
  } };
8625
8625
  }
8626
8626
  if (!connector) {
8627
- throw new Error("MCP stdio servers require the Node package entry '@letta-ai/letta-agent-sdk'; they are not available from '@letta-ai/letta-agent-sdk/client'.");
8627
+ throw new Error("MCP servers require the Node package entry '@letta-ai/letta-agent-sdk'; they are not available from '@letta-ai/letta-agent-sdk/client'.");
8628
8628
  }
8629
8629
  return connector(servers, options);
8630
8630
  }
8631
+ function expandMcpToolWildcards(allowedTools, mcpTools) {
8632
+ if (allowedTools === undefined)
8633
+ return;
8634
+ const available = [...mcpTools];
8635
+ const expanded = [];
8636
+ for (const entry of allowedTools) {
8637
+ if (entry.startsWith("mcp__") && entry.endsWith("*")) {
8638
+ const prefix = entry.slice(0, -1);
8639
+ expanded.push(...available.filter((name) => name.startsWith(prefix)));
8640
+ } else {
8641
+ expanded.push(entry);
8642
+ }
8643
+ }
8644
+ return [...new Set(expanded)];
8645
+ }
8631
8646
 
8632
8647
  // src/remote-session-protocol.ts
8633
8648
  var FAILURE_STOP_REASONS = new Set([
@@ -9836,6 +9851,7 @@ class RemoteClientSessionCore {
9836
9851
  }
9837
9852
  async[Symbol.asyncDispose]() {
9838
9853
  this.close();
9854
+ await this.onCoreDisposed();
9839
9855
  }
9840
9856
  async changeDeviceState(updates) {
9841
9857
  if (!this.initialized) {
@@ -9877,6 +9893,7 @@ class RemoteClientSessionCore {
9877
9893
  async afterRuntimeInitialized() {}
9878
9894
  async beforeTurn() {}
9879
9895
  onCoreClose() {}
9896
+ async onCoreDisposed() {}
9880
9897
  currentOptions() {
9881
9898
  return this.mode.options;
9882
9899
  }
@@ -10126,7 +10143,8 @@ function createExternalToolCallHandler(externalTools) {
10126
10143
  ...part.text !== undefined ? { text: part.text } : {},
10127
10144
  ...part.data !== undefined ? { data: part.data } : {},
10128
10145
  ...part.mimeType !== undefined ? { mimeType: part.mimeType } : {}
10129
- }))
10146
+ })),
10147
+ ...result.isError === true ? { is_error: true } : {}
10130
10148
  };
10131
10149
  };
10132
10150
  }
@@ -10418,6 +10436,7 @@ class AppServerSession extends RemoteClientSessionCore {
10418
10436
  ownedConnection = null;
10419
10437
  externalTools = new Map;
10420
10438
  mcpBridge = null;
10439
+ mcpCleanup = Promise.resolve();
10421
10440
  removeExternalToolHandler = null;
10422
10441
  removeControlRequestHandler = null;
10423
10442
  constructor(remoteOptions, mode) {
@@ -10437,6 +10456,7 @@ class AppServerSession extends RemoteClientSessionCore {
10437
10456
  return options.memfs !== false;
10438
10457
  }
10439
10458
  async initializeRuntimeController() {
10459
+ await this.mcpCleanup;
10440
10460
  const url = await this.resolveAppServerUrl();
10441
10461
  const client = applyUniqueRequestIds(createAppServerClient({
10442
10462
  url,
@@ -10468,12 +10488,15 @@ class AppServerSession extends RemoteClientSessionCore {
10468
10488
  }
10469
10489
  const tools = agentToolNames(response.agent);
10470
10490
  const skillSources = this.currentOptions().skillSources;
10491
+ const mcpToolNames = this.mcpBridge?.tools.map((tool) => tool.name) ?? [];
10492
+ const allowedTools = expandMcpToolWildcards(this.currentOptions().allowedTools, mcpToolNames);
10493
+ const availableTools = tools === undefined && mcpToolNames.length === 0 ? undefined : [...tools ?? [], ...mcpToolNames];
10471
10494
  return {
10472
- controller: new AppServerRuntimeController(client, this.remoteOptions, this.currentOptions().allowedTools),
10495
+ controller: new AppServerRuntimeController(client, this.remoteOptions, allowedTools),
10473
10496
  runtime: response.runtime,
10474
10497
  model: typeof response.agent?.model === "string" ? response.agent.model : "",
10475
10498
  modelSettings: objectRecord(response.agent?.model_settings) ?? null,
10476
- ...tools !== undefined ? { tools } : {},
10499
+ ...availableTools !== undefined ? { tools: availableTools } : {},
10477
10500
  ...skillSources !== undefined ? { skillSources: [...skillSources] } : {}
10478
10501
  };
10479
10502
  } catch (error) {
@@ -10481,8 +10504,7 @@ class AppServerSession extends RemoteClientSessionCore {
10481
10504
  this.removeExternalToolHandler = null;
10482
10505
  this.removeControlRequestHandler?.();
10483
10506
  this.removeControlRequestHandler = null;
10484
- this.mcpBridge?.close();
10485
- this.mcpBridge = null;
10507
+ await this.closeMcpBridge();
10486
10508
  client.close();
10487
10509
  throw error;
10488
10510
  }
@@ -10492,11 +10514,20 @@ class AppServerSession extends RemoteClientSessionCore {
10492
10514
  this.removeExternalToolHandler = null;
10493
10515
  this.removeControlRequestHandler?.();
10494
10516
  this.removeControlRequestHandler = null;
10495
- this.mcpBridge?.close();
10496
- this.mcpBridge = null;
10517
+ this.mcpCleanup = this.closeMcpBridge();
10497
10518
  this.ownedConnection?.close();
10498
10519
  this.ownedConnection = null;
10499
10520
  }
10521
+ async onCoreDisposed() {
10522
+ await this.mcpCleanup;
10523
+ }
10524
+ closeMcpBridge() {
10525
+ const bridge = this.mcpBridge;
10526
+ this.mcpBridge = null;
10527
+ for (const tool of bridge?.tools ?? [])
10528
+ this.externalTools.delete(tool.name);
10529
+ return bridge?.close() ?? Promise.resolve();
10530
+ }
10500
10531
  async resolveAppServerUrl() {
10501
10532
  if (this.remoteOptions.url) {
10502
10533
  return this.remoteOptions.url;
@@ -11152,6 +11183,7 @@ class CloudEnvironmentSession extends RemoteClientSessionCore {
11152
11183
  removeControlRequestHandler = null;
11153
11184
  externalTools = new Map;
11154
11185
  mcpBridge = null;
11186
+ mcpCleanup = Promise.resolve();
11155
11187
  managedSandbox = null;
11156
11188
  sandboxRefreshTimer = null;
11157
11189
  sandboxRefreshInFlight = null;
@@ -11173,6 +11205,7 @@ class CloudEnvironmentSession extends RemoteClientSessionCore {
11173
11205
  this.externalTools = externalToolsByName(tools);
11174
11206
  }
11175
11207
  async initializeRuntimeController() {
11208
+ await this.mcpCleanup;
11176
11209
  const resolved = await this.resolveRuntime();
11177
11210
  const connection = await this.resolveConnectionForRuntime(resolved.runtime).catch(async (error) => {
11178
11211
  await this.cleanupSessionRepositories(resolved.runtime.agent_id, resolved.runtime.conversation_id);
@@ -11223,14 +11256,17 @@ class CloudEnvironmentSession extends RemoteClientSessionCore {
11223
11256
  }
11224
11257
  const tools = agentToolNames(response.agent);
11225
11258
  const skillSources = this.currentOptions().skillSources;
11259
+ const mcpToolNames = this.mcpBridge?.tools.map((tool) => tool.name) ?? [];
11260
+ const allowedTools = expandMcpToolWildcards(this.currentOptions().allowedTools, mcpToolNames);
11261
+ const availableTools = tools === undefined && mcpToolNames.length === 0 ? undefined : [...tools ?? [], ...mcpToolNames];
11226
11262
  return {
11227
11263
  controller: new AppServerRuntimeController(client, {
11228
11264
  requestTimeoutMs: this.cloudOptions.requestTimeoutMs ?? DEFAULT_TURN_TIMEOUT_MS
11229
- }, this.currentOptions().allowedTools),
11265
+ }, allowedTools),
11230
11266
  runtime: response.runtime,
11231
11267
  model: typeof response.agent?.model === "string" ? response.agent.model : "",
11232
11268
  modelSettings: response.agent?.model_settings ?? null,
11233
- ...tools !== undefined ? { tools } : {},
11269
+ ...availableTools !== undefined ? { tools: availableTools } : {},
11234
11270
  ...skillSources !== undefined ? { skillSources: [...skillSources] } : {}
11235
11271
  };
11236
11272
  } catch (error) {
@@ -11238,8 +11274,7 @@ class CloudEnvironmentSession extends RemoteClientSessionCore {
11238
11274
  this.removeExternalToolHandler = null;
11239
11275
  this.removeControlRequestHandler?.();
11240
11276
  this.removeControlRequestHandler = null;
11241
- this.mcpBridge?.close();
11242
- this.mcpBridge = null;
11277
+ await this.closeMcpBridge();
11243
11278
  client.close();
11244
11279
  await this.cleanupManagedSandbox();
11245
11280
  await this.cleanupSessionRepositories(resolved.runtime.agent_id, resolved.runtime.conversation_id);
@@ -11292,13 +11327,22 @@ class CloudEnvironmentSession extends RemoteClientSessionCore {
11292
11327
  this.removeExternalToolHandler = null;
11293
11328
  this.removeControlRequestHandler?.();
11294
11329
  this.removeControlRequestHandler = null;
11295
- this.mcpBridge?.close();
11296
- this.mcpBridge = null;
11330
+ this.mcpCleanup = this.closeMcpBridge();
11297
11331
  this.cleanupManagedSandbox();
11298
11332
  if (this.runtime?.agent_id) {
11299
11333
  this.cleanupSessionRepositories(this.runtime.agent_id, this.runtime.conversation_id);
11300
11334
  }
11301
11335
  }
11336
+ async onCoreDisposed() {
11337
+ await this.mcpCleanup;
11338
+ }
11339
+ closeMcpBridge() {
11340
+ const bridge = this.mcpBridge;
11341
+ this.mcpBridge = null;
11342
+ for (const tool of bridge?.tools ?? [])
11343
+ this.externalTools.delete(tool.name);
11344
+ return bridge?.close() ?? Promise.resolve();
11345
+ }
11302
11346
  async resolveRuntime() {
11303
11347
  let agentId = this.cloudMode.agentId;
11304
11348
  let conversationId = this.cloudMode.conversationId;
@@ -11819,8 +11863,36 @@ function validateReasoningEffort(value) {
11819
11863
  throw new Error(`Invalid reasoningEffort '${String(value)}'. Valid values: ${VALID_REASONING_EFFORTS.join(", ")}`);
11820
11864
  }
11821
11865
  }
11866
+ function validateMcpServers(servers) {
11867
+ if (servers === undefined)
11868
+ return;
11869
+ if (!servers || typeof servers !== "object" || Array.isArray(servers)) {
11870
+ throw new Error("Invalid mcpServers. Expected an object keyed by server name.");
11871
+ }
11872
+ for (const [name, config] of Object.entries(servers)) {
11873
+ if (name.length === 0) {
11874
+ throw new Error("Invalid mcpServers. Server names must be non-empty.");
11875
+ }
11876
+ if (!config || typeof config !== "object" || Array.isArray(config)) {
11877
+ throw new Error(`Invalid MCP server '${name}'. Expected a configuration object.`);
11878
+ }
11879
+ if (config.type === "http" || config.type === "sse") {
11880
+ if (typeof config.url !== "string" || config.url.length === 0) {
11881
+ throw new Error(`Invalid MCP server '${name}'. Expected a non-empty url.`);
11882
+ }
11883
+ continue;
11884
+ }
11885
+ if (config.type !== undefined && config.type !== "stdio") {
11886
+ throw new Error(`Invalid MCP server '${name}' type '${String(config.type)}'. Valid values: stdio, http, sse.`);
11887
+ }
11888
+ if (typeof config.command !== "string" || config.command.length === 0) {
11889
+ throw new Error(`Invalid MCP server '${name}'. Expected a non-empty command.`);
11890
+ }
11891
+ }
11892
+ }
11822
11893
  function validateCreateSessionOptions(options) {
11823
11894
  validateSkillSources(options.skillSources);
11895
+ validateMcpServers(options.mcpServers);
11824
11896
  validateReasoningEffort(options.reasoningEffort);
11825
11897
  validateDreamingOptions(options.dreaming);
11826
11898
  validateApprovalRecoveryOptions(options);
@@ -12249,4 +12321,4 @@ export {
12249
12321
  CloudManagedSandboxExpiredError
12250
12322
  };
12251
12323
 
12252
- //# debugId=6301765BD48499AE64756E2164756E21
12324
+ //# debugId=713A28441574387164756E2164756E21