title: Server
A2A Node SDK - v0.1.0 / Modules / Server
Module: Server
Description
Server implementation for the A2A protocol
This package provides a complete server implementation for the Agent-to-Agent (A2A) protocol. It includes an Express-based HTTP and WebSocket server, request handlers, task management, agent execution, and push notification support.
Table of contents
Classes
Interfaces
Functions
Functions
buildErrorResponse
▸ buildErrorResponse(id
, error
): JsonRpcResponse
<null
>
Builds an error JSON-RPC response
This function creates a properly formatted JSON-RPC 2.0 error response with the provided error object.
Parameters
Name | Type | Description |
---|---|---|
id | undefined | string | number | The request ID from the original JSON-RPC request |
error | A2AError | The error object to include in the response |
Returns
JsonRpcResponse
<null
>
A properly formatted JSON-RPC error response
Example
// Create an error response
const error = new A2AError('Task not found', -32011);
const response = buildErrorResponse('request-123', error);
buildSuccessResponse
▸ buildSuccessResponse<T
>(id
, result
): JsonRpcResponse
<T
>
Builds a successful JSON-RPC response
This function creates a properly formatted JSON-RPC 2.0 success response with the provided result data.
Type parameters
Name | Type |
---|---|
T | extends SuccessResponseTypes |
Parameters
Name | Type | Description |
---|---|---|
id | undefined | string | number | The request ID from the original JSON-RPC request |
result | T | The result data to include in the response |
Returns
A properly formatted JSON-RPC success response
Example
// Create a success response with a task result
const response = buildSuccessResponse('request-123', {
id: 'task-456',
state: 'completed',
result: { data: 'Task output' }
});
prepareResponse
▸ prepareResponse<T
>(id
, response
, expectedTypes
): JsonRpcResponse
<T
>
Prepares a JSON-RPC response with validation
This function handles both success and error cases, validating that success responses match the expected types. If validation fails, it automatically generates an appropriate error response.
Type parameters
Name | Type |
---|---|
T | extends Task | { content : string ; format : "plain" | "markdown" ; type : "text" } | { content : string | Uint8Array <ArrayBuffer > ; mimeType : string ; name : string ; size? : number ; type : "file" } | { content : Record <string , any > ; schema? : string ; type : "data" } | { content : string ; format : "plain" ; type : "heartbeat" } | Record <string , unknown > |
Parameters
Name | Type | Description |
---|---|---|
id | undefined | string | number | The request ID from the original JSON-RPC request |
response | A2AError | T | The response object or error to include |
expectedTypes | string [] | Array of valid response types |
Returns
A properly formatted JSON-RPC response
Example
// Prepare a response with validation
const result = await handleRequest();
const response = prepareResponse(
'request-123',
result,
['text', 'image']
);
validateResponseType
▸ validateResponseType(response
, expectedTypes
): boolean
Validates that a response matches one of the expected types
This function checks if a response object has a type that matches one of the expected types. For string responses, it checks if ‘string’ is in the expected types array.
Parameters
Name | Type | Description |
---|---|---|
response | unknown | The response object to validate |
expectedTypes | string [] | Array of valid type strings |
Returns
boolean
True if the response type is valid, false otherwise
Example
// Check if a response is a valid message part
const isValid = validateResponseType(
{ type: 'text', content: 'Hello' },
['text', 'image']
);