๐Ÿค– MCP Server Examples with AutoGen

๐Ÿค– MCP Server Examples with AutoGen

This repository demonstrates how to use AutoGen to integrate local and remote MCP (Model Context Protocol) servers. It showcases a local math tool (math_server.py) using Stdio and a remote Apify tool (RAG Web Browser Actor) via SSE for tasks like arithmetic and web browsing.

SaM-92

Research & Data
Visit Server

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:

    • Sets up local math tools using StdioServerParams
    • Connects to Apify's web browser using SseServerParams (API from https://apify.com/apify/actors-mcp-server)
  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!

Recommended Servers

Crypto Price & Market Analysis MCP Server

Crypto Price & Market Analysis MCP Server

A Model Context Protocol (MCP) server that provides comprehensive cryptocurrency analysis using the CoinCap API. This server offers real-time price data, market analysis, and historical trends through an easy-to-use interface.

Featured
TypeScript
MCP PubMed Search

MCP PubMed Search

Server to search PubMed (PubMed is a free, online database that allows users to search for biomedical and life sciences literature). I have created on a day MCP came out but was on vacation, I saw someone post similar server in your DB, but figured to post mine.

Featured
Python
dbt Semantic Layer MCP Server

dbt Semantic Layer MCP Server

A server that enables querying the dbt Semantic Layer through natural language conversations with Claude Desktop and other AI assistants, allowing users to discover metrics, create queries, analyze data, and visualize results.

Featured
TypeScript
mixpanel

mixpanel

Connect to your Mixpanel data. Query events, retention, and funnel data from Mixpanel analytics.

Featured
TypeScript
Sequential Thinking MCP Server

Sequential Thinking MCP Server

This server facilitates structured problem-solving by breaking down complex issues into sequential steps, supporting revisions, and enabling multiple solution paths through full MCP integration.

Featured
Python
Nefino MCP Server

Nefino MCP Server

Provides large language models with access to news and information about renewable energy projects in Germany, allowing filtering by location, topic (solar, wind, hydrogen), and date range.

Official
Python
Vectorize

Vectorize

Vectorize MCP server for advanced retrieval, Private Deep Research, Anything-to-Markdown file extraction and text chunking.

Official
JavaScript
Mathematica Documentation MCP server

Mathematica Documentation MCP server

A server that provides access to Mathematica documentation through FastMCP, enabling users to retrieve function documentation and list package symbols from Wolfram Mathematica.

Local
Python
kb-mcp-server

kb-mcp-server

An MCP server aimed to be portable, local, easy and convenient to support semantic/graph based retrieval of txtai "all in one" embeddings database. Any txtai embeddings db in tar.gz form can be loaded

Local
Python
Research MCP Server

Research MCP Server

The server functions as an MCP server to interact with Notion for retrieving and creating survey data, integrating with the Claude Desktop Client for conducting and reviewing surveys.

Local
Python