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

NameTypeDescription
idundefined | string | numberThe request ID from the original JSON-RPC request
errorA2AErrorThe 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

NameType
Textends SuccessResponseTypes

Parameters

NameTypeDescription
idundefined | string | numberThe request ID from the original JSON-RPC request
resultTThe result data to include in the response

Returns

JsonRpcResponse<T>

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

NameType
Textends 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

NameTypeDescription
idundefined | string | numberThe request ID from the original JSON-RPC request
responseA2AError | TThe response object or error to include
expectedTypesstring[]Array of valid response types

Returns

JsonRpcResponse<T>

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

NameTypeDescription
responseunknownThe response object to validate
expectedTypesstring[]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']
);