Skip to content

Hackster Learning Series about AI Agentic protocols (MCP, A2A, ACP) with practical code examples.

License

Notifications You must be signed in to change notification settings

LazaUK/Hackster-AIAgenticProtocols--MCP-A2A-ACP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hackster Learning Series: AI Agentic Protocols

This GitHub repo complements The Learning Series articles on Hackster.io about AI Agentic Protocols. It covers:

  • the Model Context Protocol (MCP) for dynamic tool and data access,
  • the Agent2Agent Protocol (A2A) for direct agent collaboration,
  • and the Agent Communication Protocol (ACP) for robust, interoperable communication.

The repo includes Python code to jump-start practical implementation of these protocols, offering immediate application opportunities.

📑 Table of contents:

Environment Setup

  1. Install the required Python packages, listed in the provided requirements.txt:
pip install -r requirements.txt
  1. If using Azure OpenAI as your AI backend, set the following environment variables:
Variable Description
AOAI_API_BASE Base URL of the Azure OpenAI endpoint
AOAI_API_VERSION API version of the Azure OpenAI endpoint
AOAI_DEPLOYMENT Deployment name of the Azure OpenAI model

Part 1: Model Context Protocol (MCP)

This section demonstrates how an AI agent can dynamically discover and use external tools. The implementation uses an MCP Server (MCPServer_HomeAutomation.py) to expose home automation functionalities (tools) and an MCP Client (MCPClient_GradioUI.py) as a Gradio UI for user interaction.

  1. Defining MCP Tools and Resources (MCP Server): functions decorated with @mcp.tool() or @mcp.resource() in the MCPServer_HomeAutomation.py file define callable actions (Tools) or retrievable data (Resources) for the AI.

    from mcp.server.fastmcp import FastMCP
    mcp = FastMCP("Home Automation")
    
    @mcp.tool()
    def control_light(action: str) -> str: # Generalized snippet
        # ...
        return "Light controlled."
    
    @mcp.resource("home://device_status")
    def get_device_status() -> str: # Generalized snippet
        # ...
        return "{}"
    
    if __name__ == "__main__":
        mcp.run() # Starts the MCP server
  2. Establishing MCP Server Connection (MCP Client): the MCPClient_GradioUI.py starts the server as a subprocess and connects using MCPServerStdio to enable tool discovery.

    import subprocess
    from agents.mcp import MCPServerStdio
    
    server_process = subprocess.Popen([...]) # Start server
    
    mcp_server = MCPServerStdio(...)
    await mcp_server.__aenter__() # Initialize connection
  3. Initialising AI Agent with MCP Servers (MCP Client): An Agent is initialised with the connected mcp_servers. The agent's instructions are dynamically updated based on MCP tool availability.

    from agents import Agent, OpenAIChatCompletionsModel
    
    agent = Agent(
        name="Home Assistant",
        instructions="Use tools...",
        model=OpenAIChatCompletionsModel(...),
        mcp_servers=[mcp_server], # Link MCP server
    )
  4. Processing User Input and Running Agent (MCP Client): when a user inputs a query, Runner.run() is invoked. The AI model, aware of the MCP tools, decides whether to call a relevant tool or access a resource via the MCP layer to fulfill the request.

    from agents import Runner
    
    async def process_user_input(user_input, agent):
        result = await Runner.run(starting_agent=agent, input=user_input)
        return result.final_output

Note

Hackster article about MCP can be found here.

Part 2: Agent2Agent Protocol (A2A)

Caution

Work in progress. To be updated soon!

Part 3: Agent Communication Protocol (ACP)

Caution

Work in progress. To be updated soon!

Demo videos on YouTube

About

Hackster Learning Series about AI Agentic protocols (MCP, A2A, ACP) with practical code examples.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages