From the course: Hands-On AI: Building AI Agents with Model Context Protocol (MCP) and Agent2Agent (A2A)

Implement A2A Server for agents

- [Instructor] In this video, we will wrap existing agents, namely the policy agent and the timeoff agent with an a2a server implementation. The code is similar for both agents. Code for this chapter is available in the folder chapter six. Let's first explore the a2a server for HR policy agent. This is implemented in the file, a2a.wrapper_hr_policy_agent.py. In order to import the HR policy agent from chapter three, we first add chapter three to the import path. Then, we import the HR policy agent file. Then we implement the agent executor for the policy agent. This is the connector between the a2a server and the agent. This inherits from the agent executor class. This base class provides all the plumbing needed to run an eight to a server. We only need to implement what is specific to our agent. We first have the innate function where we can add any startup tasks. Then we have the execute function that we will override. This function takes a request context as input. This carries the actual user request. We also have an event queue pass here. This is where we will push the output from the agent to be sent via a2a to the client. We first load the user input from the context and print it. Then we make an asynchronous call to the HR policy agents run_hr_policy_agent function with the prompt. Now the HR policy agent will execute the request and return the results. We have already discussed this code in chapter three. The results received is then added to the event queue. The cancel method is used when a long running request needs to be canceled by the client. We are not providing an implementation for it in this example. Now we move on to defining the a2a server. We first define a list of skills for the HR Policy Agent, the ID, name, and description are descriptive of the capabilities of this agent. We can also provide tax and few short examples for the skill. This can be used by the client to understand the capabilities of the agent from the agent card. Next, we define the agent card for the agent, the name and description are used to describe the agent. The URL is where the actual a2a server for the agent can be accessed. In this case, the agent card and the a2a server have the same URL as they run inside the same process. The version number can be used to track the specific version of the agent to be used. The input and output modes describe the kinds of inputs and outputs this agent can handle. Capabilities of the agent indicates whether streaming is supported by the agent. The skill parameter assigns the skill object we have already created. Now, we move on to some boilerplate code needed for the a2a server. We first create a request handler with the agent executor and a default task memory store. Next, we create an A2AStarletteApplication with the agent card and the request handler. Finally, we get to run the actual server. We use the server application, a host, port, and a log level. This server will run on port 9001 on the local host. Do note this host and port is where the agent card is available. The agent card then contains the URL to where the actual agent is available. Next, we create a similar a2a server for the timeoff agent. This code is available in the file, a2a.wrapper_timeoff_agent.py. The code is similar to the policy a2a server. We will then import the timeoff agent from chapter four. We then define a TimeoffAgentExecutor that invokes the timeoff agent. This code is similar to the HR policy agent. We then proceed to define the skill, the agent card request handler and the starlette application. Finally, we run the server on port 9002.

Contents