@multi-agent-protocol/sdk 0.1.4 → 0.1.6

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/dist/index.cjs CHANGED
@@ -232,6 +232,7 @@ var TASK_METHODS = {
232
232
  var NOTIFICATION_METHODS = {
233
233
  EVENT: "map/event",
234
234
  MESSAGE: "map/message",
235
+ SEND: "map/send",
235
236
  /** Client acknowledges received events (for backpressure) */
236
237
  SUBSCRIBE_ACK: "map/subscribe.ack",
237
238
  /** Server notifies client that auth is about to expire */
@@ -3720,7 +3721,8 @@ var ClientConnection = class _ClientConnection {
3720
3721
  }
3721
3722
  break;
3722
3723
  }
3723
- case NOTIFICATION_METHODS.MESSAGE: {
3724
+ case NOTIFICATION_METHODS.MESSAGE:
3725
+ case "map/send": {
3724
3726
  break;
3725
3727
  }
3726
3728
  default:
@@ -3873,7 +3875,9 @@ var AgentConnection = class _AgentConnection {
3873
3875
  constructor(stream, options = {}) {
3874
3876
  this.#connection = new BaseConnection(stream, options);
3875
3877
  this.#options = options;
3876
- this.#connection.setNotificationHandler(this.#handleNotification.bind(this));
3878
+ this.#connection.setNotificationHandler(
3879
+ this.#handleNotification.bind(this)
3880
+ );
3877
3881
  if (options.reconnection?.enabled && options.createStream) {
3878
3882
  this.#connection.onStateChange((newState) => {
3879
3883
  if (newState === "closed" && this.#connected && !this.#isReconnecting) {
@@ -3942,6 +3946,52 @@ var AgentConnection = class _AgentConnection {
3942
3946
  await agent.connect({ auth: options?.auth });
3943
3947
  return agent;
3944
3948
  }
3949
+ /**
3950
+ * Create an AgentConnection over WebSocket without performing the MAP handshake.
3951
+ *
3952
+ * Use this when you need to control the connection flow — e.g., to handle
3953
+ * server-driven auth negotiation before registration.
3954
+ *
3955
+ * @example
3956
+ * ```typescript
3957
+ * const agent = await AgentConnection.createConnection('ws://localhost:8080', {
3958
+ * name: 'Worker',
3959
+ * role: 'processor',
3960
+ * });
3961
+ *
3962
+ * const result = await agent.connectOnly();
3963
+ * if (result.authRequired) {
3964
+ * await agent.authenticate({ method: result.authRequired.methods[0], token: cred });
3965
+ * }
3966
+ * await agent.register();
3967
+ * ```
3968
+ */
3969
+ static async createConnection(url, options) {
3970
+ const parsedUrl = new URL(url);
3971
+ if (!["ws:", "wss:"].includes(parsedUrl.protocol)) {
3972
+ throw new Error(`Unsupported protocol: ${parsedUrl.protocol}. Use ws: or wss:`);
3973
+ }
3974
+ const timeout = options?.connectTimeout ?? 1e4;
3975
+ const ws = new WebSocket(url);
3976
+ await waitForOpen(ws, timeout);
3977
+ const stream = websocketStream(ws);
3978
+ const createStream = async () => {
3979
+ const newWs = new WebSocket(url);
3980
+ await waitForOpen(newWs, timeout);
3981
+ return websocketStream(newWs);
3982
+ };
3983
+ const reconnection = options?.reconnection === true ? { enabled: true } : typeof options?.reconnection === "object" ? options.reconnection : void 0;
3984
+ return new _AgentConnection(stream, {
3985
+ name: options?.name,
3986
+ role: options?.role,
3987
+ capabilities: options?.capabilities,
3988
+ visibility: options?.visibility,
3989
+ parent: options?.parent,
3990
+ scopes: options?.scopes,
3991
+ createStream,
3992
+ reconnection
3993
+ });
3994
+ }
3945
3995
  /**
3946
3996
  * Connect and register an agent via agentic-mesh transport.
3947
3997
  *
@@ -4038,6 +4088,65 @@ var AgentConnection = class _AgentConnection {
4038
4088
  this.#connection._transitionTo("connected");
4039
4089
  return { connection: connectResult, agent: registerResult.agent };
4040
4090
  }
4091
+ /**
4092
+ * Connect to the MAP system without registering an agent.
4093
+ *
4094
+ * Use this when the server may require authentication before registration.
4095
+ * After connecting, check `authRequired` in the response and call
4096
+ * `authenticate()` if needed, then `register()` to complete the handshake.
4097
+ *
4098
+ * @example
4099
+ * ```typescript
4100
+ * const result = await agent.connectOnly();
4101
+ *
4102
+ * if (result.authRequired) {
4103
+ * const method = result.authRequired.methods[0];
4104
+ * await agent.authenticate({ method, token: myCredential });
4105
+ * }
4106
+ *
4107
+ * await agent.register();
4108
+ * ```
4109
+ */
4110
+ async connectOnly(options) {
4111
+ const connectParams = {
4112
+ protocolVersion: PROTOCOL_VERSION,
4113
+ participantType: "agent",
4114
+ participantId: options?.agentId,
4115
+ name: this.#options.name,
4116
+ capabilities: this.#options.capabilities,
4117
+ resumeToken: options?.resumeToken,
4118
+ auth: options?.auth
4119
+ };
4120
+ const connectResult = await this.#connection.sendRequest(CORE_METHODS.CONNECT, connectParams);
4121
+ this.#sessionId = connectResult.sessionId;
4122
+ this.#serverCapabilities = connectResult.capabilities;
4123
+ this.#connected = true;
4124
+ this.#lastConnectOptions = options;
4125
+ return connectResult;
4126
+ }
4127
+ /**
4128
+ * Register as an agent after connecting.
4129
+ *
4130
+ * Call this after `connectOnly()` and optional `authenticate()` to complete
4131
+ * the connection handshake. Uses the options provided during construction
4132
+ * (name, role, scopes, etc.) unless overridden.
4133
+ */
4134
+ async register(overrides) {
4135
+ const registerParams = {
4136
+ agentId: overrides?.agentId ?? this.#lastConnectOptions?.agentId,
4137
+ name: overrides?.name ?? this.#options.name,
4138
+ role: overrides?.role ?? this.#options.role,
4139
+ parent: this.#options.parent,
4140
+ scopes: this.#options.scopes,
4141
+ visibility: this.#options.visibility,
4142
+ capabilities: this.#options.capabilities
4143
+ };
4144
+ const registerResult = await this.#connection.sendRequest(LIFECYCLE_METHODS.AGENTS_REGISTER, registerParams);
4145
+ this.#agentId = registerResult.agent.id;
4146
+ this.#currentState = registerResult.agent.state;
4147
+ this.#connection._transitionTo("connected");
4148
+ return registerResult.agent;
4149
+ }
4041
4150
  /**
4042
4151
  * Authenticate with the server after connection.
4043
4152
  *
@@ -4050,20 +4159,15 @@ var AgentConnection = class _AgentConnection {
4050
4159
  *
4051
4160
  * @example
4052
4161
  * ```typescript
4053
- * const agent = new AgentConnection(stream, { name: 'MyAgent' });
4054
- *
4055
- * // First connect to get auth requirements
4056
- * const connectResult = await agent.connectOnly();
4162
+ * const result = await agent.connectOnly();
4057
4163
  *
4058
- * if (connectResult.authRequired) {
4164
+ * if (result.authRequired) {
4059
4165
  * const authResult = await agent.authenticate({
4060
- * method: 'api-key',
4061
- * token: process.env.AGENT_API_KEY,
4166
+ * method: result.authRequired.methods[0],
4167
+ * token: myCredential,
4062
4168
  * });
4063
- *
4064
4169
  * if (authResult.success) {
4065
- * // Now register the agent
4066
- * await agent.register({ name: 'MyAgent', role: 'worker' });
4170
+ * await agent.register();
4067
4171
  * }
4068
4172
  * }
4069
4173
  * ```
@@ -4109,10 +4213,7 @@ var AgentConnection = class _AgentConnection {
4109
4213
  reason
4110
4214
  });
4111
4215
  }
4112
- const result = await this.#connection.sendRequest(
4113
- CORE_METHODS.DISCONNECT,
4114
- reason ? { reason } : void 0
4115
- );
4216
+ const result = await this.#connection.sendRequest(CORE_METHODS.DISCONNECT, reason ? { reason } : void 0);
4116
4217
  resumeToken = result.resumeToken;
4117
4218
  } finally {
4118
4219
  for (const subscription of this.#subscriptions.values()) {
@@ -4428,10 +4529,7 @@ var AgentConnection = class _AgentConnection {
4428
4529
  * @returns Created conversation and participant info
4429
4530
  */
4430
4531
  async createConversation(params) {
4431
- return this.#connection.sendRequest(
4432
- MAIL_METHODS.MAIL_CREATE,
4433
- params ?? {}
4434
- );
4532
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_CREATE, params ?? {});
4435
4533
  }
4436
4534
  /**
4437
4535
  * Get a conversation by ID with optional includes.
@@ -4441,10 +4539,7 @@ var AgentConnection = class _AgentConnection {
4441
4539
  * @returns Conversation details with requested includes
4442
4540
  */
4443
4541
  async getConversation(conversationId, include) {
4444
- return this.#connection.sendRequest(
4445
- MAIL_METHODS.MAIL_GET,
4446
- { conversationId, include }
4447
- );
4542
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_GET, { conversationId, include });
4448
4543
  }
4449
4544
  /**
4450
4545
  * List conversations with optional filters.
@@ -4453,10 +4548,7 @@ var AgentConnection = class _AgentConnection {
4453
4548
  * @returns Paginated list of conversations
4454
4549
  */
4455
4550
  async listConversations(params) {
4456
- return this.#connection.sendRequest(
4457
- MAIL_METHODS.MAIL_LIST,
4458
- params ?? {}
4459
- );
4551
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_LIST, params ?? {});
4460
4552
  }
4461
4553
  /**
4462
4554
  * Close a conversation.
@@ -4466,10 +4558,7 @@ var AgentConnection = class _AgentConnection {
4466
4558
  * @returns The closed conversation
4467
4559
  */
4468
4560
  async closeConversation(conversationId, reason) {
4469
- return this.#connection.sendRequest(
4470
- MAIL_METHODS.MAIL_CLOSE,
4471
- { conversationId, reason }
4472
- );
4561
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_CLOSE, { conversationId, reason });
4473
4562
  }
4474
4563
  /**
4475
4564
  * Join an existing conversation.
@@ -4478,10 +4567,7 @@ var AgentConnection = class _AgentConnection {
4478
4567
  * @returns Conversation, participant, and optional history
4479
4568
  */
4480
4569
  async joinConversation(params) {
4481
- return this.#connection.sendRequest(
4482
- MAIL_METHODS.MAIL_JOIN,
4483
- params
4484
- );
4570
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_JOIN, params);
4485
4571
  }
4486
4572
  /**
4487
4573
  * Leave a conversation.
@@ -4491,10 +4577,7 @@ var AgentConnection = class _AgentConnection {
4491
4577
  * @returns Leave confirmation with timestamp
4492
4578
  */
4493
4579
  async leaveConversation(conversationId, reason) {
4494
- return this.#connection.sendRequest(
4495
- MAIL_METHODS.MAIL_LEAVE,
4496
- { conversationId, reason }
4497
- );
4580
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_LEAVE, { conversationId, reason });
4498
4581
  }
4499
4582
  /**
4500
4583
  * Invite a participant to a conversation.
@@ -4503,10 +4586,7 @@ var AgentConnection = class _AgentConnection {
4503
4586
  * @returns Invite result
4504
4587
  */
4505
4588
  async inviteToConversation(params) {
4506
- return this.#connection.sendRequest(
4507
- MAIL_METHODS.MAIL_INVITE,
4508
- params
4509
- );
4589
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_INVITE, params);
4510
4590
  }
4511
4591
  /**
4512
4592
  * Record a turn (message) in a conversation.
@@ -4515,10 +4595,7 @@ var AgentConnection = class _AgentConnection {
4515
4595
  * @returns The created turn
4516
4596
  */
4517
4597
  async recordTurn(params) {
4518
- return this.#connection.sendRequest(
4519
- MAIL_METHODS.MAIL_TURN,
4520
- params
4521
- );
4598
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_TURN, params);
4522
4599
  }
4523
4600
  /**
4524
4601
  * List turns in a conversation with optional filters.
@@ -4527,10 +4604,7 @@ var AgentConnection = class _AgentConnection {
4527
4604
  * @returns Paginated list of turns
4528
4605
  */
4529
4606
  async listTurns(params) {
4530
- return this.#connection.sendRequest(
4531
- MAIL_METHODS.MAIL_TURNS_LIST,
4532
- params
4533
- );
4607
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_TURNS_LIST, params);
4534
4608
  }
4535
4609
  /**
4536
4610
  * Create a thread in a conversation.
@@ -4539,10 +4613,7 @@ var AgentConnection = class _AgentConnection {
4539
4613
  * @returns The created thread
4540
4614
  */
4541
4615
  async createThread(params) {
4542
- return this.#connection.sendRequest(
4543
- MAIL_METHODS.MAIL_THREAD_CREATE,
4544
- params
4545
- );
4616
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_THREAD_CREATE, params);
4546
4617
  }
4547
4618
  /**
4548
4619
  * List threads in a conversation.
@@ -4551,10 +4622,7 @@ var AgentConnection = class _AgentConnection {
4551
4622
  * @returns Paginated list of threads
4552
4623
  */
4553
4624
  async listThreads(params) {
4554
- return this.#connection.sendRequest(
4555
- MAIL_METHODS.MAIL_THREAD_LIST,
4556
- params
4557
- );
4625
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_THREAD_LIST, params);
4558
4626
  }
4559
4627
  /**
4560
4628
  * Get a summary of a conversation.
@@ -4563,10 +4631,7 @@ var AgentConnection = class _AgentConnection {
4563
4631
  * @returns Generated summary with optional key points, decisions, and questions
4564
4632
  */
4565
4633
  async getConversationSummary(params) {
4566
- return this.#connection.sendRequest(
4567
- MAIL_METHODS.MAIL_SUMMARY,
4568
- params
4569
- );
4634
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_SUMMARY, params);
4570
4635
  }
4571
4636
  /**
4572
4637
  * Replay turns from a conversation, optionally from a specific point.
@@ -4575,10 +4640,7 @@ var AgentConnection = class _AgentConnection {
4575
4640
  * @returns Replayed turns with pagination info
4576
4641
  */
4577
4642
  async replayConversation(params) {
4578
- return this.#connection.sendRequest(
4579
- MAIL_METHODS.MAIL_REPLAY,
4580
- params
4581
- );
4643
+ return this.#connection.sendRequest(MAIL_METHODS.MAIL_REPLAY, params);
4582
4644
  }
4583
4645
  /**
4584
4646
  * Send a message to an agent with mail context attached.
@@ -4625,10 +4687,7 @@ var AgentConnection = class _AgentConnection {
4625
4687
  * @returns The created task
4626
4688
  */
4627
4689
  async createTask(params) {
4628
- return this.#connection.sendRequest(
4629
- TASK_METHODS.TASKS_CREATE,
4630
- params
4631
- );
4690
+ return this.#connection.sendRequest(TASK_METHODS.TASKS_CREATE, params);
4632
4691
  }
4633
4692
  /**
4634
4693
  * Assign a task to an agent.
@@ -4638,10 +4697,7 @@ var AgentConnection = class _AgentConnection {
4638
4697
  * @returns The updated task
4639
4698
  */
4640
4699
  async assignTask(taskId, agentId) {
4641
- return this.#connection.sendRequest(
4642
- TASK_METHODS.TASKS_ASSIGN,
4643
- { taskId, agentId }
4644
- );
4700
+ return this.#connection.sendRequest(TASK_METHODS.TASKS_ASSIGN, { taskId, agentId });
4645
4701
  }
4646
4702
  /**
4647
4703
  * Update a task's status or fields.
@@ -4650,10 +4706,7 @@ var AgentConnection = class _AgentConnection {
4650
4706
  * @returns The updated task
4651
4707
  */
4652
4708
  async updateTask(params) {
4653
- return this.#connection.sendRequest(
4654
- TASK_METHODS.TASKS_UPDATE,
4655
- params
4656
- );
4709
+ return this.#connection.sendRequest(TASK_METHODS.TASKS_UPDATE, params);
4657
4710
  }
4658
4711
  /**
4659
4712
  * List tasks with optional filters.
@@ -4662,10 +4715,7 @@ var AgentConnection = class _AgentConnection {
4662
4715
  * @returns Paginated list of tasks
4663
4716
  */
4664
4717
  async listTasks(params) {
4665
- return this.#connection.sendRequest(
4666
- TASK_METHODS.TASKS_LIST,
4667
- params ?? {}
4668
- );
4718
+ return this.#connection.sendRequest(TASK_METHODS.TASKS_LIST, params ?? {});
4669
4719
  }
4670
4720
  // ===========================================================================
4671
4721
  // Internal
@@ -4677,17 +4727,21 @@ var AgentConnection = class _AgentConnection {
4677
4727
  switch (method) {
4678
4728
  case NOTIFICATION_METHODS.EVENT: {
4679
4729
  const eventParams = params;
4680
- const subscription = this.#subscriptions.get(eventParams.subscriptionId);
4730
+ const subscription = this.#subscriptions.get(
4731
+ eventParams.subscriptionId
4732
+ );
4681
4733
  if (subscription) {
4682
4734
  subscription._pushEvent(eventParams);
4683
4735
  }
4684
4736
  break;
4685
4737
  }
4686
- case NOTIFICATION_METHODS.MESSAGE: {
4738
+ case NOTIFICATION_METHODS.MESSAGE:
4739
+ case NOTIFICATION_METHODS.SEND: {
4687
4740
  const messageParams = params;
4741
+ const message = messageParams.message ?? messageParams;
4688
4742
  for (const handler of this.#messageHandlers) {
4689
4743
  try {
4690
- await handler(messageParams.message);
4744
+ await handler(message);
4691
4745
  } catch (error) {
4692
4746
  console.error("MAP: Message handler error:", error);
4693
4747
  }
@@ -4780,7 +4834,11 @@ var AgentConnection = class _AgentConnection {
4780
4834
  try {
4781
4835
  await this.joinScope(scopeId);
4782
4836
  } catch (error) {
4783
- console.warn("MAP: Failed to restore scope membership:", scopeId, error);
4837
+ console.warn(
4838
+ "MAP: Failed to restore scope membership:",
4839
+ scopeId,
4840
+ error
4841
+ );
4784
4842
  }
4785
4843
  }
4786
4844
  }