MCP HubMCP Hub
p2c2e

mcp_proxy_pydantic_agent

by: p2c2e

Example for exposing MCP servers to Pydantic Agents

15created 02/01/2025
Visit
Pydantic
Example

📌Overview

Purpose: The framework aims to demonstrate the integration of Model Context Protocol (MCP) servers with Pydantic.AI, showcasing how to utilize different language models (LLMs) effectively.

Overview: This example utilizes a weather server code and provides two alternative client implementations using different LLMs—gpt-4o via a Proxy Agent and sonnet for tool usage. The setup is designed to be straightforward for users to follow and interact with the implemented models.

Key Features:

  • Multiple LLM Support: Allows for the use of different language models (gpt-4o and sonnet), providing flexibility in model selection for diverse applications.

  • Pydantic Integration: Ensures compatibility with Pydantic.AI, simplifying the process for users to work with function calling mechanisms in language models without direct dependencies on external libraries.


Sample to Show Integration of MCP (Model Context Protocol) Servers with Pydantic.AI

This example demonstrates how to integrate MCP servers with Pydantic.AI using code adapted from the weather 'server' example in the Model Context Protocol quickstart resources.

The code uses two different LLMs for demonstration purposes:

  • The Proxy Agent uses GPT-4o.
  • The tool uses Sonnet.

To run the example, export both OPENAI_API_KEY and ANTHROPIC_API_KEY, or modify the code to suit your preferred models.

The pyproject.toml assumes you are using the uv package manager.

Steps to Run

  1. Clone this repository

  2. Run uv sync

  3. Change directory to mcp-client

  4. Run one of the following commands:

    • uv run client.py
      (Requires OpenAI and Anthropic keys, uses Anthropic libraries directly)

    • uv run client2.py
      (Pure PydanticAI solution with no direct dependency on Anthropic libraries)

Now, try interacting with the client by asking questions such as:

What is the time in NY when it is 7:30pm in Bangalore?

What is the weather currently in Chicago?

Quit when done.