Skip to main content

MCP-AG2 Integration Example

This project demonstrates the integration of the Model Context Protocol (MCP) with AutoGen (AG2), showcasing a powerful pattern for building modular, tool-enabled AI agents.

Overview

The example implements three key components:

  1. MCP Server: A process that exposes resources and tools following the MCP specification
  2. MCPAssistantAgent: An AutoGen AssistantAgent extension that implements the MCP client interface
  3. Example Script: Demonstrates an MCP-enabled agent using LLM capabilities with MCP resources/tools

Setup

Prerequisites

  1. Install uv package manager:
# macOS
brew install uv

# Other platforms
curl -LsSf https://astral.sh/uv/install.sh | sh

Installation

# Clone the repository
git clone https://github.com/jtanningbed/mcp-ag2-example
cd mcp-ag2-example

# Install dependencies
uv sync

# Run the example
uv run example.py

Key Benefits

This integration pattern offers several advantages over traditional tool/function calling implementations:

1. Protocol-Level Interface Abstraction

  • The MCP client interface itself (read_resource, call_tool, etc.) is exposed to the LLM agent through AG2's tool registration
  • Rather than registering individual tools directly with AG2, we register only the core MCP interface methods
  • All specific tools (e.g., write_file) are proxied through the call_tool interface to the MCP server
  • This means tools defined on the MCP server don't need any format conversion for different LLMs - they remain in Anthropic schema format

2. Dynamic Tool Discovery and Model Agnosticism

  • Agents use list_tools to discover available server tools
  • While the MCP server defines tools using Anthropic's schema format, the LLM never sees these directly
  • The LLM only needs to understand how to use call_tool with a name and arguments
  • No need to convert tool schemas between different LLM formats since they're abstracted behind the MCP interface

3. Clean Separation of Concerns

  • MCP server handles:
    • Tool implementation details
    • Tool schema definitions (in Anthropic format)
    • Resource management
  • Agent only handles:
    • Understanding the core MCP interface methods
    • Using call_tool to proxy specific tool requests to the server
  • LLM integration layer only handles:
    • Registering MCP interface methods as tools (e.g., call_tool in OpenAI format)
    • Routing tool calls through the MCP client

AutoGen-Specific Benefits

When compared to traditional AutoGen tool implementations:

  1. Simplified Tool Integration

    • No need to define tool schemas in multiple formats
    • Tools are defined once on the MCP server in Anthropic format
    • AG2 only needs the call_tool interface registered
  2. Enhanced Modularity

    • MCP servers can be used by any MCP-compatible client
    • Tools and resources are completely decoupled from agent implementation
    • New tools can be added to the server without any agent changes

Architecture Overview

graph LR
    A[LLM] -->|Uses| B[AG2 Tool Interface]
    B -->|Registered| C[MCP Interface Methods]
    C -->|Proxy| D[MCP Client]
    D -->|Protocol| E[MCP Server]
    E -->|Implements| F[Tools & Resources]

Metadata

Release files for iflow-mcp_jtanningbed-mcp-ag2-example 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for iflow-mcp_jtanningbed-mcp-ag2-example 0.1.0
File Size Uploaded
iflow_mcp_jtanningbed_mcp_ag2_example-0.1.0.tar.gz 41.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for iflow-mcp_jtanningbed-mcp-ag2-example 0.1.0
File Interpreter ABI Platform
iflow_mcp_jtanningbed_mcp_ag2_example-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 48.5 kB

Release files / iflow_mcp_jtanningbed_mcp_ag2_example-0.1.0.tar.gz

Download URL iflow_mcp_jtanningbed_mcp_ag2_example-0.1.0.tar.gz
Size 41.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c2c9134a4615f1c61c5237e71f1da243102471d0ef6a13ac6cc76fcb985ef347
BLAKE2b-256 checksum
How to use checksums
29680733efced91de34982503acd814e61f463626a5582248153189ab544461e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / iflow_mcp_jtanningbed_mcp_ag2_example-0.1.0-py3-none-any.whl

Download URL iflow_mcp_jtanningbed_mcp_ag2_example-0.1.0-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1b181c7c6c8e983836e8a52133ada0e0445427af1a185e5e6d4b1570a8c774e4
BLAKE2b-256 checksum
How to use checksums
358f82f78842bf4201e2edd68fe47e5d5714506ff047269dac3b9a647ed64751
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page