Skip to content

rashi283/mcp-simple2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Model Context Protocol (MCP) Example

A very very simple code example to demonstrate Model Context Protocol (MCP).

What is MCP?

The Model Context Protocol (MCP) is a protocol that allows AI assistants to connect to external data sources and tools. It enables AI models to access real-time information, perform actions, and interact with various services through a standardized interface.

This Example

This simple example demonstrates:

  • A basic MCP server with three simple tools
  • A client that connects to and uses the server
  • How to implement tools that an AI can call

Tools Provided

The MCP server provides three simple tools:

  1. get_time - Returns the current date and time
  2. calculator - Performs basic mathematical calculations
  3. echo - Echoes back any message sent to it

Setup

  1. Install the required dependencies:
pip install -r requirements.txt

Usage

Running the Server

To run the MCP server:

python simple_mcp_server.py

The server will start and wait for client connections via stdin/stdout.

Running the Client Example

To see how the client connects to and uses the server:

python simple_mcp_client.py

This will:

  1. Start the MCP server as a subprocess
  2. Connect to it
  3. List all available tools
  4. Test each tool with example calls
  5. Display the results

Expected Output

When you run the client, you should see output like:

Connected to MCP server!

Available tools:
  - get_time: Get the current time and date
  - calculator: Perform basic mathematical calculations
  - echo: Echo back the input message

Testing tools:

1. Testing get_time:
   Result: Current time: 2024-01-15 14:30:25

2. Testing calculator:
   Result: Result of '2 + 3 * 4' = 14

3. Testing echo:
   Result: Echo: Hello, MCP!

How It Works

  1. Server (simple_mcp_server.py):

    • Implements the MCP protocol
    • Defines tools with their schemas
    • Handles tool calls and returns results
  2. Client (simple_mcp_client.py):

    • Connects to the server
    • Lists available tools
    • Makes tool calls
    • Processes responses

Extending the Example

To add your own tools:

  1. Add a new tool definition in the handle_list_tools() function
  2. Add a corresponding handler in the handle_call_tool() function
  3. Define the tool's input schema and implementation

Files

  • simple_mcp_server.py - The MCP server implementation
  • simple_mcp_client.py - Example client that uses the server
  • requirements.txt - Python dependencies
  • README.md - This file

Next Steps

This is a basic example. In a real-world scenario, you might:

  • Add more sophisticated tools
  • Implement resource management
  • Add authentication
  • Handle errors more robustly
  • Use the server with actual AI assistants like Claude

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages