Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Page
1
(This page has no text content)
Page
2
Design Multi-Agent AI Systems Using MCP and A2A Engineer your own Python-based agentic AI framework with tool use, memory, and multi-agent workflows Gigi Sayfan
Page
3
Design Multi-Agent AI Systems Using MCP and A2A Copyright © 2026 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. This book was written by Gigi Sayfan. Generative AI tools were used only to assist with ideation, phrasing, and diagram drafts, and all technical content and code were created, verified, and tested by the author and Packt’s editorial team. Packt does not accept AI-generated content that replaces expert authorship. Portfolio Director: Gebin George Relationship Lead: Gebin George/Sonia Chauhan Project Manager: Prajakta Naik Content Engineer: Aditi Chatterjee Technical Editor: Rahul Limbachiya Copy Editor: Safis Editing Indexer: Hemangini Bari Proofreader: Aditi Chatterjee Production Designer: Shantanu Zagade Growth Lead: Nimisha Dua First published: February 2026 Production reference: 1100226 Published by Packt Publishing Ltd. Grosvenor House 11 St Paul's Square Birmingham B3 1RB, UK. ISBN 978-1-80611-647-8 www.packtpub.com
Page
4
Contributors About the author Gigi Sayfan is a member of the AI agents infra team at Perplexity, where he is focused on building large-scale environments and harnesses for AI agents. He has been developing software professionally for more than 30 years in domains as diverse as instant messaging, morphing, chip fabrication process control, embedded multimedia applications for game consoles, brain-inspired machine learning, custom browser development, web services for 3D distributed game platforms, IoT sensors, and virtual reality. He has written production code in Go, Python, Java, C#, C++, and Typescript/JavaScript. His technical expertise includes AI agents, generative AI, cloud-native technologies, DevOps, databases, low-level networking, distributed systems, unorthodox user interfaces, and the full software development life cycle. Gigi has authored several books on Kubernetes and microservices and written hundreds of technical articles on a wide range of topics.
Page
5
About the reviewers Shruti Dhumak is a senior technology executive and the head of customer engineering at Google, where she leads large-scale enterprise transformation through data, the cloud, and AI. She is recognized for building and scaling high-performing teams and aligning complex technology strategy with executive priorities. Shruti partners closely with C-suite leaders to enable sustainable growth and measurable business outcomes, bringing a strong focus on operational excellence and customer impact. As a deep technical expert in advanced cloud architectures and autonomous agentic systems, she serves as a trusted advisor and mentor passionate about results-driven leadership and advancing the next generation of technology leaders. Nicolas Bievre is a senior machine learning engineer at Meta, specializing in AI, recommender systems, and generative AI for advertising and healthcare. He has held critical leadership roles at Meta and PayPal, architecting systems that personalize content for hundreds of millions of users and ensuring compliance for major revenue-driving models. A Stanford alumnus, his peer-reviewed research in leading AI and bioinformatics journals is now utilized in hospitals for cancer research. Internationally recognized, he has received the AdKDD Best Paper Award, the Core Ads Growth Privacy Award, and the Outre-Mer Outstanding Talent Award. He also serves as an AI consultant to the French government and a reviewer for top AI organizations.
Page
6
Subscribe for a free eBook New frameworks, evolving architectures, research drops, production breakdowns—AI_Distilled filters the noise into a weekly briefing for engineers and researchers working hands-on with LLMs and GenAI systems. Subscribe now and receive a free eBook, along with weekly insights that help you stay focused and informed. Subscribe at https://packt.link/8Oz6Y or scan the QR code below.
Page
7
(This page has no text content)
Page
8
Table of Contents Preface xxi Free benefits with your book .......................................................................... xxviii Part 1: Foundations of Agentic AI 1 Chapter 1: Introduction to Generative AI and AI Agents 3 Free benefits with your book • 4 The evolution of generative AI .............................................................................. 4 The early days of symbolic AI • 5 The expert systems era • 6 The rise of machine learning and statistical methods • 7 Deep learning and the neural network renaissance • 7 The emergence of generative models, transformers, and language models • 8 The rise of AI agents • 9 Introducing AI agents ......................................................................................... 10 AI agent characteristics • 10 Key differences: chatbots versus agents • 12 Use cases for agentic AI • 13 Introducing the components of agentic AI systems ............................................... 15 Common components of agentic AI systems • 16 Types of agentic AI architectures • 17 Single-agent loop • 17 Planner and executors • 19 Multi-agent system • 20 Graph-based architecture • 21 Summary ........................................................................................................... 22
Page
9
Chapter 2: Understanding How AI Agents Work 25 Understanding the agent loop: sense, think, act .................................................. 26 The session workflow • 26 The agent loop phases • 27 Workings of an agent loop in AutoGen • 30 The core implementation of AutoGen • 30 Managing long conversations with memory, goals, and state ............................... 35 The context window • 37 Why do LLMs have no memory? • 38 Memory in AutoGen • 40 Planning and reasoning mechanisms ................................................................... 41 Working with reasoning models • 41 Single-agent planning and reasoning • 42 Multi-agent planning and reasoning • 42 Tool use and environment interactions ............................................................... 43 How are user-defined tools implemented? • 44 How are tools declared? • 45 How does the LLM decide which tool to use? • 46 Tool use in AutoGen • 48 Agent evaluation and feedback loops .................................................................. 50 Human feedback • 51 Automated evaluation • 52 Industry benchmarks • 55 Summary ........................................................................................................... 56 Chapter 3: A Hands-On Walk-Through of a Simple AI Agent 59 Technical requirements ...................................................................................... 60 Introducing the k8s-ai agent .............................................................................. 60 Setting up a local kind cluster • 61 Running the k8s-ai agent • 64 Checking the cluster with k8s-ai • 65 Table of Contents viii
Page
10
Understanding the k8s-ai agentic system ............................................................ 70 Code walk-through of the k8s-ai agent • 71 Defining and registering tools with OpenAI • 73 Implementing the tool-calling execution loop • 76 Running and interacting with the Kubernetes chat agent • 80 Summary ........................................................................................................... 81 Part 2: Building Your Own Agentic AI Framework 83 Chapter 4: Building a Tool-Based Agentic AI Framework 85 Technical requirements ...................................................................................... 86 AI-6 framework backend architecture and components ....................................... 86 Core engine • 87 Initialization • 87 Running the engine • 91 Executing the agentic loop • 92 The LLM provider abstraction • 97 The LLMProvider interface • 97 The supported providers • 99 Tool system architecture .................................................................................... 105 Custom tool support • 106 What about built-in tools? • 107 Tool security and permissions • 108 Memory management ........................................................................................ 111 What is a session? • 111 Short-term memory in the agentic loop • 115 Sessions and conversations • 115 Long-term memory and session loading • 115 Handling long contexts • 118 Summary .......................................................................................................... 122 Chapter 5: Implementing Custom Tools 125 ix Table of Contents
Page
11
Technical requirements ..................................................................................... 126 Defining provider-agnostic tools ........................................................................ 126 OpenAI Python tool specification • 126 Ollama Python tool specification • 133 Defining the AI-6 tool specification schema • 135 Defining AI-6 custom tools ................................................................................ 136 General command-line tools • 136 Filesystem tools • 138 Implementing the echo tool • 140 Test runner tool • 142 Bootstrap tool • 144 Memory tools • 145 Listing sessions • 146 Loading sessions • 148 Getting the current session ID • 149 Deleting sessions • 151 Implementing a new tool from scratch with AI-6 ............................................... 152 How to interact with AI-6 • 153 Building a new GitHub tool with AI-6 • 153 Implementing a new tool manually .................................................................... 161 Summary .......................................................................................................... 164 Chapter 6: Creating Chat Interfaces Using Slack and Chainlit 167 Technical requirements .................................................................................... 168 Why the UI matters in agentic AI workflows ..................................................... 168 Agent feedback loops and visibility • 168 Interruptibility and human-in-the-loop design • 169 Diagnosing and debugging agent behaviors • 170 Balancing autonomy and user control • 171 UI as a trust-building mechanism • 173 Building a Slack bot interface ............................................................................. 174 Taking the AI-6 Slack bot for a spin • 175 Table of Contents x
Page
12
Authentication and configuration setup • 179 **/.env.*Slack Bot initialization • 183 Message handling and event processing • 185 Channel management and multi-user support • 188 Integration with the AI-6 engine • 191 Developing a web UI with Chainlit ..................................................................... 192 Chainlit framework overview • 193 Taking AI-6 Chainlit for a spin • 193 Configuration and engine integration • 200 Application life cycle and event handlers • 201 The on_chat_start event handler • 202 The on_message event handler • 204 The on_settings event handler • 206 Best practices for interactive agent interfaces .................................................... 208 Clarify the agent’s role and capabilities • 208 Safeguards – confirmations, dry-runs, and safety nets • 208 Design for turn-based transparency • 209 Prioritize tool invocation and feedback loops • 209 Embrace configurability and runtime control • 209 Logs, metrics, and distributed tracing for agentic systems • 210 Summary ......................................................................................................... 210 Chapter 7: Integrating with the Model Context Protocol Ecosystem 213 Technical requirements ..................................................................................... 214 What is MCP? .................................................................................................... 214 The motivation behind MCP • 214 MCP architecture core and core components ...................................................... 215 MCP servers • 216 MCP clients • 217 MCP hosts • 217 Local versus remote MCP servers • 217 Benefits of using MCP • 218 xi Table of Contents
Page
13
The MCP ecosystem • 219 Building MCP servers ........................................................................................ 219 Filesystem MCP server • 220 GitHub MCP server • 224 Building MCP clients ........................................................................................ 229 The MCPClient class • 229 Connecting to an MCP server • 230 Invoking an MCP tool • 232 Integrating MCP into the AI-6 framework ......................................................... 234 The universal MCPTool class • 235 MCP tool discovery • 239 The tool manager • 240 How the engine uses the tool manager • 245 Putting it all together ....................................................................................... 248 Summary ......................................................................................................... 250 Part 3: Constructing Multi-Agent Systems 253 Chapter 8: Designing Multi-Agent Systems 255 Technical requirements .................................................................................... 256 What is multi-agent orchestration and why does it matter? ............................... 256 What is an AI agent? • 256 Adding explicit agents to AI-6 • 256 Renaming Engine to Agent • 257 Adding a system prompt to the Agent configuration • 257 Adding enabled_tools and disabled_tools fields to the Agent configuration • 258 Adding a new AgentTool that wraps an AI-6 Agent instance • 258 Why do we need multi-agent systems? • 260 Context window limitations • 260 Specialization challenges • 261 Model selection constraints • 261 Cognitive load distribution • 261 Table of Contents xii
Page
14
Fault tolerance and resilience • 261 Testability and debugging • 262 How to orchestrate multiple agents • 262 Sequential orchestration • 262 Parallel orchestration • 263 Hierarchical orchestration • 265 Event-driven orchestration • 266 Collaborative orchestration • 268 Hybrid orchestration • 269 Design considerations • 270 Introduction to the A2A protocol ........................................................................ 271 Core components of the A2A protocol • 272 A2A communication mechanisms • 274 Adding A2A support to AI-6 • 274 Integrating A2A client capabilities into AI-6 • 274 The A2ATool class • 277 Configuration and setup • 279 Designing collaborative agent workflows ........................................................... 281 Agent decomposition strategies • 281 Decomposition by domain expertise • 281 Decomposition by workflow stage • 282 Decomposition by capability pattern • 282 Decomposition decision framework • 283 System prompt design patterns • 284 The specialist agent template • 285 The coordinator agent template • 286 The validator agent template • 288 Domain-specific prompt examples • 290 Tool assignment decision framework • 292 Context engineering for multi-agent systems • 294 Types of context in multi-agent systems • 295 Context management strategies • 296 xiii Table of Contents
Page
15
Context window management • 298 Inter-agent context sharing • 300 Taking AI-6 and A2A for a ride • 302 Multi-agent patterns in action • 304 Conflict resolution and role assignment in multi-agent systems ........................ 306 Best practices for conflict prevention • 308 Design-time conflict analysis • 308 Runtime conflict detection • 308 Clear escalation paths • 309 Summary ......................................................................................................... 309 Chapter 9: Implementing Multi-Agent Systems with A2A 311 Technical requirements ..................................................................................... 312 Building an AI-driven DevOps team ................................................................... 312 What makes DevOps suitable for multi-agent AI? • 312 MAKDO – a multi-agent Kubernetes DevOps system • 313 System architecture • 313 The coordinator agent – orchestrating operations • 314 Agent interaction patterns • 316 LLM-driven agent coordination • 316 Extending AI-6 with new tools • 318 Tool discovery and assignment • 319 Context sharing and state management • 320 Defining roles and responsibilities for specialized agents .................................... 321 From DevOps workflows to agent architecture • 321 Why four agents instead of one? • 322 Why separate the analyzer and fixer? • 323 Why separate Slack_Bot? • 323 Tool access boundaries • 324 Extending the architecture • 324 Key design patterns • 325 Implementing the agents .................................................................................. 326 Table of Contents xiv
Page
16
Configuration-driven agent implementation • 326 Tool discovery and assignment • 330 Session isolation • 331 Configuration over code • 332 Key takeaways • 332 Orchestrating the team with a manager agent .................................................... 333 Dynamic workflow execution • 333 Decision points and branching • 334 Handling complex multi-step operations • 335 Multi-cluster coordination • 335 Conditional remediation • 336 Retry and verification loops • 336 Error handling and recovery • 337 Agent communication failures • 337 Partial failures • 337 Cascading failures • 338 State management and coordination • 338 Memory tools for long-term state • 338 Avoiding duplicate operations • 339 Proactive versus reactive orchestration • 340 Reactive mode – user-initiated operations • 340 Proactive mode – autonomous monitoring • 340 Hybrid approach – event-driven coordination • 340 Integrating human feedback and control channels ............................................. 341 The human-AI interface challenge • 341 The Slack_Bot agent architecture • 341 Building a custom MCP server with FastMCP • 342 The FastMCP Slack server • 343 Communication patterns • 346 Notification hierarchy • 346 Message formatting • 347 Contextual messaging • 348 xv Table of Contents
Page
17
Escalation pattern for restricted operations • 348 Alternative communication channels • 350 Security and access control • 350 Observability and audit trail • 351 MAKDO in action – a complete demo walk-through ........................................... 352 Demo setup • 352 Creating cluster problems • 352 Step 1 – demo start notification • 354 Step 2 – analysis phase • 355 Step 3 – remediation phase • 360 Step 4 – demo complete • 364 Key observations • 365 Summary ......................................................................................................... 366 Chapter 10: Testing, Debugging, and Troubleshooting Multi-Agent Systems 369 Common failure modes in agentic systems ........................................................ 370 Agent hallucination • 370 Wrong tool use and tool misuse • 372 Poor instruction following • 373 Infinite loops and runaway agents • 375 Communication and coordination breakdowns • 376 Timeout and latency issues • 377 State management and consistency errors • 378 Security and permission failures • 379 Instrumentation and observability for AI agents ............................................... 379 Core observability primitives • 380 LLM call tracing • 380 Tool execution logs • 380 Agent state snapshots • 381 Span and trace hierarchies • 381 Metadata enrichment • 381 Table of Contents xvi
Page
18
Structured logging and trace formats • 381 LLM-specific observability tools • 382 Metrics and KPIs for agent performance • 383 Real-time monitoring and alerting • 384 Debugging with observability data • 385 Privacy and security considerations • 386 Debugging tool use and function call chains ...................................................... 387 Understanding the tool call life cycle • 387 Tracing function call chains • 388 Common tool use anti-patterns • 389 Debugging strategies for tool use • 390 Building tool use guardrails • 391 Diagnosing coordination failures in multi-agent workflows .............................. 392 Understanding coordination failure patterns • 392 Agent interaction patterns and their failure modes • 395 Tracing dependencies and data flow • 397 Managing shared state and detecting deadlocks • 399 State synchronization challenges • 400 Detecting and resolving deadlocks • 401 Building coordination guardrails • 403 Designing for resilience and recoverability ........................................................ 404 Redundancy and failover mechanisms • 404 Graceful degradation strategies • 405 Retry logic and circuit breakers • 407 Checkpointing and state recovery • 409 Human escalation patterns • 410 Summary .......................................................................................................... 411 Chapter 11: Deploying Multi-Agent Systems 413 Technical requirements ..................................................................................... 414 Setting up dual KinD clusters for multi-agent deployment ................................. 414 Installing KinD and prerequisites • 414 xvii Table of Contents
Page
19
Creating the control cluster configuration • 415 Creating the worker cluster configuration • 417 Setting up cluster networking and port forwarding • 419 Verifying cross-cluster connectivity • 420 Deploying a test workload to the worker cluster • 421 Deploying MAKDO to the control cluster ........................................................... 424 Preparing the MAKDO configuration • 424 Creating Kubernetes secrets for API keys • 428 Creating the kubeconfig ConfigMap • 430 Deploying MAKDO as a Kubernetes deployment • 431 Building and loading the MAKDO image • 432 Configuring MAKDO to connect to the worker cluster • 434 Verifying MAKDO deployment and logs • 435 Deploying k8s-ai to the worker cluster ............................................................. 436 Building the k8s-ai Docker image • 437 Creating k8s-ai deployment manifests • 439 Deploying k8s-ai to the worker cluster • 439 Exposing the k8s-ai service for cross-cluster access • 442 Verifying k8s-ai deployment • 443 Check pod status • 443 Verify pod health • 444 Check server logs • 444 Test the A2A endpoint • 445 Verify service configuration • 446 Testing the k8s-ai Admin API and session management • 446 Exposing the Admin API port • 447 Creating a session • 447 Listing active sessions • 448 Deleting a session • 449 Using sessions with the A2A protocol • 450 Enabling secure communication between clusters ............................................ 450 Service discovery and networking • 450 Table of Contents xviii
Page
20
How MAKDO discovers k8s-ai • 451 Tracing a request path • 451 Service discovery patterns • 452 Cross-cluster networking approaches • 453 Testing end-to-end connectivity • 455 Understanding the security architecture • 456 API key authentication • 457 k8s-ai authentication • 457 MAKDO configuration • 458 Session-based security • 458 RBAC and least privilege • 460 Inspecting RBAC configuration • 460 Testing permission boundaries • 461 Network segmentation in KinD • 461 Port mapping security • 461 Testing network isolation • 462 Production security enhancements • 463 Summary ......................................................................................................... 466 Chapter 12: Advanced Topics and Future Directions 469 Superhuman reasoning and strategic foresight .................................................. 470 What is superhuman reasoning? • 470 Why is strategic foresight so important? • 471 Challenges and limitations of superhuman reasoning • 472 Implications for society and ethics • 473 Massive context windows and their implications .............................................. 474 The evolution of context windows • 474 What can you do with million-token context windows? • 475 Challenges and trade-offs of large context windows • 476 Context windows and multi-agent systems • 476 The future – infinite context and continuous learning • 476 Architectures for long-horizon planning • 477 xix Table of Contents
The above is a preview of the first 20 pages. Register to read the complete e-book.
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
【One-Line Pitch】
A hands-on builder's guide for Python developers who want to move beyond single-prompt chatbots and construct a working multi-agent framework—complete with tool calling, memory, provider abstraction, and cross-cluster deployment. Read it if you learn best by assembling real systems rather than surveying theory.
【Book Arc】
- **Opening (~0%–10%)**: Frames the historical arc from rule-based expert systems (MYCIN) through machine learning to generative AI, then surveys agent categories—productivity assistants, companions, and domain agents—to establish why agentic systems matter and what they must do.
- **Early (~10%–30%)**: Introduces the anatomy of agentic systems (perception, reasoning, action) and walks through a simple Kubernetes chat agent, covering tool registration with OpenAI, the tool-calling execution loop, and evaluation approaches including human feedback and automated checks.
- **Middle (~30%–50%)**: Builds the AI-6 framework core—engine initialization, the agentic loop, an LLM provider abstraction supporting multiple backends, a tool system with security and permissions, and session-based short- and long-term memory with context summarization.
- **Late (~50%–75%)**: Implements concrete custom tools (test runners, session management, file operations) and extends the framework toward MCP-based tool integration, showing how the abstract tool model becomes working capabilities.
- **Ending (~75%–100%)**: Moves to deployment—setting up kind clusters, configuring control and worker clusters, deploying MAKDO and k8s-ai, exposing A2A endpoints, managing sessions over the protocol, and securing cross-cluster communication.
【Key Takeaways】
- **Agentic systems delegate cognition, not just computation** (Opening): Unlike traditional software where engineers encode all logic, agentic systems hand planning and reasoning to the LLM while the framework supplies tools and structure—this distinction drives every architectural choice that follows.
- **The tool list defines the agent's capability ceiling** (Early): An agent is only as useful as the tools it can call; the book illustrates this with a software-engineer agent needing file operations, test execution, and sandboxed code running.
- **A provider-agnostic LLM interface is a deliberate trade-off** (Middle): Rather than locking into the OpenAI API (or a proxy like OpenRouter), AI-6 builds its own abstraction to control latency, cost, and security—useful context for anyone deciding how much portability they need.
- **Sessions are the memory backbone** (Middle): Short-term memory accumulates every tool call and response into the context window, while a session manager handles persistence, listing, deletion, and titling—making conversations resumable and long contexts manageable via summarization thresholds.
- **Tools are simple, immutable data classes** (Middle): The Tool abstraction (name, description, parameters, required fields) is deliberately lightweight, with slots for memory efficiency—a pattern worth copying when designing your own tool registries.
- **Evaluation needs both humans and automation** (Early): Automated checks work when outputs are verifiable (e.g., CSV format); human feedback catches coordination breakdowns in multi-agent settings—the book treats them as complementary, not alternatives.
- **Deployment is part of the design** (Ending): The final stretch treats multi-agent systems as distributed services—clusters, networking, secrets, and A2A endpoints—signaling that production readiness requires infrastructure thinking, not just prompt engineering.
【Reading Tips】
- **Skim the history in Chapter 1** if you already know the expert-systems-to-LLM arc; the value is in the agent taxonomy, not the timeline.
- **Deep-read the AI-6 framework chapters (roughly the middle third)**—the engine loop, provider abstraction, and session manager are the reusable core you'll adapt to your own projects.
- **Type the code, don't just read it**: the book is structured as a walk-through, and the tool implementations (test runner, session tools) only click when you run them.
- **Treat the deployment chapters as a checklist** rather than prose; the cluster setup, secrets, and A2A verification steps are procedural and reward skimming for structure first.
- **Watch for the author's asides** (e.g., the linked blog post on OpenAI's tool-choice API)—they flag real friction points you'll hit in practice.
【Coverage Limits】
These excerpts are drawn from the table of contents and scattered sections; the guide reflects the book's structure and themes but does not cover every chapter's detailed content, and specific code listings or later-chapter arguments may be underrepresented.
Passage locations
Page 19
417 Setting up cluster networking and port forwarding • 419 Verifying cross-cluster connectivity • 420 Deploying a test workload to the worker cluster • 421...
View in text
Excerpt 2
message=call, recipient=tool_agent_id, cancellation_token=cancellation_token, ) for call in response.content return_exceptions=True ) # Combine the results i...
View in text
Excerpt 3
and it can be used to resume a session from a previous run • checkpoint_interval is the number of iterations after which a checkpoint will be saved • summary...
View in text
Excerpt 4
e.gigi/fixing-the-openai-tool-calling-api- ede67419f9f5. OK. Let’s keep this in mind idx_4a69ec25 nd see how the Ollama Python SDK specifies tool calls. Olla...
View in text
Recommended for You
{{#thumbnailUrl}}
{{/thumbnailUrl}}
{{^thumbnailUrl}}
{{/thumbnailUrl}}
Loading recommended books...
Failed to load, please try again later
Tip the Site
Scan the WeChat Pay or Alipay code to tip. No login required.
WeChat Pay
Alipay