@multi-agent-protocol/sdk 0.1.5 → 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.
@@ -5568,7 +5568,7 @@ interface AgentConnectOptions {
5568
5568
  metadata?: Record<string, unknown>;
5569
5569
  /** Authentication credentials */
5570
5570
  auth?: {
5571
- method: "bearer" | "api-key" | "mtls" | "none";
5571
+ method: AuthMethod;
5572
5572
  token?: string;
5573
5573
  };
5574
5574
  /**
@@ -5662,6 +5662,27 @@ declare class AgentConnection {
5662
5662
  * ```
5663
5663
  */
5664
5664
  static connect(url: string, options?: AgentConnectOptions): Promise<AgentConnection>;
5665
+ /**
5666
+ * Create an AgentConnection over WebSocket without performing the MAP handshake.
5667
+ *
5668
+ * Use this when you need to control the connection flow — e.g., to handle
5669
+ * server-driven auth negotiation before registration.
5670
+ *
5671
+ * @example
5672
+ * ```typescript
5673
+ * const agent = await AgentConnection.createConnection('ws://localhost:8080', {
5674
+ * name: 'Worker',
5675
+ * role: 'processor',
5676
+ * });
5677
+ *
5678
+ * const result = await agent.connectOnly();
5679
+ * if (result.authRequired) {
5680
+ * await agent.authenticate({ method: result.authRequired.methods[0], token: cred });
5681
+ * }
5682
+ * await agent.register();
5683
+ * ```
5684
+ */
5685
+ static createConnection(url: string, options?: AgentConnectOptions): Promise<AgentConnection>;
5665
5686
  /**
5666
5687
  * Connect and register an agent via agentic-mesh transport.
5667
5688
  *
@@ -5707,13 +5728,52 @@ declare class AgentConnection {
5707
5728
  /** Token to resume a previously disconnected session */
5708
5729
  resumeToken?: string;
5709
5730
  auth?: {
5710
- method: "bearer" | "api-key" | "mtls" | "none";
5731
+ method: AuthMethod;
5711
5732
  token?: string;
5712
5733
  };
5713
5734
  }): Promise<{
5714
5735
  connection: ConnectResponseResult;
5715
5736
  agent: Agent;
5716
5737
  }>;
5738
+ /**
5739
+ * Connect to the MAP system without registering an agent.
5740
+ *
5741
+ * Use this when the server may require authentication before registration.
5742
+ * After connecting, check `authRequired` in the response and call
5743
+ * `authenticate()` if needed, then `register()` to complete the handshake.
5744
+ *
5745
+ * @example
5746
+ * ```typescript
5747
+ * const result = await agent.connectOnly();
5748
+ *
5749
+ * if (result.authRequired) {
5750
+ * const method = result.authRequired.methods[0];
5751
+ * await agent.authenticate({ method, token: myCredential });
5752
+ * }
5753
+ *
5754
+ * await agent.register();
5755
+ * ```
5756
+ */
5757
+ connectOnly(options?: {
5758
+ agentId?: AgentId;
5759
+ resumeToken?: string;
5760
+ auth?: {
5761
+ method: AuthMethod;
5762
+ token?: string;
5763
+ };
5764
+ }): Promise<ConnectResponseResult>;
5765
+ /**
5766
+ * Register as an agent after connecting.
5767
+ *
5768
+ * Call this after `connectOnly()` and optional `authenticate()` to complete
5769
+ * the connection handshake. Uses the options provided during construction
5770
+ * (name, role, scopes, etc.) unless overridden.
5771
+ */
5772
+ register(overrides?: {
5773
+ agentId?: AgentId;
5774
+ name?: string;
5775
+ role?: string;
5776
+ }): Promise<Agent>;
5717
5777
  /**
5718
5778
  * Authenticate with the server after connection.
5719
5779
  *
@@ -5726,26 +5786,21 @@ declare class AgentConnection {
5726
5786
  *
5727
5787
  * @example
5728
5788
  * ```typescript
5729
- * const agent = new AgentConnection(stream, { name: 'MyAgent' });
5730
- *
5731
- * // First connect to get auth requirements
5732
- * const connectResult = await agent.connectOnly();
5789
+ * const result = await agent.connectOnly();
5733
5790
  *
5734
- * if (connectResult.authRequired) {
5791
+ * if (result.authRequired) {
5735
5792
  * const authResult = await agent.authenticate({
5736
- * method: 'api-key',
5737
- * token: process.env.AGENT_API_KEY,
5793
+ * method: result.authRequired.methods[0],
5794
+ * token: myCredential,
5738
5795
  * });
5739
- *
5740
5796
  * if (authResult.success) {
5741
- * // Now register the agent
5742
- * await agent.register({ name: 'MyAgent', role: 'worker' });
5797
+ * await agent.register();
5743
5798
  * }
5744
5799
  * }
5745
5800
  * ```
5746
5801
  */
5747
5802
  authenticate(auth: {
5748
- method: "bearer" | "api-key" | "mtls" | "none";
5803
+ method: AuthMethod;
5749
5804
  token?: string;
5750
5805
  }): Promise<AuthenticateResponseResult>;
5751
5806
  /**
@@ -5757,7 +5812,7 @@ declare class AgentConnection {
5757
5812
  * @returns Updated principal information
5758
5813
  */
5759
5814
  refreshAuth(auth: {
5760
- method: "bearer" | "api-key" | "mtls" | "none";
5815
+ method: AuthMethod;
5761
5816
  token?: string;
5762
5817
  }): Promise<{
5763
5818
  success: boolean;
@@ -5568,7 +5568,7 @@ interface AgentConnectOptions {
5568
5568
  metadata?: Record<string, unknown>;
5569
5569
  /** Authentication credentials */
5570
5570
  auth?: {
5571
- method: "bearer" | "api-key" | "mtls" | "none";
5571
+ method: AuthMethod;
5572
5572
  token?: string;
5573
5573
  };
5574
5574
  /**
@@ -5662,6 +5662,27 @@ declare class AgentConnection {
5662
5662
  * ```
5663
5663
  */
5664
5664
  static connect(url: string, options?: AgentConnectOptions): Promise<AgentConnection>;
5665
+ /**
5666
+ * Create an AgentConnection over WebSocket without performing the MAP handshake.
5667
+ *
5668
+ * Use this when you need to control the connection flow — e.g., to handle
5669
+ * server-driven auth negotiation before registration.
5670
+ *
5671
+ * @example
5672
+ * ```typescript
5673
+ * const agent = await AgentConnection.createConnection('ws://localhost:8080', {
5674
+ * name: 'Worker',
5675
+ * role: 'processor',
5676
+ * });
5677
+ *
5678
+ * const result = await agent.connectOnly();
5679
+ * if (result.authRequired) {
5680
+ * await agent.authenticate({ method: result.authRequired.methods[0], token: cred });
5681
+ * }
5682
+ * await agent.register();
5683
+ * ```
5684
+ */
5685
+ static createConnection(url: string, options?: AgentConnectOptions): Promise<AgentConnection>;
5665
5686
  /**
5666
5687
  * Connect and register an agent via agentic-mesh transport.
5667
5688
  *
@@ -5707,13 +5728,52 @@ declare class AgentConnection {
5707
5728
  /** Token to resume a previously disconnected session */
5708
5729
  resumeToken?: string;
5709
5730
  auth?: {
5710
- method: "bearer" | "api-key" | "mtls" | "none";
5731
+ method: AuthMethod;
5711
5732
  token?: string;
5712
5733
  };
5713
5734
  }): Promise<{
5714
5735
  connection: ConnectResponseResult;
5715
5736
  agent: Agent;
5716
5737
  }>;
5738
+ /**
5739
+ * Connect to the MAP system without registering an agent.
5740
+ *
5741
+ * Use this when the server may require authentication before registration.
5742
+ * After connecting, check `authRequired` in the response and call
5743
+ * `authenticate()` if needed, then `register()` to complete the handshake.
5744
+ *
5745
+ * @example
5746
+ * ```typescript
5747
+ * const result = await agent.connectOnly();
5748
+ *
5749
+ * if (result.authRequired) {
5750
+ * const method = result.authRequired.methods[0];
5751
+ * await agent.authenticate({ method, token: myCredential });
5752
+ * }
5753
+ *
5754
+ * await agent.register();
5755
+ * ```
5756
+ */
5757
+ connectOnly(options?: {
5758
+ agentId?: AgentId;
5759
+ resumeToken?: string;
5760
+ auth?: {
5761
+ method: AuthMethod;
5762
+ token?: string;
5763
+ };
5764
+ }): Promise<ConnectResponseResult>;
5765
+ /**
5766
+ * Register as an agent after connecting.
5767
+ *
5768
+ * Call this after `connectOnly()` and optional `authenticate()` to complete
5769
+ * the connection handshake. Uses the options provided during construction
5770
+ * (name, role, scopes, etc.) unless overridden.
5771
+ */
5772
+ register(overrides?: {
5773
+ agentId?: AgentId;
5774
+ name?: string;
5775
+ role?: string;
5776
+ }): Promise<Agent>;
5717
5777
  /**
5718
5778
  * Authenticate with the server after connection.
5719
5779
  *
@@ -5726,26 +5786,21 @@ declare class AgentConnection {
5726
5786
  *
5727
5787
  * @example
5728
5788
  * ```typescript
5729
- * const agent = new AgentConnection(stream, { name: 'MyAgent' });
5730
- *
5731
- * // First connect to get auth requirements
5732
- * const connectResult = await agent.connectOnly();
5789
+ * const result = await agent.connectOnly();
5733
5790
  *
5734
- * if (connectResult.authRequired) {
5791
+ * if (result.authRequired) {
5735
5792
  * const authResult = await agent.authenticate({
5736
- * method: 'api-key',
5737
- * token: process.env.AGENT_API_KEY,
5793
+ * method: result.authRequired.methods[0],
5794
+ * token: myCredential,
5738
5795
  * });
5739
- *
5740
5796
  * if (authResult.success) {
5741
- * // Now register the agent
5742
- * await agent.register({ name: 'MyAgent', role: 'worker' });
5797
+ * await agent.register();
5743
5798
  * }
5744
5799
  * }
5745
5800
  * ```
5746
5801
  */
5747
5802
  authenticate(auth: {
5748
- method: "bearer" | "api-key" | "mtls" | "none";
5803
+ method: AuthMethod;
5749
5804
  token?: string;
5750
5805
  }): Promise<AuthenticateResponseResult>;
5751
5806
  /**
@@ -5757,7 +5812,7 @@ declare class AgentConnection {
5757
5812
  * @returns Updated principal information
5758
5813
  */
5759
5814
  refreshAuth(auth: {
5760
- method: "bearer" | "api-key" | "mtls" | "none";
5815
+ method: AuthMethod;
5761
5816
  token?: string;
5762
5817
  }): Promise<{
5763
5818
  success: boolean;
package/dist/index.cjs CHANGED
@@ -3946,6 +3946,52 @@ var AgentConnection = class _AgentConnection {
3946
3946
  await agent.connect({ auth: options?.auth });
3947
3947
  return agent;
3948
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
+ }
3949
3995
  /**
3950
3996
  * Connect and register an agent via agentic-mesh transport.
3951
3997
  *
@@ -4042,6 +4088,65 @@ var AgentConnection = class _AgentConnection {
4042
4088
  this.#connection._transitionTo("connected");
4043
4089
  return { connection: connectResult, agent: registerResult.agent };
4044
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
+ }
4045
4150
  /**
4046
4151
  * Authenticate with the server after connection.
4047
4152
  *
@@ -4054,20 +4159,15 @@ var AgentConnection = class _AgentConnection {
4054
4159
  *
4055
4160
  * @example
4056
4161
  * ```typescript
4057
- * const agent = new AgentConnection(stream, { name: 'MyAgent' });
4162
+ * const result = await agent.connectOnly();
4058
4163
  *
4059
- * // First connect to get auth requirements
4060
- * const connectResult = await agent.connectOnly();
4061
- *
4062
- * if (connectResult.authRequired) {
4164
+ * if (result.authRequired) {
4063
4165
  * const authResult = await agent.authenticate({
4064
- * method: 'api-key',
4065
- * token: process.env.AGENT_API_KEY,
4166
+ * method: result.authRequired.methods[0],
4167
+ * token: myCredential,
4066
4168
  * });
4067
- *
4068
4169
  * if (authResult.success) {
4069
- * // Now register the agent
4070
- * await agent.register({ name: 'MyAgent', role: 'worker' });
4170
+ * await agent.register();
4071
4171
  * }
4072
4172
  * }
4073
4173
  * ```