一、实施步骤
1.1 需要的工具或软件
安装 Claude Desktop(需要魔法)
Python 3.10+ 环境(下述步骤中会安装)
Python MCP SDK 1.2.0+(下述步骤中会安装)
1.2 配置应用环境
1 2 3 4 5 6 7 8 9 10 # 安装uv,查看其github官方网址“https://github.com/astral-sh/uv” # 在Windows上,执行如下命令 powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"# 管理员模式打开cmd窗口中执行如下命令。 # 创建项目目录 E:\Hdisk\学习\06_AI学习\13_LatestTechKnowledge\01_MCP\try02>uv init scan_kantu Initialized project `scan-kantu` at `E:\Hdisk\学习\06_AI学习\13_LatestTechKnowledge\01_MCP\try02\scan_kantu` E:\Hdisk\学习\06_AI学习\13_LatestTechKnowledge\01_MCP\try02>cd scan_kantu
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 # 使用uv创建虚拟python环境并激活(这里默认就是使用CPython3.11.12) E:\Hdisk\学习\06_AI学习\13_LatestTechKnowledge\01_MCP\try02\scan_kantu>uv venv Using CPython 3.11.12 Creating virtual environment at: .venv Activate with: .venv\Scripts\activate# 此时在当前目录下,可以看到一个名为“.python-version”的文件,里面内容包含“3.11” # (1)如果不明确指定使用的python版本,uv会默认有自己的机制去决定使用哪个python版本 # 使用"uv python list" 可以看到uv当前能看到与使用的python环境与版本(而且uv自己会维护跟自己版本匹配的python环境),后续如果不给uv命令加--python参数指定python版本,它会首选在自己维护的python环境列表中查找一个已经安装了的、最匹配的python版本(从下面内容可知,它从自己维护的python3.10.16与python3.11.12 两个python版本中,选择了后者),整个过程通过“uv init xxx --verbose”可以看得更清楚 E:\Hdisk\学习\06_AI学习\13_LatestTechKnowledge\01_MCP\try01\add_numbers>uv python list cpython-3.14.0a6-windows-x86_64-none <download available> cpython-3.14.0a6+freethreaded-windows-x86_64-none <download available> cpython-3.13.3-windows-x86_64-none C:\Python313\python.exe cpython-3.13.3-windows-x86_64-none C:\ProgramData\chocolatey\bin\python3.13.exe cpython-3.13.3-windows-x86_64-none <download available> cpython-3.13.3+freethreaded-windows-x86_64-none <download available> cpython-3.12.10-windows-x86_64-none <download available> cpython-3.12.7-windows-x86_64-none D:\ProgramData\anaconda3\python.exe cpython-3.11.12-windows-x86_64-none C:\Users\jiangsanyin\AppData\Roaming\uv\python\cpython-3.11.12-windows-x86_64-none\python.exe cpython-3.10.17-windows-x86_64-none <download available> cpython-3.10.16-windows-x86_64-none C:\Users\jiangsanyin\AppData\Roaming\uv\python\cpython-3.10.16-windows-x86_64-none\python.exe cpython-3.9.22-windows-x86_64-none <download available> cpython-3.8.20-windows-x86_64-none <download available> cpython-3.7.9-windows-x86_64-none <download available># (2)也可以在使用uv init xxx命令时,明确指定要使用的python版本 # 比如:“uv init scan_kantu --python 3.12” # 此时,scan_kantu 目录会自动创建一个.python-version文件,其中的内容是 3.12 # 然后执行“uv venv”时将会初始化这个虚拟python环境,如果uv维护的python环境没有此版本,将会使用上述python列表中匹配python版本(但不会使用同一个python可执行文件,而是会新创建一个)
1 2 E:\Hdisk\学习\06 _AI学习\13 _LatestTechKnowledge\01 _MCP\try02\scan_kantu>set-ExecutionPolicy RemoteSigned
1 2 3 4 5 6 7 # 激活 E:\Hdisk\学习\06_AI学习\13_LatestTechKnowledge\01_MCP\try02\scan_kantu>.venv\Scripts\activate (scan_kantu) E:\Hdisk\学习\06_AI学习\13_LatestTechKnowledge\01_MCP\try02\scan_kantu>python -V Python 3.11.12# 安装依赖 (scan_kantu) E:\Hdisk\学习\06_AI学习\13_LatestTechKnowledge\01_MCP\try02\scan_kantu>uv add "mcp[cli]" httpx
1.3 编写提示词
在 windows
上进行开发测试,使用如下提示词,以让LLM为我们自动生成mcp服务端文件(我以看图王这个软件为例,是因为其总是试图修改我电脑上打开pdf或图片的默认软件,让我觉得比较烦,想干掉它。当然有其他可行的方法完全将其卸载,此处不探讨):
1 2 3 4 5 6 7 打造一个 MCP 服务器,它能够: - 功能: - 统计当前桌面上的是否存在名称中包含“看图”的快捷方式 - 获取对应快捷方式的名字 - 要求: - 你可以假设我的桌面路径为 C:\Users\{username}\Desktop
或者,如果我们对其他类型文件比如.py、.java等感兴趣,可以编写如下提示词:
1 2 3 4 5 6 7 8 打造一个 MCP 服务器,它能够: - 功能: - 检查当前桌面上的是否存在python源码文件 - 获取python源码文件的名字 - 解读python源码文件的内容 - 要求: - 你可以假设我的桌面路径为 C:\Users\{username}\Desktop
或者,如果我们对其他类型文件(或快捷方式)比如操作数据库的客户端软件感兴趣,可以编写如下提示词:
1 2 3 4 5 6 7 打造一个 MCP 服务器,它能够:- 功能: - 检查当前桌面上的是否存在“连接与操作数据库的客户端软件”的快捷方式或工具 - 获取这类快捷方式或工具的名字- 要求: - 你可以假设我的桌面路径为 C:\Users\{username}\Desktop
1.4 编写mcp服务端文件
使用 Claude 3.7
大模型直接生成相关代码(输出类似如下图。记得根据开发环境操作系统使用不同的提示词)。
输入提示词前,还需要上传一个README.md文件(这个文件在此处叫做Domain
Knowledge),下载自如下链接:
https://raw.githubusercontent.com/modelcontextprotocol/python-sdk/refs/heads/main/README.md
image-20250518223140526
可以直接保存为“E:\06_AI学习\13_LatestTechKnowledge\01_MCP_kantu_kantu.py”,作为mcp服务端文件。(本来这个文件的内容是需要开发人员手动编写的,现使用claude帮忙生成)
image-20250518223241719
如下是在win11上经过验证好的内容(某些情况下如果有需要,可能需要对LLM生成的代码再修改):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 import osimport globimport win32com.clientfrom pathlib import Pathfrom mcp.server.fastmcp import FastMCP, Context mcp = FastMCP("桌面快捷方式扫描器" )def get_desktop_path (): """获取当前用户的桌面路径""" username = os.environ.get("USERNAME" ) return os.path.join("C:" , "Users" , username, "Desktop" )def get_shortcut_target (shortcut_path ): """获取快捷方式的目标路径""" try : shell = win32com.client.Dispatch("WScript.Shell" ) shortcut = shell.CreateShortCut(shortcut_path) return shortcut.TargetPath except Exception as e: return f"无法获取目标: {str (e)} " def find_shortcuts_with_keyword (keyword="看图" ): """查找包含特定关键词的快捷方式""" desktop_path = get_desktop_path() shortcuts = [] shortcut_files = glob.glob(os.path.join(desktop_path, "*.lnk" )) for shortcut_path in shortcut_files: filename = os.path.basename(shortcut_path) if keyword in filename: target = get_shortcut_target(shortcut_path) shortcuts.append({ "name" : filename, "path" : shortcut_path, "target" : target }) return shortcuts@mcp.tool() def count_image_viewers (ctx: Context ) -> int : """统计桌面上名称包含'看图'的快捷方式数量""" shortcuts = find_shortcuts_with_keyword("看图" ) return len (shortcuts)@mcp.tool() def get_image_viewer_names () -> list : """获取桌面上所有名称包含'看图'的快捷方式名称列表""" shortcuts = find_shortcuts_with_keyword("看图" ) return [shortcut["name" ] for shortcut in shortcuts]@mcp.tool() def get_image_viewer_details () -> list : """获取桌面上所有名称包含'看图'的快捷方式详细信息""" shortcuts = find_shortcuts_with_keyword("看图" ) return shortcuts@mcp.resource("desktop://viewers" ) def get_viewers_resource () -> str : """获取桌面上的图片查看器资源信息""" shortcuts = find_shortcuts_with_keyword("看图" ) if not shortcuts: return "桌面上未找到包含'看图'的快捷方式。" result = "桌面上找到以下包含'看图'的快捷方式:\n\n" for i, shortcut in enumerate (shortcuts, 1 ): result += f"{i} . 名称: {shortcut['name' ]} \n" result += f" 路径: {shortcut['path' ]} \n" result += f" 目标: {shortcut['target' ]} \n\n" return result@mcp.prompt() def how_to_use () -> str : """提供如何使用此服务器的说明""" return """ 我是桌面快捷方式扫描器,可以帮助您检查桌面上是否存在包含"看图"的快捷方式。 您可以使用以下工具: 1. count_image_viewers - 计算桌面上包含"看图"的快捷方式数量 2. get_image_viewer_names - 获取所有包含"看图"的快捷方式名称 3. get_image_viewer_details - 获取所有包含"看图"的快捷方式的详细信息 您也可以访问以下资源: - desktop://viewers - 获取所有包含"看图"的快捷方式的详细信息 请问您想了解哪方面的信息? """ if __name__ == "__main__" : mcp.run()
上述内容保存成E:\Hdisk\学习\06_AI学习\13_LatestTechKnowledge\01_MCP\try02\scan_kantu\scan_kantu.py
1.5. 测试MCP应用
1 2 3 4 5 6 7 8 9 10 11 (scan_kantu) E:\Hdisk\学习\06 _AI学习\13 _LatestTechKnowledge\01 _MCP\try02\scan_kantu>uv add pywin32 (scan_kantu) E:\Hdisk\学习\06 _AI学习\13 _LatestTechKnowledge\01 _MCP\try02\scan_kantu>mcp dev scan_kantu.py Need to install the following packages: @modelcontextprotocol/inspector@0.12 .0 Ok to proceed? (y) y Starting MCP inspector... 🔍 MCP Inspector is up and running at http://127.0 .0.1 :6274 🚀 ⚙️ Proxy server listening on port 6277
在自己win11的chrome浏览器中访问上述地址http://127.0.0.1:6274
,将看到如下内容:
image-20250518224358340
image-20250519143105848
image-20250519144333266
其他的工具如“count_image_viewers”、“get_image_viewer_details”也可以在此界面上进行测试
1.6 MCP应用集成到claude
将上述编写的scan_kantu.py文件(此处是使用claude3.7大模型生成的)接入到Claude
Desktop中。需要编辑Claude
Desktop的claude_desktop_config.json
文件,按照如下方式打开:File->Settings->Developer->"Edit
Config",编辑后的claude_desktop_config.json
文件内容可能如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 { "mcpServers" : { "scan_kantu" : { "command" : "C:\\ Users\\ jiangsanyin\\ .local\\ bin\\ uv.exe" , "args" : [ "--directory" , "E:\\ Hdisk\\ 学习\\ 06_AI学习\\ 13_LatestTechKnowledge\\ 01_MCP\\ try02\\ scan_kantu" , "run" , "scan_kantu.py" ] } } }
然后重启 Claude Desktop(是先Exit而不是Close,再重新打开Claude
Desktop)。此时就可以看到有几个MCP tools可用了。
image-20250519150226210
image-20250519150239800
1.7 在claude中使用mcp应用
使用一个简单的 prompt 来进行实际测试:“能推测我当前桌面上 txt
文件名的含义吗?”
image-20250519150417991
执行结果正确,情况如下:
image-20250519150612965
1.8 MCP应用工作流程
MCP客户端将请求发送给Claude LLM
Claude LLM分析可用的工具、决定使用哪个(些)工具
MCP客户端通过MCP服务端执行LLM决定使用的那些工具
工具执行后的结果被返回给Claude LLM
Claude
LLM根据历史会话消息与工具执行后返回的结果,生成结果并组织成流畅可读的自然语言形式的回复
这些回复最终被展示在用户(你)的面前
二、相关文档
For Server
Developers:https://modelcontextprotocol.io/quickstart/server
Example Servers:https://modelcontextprotocol.io/examples
quickstart-resources:https://github.com/modelcontextprotocol/quickstart-resources/tree/main