agent2agentSourceA2AAgent

class Agent

DSL wrapper that collects operation handlers for an A2A agent.

An Agent produces handler objects that conform to the Dispatcher's contract (#operations, #call). Register an agent on a Server the same way you would register a plain handler.

agent = A2A::Agent.new do on "SendMessage" do respond_with -> (env) { A2A::Schema["Send Message Response"].new() } end

on "GetTask" do
  respond_with -> (env) {
    task = Task.find_by(id: env["a2a.request"].id)
    raise A2A::TaskNotFoundError.new(env["a2a.request"].id) unless task
    task.to_a2a
  }
end

end

server.register(agent)

Nested

Definitions

def on(*operations, &block)

Define a handler stack for one or more A2A operations.

The block is evaluated at definition time to build the middleware stack. Use use to add middleware, respond_with to set the terminal handler.

on "SendMessage" do use SomeMiddleware respond_with -> (env) ... end

Implementation

def on(*operations, &block)
  raise ArgumentError, "on requires at least one operation" if operations.empty?
  raise ArgumentError, "on requires a block" unless block

  builder = StackBuilder.new
  builder.instance_eval(&block)

  handler = Handler.new(
    operations: operations.flatten,
    app:        builder.to_app
  )

  @handlers << handler
  handler
end