
The Agent Communication Protocol (ACP) is an open standard designed to enable seamless communication between AI agents, applications, and humans. As AI systems are often developed using diverse frameworks and infrastructures, they can end up isolated and incompatible, limiting their ability to collaborate. ACP addresses this fragmentation by offering a unified RESTful API that facilitates:
Multimodal communication
Both synchronous and asynchronous messaging
Real-time streaming
Support for stateful and stateless agent interactions
Discovery of agents, whether online or offline
Execution of long-running tasks
In this tutorial, weâll take our first steps with ACP by building a basic server that provides Londonâs weather information and a simple client that can interact with it.
Setting up the dependencies
Installing the libraries
Creating the ACP Server
Weâll begin by setting up the ACP server, starting with the creation of an agent.py file.
Weâll begin by importing the necessary libraries. To fetch Londonâs weather data, weâll use the httpx library to make a request to the OpenâMeteo API.
from collections.abc import AsyncGenerator
import httpx
from acp_sdk.models import Message, MessagePart
from acp_sdk.server import Context, RunYield, RunYieldResume, Server
server = Server()
Next, weâll define an asynchronous helper function called get_london_weather that retrieves the current weather in London using the OpenâMeteo API. This function sends a request with Londonâs coordinates and returns a formatted weather summary including temperature, wind speed, and weather condition code.
“””Fetch current London weather from the free OpenâMeteo API.”””
params = {
“latitude”: 51.5072, # London coordinates
“longitude”: -0.1276,
“current_weather”: True,
“timezone”: “Europe/London”
}
url = “https://api.open-meteo.com/v1/forecast”
async with httpx.AsyncClient(timeout=10) as client:
resp = await client.get(url, params=params)
resp.raise_for_status()
cw = resp.json()[“current_weather”]
return (
f”Weather in London: {cw[‘temperature’]}âŻÂ°C, ”
f”wind {cw[‘windspeed’]}âŻkm/h, code {cw[‘weathercode’]}.”
)
This code defines an ACP-compatible agent using the @server.agent() decorator. The london_weather_agent function handles incoming messages by first yielding a thought message, then asynchronously fetching the current weather in London using the get_london_weather() helper. The weather data is then returned as a plain text message. Finally, server.run() starts the ACP server and makes the agent available to handle requests
async def london_weather_agent(
input: list[Message], context: Context
) -> AsyncGenerator[RunYield, RunYieldResume]:
“””Returns current London weather.”””
for _ in input:
yield {“thought”: “Fetching London weather…”}
weather = await get_london_weather()
yield Message(
role=”agent”,
parts=[MessagePart(content=weather, content_type=”text/plain”)]
)
server.run()
Running the server
Next, weâll run the agent.py file to start the server. Once running, the ACP agent will be available to handle requests at http://localhost:8000
To verify that your agent is up and running, open a new terminal and execute the following curl command:
If everything is working correctly, youâll receive a JSON response listing your agent, confirming that itâs available and ready to handle requests.
“agents”: [
{
“name”: “london_weather_agent”,
“description”: “Returns current London weather.”,
“metadata”: {
“annotations”: null,
“documentation”: null,
“license”: null,
“programming_language”: null,
“natural_languages”: null,
“framework”: null,
“capabilities”: null,
“domains”: null,
“tags”: null,
“created_at”: null,
“updated_at”: null,
“author”: null,
“contributors”: null,
“links”: null,
“dependencies”: null,
“recommended_models”: null
},
“input_content_types”: [
“*/*”
],
“output_content_types”: [
“*/*”
]
}
]
}
Creating the ACP Client
We will now create an ACP client (client.py) to interact with our server.Â
This client script uses the ACP SDK to connect to the locally running london_weather_agent via the ACP server at http://localhost:8000. It sends a synchronous message asking for the weather using the run_sync method. Once the agent responds, the script prints out the returned weather details.
from acp_sdk.client import Client
from acp_sdk.models import Message, MessagePart
async def call_london_weather_agent() -> None:
async with Client(base_url=”http://localhost:8000″) as client:
run = await client.run_sync(
agent=”london_weather_agent”,
input=[
Message(
parts=[MessagePart(content=”Tell me the weather”, content_type=”text/plain”)]
)
],
)
print(“Response from london_weather_agent:”)
for message in run.output:
for part in message.parts:
print(“-“, part.content)
if __name__ == “__main__”:
asyncio.run(call_london_weather_agent())
Running the Client
In another terminal, run the following command to send request to our ACP server
You should see a response from the server containing the current weather in London, returned by the london_weather_agent.
– Weather in London: 20.8âŻÂ°C, wind 10.1âŻkm/h, code 3.
Check out the Codes. All credit for this research goes to the researchers of this project. Also, feel free to follow us on Twitter, Youtube and Spotify and donât forget to join our 100k+ ML SubReddit and Subscribe to our Newsletter.
I am a Civil Engineering Graduate (2022) from Jamia Millia Islamia, New Delhi, and I have a keen interest in Data Science, especially Neural Networks and their application in various areas.
Be the first to comment