mcpField
πŸ€–
πŸ€– MCP Server Examples with AutoGen

πŸ€– MCP Server Examples with AutoGen

Local
github.com/SaM-92AI & Agents29Updated 80w ago

About πŸ€– MCP Server Examples with AutoGen

This example project demonstrates MCP tool integration with AutoGen for developers building AI agents. It connects a local math tool and a remote Apify web-browsing tool to one agent over Stdio and SSE, respectively.

Key features

  1. 01AutoGen agent uses MCP tools
  2. 02Connects local math server over Stdio
  3. 03Connects Apify service over SSE
  4. 04Demonstrates arithmetic and web browsing

Use cases

  1. 01Have an agent solve arithmetic problems
  2. 02Search and summarize recent news

MCP support

The project shows AutoGen connecting to a local Stdio MCP server and a remote SSE MCP server, then using tools from both.

Configuration

Local server config is the same for every client. Copy it into your client’s MCP config file.

No install method listed yet

We haven’t collected a package or remote endpoint for this server. Follow the instructions in the project README.

Repository

Tools

No tool list yet

Local servers expose tools only once running on your machine, and the maintainer has not provided a tool list.

Details

From the project README

πŸ€– MCP Server Examples with AutoGen

This repository provides a practical demonstration of integrating tools with AI agents using the Model Context Protocol (MCP) within the AutoGen framework.

Key Features Demonstrated:

  • Dual MCP Integration: Shows how to connect an AutoGen agent to:
    • A local tool server (math_server.py) using Stdio transport.
    • A remote tool server (Apify's RAG Web Browser Actor) using Server-Sent Events (SSE) transport.
  • Local Tool Example: A simple calculator (add, multiply) running locally via math_server.py.
  • Remote Tool Example: Leveraging Apify's RAG Web Browser Actor via their MCP Server for web searching and content retrieval.
  • AutoGen Agent: An AssistantAgent configured to utilize both sets of tools to answer user queries.

Goal: To illustrate the flexibility of MCP in enabling AI agents to access diverse tools, whether hosted locally or remotely, through standardized communication protocols (Stdio and SSE).

Scenario: The example agent answers two distinct questions:

  1. A math problem ((3 + 5) x 12?), expected to use the local math_server.py.
  2. A request for recent news ("Summarise the latest news of Iran and US negotiations..."), expected to use the remote Apify web browsing tool.

MCP Workflow

πŸ“š Libraries & Frameworks Used

  • AutoGen: AI agent framework (autogen_agentchat, autogen_core, autogen_ext)
  • MCP: Model Context Protocol for tool integration
  • Python-dotenv: For environment variable management
  • OpenAI API: For LLM capabilities
  • Apify API: For web browsing capabilities

πŸ› οΈ Setup

Follow these steps carefully to set up your environment:

  1. Prerequisites:

    • Ensure you have Python 3.12 installed.
    • Install uv if not already installed:
      pip install uv
      
  2. Navigate to Project Directory:

    cd mcp_autogen_sse_stdio
    
  3. Create and Activate Virtual Environment:

    # Create virtual environment using uv
    uv venv --python 3.12
    
    # Activate the virtual environment
    source .venv/bin/activate  # On macOS/Linux
    # OR
    .\.venv\Scripts\activate  # On Windows
    
  4. Install Dependencies:

    # Install project dependencies
    uv pip install -e .
    

    Troubleshooting Note: If you encounter any issues with the MCP CLI installation, you can manually install it:

    uv add "mcp[cli]"
    
  5. Configure Environment Variables:

    • Create a .env file in the mcp_autogen_sse_stdio directory.
    • Add your API keys:
      OPENAI_API_KEY=your_openai_api_key_here
      APIFY_API_KEY=your_apify_api_key_here
      
    • Get your Apify API key from Apify MCP Server page

πŸš€ Running the Project

  1. Make sure you're in the parent directory (one level up from the project directory):

    cd ..
    
  2. Run the main script using uv:

    uv run mcp_autogen_sse_stdio/main.py
    

This will run the demo that:

  1. Summarizes news about Iran-US negotiations using the Apify tool
  2. Solves a simple math problem: (3 + 5) x 12 using the local math tool

πŸ”Œ Understanding MCP (Model Context Protocol)

MCP is a protocol that standardizes communication between AI models and tools. This example demonstrates two ways to use MCP:

1. Local Tools (StdioServerParams)
  • Uses standard input/output for communication
  • Tools run locally on your machine
  • Example: Our math_server.py provides simple math operations
2. Remote Tools (SseServerParams)
  • Uses Server-Sent Events (SSE) for communication
  • Tools run on remote servers (like Apify)
  • Example: Web browsing capabilities via Apify's rag-web-browser

πŸ“ Code Walkthrough

Our main.py demonstrates:

  1. Environment Setup:

    • Loads API keys and validates them
  2. Tool Configuration:

  3. Agent Creation:

    • Creates an AutoGen assistant with both tool sets
    • Uses GPT-4 as the base model
  4. Task Execution:

    • Runs two demo tasks showing both tools in action
    • Web browsing for news summarization
    • Math calculations for arithmetic problem

πŸ”„ Communication Flow

User β†’ AutoGen Agent β†’ MCP Tools β†’ Results β†’ User

This example shows how easily different tool types can be integrated into one agent using MCP!

Comments

Markdown supported

New to MCP?

From what the protocol is to installing your first server, these answers cover most first questions.

An open protocol that defines how AI applications connect to external tools and data in a uniform way. Clients talk to the model; MCP servers provide tools, resources and prompt templates. They exchange standard messages, so one server can be reused by many clients.