Integrations
Provides a Python implementation of both MCP client and server components, allowing Python applications to expose and access MCP capabilities.
Employs WebSocket connections for real-time bidirectional communication between MCP clients and servers, supporting the complete MCP handshake and capability negotiation process.
模型上下文协议 (MCP) Python 实现
该项目遵循 Anthropic MCP 规范,使用 Python 实现了一个可运行的模型上下文协议 (MCP) 服务器和客户端。它通过一个简单的交互式示例演示了 MCP 协议的关键模式。
什么是 MCP?
模型上下文协议 (MCP) 是基于 JSON-RPC 2.0 构建的开放标准,用于将 AI 模型连接到外部数据源和工具。它定义了一种客户端-服务器架构,其中 AI 应用程序与一个或多个 MCP 服务器通信,每个 MCP 服务器都提供以下功能:
- 工具:执行操作的可执行函数
- 资源:提供信息的数据源
- 提示:预定义模板或工作流程
MCP 标准化了这些功能的发现和调用方式,充当“AI 的 USB-C”,允许模型以结构化的方式与外部系统交互。
项目结构
server/
:MCP 服务器实现server.py
:处理 MCP 请求并提供示例工具/资源的 WebSocket 服务器
client/
:MCP 客户端实现client.py
:连接到服务器并练习所有 MCP 功能的演示客户端
功能演示
此实现展示了核心 MCP 协议流程:
- 能力协商:客户端与服务器通过
initialize
进行握手 - 能力发现:列出可用的工具和资源
- 工具调用:使用参数调用
add_numbers
工具 - 资源访问:从资源中读取文本内容
设置
- 创建虚拟环境:Copy
- 安装依赖项:Copy
用法
- 启动 MCP 服务器(在一个终端中):Copy
- 运行 MCP 客户端(在另一个终端中):Copy
客户端将连接到服务器,执行 MCP 握手,发现功能,并以格式化的输出演示调用工具和访问资源。
工作原理
MCP 服务器
服务器:
- 接受 WebSocket 连接
- 响应符合 MCP 规范的 JSON-RPC 请求
- 提供示例工具(
add_numbers
) - 提供示例资源(
example.txt
) - 支持MCP握手和能力发现
MCP 客户端
客户:
- 通过 WebSocket 连接到服务器
- 执行 MCP 握手
- 发现可用的工具和资源
- 演示如何调用工具和读取资源
- 以格式化的显示方式呈现结果
协议详细信息
MCP 实现了以下关键方法:
方法 | 描述 |
---|---|
initialize | 握手建立能力 |
tools/list | 列出可用的工具 |
tools/call | 调用带参数的工具 |
resources/list | 列出可用资源 |
resources/read | 读取资源内容 |
prompts/list | 列出可用的提示 |
扩展项目
您可以通过以下方式扩展此实现:
- 添加更多具有不同功能的工具
- 添加每次读取时都会改变的动态资源
- 实施引导式交互的提示模板
- 创建更具交互性的客户端应用程序
参考
This server cannot be installed
remote-capable server
The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.
MCP 服务器的 Python 实现,使 AI 模型能够通过标准化协议与外部工具和数据源连接,支持通过 JSON-RPC 进行工具调用和资源访问。
Related MCP Servers
- -securityAlicense-qualityA simple MCP server implementation that provides AI models with basic Solana RPC methods and helpful development prompts.Last updated -21TypeScriptMIT License
- -securityFlicense-qualityImplements the Model Context Protocol (MCP) to provide AI models with a standardized interface for connecting to external data sources and tools like file systems, databases, or APIs.Last updated -90Python
- -securityAlicense-qualityA streamlined foundation for building Model Context Protocol servers in Python, designed to make AI-assisted development of MCP tools easier and more efficient.Last updated -13PythonMIT License
- -securityFlicense-qualityA simple demonstration project for the Model Control Protocol (MCP) server that provides tools for AI assistants to fetch news articles, perform calculations, retrieve weather data, and generate personalized greetings.Last updated -Python