前言

官方sdk仓库:https://github.com/modelcontextprotocol/python-sdk

环境准备

UV安装

mac环境安装命令:

1
curl -LsSf https://astral.sh/uv/install.sh | sh

安装python 3.13

1
2
3
4
5
# 查看已安装的python版本
uv python list

# 安装python版本3.13
uv python install 3.13

创建工作目录:mcp-server

1
2
3
4
5
# 使用指定python版本初始化工作目录
uv init mcp-server -p 3.13

# 进入工作目录
cd mcp-server

安装mcp sdk

1
2
3
4
5
# 将MCP添加到项目依赖关系中
uv add "mcp[cli]"

# 对于使用pip作为依赖关系的项目
pip install "mcp[cli]"

Mcp demo

测试-stdio(本地)测试

将下面的代码保存为server.py文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# server.py
from mcp.server.fastmcp import FastMCP

# Create an MCP server
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}!"

if __name__ == '__main__':
mcp.run(transport='stdio')

执行下面的命令启动mcp inspector:

1
npx @modelcontextprotocol/inspector

会有类似的输出:

1
2
3
4
5
6
7
8
9
Starting MCP inspector...
⚙️ Proxy server listening on 127.0.0.1:6277
🔑 Session token: c6e0d57bbbb6b4ffb134add451bf0b9332582485bfc005e56ad9b85765b89c46
Use this token to authenticate requests or set DANGEROUSLY_OMIT_AUTH=true to disable auth

🔗 Open inspector with token pre-filled:
http://localhost:6274/?MCP_PROXY_AUTH_TOKEN=c6e0d57bbbb6b4ffb134add451bf0b9332582485bfc005e56ad9b85765b89c46

🔍 MCP Inspector is up and running at http://127.0.0.1:6274 🚀

根据提示在前端访问:http://localhost:6274/?MCP_PROXY_AUTH_TOKEN=c6e0d57bbbb6b4ffb134add451bf0b9332582485bfc005e56ad9b85765b89c46

前端需设置的参数:

  • Transport Type:STDIO
  • Command参数:uv
  • Arguments参数:–directory /Users/king/workspace-test/mcp-server run server.py
  • img.png

测试:

img_1.png

img_2.png

测试-sse(远程)测试

将下面的代码保存为server.py文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# server.py
from mcp.server.fastmcp import FastMCP

# Create an MCP server
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}!"

if __name__ == '__main__':
mcp.run(transport='sse')

并将脚本运行起来!

执行下面的命令启动mcp inspector:

1
npx @modelcontextprotocol/inspector

会有类似的输出:

1
2
3
4
5
6
7
8
9
Starting MCP inspector...
⚙️ Proxy server listening on 127.0.0.1:6277
🔑 Session token: fec89a2677061c6e71dc76da9cb7d3b05305cc5497fc2c2d41bcc2a1c3c20676
Use this token to authenticate requests or set DANGEROUSLY_OMIT_AUTH=true to disable auth

🔗 Open inspector with token pre-filled:
http://localhost:6274/?MCP_PROXY_AUTH_TOKEN=fec89a2677061c6e71dc76da9cb7d3b05305cc5497fc2c2d41bcc2a1c3c20676

🔍 MCP Inspector is up and running at http://127.0.0.1:6274 🚀

根据提示在前端访问:http://localhost:6274/?MCP_PROXY_AUTH_TOKEN=fec89a2677061c6e71dc76da9cb7d3b05305cc5497fc2c2d41bcc2a1c3c20676

前端需设置的参数:

此处的8000端口根据实际情况修改

img_3.png

img_4.png

测试:

img_5.png

img_6.png

测试-streamable http

将下面的代码保存为server.py文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# server.py
from mcp.server.fastmcp import FastMCP

# Create an MCP server
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}!"

if __name__ == '__main__':
mcp.run(transport='streamable-http')

并将脚本运行起来!

执行下面的命令启动mcp inspector:

1
npx @modelcontextprotocol/inspector

会有类似的输出:

1
2
3
4
5
6
7
8
9
Starting MCP inspector...
⚙️ Proxy server listening on 127.0.0.1:6277
🔑 Session token: 54807640c398f18ae4067ce86afe5e9cc56dbb10851b3c9be9de0dfc5af120d4
Use this token to authenticate requests or set DANGEROUSLY_OMIT_AUTH=true to disable auth

🔗 Open inspector with token pre-filled:
http://localhost:6274/?MCP_PROXY_AUTH_TOKEN=54807640c398f18ae4067ce86afe5e9cc56dbb10851b3c9be9de0dfc5af120d4

🔍 MCP Inspector is up and running at http://127.0.0.1:6274 🚀

根据提示在前端访问:http://localhost:6274/?MCP_PROXY_AUTH_TOKEN=54807640c398f18ae4067ce86afe5e9cc56dbb10851b3c9be9de0dfc5af120d4

前端需设置的参数:

此处的8000端口根据实际情况修改

img_7.png

img_8.png

测试:

img_9.png

img_10.png