Building Multi Agent Systems With ADK and MCP






Building Multi-Agent Systems with ADK and MCP: The Ultimate Guide

In my previous tutorial, I demonstrated how easy it is to create agents using the Agent Development Kit (ADK). Today, I'm taking it to the next level by showing you how to supercharge your agent development through integration with the Model Context Protocol (MCP).

By popular request, I'll show you how to build a **multi-agent system** where different agents connect to different MCP servers with different functions, all coordinated by a single master agent. This eliminates the need to manually switch between subagents.



What We're Building Today

In this tutorial, we're creating a multi-agent system that can:

1. Search for hot Reddit posts from specified subreddits

2. Summarize these posts in a newscaster style

3. Convert the summary into speech

The beauty of the ADK's web interface is that it allows you to step through exactly what's happening under the hood. When you pass an instruction to the agent, you can see it delegate to subagents and watch as they process the information and return results.




Why MCP Matters

Before diving into the tutorial, let me explain why using MCP servers is significantly more powerful than directly using APIs:

- **No API Knowledge Required**: You don't need to understand the details of any API
- **Tool Discovery**: All tools are automatically exposed by the MCP server to your agent
- **Text-Based Interface**: You can use simple text prompts instead of figuring out complex parameter formats
- **Universal Pattern**: The same boilerplate code works across different MCP servers
- **Simplified Integration**: Replace chunks of API-specific code with clean, standardized interactions


 The Tutorial Roadmap

1. Set up the development environment
2. Create an MCP-powered Reddit Scout agent
3. Build a text-to-speech agent using ElevenLabs MCP
4. Create a summarizer agent to format Reddit posts
5. Build a coordinator agent to orchestrate the entire workflow
6. Test the complete multi-agent system




 Setting Up Your Environment

If you're starting fresh, clone the repository and check out lesson one. For those who've seen my previous tutorial, we'll pick up where we left off.

Quick setup:
```bash
# Create and activate virtual environment
python -m venv .venv
source .venv/bin/activate

# Verify Python path
which python

# Install dependencies
pip install -r requirements.txt
```


Building the Reddit Scout Agent with MCP

In our previous tutorial, we built a Reddit Scout that directly used the PRAW library to interact with Reddit's API. Today, we'll replace it with an MCP-powered version.



Create the Agent Structure

```bash
# Create directory structure
mkdir -p agents/async_reddit_scout
touch agents/async_reddit_scout/__init__.py
touch agents/async_reddit_scout/agent.py
```



 The Async Reddit Scout Agent

Here's our MCP-powered Reddit Scout agent:

```python
# agents/async_reddit_scout/agent.py
import os
import contextlib
from adk import Agent
from adk.managers import MCP

async def get_tools_async():
    """Fetches tools from the Reddit MCP server."""
    from adk.managers.mcp import MCPToolSetFromServer
    
    print("Connecting to Reddit MCP server...")
    tool_set, exit_stack = await MCPToolSetFromServer.from_uvx(
        "mcp-reddit",
        environ_vars={
            "REDDIT_CLIENT_ID": os.environ.get("REDDIT_CLIENT_ID"),
            "REDDIT_CLIENT_SECRET": os.environ.get("REDDIT_CLIENT_SECRET"),
            "REDDIT_USER_AGENT": os.environ.get("REDDIT_USER_AGENT"),
        }
    )
    print(f"Connected to Reddit MCP server, found tools: {[t.name for t in tool_set.tools]}")
    return tool_set, exit_stack

async def create_agent():
    """Creates an agent instance after fetching tools from the MCP server."""
    try:
        tools, exit_stack = await get_tools_async()
    except Exception as e:
        print(f"Failed to get tools: {e}")
        tools, exit_stack = [], contextlib.ExitStack()
    
    agent_instance = Agent(
        name="Async Reddit Scout Agent",
        description="Searches for hot posts in given subreddits using external MCP Reddit tools",
        model="gemini-1.5-flash-latest",
        instruction="""
        You are a Reddit news scout. Your job is to fetch hot posts from subreddits requested by the user.
        When asked about a subreddit, use the Reddit MCP tools to fetch hot posts.
        Present the information in a clear, organized manner with titles, links, and brief descriptions.
        """,
        tools=tools
    )
    
    return agent_instance, exit_stack

root_agent = create_agent
```

```python
# agents/async_reddit_scout/__init__.py
from agent import root_agent
```




Creating the Text-to-Speech Speaker Agent

Next, let's create an agent that leverages the ElevenLabs MCP for text-to-speech conversion.




Create the Agent Structure

```bash
mkdir -p agents/speaker
touch agents/speaker/__init__.py
touch agents/speaker/agent.py
```



The Speaker Agent

```python
# agents/speaker/agent.py
import os
import contextlib
from adk import Agent
from litellm import completion
from adk.managers.mcp import MCPToolSetFromServer

async def create_agent():
    """Creates a text-to-speech speaker agent by connecting to ElevenLabs MCP server via UVX."""
    print("Setting up text-to-speech speaker agent...")
    
    # Get tools from ElevenLabs MCP server
    try:
        print("Connecting to ElevenLabs MCP server...")
        tools, exit_stack = await MCPToolSetFromServer.from_uvx(
            "elevenlabs-mcp",
            environ_vars={
                "ELEVENLABS_API_KEY": os.environ.get("ELEVENLABS_API_KEY"),
            }
        )
        print(f"Connected to ElevenLabs MCP server, found tools: {[t.name for t in tools.tools]}")
    except Exception as e:
        print(f"Failed to get tools: {e}")
        tools, exit_stack = [], contextlib.ExitStack()
    


    Using LiteLLM to select model
    llm = completion
    
    agent_instance = Agent(
        name="TTS Speaker Agent",
        description="Converts provided text into speech using the ElevenLabs text-to-speech MCP",
        model="gemini-1.5-flash-latest",
        instruction="""
        You are a text-to-speech agent. Convert text provided by the user into audio.
        Use the 'Will' voice by default, unless the user specifies another voice.
        Return the location where the audio file is saved.
        """,
        tools=tools
    )
    
    return agent_instance, exit_stack

root_agent = create_agent
```

```python
# agents/speaker/__init__.py
from agent import root_agent
```

Don't forget to update your requirements.txt file to include LiteLLM:

```
litellm>=1.0.0
```

Then install the updated dependencies:

```bash
pip install -r requirements.txt
```




Creating the Summarizer Agent

Now let's create a simple agent that summarizes Reddit content in a newscaster style.




 Create the Agent Structure

```bash
mkdir -p agents/summarizer
touch agents/summarizer/__init__.py
touch agents/summarizer/agent.py
```




 The Summarizer Agent

```python
# agents/summarizer/agent.py
from adk import Agent
from litellm import completion

def create_agent():
    """Creates a news anchor style summarizer agent."""
    agent_instance = Agent(
        name="Newscaster Summarizer",
        description="Summarizes Reddit posts in a newscaster style",
        model="gemini-1.5-flash-latest",
        instruction="""
        You are a news anchor summarizing Reddit headlines.
        Given a list of post titles, create a clear, engaging summary in a newscaster style.
        Highlight themes or interesting points, and start with an anchor intro like 
        "Here are today's top stories from [subreddit]..."
        """
    )
    
    return agent_instance

root_agent = create_agent
```

```python
# agents/summarizer/__init__.py
from agent import root_agent
```



 Building the Coordinator Agent

Finally, let's create the coordinator agent that will orchestrate our multi-agent system.



 Create the Agent Structure

```bash
mkdir -p agents/coordinator
touch agents/coordinator/__init__.py
touch agents/coordinator/agent.py
```


The Coordinator Agent

```python
# agents/coordinator/agent.py
import asyncio
import contextlib
from adk import Agent
from litellm import completion

async def create_coordinator_agent():
    """Creates a coordinator agent that manages multiple subagents."""
    # Create an exit stack for resource management
    exit_stack = contextlib.AsyncExitStack()
    
    # Import and initialize subagents
    from agents.async_reddit_scout.agent import create_agent as create_reddit_agent
    from agents.summarizer.agent import create_agent as create_summarizer_agent
    from agents.speaker.agent import create_agent as create_speaker_agent
    
    # Create instances of each subagent
    reddit_agent, reddit_stack = await create_reddit_agent()
    summarizer_agent = create_summarizer_agent()  # Synchronous agent, no stack
    speaker_agent, speaker_stack = await create_speaker_agent()
    
    # Add exit stacks to main stack
    exit_stack.enter_context(reddit_stack)
    exit_stack.enter_context(speaker_stack)
    
    # Set up LLM
    llm = completion
    
    # Create the coordinator agent
    coordinator = Agent(
        name="Multi-Agent Coordinator",
        description="Coordinates finding Reddit posts, summarizing titles, and converting text to speech",
        model="gemini-1.5-flash-latest",
        instruction="""
        You are a coordinator agent that manages three subagents:
        1. Reddit Scout Agent: Fetches hot posts from specified subreddits
        2. Summarizer Agent: Summarizes Reddit posts in a newscaster style
        3. Speaker Agent: Converts text to speech using ElevenLabs
        
        When users ask for Reddit content:
        - Use the Reddit Scout to fetch posts
        - Use the Summarizer to create a newscaster-style summary
        - Use the Speaker to convert the summary to speech when requested
        
        Always explain what you're doing as you coordinate between agents.
        """,
        subagents={
            "reddit_agent": reddit_agent,
            "summarizer_agent": summarizer_agent,
            "speaker_agent": speaker_agent
        }
    )
    
    return coordinator, exit_stack

root_agent = create_coordinator_agent
```

```python
# agents/coordinator/__init__.py
from agent import root_agent
```


Testing Our Multi-Agent System

Now we're ready to test our complete multi-agent system:

```bash
cd agents
adk web
```

In the web interface:

1. Select the "coordinator" agent

2. Ask it to "Find hot posts from /r/cursor"

3. Then tell it to "Please summarize it like a newscaster"

4. Finally, "Convert that text to speech"

You'll see the coordinator agent delegate to each subagent in sequence, fetching Reddit posts, summarizing them in a newscaster style, and converting the summary to speech.



Bonus: Adding Video Generation

As a bonus, you can take this system even further by integrating with AI video generation tools:

1. Use Midjourney to generate a 3D avatar of a geeky newscaster

2. Upload the image to KlingAI

3. Upload the generated audio file for lip-syncing

4. Generate a video of the newscaster delivering your Reddit summary

This demonstrates the true power of combining agents with MCP through the ADK. You could potentially find MCP servers for image generation and video production to automate this entire process!


Conclusion

In this tutorial, we've built a powerful multi-agent system using the Agent Development Kit (ADK) and Model Context Protocol (MCP). We've seen how easy it is to:

1. Create agents that leverage external MCP servers

2. Build specialized agents for specific tasks

3. Coordinate multiple agents through a master coordinator

4. Process and transform information across agent boundaries

The combination of ADK and MCP creates endless possibilities for building sophisticated AI systems without getting bogged down in API details. The standardized interface provided by MCP means you can easily swap in new capabilities as new servers become available.


---

*This blog post is based on a tutorial by @AI Oriented (https://www.youtube.com/c/AIOriented). Check out the channel for more tutorials on AI development.*

Comments

Popular posts from this blog

Video From YouTube

GPT Researcher: Deploy POWERFUL Autonomous AI Agents

Building AI Ready Codebase Indexing With CocoIndex