Model Context Protocol (MCP) Server

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 协议流程:

  1. 能力协商:客户端与服务器通过initialize进行握手
  2. 能力发现:列出可用的工具和资源
  3. 工具调用:使用参数调用add_numbers工具
  4. 资源访问:从资源中读取文本内容

设置

  1. 创建虚拟环境:
    python3 -m venv .venv source .venv/bin/activate
  2. 安装依赖项:
    pip install -r requirements.txt

用法

  1. 启动 MCP 服务器(在一个终端中):
    python server/server.py
  2. 运行 MCP 客户端(在另一个终端中):
    python client/client.py

客户端将连接到服务器,执行 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列出可用的提示

扩展项目

您可以通过以下方式扩展此实现:

  • 添加更多具有不同功能的工具
  • 添加每次读取时都会改变的动态资源
  • 实施引导式交互的提示模板
  • 创建更具交互性的客户端应用程序

参考

-
security - not tested
F
license - not found
-
quality - not tested

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 进行工具调用和资源访问。

  1. 什么是 MCP?
    1. 项目结构
      1. 功能演示
        1. 设置
          1. 用法
            1. 工作原理
              1. MCP 服务器
              2. MCP 客户端
            2. 协议详细信息
              1. 扩展项目
                1. 参考

                  Related MCP Servers

                  • -
                    security
                    F
                    license
                    -
                    quality
                    Implements 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 -
                    90
                    Python
                    • Apple
                    • Linux
                  • -
                    security
                    A
                    license
                    -
                    quality
                    A 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 -
                    13
                    Python
                    MIT License
                  • -
                    security
                    F
                    license
                    -
                    quality
                    A 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

                  View all related MCP servers

                  ID: 35niv312p3