Chinese:【python】MCP 服务器及其应用-CSDN博客
PS:This file only provides the method which sets up the Virtual Environment by UV.Thus if you want to use the following examples,you ought to download that.
PS:You can open the file "UV And It's Functions.txt" or visit "https://docs.astral.sh/uv/" to learn more about UV.If you've already download UV,just type UV in the Power Shell.
PS:We use the VS Code and Cline to solve the tasks
I.MCP And MCP Server
MCP:Model Context Protocol
MCP (Model Context Protocol) is an open protocol that enables AI models to securely interact with local and remote resources through standardized server implementations. It follows a client-host-server architecture, allowing integration of AI functionality in applications while maintaining clear security boundaries and isolation focus.
MCP Sever:
A file or any other things that provides MCP serves.
II.Steps To Build Up A MCP Server
1st. Set Up Virtual Environment
Use the following codes to set up a new Virtual Environment:
uv venv <NAME>(If the <NAME> remains unfilled ,it uses <.venv>)
2nd.Alter And Check The Environment
Press "Ctrl" +"Shift"+"P",choose "Python:Select Interpreter" and then click your Virtual Environment(<.venv> etc.)If the environment variable has been changed,please reboot the VS Code to make sure the environment is available.The VS Code has already activated the environment so you needn't stir it up by yourself
3rd.Install The Required Resources
Use the following codes install the resources:
uv pip install <RESOURCE NAME>
4th.Write your MCP Server File(Python Files)
our file must include the following codes:
from fastmcp import FastMCP
# Initialize FastMCP server
mcp = FastMCP("<SERVER'S NAME>")
#Constants
@mcp.tool()
async def <TOOL NAME>(<PARAMETERS NAME1>:<INPUT TYPE>,<PARAMETERS NAME2>:<INPUT TYPE>,......)-><OUTPUT TYPE>:
"""<FUNCTION AND DESCRIPTION OF THE TOOL>
Args:
<PARAMETERS NAME1>:<FUNCTION AND DESCRIPTION OF THE PARAMETERS>
<PARAMETERS NAME2>:<FUNCTION AND DESCRIPTION OF THE PARAMETERS>
......
"""
<SUBJECT OF THE TOOL>
if __name__ == "__main__":
# Initialize and run the server
mcp.run()
5th.Write your MCP Settings File("mcp_settings.json")
Your file must include the following codes:
{
"mcpServers": {
"<SERVER'S NAME>": {
"disabled": <BOOL>,
"timeout": <TIME>,
"type": <INTERACTION TYPE>,
"command": "uv",
"args": [
"--directory",
"<LOCTION OF YOUR FILE>",
"run",
"<NAME OF YOUR MCP SERVER FILE>"
]
}
浙公网安备 33010602011771号