A very very simple code example to demonstrate Model Context Protocol (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 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
The MCP server provides three simple tools:
- get_time - Returns the current date and time
- calculator - Performs basic mathematical calculations
- echo - Echoes back any message sent to it
- Install the required dependencies:
pip install -r requirements.txtTo run the MCP server:
python simple_mcp_server.pyThe server will start and wait for client connections via stdin/stdout.
To see how the client connects to and uses the server:
python simple_mcp_client.pyThis will:
- Start the MCP server as a subprocess
- Connect to it
- List all available tools
- Test each tool with example calls
- Display the results
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!
-
Server (
simple_mcp_server.py):- Implements the MCP protocol
- Defines tools with their schemas
- Handles tool calls and returns results
-
Client (
simple_mcp_client.py):- Connects to the server
- Lists available tools
- Makes tool calls
- Processes responses
To add your own tools:
- Add a new tool definition in the
handle_list_tools()function - Add a corresponding handler in the
handle_call_tool()function - Define the tool's input schema and implementation
simple_mcp_server.py- The MCP server implementationsimple_mcp_client.py- Example client that uses the serverrequirements.txt- Python dependenciesREADME.md- This file
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