Skip to content

Commit 987fbed

Browse files
docs: Document supported GRPC transport (#306)
# Description Integration of gRpc spec in the documentation
1 parent 40acbd4 commit 987fbed

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

README.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ If you plan to use the Express integration (imports from `@a2a-js/sdk/server/exp
2929
npm install express
3030
```
3131

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+
3240
You can also find some samples [here](https://github.com/a2aproject/a2a-js/tree/main/src/samples).
3341

3442
---
@@ -41,7 +49,7 @@ This SDK implements the A2A Protocol Specification [`v0.3.0`](https://a2a-protoc
4149
| :--- | :---: | :---: |
4250
| **JSON-RPC** |||
4351
| **HTTP+JSON/REST** |||
44-
| **gRPC** | | |
52+
| **GRPC** (Node.js only) | | |
4553

4654
## Quickstart
4755

@@ -54,6 +62,7 @@ The core of an A2A server is the `AgentExecutor`, which contains your agent's lo
5462
```typescript
5563
// server.ts
5664
import express from 'express';
65+
import { Server, ServerCredentials } from '@grpc/grpc-js';
5766
import { v4 as uuidv4 } from 'uuid';
5867
import { AgentCard, Message, AGENT_CARD_PATH } from '@a2a-js/sdk';
5968
import {
@@ -64,6 +73,7 @@ import {
6473
InMemoryTaskStore,
6574
} from '@a2a-js/sdk/server';
6675
import { agentCardHandler, jsonRpcHandler, restHandler, UserBuilder } from '@a2a-js/sdk/server/express';
76+
import { grpcService, A2AService } from '@a2a-js/sdk/server/grpc';
6777

6878
// 1. Define your agent's identity card.
6979
const helloAgentCard: AgentCard = {
@@ -81,6 +91,7 @@ const helloAgentCard: AgentCard = {
8191
additionalInterfaces: [
8292
{ url: 'http://localhost:4000/a2a/jsonrpc', transport: 'JSONRPC' }, // Default JSON-RPC transport
8393
{ url: 'http://localhost:4000/a2a/rest', transport: 'HTTP+JSON' }, // HTTP+JSON/REST transport
94+
{ url: 'localhost:4001', transport: 'GRPC' }, // GRPC transport
8495
],
8596
};
8697

@@ -123,6 +134,15 @@ app.use('/a2a/rest', restHandler({ requestHandler, userBuilder: UserBuilder.noAu
123134
app.listen(4000, () => {
124135
console.log(`🚀 Server started on http://localhost:4000`);
125136
});
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+
});
126146
```
127147

128148
### Client: Sending a Message
@@ -163,6 +183,46 @@ async function run() {
163183
await run();
164184
```
165185

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+
```
166226
---
167227

168228
## A2A `Task` Support

tck/agent/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ async function main() {
240240
const server = new Server();
241241
server.addService(A2AService, grpcHandlerInstance);
242242
server.bindAsync(`localhost:${GRPC_PORT}`, ServerCredentials.createInsecure(), () => {
243-
console.log(`[SUTAgent] gRPC server running at http://localhost:${GRPC_PORT}`);
243+
console.log(`[SUTAgent] gRPC server running at localhost:${GRPC_PORT}`);
244244
});
245245
}
246246

0 commit comments

Comments
 (0)