Module: A2A::Server::Agent::ClassMethods

Defined in:
lib/a2a/server/agent.rb

Instance Method Summary collapse

Instance Method Details

#a2a_capability(name) { ... } ⇒ Object

Define a capability using the DSL

Examples:

Define a capability

a2a_capability "text_analysis" do
  method "analyze_text"
  description "Analyze text for sentiment and topics"
  input_schema type: "object", properties: { text: { type: "string" } }
  output_schema type: "object", properties: { sentiment: { type: "string" } }
  tags ["nlp", "analysis"]
  streaming_supported true
end

Parameters:

  • The capability name

Yields:

  • Block for capability definition

Raises:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/a2a/server/agent.rb', line 121

def a2a_capability(name, &block)
  if name.nil? || (respond_to?(:empty?) && empty?) || (is_a?(String) && strip.empty?)
    raise ArgumentError,
          "Capability name is required"
  end
  raise ArgumentError, "Capability block is required" unless block_given?

  builder = CapabilityBuilder.new(name.to_s)
  builder.instance_eval(&block)
  capability = builder.build

  _a2a_capabilities.register(capability)
  capability
end

#a2a_capability_registryA2A::Protocol::CapabilityRegistry

Get the capability registry

Returns:

  • The capability registry



181
182
183
# File 'lib/a2a/server/agent.rb', line 181

def a2a_capability_registry
  _a2a_capabilities
end

#a2a_config(**options) ⇒ Object

Configure the agent

Examples:

Configure agent

a2a_config name: "My Agent",
           description: "A helpful agent",
           version: "1.0.0"

Parameters:

  • Configuration options

Options Hash (**options):

  • :name (String)

    Agent name

  • :description (String)

    Agent description

  • :version (String)

    Agent version

  • :default_input_modes (Array<String>)

    Default input modes

  • :default_output_modes (Array<String>)

    Default output modes

  • :metadata (Hash)

    Additional metadata



152
153
154
# File 'lib/a2a/server/agent.rb', line 152

def a2a_config(**options)
  _a2a_config.merge!(options)
end

#a2a_method(name, **options) {|params, context| ... } ⇒ Object

Define an A2A method that can be called via JSON-RPC

Examples:

Define a simple method

a2a_method "echo" do |params|
  { message: params['message'] }
end

Define a streaming method

a2a_method "stream_data", streaming: true do |params, context|
  Enumerator.new do |yielder|
    10.times do |i|
      yielder << { count: i }
      sleep 0.1
    end
  end
end

Parameters:

  • The method name

  • Method options

Options Hash (**options):

  • :streaming (Boolean)

    Whether the method supports streaming

  • :async (Boolean)

    Whether the method supports async execution

  • :security (Array<String>)

    Required security schemes

  • :metadata (Hash)

    Additional method metadata

Yields:

  • (params, context)

    The method implementation block

Yield Parameters:

Yield Returns:

  • (Object)

    The method result

Raises:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/a2a/server/agent.rb', line 86

def a2a_method(name, **options, &block)
  if name.nil? || (respond_to?(:empty?) && empty?) || (is_a?(String) && strip.empty?)
    raise ArgumentError,
          "Method name is required"
  end
  raise ArgumentError, "Method block is required" unless block_given?

  method_name = name.to_s

  _a2a_methods[method_name] = {
    handler: block,
    options: options.dup,
    streaming: options[:streaming] || false,
    async: options[:async] || false,
    security: options[:security] || [],
    metadata: options[:metadata] || {}
  }
end

#a2a_method_definition(name) ⇒ Hash?

Get method definition

Parameters:

  • The method name

Returns:

  • The method definition or nil if not found



199
200
201
# File 'lib/a2a/server/agent.rb', line 199

def a2a_method_definition(name)
  _a2a_methods[name.to_s]
end

#a2a_method_registered?(name) ⇒ Boolean

Check if a method is registered

Parameters:

  • The method name

Returns:

  • True if the method is registered



190
191
192
# File 'lib/a2a/server/agent.rb', line 190

def a2a_method_registered?(name)
  _a2a_methods.key?(name.to_s)
end

#a2a_method_registryHash

Get all registered A2A methods

Returns:

  • Hash of method name to method definition



173
174
175
# File 'lib/a2a/server/agent.rb', line 173

def a2a_method_registry
  _a2a_methods.dup
end

#a2a_middleware(middleware_class, **options) ⇒ Object

Add middleware to the agent

Examples:

Add authentication middleware

a2a_middleware AuthenticationMiddleware, required: true

Parameters:

  • The middleware class

  • Middleware options



165
166
167
# File 'lib/a2a/server/agent.rb', line 165

def a2a_middleware(middleware_class, **options)
  _a2a_middleware << { class: middleware_class, options: options }
end