
1. Introduction to the MCP Protocol
In today’s world, where AI applications and tool integration are becoming increasingly prevalent, how to enable different systems, models, and external services to interact efficiently and securely has become a key focus for developers. The MCP (Model Context Protocol) was created for this purpose. It is an open protocol designed to standardize the connection methods between AI applications and external tools, services, and data sources. With MCP, developers can quickly build scalable and reusable AI toolchains, enhancing the intelligence and automation levels of their systems.MCP The core advantages of the MCP protocol include:
- Standardized interfaces: Unified protocol specifications reduce integration costs.
- Multi-language support: OfficialSDKcoversmainstream development languages such as Python.
- Easy to extend: Supports custom tools, resources, andPromptsto meet diverse business needs.
- High security: Supports various deployment methods, including local and cloud, flexibly adapting to enterprise security policies.
2. Implementing a Local Custom MCP Server
1. Environment Preparation
First, it is recommended to use uv as the Python package management tool. The installation steps are as follows:
For Windows platform:
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
Install Python 3 (version 3.13 or higher is recommended).
Create a project folder, such as MCP-STDIO, and open the command line in that directory.
Official documentation for UV tools: https://astral.sh/uv/
2. Install the MCP SDK
Execute the following command in the project directory:
uv init . -p 3.13uv add "mcp[cli]"
This will automatically install the official MCP Python SDK.
Official documentation for the MCP Python SDK:
https://github.com/modelcontextprotocol/python-sdk
3. Configuring the VS Code Development Environment
Open the project folder and install the Python and Python Debugger extensions.
Create a new main.py file and paste the official example code.
4. Core Code Analysis
Below is a minimal example of an MCP Server:
from mcp.server.fastmcp import FastMCP
# Create an MCP server instance
mcp = FastMCP("Demo")
# Add an addition tool
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers"""
return a + b
# Add a dynamic greeting resource
@mcp.resource("greeting://{name}")
def get_greeting(name: str) -> str:
"""Get a personalized greeting"""
return f"Hello, {name}!"
# Add a Prompt
@mcp.prompt()
def greet_user(name: str, style: str = "friendly") -> str:
"""Generate a greeting prompt"""
styles = {
"friendly": "Please write a warm, friendly greeting",
"formal": "Please write a formal, professional greeting",
"casual": "Please write a casual, relaxed greeting",
}
return f"{styles.get(style, styles['friendly'])} for someone named {name}."
if __name__ == "__main__":
mcp.run(transport='stdio') # Start the MCP server
5. Starting and Testing
Run main.py in VS Code to start the local MCP Server.
Functionality can be tested using the MCP client or integrated tools, such as addition operations and personalized greetings.
3. Implementing a Custom MCP Server for PyPI
If you want to publish the custom MCP Server as a Python package to PyPI for reuse by your team or community, you can refer to the following process:
1. Project Initialization
Create a new folder named Anna-MCP-Pypi and enter the directory.
Initialize the project and install dependencies:
uv init . --package -p 3.13uv add "mcp[cli]"
2. Writing Core Code
Place the above MCP Server code into __init__.py and adjust the entry function as needed:
def main() -> None:
mcp.run(transport='stdio')
3. Packaging and Publishing
Register a PyPI account and enable 2FA to generate a Token.
Build and publish the package:
uv build
uv publish --token <your-own-pypi-token>
After successful publication, you can view the project on the PyPI website.
PyPI MCP SDK page:
https://pypi.org/project/mcp/
4. Integration and Usage
Install the custom MCP package in other projects using pip install.
Configure the development environment, such as VS Code, as needed for quick integration.
4. Deploying MCP Server on Azure Cloud
For enterprise-level applications or scenarios requiring high availability and elastic scaling, it is recommended to deploy the MCP Server on cloud platforms like Azure.
1. Create a Virtual Machine
Create an Ubuntu virtual machine in the Azure Portal.
Connect via SSH:
ssh username@<VM-IP>
Azure Portal: https://portal.azure.com/
2. Install UV Tools
curl -LsSf https://astral.sh/uv/install.sh | sh
3. Deploy the MCP Server
Upload main.py and pyproject.toml to the cloud directory (e.g., mcp_demo).
Install dependencies and configure the environment.
Start the MCP Server, supporting SSE (Server-Sent Events) transmission:
if __name__ == "__main__":
mcp.settings.host = "0.0.0.0"
mcp.run(transport='sse')
4. Remote Access and Integration
Configure the VS Code remote development plugin for cloud debugging.
Access the MCP Server via public IP and port for integration with external systems.
5. Application Scenarios and Practical Recommendations
1. Typical Application Scenarios
-
AI assistant toolchain: such as intelligent Q&A, automated office tasks, data analysis, etc.
-
Internal enterprise integration: connecting ERP, CRM, and other business systems for intelligent upgrades.
-
Security and compliance scenarios: ensuring data security and compliance through local or private cloud deployment.
-
Developer platform: quickly build reusable AI tools to enhance team collaboration efficiency.
2. Practical Recommendations
-
Prioritize local development and cloud deployment: local debugging is more efficient, while cloud deployment is more secure and reliable.
-
Make good use of the MCP SDK’s extensibility: customize tools, resources, and Prompts according to business needs.
-
Pay attention to security and permission management: when deploying in the cloud, ensure proper access control and data isolation.
-
Continuously monitor official documentation and community updates: the MCP protocol and SDK are continuously iterating, and it is recommended to regularly update dependencies and learn new features.
Conclusion
The MCP protocol provides strong support for the integration of AI applications and external tools. By customizing the MCP Server in Python, whether locally, on PyPI, or in the Azure cloud, efficient and flexible AI toolchain construction can be achieved. I hope this article can provide practical references for developers and assist in the implementation of intelligent applications.
If you have more questions or practical experiences, feel free to leave a comment for discussion!
👉Follow for more updates!