@a2a-js/sdk 0.3.8 → 0.3.10

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
@@ -29,6 +29,14 @@ If you plan to use the Express integration (imports from `@a2a-js/sdk/server/exp
29
29
  npm install express
30
30
  ```
31
31
 
32
+ ### For gRPC Usage
33
+
34
+ If you plan to use the GRPC transport (imports from `@a2a-js/sdk/server/grpc` or `@a2a-js/sdk/client/grpc`), you must install the required peer dependencies:
35
+
36
+ ```bash
37
+ npm install @grpc/grpc-js @bufbuild/protobuf
38
+ ```
39
+
32
40
  You can also find some samples [here](https://github.com/a2aproject/a2a-js/tree/main/src/samples).
33
41
 
34
42
  ---
@@ -41,7 +49,7 @@ This SDK implements the A2A Protocol Specification [`v0.3.0`](https://a2a-protoc
41
49
  | :--- | :---: | :---: |
42
50
  | **JSON-RPC** | ✅ | ✅ |
43
51
  | **HTTP+JSON/REST** | ✅ | ✅ |
44
- | **gRPC** | | |
52
+ | **GRPC** (Node.js only) | | |
45
53
 
46
54
  ## Quickstart
47
55
 
@@ -54,6 +62,7 @@ The core of an A2A server is the `AgentExecutor`, which contains your agent's lo
54
62
  ```typescript
55
63
  // server.ts
56
64
  import express from 'express';
65
+ import { Server, ServerCredentials } from '@grpc/grpc-js';
57
66
  import { v4 as uuidv4 } from 'uuid';
58
67
  import { AgentCard, Message, AGENT_CARD_PATH } from '@a2a-js/sdk';
59
68
  import {
@@ -64,6 +73,7 @@ import {
64
73
  InMemoryTaskStore,
65
74
  } from '@a2a-js/sdk/server';
66
75
  import { agentCardHandler, jsonRpcHandler, restHandler, UserBuilder } from '@a2a-js/sdk/server/express';
76
+ import { grpcService, A2AService } from '@a2a-js/sdk/server/grpc';
67
77
 
68
78
  // 1. Define your agent's identity card.
69
79
  const helloAgentCard: AgentCard = {
@@ -81,6 +91,7 @@ const helloAgentCard: AgentCard = {
81
91
  additionalInterfaces: [
82
92
  { url: 'http://localhost:4000/a2a/jsonrpc', transport: 'JSONRPC' }, // Default JSON-RPC transport
83
93
  { url: 'http://localhost:4000/a2a/rest', transport: 'HTTP+JSON' }, // HTTP+JSON/REST transport
94
+ { url: 'localhost:4001', transport: 'GRPC' }, // GRPC transport
84
95
  ],
85
96
  };
86
97
 
@@ -123,6 +134,15 @@ app.use('/a2a/rest', restHandler({ requestHandler, userBuilder: UserBuilder.noAu
123
134
  app.listen(4000, () => {
124
135
  console.log(`🚀 Server started on http://localhost:4000`);
125
136
  });
137
+
138
+ const server = new Server();
139
+ server.addService(A2AService, grpcService({
140
+ requestHandler,
141
+ userBuilder: UserBuilder.noAuthentication,
142
+ }));
143
+ server.bindAsync(`localhost:4001`, ServerCredentials.createInsecure(), () => {
144
+ console.log(`🚀 Server started on localhost:4001`);
145
+ });
126
146
  ```
127
147
 
128
148
  ### Client: Sending a Message
@@ -163,6 +183,46 @@ async function run() {
163
183
  await run();
164
184
  ```
165
185
 
186
+ ### gRPC Client: Sending a Message
187
+
188
+ The [`ClientFactory`](src/client/factory.ts) has to be created explicitly passing the [`GrpcTransportFactory`](src/client/transports/grpc/grpc_transport.ts).
189
+
190
+ ```typescript
191
+ // client.ts
192
+ import { ClientFactory, ClientFactoryOptions } from '@a2a-js/sdk/client';
193
+ import { GrpcTransportFactory } from '@a2a-js/sdk/client/grpc';
194
+ import { Message, MessageSendParams, SendMessageSuccessResponse } from '@a2a-js/sdk';
195
+ import { v4 as uuidv4 } from 'uuid';
196
+
197
+ async function run() {
198
+ const factory = new ClientFactory({
199
+ transports: [new GrpcTransportFactory()]
200
+ });
201
+
202
+ // createFromUrl accepts baseUrl and optional path,
203
+ // (the default path is /.well-known/agent-card.json)
204
+ const client = await factory.createFromUrl('http://localhost:4000');
205
+
206
+ const sendParams: MessageSendParams = {
207
+ message: {
208
+ messageId: uuidv4(),
209
+ role: 'user',
210
+ parts: [{ kind: 'text', text: 'Hi there!' }],
211
+ kind: 'message',
212
+ },
213
+ };
214
+
215
+ try {
216
+ const response = await client.sendMessage(sendParams);
217
+ const result = response as Message;
218
+ console.log('Agent response:', result.parts[0].text); // "Hello, world!"
219
+ } catch(e) {
220
+ console.error('Error:', e);
221
+ }
222
+ }
223
+
224
+ await run();
225
+ ```
166
226
  ---
167
227
 
168
228
  ## A2A `Task` Support
@@ -0,0 +1,68 @@
1
+ // src/errors.ts
2
+ var A2A_ERROR_CODE = {
3
+ PARSE_ERROR: -32700,
4
+ INVALID_REQUEST: -32600,
5
+ METHOD_NOT_FOUND: -32601,
6
+ INVALID_PARAMS: -32602,
7
+ INTERNAL_ERROR: -32603,
8
+ TASK_NOT_FOUND: -32001,
9
+ TASK_NOT_CANCELABLE: -32002,
10
+ PUSH_NOTIFICATION_NOT_SUPPORTED: -32003,
11
+ UNSUPPORTED_OPERATION: -32004,
12
+ CONTENT_TYPE_NOT_SUPPORTED: -32005,
13
+ INVALID_AGENT_RESPONSE: -32006,
14
+ AUTHENTICATED_EXTENDED_CARD_NOT_CONFIGURED: -32007
15
+ };
16
+ var TaskNotFoundError = class extends Error {
17
+ constructor(message) {
18
+ super(message ?? "Task not found");
19
+ this.name = "TaskNotFoundError";
20
+ }
21
+ };
22
+ var TaskNotCancelableError = class extends Error {
23
+ constructor(message) {
24
+ super(message ?? "Task cannot be canceled");
25
+ this.name = "TaskNotCancelableError";
26
+ }
27
+ };
28
+ var PushNotificationNotSupportedError = class extends Error {
29
+ constructor(message) {
30
+ super(message ?? "Push Notification is not supported");
31
+ this.name = "PushNotificationNotSupportedError";
32
+ }
33
+ };
34
+ var UnsupportedOperationError = class extends Error {
35
+ constructor(message) {
36
+ super(message ?? "This operation is not supported");
37
+ this.name = "UnsupportedOperationError";
38
+ }
39
+ };
40
+ var ContentTypeNotSupportedError = class extends Error {
41
+ constructor(message) {
42
+ super(message ?? "Incompatible content types");
43
+ this.name = "ContentTypeNotSupportedError";
44
+ }
45
+ };
46
+ var InvalidAgentResponseError = class extends Error {
47
+ constructor(message) {
48
+ super(message ?? "Invalid agent response type");
49
+ this.name = "InvalidAgentResponseError";
50
+ }
51
+ };
52
+ var AuthenticatedExtendedCardNotConfiguredError = class extends Error {
53
+ constructor(message) {
54
+ super(message ?? "Authenticated Extended Card not configured");
55
+ this.name = "AuthenticatedExtendedCardNotConfiguredError";
56
+ }
57
+ };
58
+
59
+ export {
60
+ A2A_ERROR_CODE,
61
+ TaskNotFoundError,
62
+ TaskNotCancelableError,
63
+ PushNotificationNotSupportedError,
64
+ UnsupportedOperationError,
65
+ ContentTypeNotSupportedError,
66
+ InvalidAgentResponseError,
67
+ AuthenticatedExtendedCardNotConfiguredError
68
+ };
@@ -0,0 +1,41 @@
1
+ import {
2
+ Extensions
3
+ } from "./chunk-ZX6KNMCP.js";
4
+
5
+ // src/server/context.ts
6
+ var ServerCallContext = class {
7
+ _requestedExtensions;
8
+ _user;
9
+ _activatedExtensions;
10
+ constructor(requestedExtensions, user) {
11
+ this._requestedExtensions = requestedExtensions;
12
+ this._user = user;
13
+ }
14
+ get user() {
15
+ return this._user;
16
+ }
17
+ get activatedExtensions() {
18
+ return this._activatedExtensions;
19
+ }
20
+ get requestedExtensions() {
21
+ return this._requestedExtensions;
22
+ }
23
+ addActivatedExtension(uri) {
24
+ this._activatedExtensions = Extensions.createFrom(this._activatedExtensions, uri);
25
+ }
26
+ };
27
+
28
+ // src/server/authentication/user.ts
29
+ var UnauthenticatedUser = class {
30
+ get isAuthenticated() {
31
+ return false;
32
+ }
33
+ get userName() {
34
+ return "";
35
+ }
36
+ };
37
+
38
+ export {
39
+ ServerCallContext,
40
+ UnauthenticatedUser
41
+ };