HyperAIHyperAI

Command Palette

Search for a command to run...

Building a Multi-Agent System with Gemini 2.5 and Multiple MCP Servers: A Step-by-Step Guide

Building a Multi-Agent System with Multiple MCP Servers Using Smolagents In this guide, we will construct a multi-agent system using multiple MCP (Model Context Protocol) servers running locally. This system will utilize two MCP servers to perform key tasks: searching medical papers from PubMed and managing memory files in markdown format. We will use the freely available Gemini 2.5 Flash Preview for the language model, though it is limited in API access. MCP servers have gained significant popularity in the AI agent ecosystem by providing a standardized method for AI agents to interact with various tools and services, functioning much like an app store for these agents. Understanding MCP Servers MCP servers act as intermediaries that allow AI agents to access and utilize external tools and services. By standardizing the interaction protocols, MCP servers simplify the development process and enhance the capabilities of AI agents. They provide a modular approach, where agents can be equipped with different tools without needing to understand the underlying complexities of these tools. Setting Up the Environment To get started, you need to set up your local environment. This involves installing the necessary software and configurations to run the MCP servers. Here’s a step-by-step guide: Install Smolagents: First, ensure you have Smolagents installed on your machine. Smolagents is a lightweight framework for creating and managing AI agents. You can install it using the following command: pip install smolagents Set Up PubMed MCP Server: The PubMed MCP server will enable your agent to search for medical papers. You can set up this server using the Smolagents library and a few additional configurations. Here’s a basic setup: ```python from smolagents import MCP, Agent # Initialize the MCP server for PubMed pubmed_mcp = MCP("PubMed") pubmed_mcp.configure(api_key="your_pubmed_api_key") ``` Create a Custom Memory MCP Server: The custom memory MCP server will handle operations on markdown files, such as writing, updating, or deleting. This server can be created by defining custom functions and integrating them with the Smolagents framework. Here’s an example setup: ```python class MemoryMCP(MCP): def init(self, name): super().init(name) def write_memory(self, content, filename): with open(filename, 'w') as file: file.write(content) return f"Memory written to {filename}" def update_memory(self, content, filename): with open(filename, 'a') as file: file.write(content) return f"Memory updated in {filename}" def delete_memory(self, filename): import os os.remove(filename) return f"Memory deleted from {filename}" # Initialize the custom memory MCP server memory_mcp = MemoryMCP("Memory") ``` Creating the Agent Now that the MCP servers are set up, you need to create an AI agent that can utilize these servers. We will use the Gemini 2.5 Flash Preview as the language model for our agent. Initialize the Agent: Create an agent and connect it to the MCP servers. You can do this using the Smolagents framework as follows: ```python class MyAgent(Agent): def init(self): super().init() self.pubmed_mcp = pubmed_mcp self.memory_mcp = memory_mcp def search_medical_papers(self, query): results = self.pubmed_mcp.search(query) return results def manage_memory(self, action, content, filename): if action == "write": return self.memory_mcp.write_memory(content, filename) elif action == "update": return self.memory_mcp.update_memory(content, filename) elif action == "delete": return self.memory_mcp.delete_memory(filename) else: return "Invalid action" # Initialize the agent my_agent = MyAgent() ``` Integrating with Gemini 2.5 To integrate Gemini 2.5 into our multi-agent system, we need to set up an API connection and use it to generate or process text. Here’s how you can do it: Set Up API Connection: First, obtain an API key for Gemini 2.5 Flash Preview. Then, configure the agent to use this API: ```python # Import the necessary library import requests # Define the API endpoint and headers GEMINI_API_URL = "https://gemini-api.example.com/v2.5/preview" headers = { "Authorization": f"Bearer your_gemini_api_key", "Content-Type": "application/json" } # Function to interact with Gemini 2.5 def interact_with_gemini(prompt): response = requests.post(GEMINI_API_URL, json={"prompt": prompt}, headers=headers) if response.status_code == 200: return response.json()["text"] else: return f"Error: {response.status_code}" ``` Use Gemini 2.5 in the Agent: Integrate the Gemini 2.5 interaction into the agent’s functions to enhance its capabilities. For example, you can use it to generate a summary of the medical papers found: ```python class MyAgent(Agent): # ... (previous initialization code) def generate_summary(self, paper_ids): papers = self.search_medical_papers(paper_ids) prompt = "Summarize the following papers: " + ", ".join(papers) summary = interact_with_gemini(prompt) return summary ``` Running the Multi-Agent System Once your agent and MCP servers are configured, you can start running your multi-agent system. Here’s an example of how to use the agent: Search Medical Papers: python query = "AI in healthcare" results = my_agent.search_medical_papers(query) print("Search Results:", results) Generate a Summary: python paper_ids = ["12345", "67890"] summary = my_agent.generate_summary(paper_ids) print("Summary:", summary) Manage Memory: python content = "Key findings from the AI in healthcare papers" filename = "healthcare_summary.md" my_agent.manage_memory("write", content, filename) additional_content = "More details about the research methods" my_agent.manage_memory("update", additional_content, filename) my_agent.manage_memory("delete", filename) Conclusion By following this guide, you can build a multi-agent system that leverages multiple MCP servers to perform a variety of tasks, such as searching medical papers and managing memory files. The use of Gemini 2.5 Flash Preview adds a powerful language model to your system, enabling more sophisticated text processing and generation. MCP servers provide a flexible and standardized way to integrate different tools, making your system more robust and adaptable. This setup is ideal for researchers, developers, and enthusiasts looking to explore the capabilities of AI agents in a modular and efficient manner. Feel free to expand and customize the system according to your specific needs and requirements.

Related Links

Building a Multi-Agent System with Gemini 2.5 and Multiple MCP Servers: A Step-by-Step Guide | Trending Stories | HyperAI