About Mcp Server Python
Mcp Server Python is a small Python example for developers learning to expose tools to an MCP-compatible environment such as Cursor AI. It provides an addition tool and a tool that reads an API key from an environment variable.
Key features
- 01Python MCP server example
- 02Add two numbers
- 03Read API_KEY from environment
- 04Cursor AI configuration
- 05Local server testing
Use cases
- 01Learn basic MCP tool definitions
- 02Test tool calls in Cursor Composer
- 03Verify environment-variable access
MCP support
Implements an MCP server over standard I/O with two tools: adding numbers and retrieving the API_KEY environment variable. The included configuration lets Cursor AI discover and launch it.
Configuration
Local server config is the same for every client. Copy it into your client’s MCP config file.
We haven’t collected a package or remote endpoint for this server. Follow the instructions in the project README.
Tools
Local servers expose tools only once running on your machine, and the maintainer has not provided a tool list.
Details
From the project README# MCP Server in Python
## Overview
**MCP (Model Context Protocol)** is a framework that allows you to integrate custom tools into AI-assisted development environments—such as Cursor AI. MCP servers expose functionality (like data retrieval or code analysis) so that an LLM-based IDE can call these tools on demand.
This project demonstrates an MCP server built in Python that provides two basic tools. One tool, **add**, accepts two numbers and returns their sum, while the other, **getApiKey**, retrieves the API key from the environment (via the `API_KEY` variable). Learn more about MCP in the [Model Context Protocol Introduction](https://modelcontextprotocol.io/introduction).
## Requirements
- **Python:** A modern version of Python (3.8 or higher is recommended).
- **Virtual Environment:** It is recommended to use a virtual environment for dependency management.
## Features
- **MCP Integration:** Exposes tool functionality to LLM-based IDEs.
- **Addition Tool:** Accepts two numeric parameters and returns their sum.
- **Env Var Retrieval:** Demonstrates how to load an example environment variable from the configuration file.
- **CLI Support:** Provides a command-line interface via `mcp[cli]` for easy local development and testing.
- **Standard I/O Transport:** Integrates with development environments using standard I/O for seamless communication.
## Installation
1. **Clone the Repository**
```bash
git clone <repository_url>
cd <repository_directory>
```
-
Setup Virtual Environment
Create and activate a Python virtual environment:
python -m venv .venvOn Linux and macOS:
source .venv/bin/activateOn Windows:
.venv\Scripts\activate -
Install Dependencies
Install the MCP package and its CLI extensions with:
pip install mcp mcp[cli]
Testing the Server Locally
To run the MCP server locally, execute:
mcp dev mcp-server.py
After starting the server, open the provided URL in your browser. Click on Connect, then click on List Templates, select one and add your name, and finally click Read Resources to verify the output. You can also click on Tools, choose one, input the required values, and click Run Tool to test tool functionality.
Integrating with Cursor AI
This project includes a mcp.json configuration that allows Cursor AI to discover and launch your MCP server automatically. Open or create the mcp.json file with the following structure:
{
"mcpServers": {
"MCP Server": {
"command": "/path/to/mcp-server-python/.venv/bin/python3",
"args": ["/path/to/mcp-server-python/mcp-server.py"],
"env": {
"API_KEY": "abc-1234567890"
}
}
}
}
-
mcpServers:
An object mapping server names to their configuration. -
MCP Server:
This is the key for your server configuration. You can name it as you like. -
command:
Specifies the absolute path to your Python executable from the virtual environment. For example:/home/john/mcp-server-python/.venv/bin/python3 -
args:
An array containing the absolute path to your MCP server file. For example:["/home/john/mcp-server-python/mcp-server.py"] -
env: (Optional)
Defines environment variables for your MCP server process. In this example, theAPI_KEYis set to"abc-1234567890". Adjust this value as needed for your environment.You can verify the absolute path to your Python executable by running
which pythonin your terminal.
Optional: Configuration Automation Scripts
Easily configure your local environment by automatically updating the mcp.json file with the correct absolute paths. To apply your local settings, run the following commands from your project root:
chmod +x ./scripts/update_config.sh
./scripts/update_config.sh
This script replaces the placeholder paths in mcp.json with your machine’s absolute paths for Python and the server script, ensuring your configuration settings are always accurate.
Optional: Global Cursor settings
You can also move the mcp.json file to your global Cursor AI configuration directory located at ~/.cursor to make the configuration available globally.
Using the MCP Tool in Cursor Composer
With the MCP server integrated into Cursor AI and with Agent mode enabled in Cursor Composer, simply use a natural language prompt like:
add 3 and 5
or
what is my API key?
The AI agent will infer the available add or getApiKey tool from your MCP server and execute it accordingly.
Code Overview
The project comprises the following key parts:
-
MCP Server Initialization:
The MCP server is instantiated using the MCP library and connected via standard I/O transport. -
Tool Definitions:
- add:
Defined to accept two numeric inputs and return their sum. - getApiKey:
Retrieves the API key from the environment variableAPI_KEYand returns it.
- add:
What is MCP?
Model Context Protocol (MCP) provides a standardized approach to integrate custom tools into AI-assisted development environments. With MCP, you can define tools that perform specific tasks—such as retrieving external data, validating code, or enforcing coding standards—and the AI assistant in your IDE can call these tools automatically based on context. This helps improve developer productivity, ensures consistent quality, and streamlines workflows.
Reference & Sources
- Model Context Protocol: python-sdk
- How to Build MCP Servers and Clients from Scratch
- Model Context Protocol Introduction
License
This project is licensed under the MIT License.
Comments
New to MCP?
From what the protocol is to installing your first server, these answers cover most first questions.
An open protocol that defines how AI applications connect to external tools and data in a uniform way. Clients talk to the model; MCP servers provide tools, resources and prompt templates. They exchange standard messages, so one server can be reused by many clients.