MCP ExplorerExplorer

Intro To Mcp

@AmmarMohannaon 10 months ago
1 MIT
FreeCommunity
AI Systems
A demo implementation of Model Context Protocol (MCP) with OpenAI API.

Overview

What is Intro To Mcp

intro_to_MCP is a demo implementation of the Model Context Protocol (MCP) using the OpenAI API. It showcases how large language models can interact with external data and tools through a standardized interface.

Use cases

Use cases for intro_to_MCP include building conversational agents, enhancing data retrieval systems, and creating interactive applications that leverage the capabilities of large language models.

How to use

To use intro_to_MCP, install the required packages with ‘pip install mcp openai’, set up your project by cloning the repository, configure your OpenAI API key in ‘simple_client.py’, and run the demo with ‘python simple_client.py’.

Key features

Key features of intro_to_MCP include the ability to define and use custom tools, maintain context during interactions, and demonstrate integration with OpenAI’s API through example tools like echo and add.

Where to use

intro_to_MCP can be used in various fields such as natural language processing, AI development, and any application requiring interaction between language models and external tools.

Content

Model Context Protocol (MCP) with OpenAI

A simple implementation demonstrating how to use Anthropic’s Model Context Protocol (MCP) with the OpenAI API.

What is MCP?

Model Context Protocol (MCP) is an open standard by Anthropic that allows large language models to access external data and tools through a standardized interface. It enables models to interact with tools and resources while maintaining context.

Prerequisites

  • Python 3.10 or higher
  • OpenAI API key

Quick Start Guide

1. Install Required Packages

pip install mcp openai

2. Set Up Your Project

Clone this repository or download the files:

  • server.py - MCP server with example tools
  • simple_client.py - Client that demonstrates OpenAI integration

3. Configure Your API Key

Update simple_client.py with your OpenAI API key:

OPENAI_API_KEY = "your_openai_api_key_here"

4. Run the Demo

python simple_client.py

This will:

  1. Start the MCP server with example tools
  2. Demonstrate calling the echo tool
  3. Demonstrate calling the add tool
  4. Show how to chain multiple tools together

How It Works

MCP Server (server.py)

The server defines resources and tools:

# Define a tool that echoes a message
@mcp.tool()
def echo(message: str) -> str:
    """Echoes back the message provided"""
    return f"Echo: {message}"

# Define a tool that performs addition
@mcp.tool()
def add(a: int, b: int) -> int:
    """Adds two numbers together"""
    return a + b

OpenAI Integration

The client demonstrates how to:

  1. Define tool specifications for OpenAI
  2. Send function calls to the MCP server
  3. Return tool results to OpenAI

Example tool definition for OpenAI:

{
    "type": "function",
    "function": {
        "name": "echo",
        "description": "Echoes back the message provided",
        "parameters": {
            "type": "object",
            "properties": {
                "message": {"type": "string"}
            },
            "required": ["message"]
        }
    }
}

Creating Your Own Tools

To add new tools to the MCP server:

  1. Add a new function to server.py with the @mcp.tool() decorator
  2. Define the input parameters and return type
  3. Add the corresponding tool definition in your OpenAI client

Example:

@mcp.tool()
def multiply(a: int, b: int) -> int:
    """Multiplies two numbers together"""
    return a * b

Resources

Tools

No tools

Comments

Recommend MCP Servers

View All MCP Servers