Model Context Protocol (MCP) Server
模型上下文协议 (MCP) Python 实现
该项目遵循 Anthropic MCP 规范,使用 Python 实现了一个可运行的模型上下文协议 (MCP) 服务器和客户端。它通过一个简单的交互式示例演示了 MCP 协议的关键模式。
什么是 MCP?
模型上下文协议 (MCP) 是基于 JSON-RPC 2.0 构建的开放标准,用于将 AI 模型连接到外部数据源和工具。它定义了一种客户端-服务器架构,其中 AI 应用程序与一个或多个 MCP 服务器通信,每个 MCP 服务器都提供以下功能:
工具:执行操作的可执行函数
资源:提供信息的数据源
提示:预定义模板或工作流程
MCP 标准化了这些功能的发现和调用方式,充当“AI 的 USB-C”,允许模型以结构化的方式与外部系统交互。
Related MCP server: simple_mcp
项目结构
server/:MCP 服务器实现server.py:处理 MCP 请求并提供示例工具/资源的 WebSocket 服务器
client/:MCP 客户端实现client.py:连接到服务器并练习所有 MCP 功能的演示客户端
功能演示
此实现展示了核心 MCP 协议流程:
能力协商:客户端与服务器通过
initialize进行握手能力发现:列出可用的工具和资源
工具调用:使用参数调用
add_numbers工具资源访问:从资源中读取文本内容
设置
创建虚拟环境:
python3 -m venv .venv source .venv/bin/activate安装依赖项:
pip install -r requirements.txt
用法
启动 MCP 服务器(在一个终端中):
python server/server.py运行 MCP 客户端(在另一个终端中):
python client/client.py
客户端将连接到服务器,执行 MCP 握手,发现功能,并以格式化的输出演示调用工具和访问资源。
工作原理
MCP 服务器
服务器:
接受 WebSocket 连接
响应符合 MCP 规范的 JSON-RPC 请求
提供示例工具(
add_numbers)提供示例资源(
example.txt)支持MCP握手和能力发现
MCP 客户端
客户:
通过 WebSocket 连接到服务器
执行 MCP 握手
发现可用的工具和资源
演示如何调用工具和读取资源
以格式化的显示方式呈现结果
协议详细信息
MCP 实现了以下关键方法:
方法 | 描述 |
| 握手建立能力 |
| 列出可用的工具 |
| 调用带参数的工具 |
| 列出可用资源 |
| 读取资源内容 |
| 列出可用的提示 |
扩展项目
您可以通过以下方式扩展此实现:
添加更多具有不同功能的工具
添加每次读取时都会改变的动态资源
实施引导式交互的提示模板
创建更具交互性的客户端应用程序
参考
This server cannot be deployed
Maintenance
Related MCP Connectors
The Mercado Pago MCP Server implements the Model Context Protocol to provide AI agents and LLMs with access to Mercado Pago's APIs and tools within compatible development environments. It acts as an intermediary that translates Mercado Pago resources into executable functions (tools) that AI applications can invoke to perform actions and automate flows. The server simplifies integration, enables using documentation to implement or improve code, and optimizes operations through natural language interactions without manual implementations.
Nifty's MCP server — exposes tasks, projects, messages, and files as tools for AI agents.
MCP server for progressive tool usage at any scale (see https://klavis.ai)
A comprehensive Model Context Protocol (MCP) server that enables AI assistants to interact with yo…
Related MCP Servers
- AlicenseNot gradedqualityNot gradedmaintenanceAn MCP server implementation that standardizes how AI applications access tools and context, providing a central hub that manages tool discovery, execution, and context management with a simplified configuration system.13-
- FlicenseNot gradedqualityDmaintenanceAn educational MCP server that teaches implementing the Model Context Protocol from scratch, enabling AI models to discover and invoke tools via JSON-RPC over stdio.-
- FlicenseNot gradedqualityFmaintenanceA basic MCP server implementation using Node.js and TypeScript that bridges AI models with external tools and data sources via JSON-RPC.2-
- FlicenseNot gradedqualityBmaintenanceA demo MCP server that connects an AI to external systems by exposing Python functions as tools and data as resources, using the stdio transport.-