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 installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Servers
- Alicense-quality-maintenanceAn 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
- Flicense-qualityDmaintenanceAn 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.
- Flicense-qualityDmaintenanceA basic MCP server implementation using Node.js and TypeScript that bridges AI models with external tools and data sources via JSON-RPC.2
- Flicense-qualityBmaintenanceA demo MCP server that connects an AI to external systems by exposing Python functions as tools and data as resources, using the stdio transport.
Related MCP Connectors
A comprehensive Model Context Protocol (MCP) server that enables AI assistants to interact with yo…
MCP server exposing the Backtest360 engine API as tools for AI agents.
MCP server for Clipkit — gives AI agents a video toolbox via the Clipkit schema.
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/suspicious-cow/MCP2'
If you have feedback or need assistance with the MCP directory API, please join our Discord server