Class: A2A::Protocol::AgentCardEndpoints

Inherits:
Object
  • Object
show all
Defined in:
lib/a2a/protocol/agent_card_server.rb

Overview

HTTP endpoint handlers for agent card serving

Provides Rack-compatible handlers for serving agent cards over HTTP with proper content negotiation and caching.

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ AgentCardEndpoints

Returns a new instance of AgentCardEndpoints.



477
478
479
# File 'lib/a2a/protocol/agent_card_server.rb', line 477

def initialize(server)
  @server = server
end

Instance Method Details

#call(env) ⇒ Array

Handle agent card requests

Parameters:

  • env (Hash)

    Rack environment

Returns:

  • (Array)

    Rack response [status, headers, body]



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/a2a/protocol/agent_card_server.rb', line 486

def call(env)
  Rack::Request.new(env) if defined?(Rack::Request)

  # Simple request parsing for non-Rack environments
  method = env["REQUEST_METHOD"] || "GET"
  path = env["PATH_INFO"] || env["REQUEST_URI"] || "/"
  query_params = parse_query_string(env["QUERY_STRING"] || "")

  case path
  when "/agent-card", "/agent-card.json"
    handle_agent_card_request(method, query_params)
  when "/agent-card.jws"
    handle_agent_card_jws_request(method, query_params)
  when "/capabilities"
    handle_capabilities_request(method, query_params)
  else
    [404, { "Content-Type" => "application/json" }, ['{"error":"Not found"}']]
  end
end

#handle_agent_card_jws_request(method, params) ⇒ Object (private)

Handle agent card JWS requests



527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/a2a/protocol/agent_card_server.rb', line 527

def handle_agent_card_jws_request(method, params)
  return method_not_allowed unless method == "GET"

  response = @server.serve_card(
    format: "jws",
    cache_key: params["cache_key"] || "default",
    name: params["name"] || "Agent",
    description: params["description"] || "An A2A agent",
    version: params["version"] || "1.0.0",
    url: params["url"] || "https://example.com/agent"
  )

  [response[:status], response[:headers], [response[:body]]]
end

#handle_agent_card_request(method, params) ⇒ Object (private)

Handle agent card JSON requests



510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/a2a/protocol/agent_card_server.rb', line 510

def handle_agent_card_request(method, params)
  return method_not_allowed unless method == "GET"

  response = @server.serve_card(
    format: "json",
    cache_key: params["cache_key"] || "default",
    name: params["name"] || "Agent",
    description: params["description"] || "An A2A agent",
    version: params["version"] || "1.0.0",
    url: params["url"] || "https://example.com/agent"
  )

  [response[:status], response[:headers], [response[:body]]]
end

#handle_capabilities_request(method, _params) ⇒ Object (private)

Handle capabilities listing requests



544
545
546
547
548
549
550
551
552
553
554
# File 'lib/a2a/protocol/agent_card_server.rb', line 544

def handle_capabilities_request(method, _params)
  return method_not_allowed unless method == "GET"

  capabilities = @server.capability_registry.to_h

  [
    200,
    { "Content-Type" => "application/json" },
    [capabilities.to_json]
  ]
end

#method_not_allowedObject (private)

Return method not allowed response



575
576
577
578
579
580
581
# File 'lib/a2a/protocol/agent_card_server.rb', line 575

def method_not_allowed
  [
    405,
    { "Content-Type" => "application/json" },
    ['{"error":"Method not allowed"}']
  ]
end

#parse_query_string(query_string) ⇒ Object (private)

Parse query string into hash



558
559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/a2a/protocol/agent_card_server.rb', line 558

def parse_query_string(query_string)
  return {} if query_string.empty?

  params = {}
  query_string.split("&").each do |pair|
    key, value = pair.split("=", 2)
    next unless key

    key = URI.decode_www_form_component(key) if defined?(URI.decode_www_form_component)
    value = URI.decode_www_form_component(value || "") if defined?(URI.decode_www_form_component)
    params[key] = value
  end
  params
end