Skip to main content
Glama
suspicious-cow

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

  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

列出可用的提示

扩展项目

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

  • 添加更多具有不同功能的工具

  • 添加每次读取时都会改变的动态资源

  • 实施引导式交互的提示模板

  • 创建更具交互性的客户端应用程序

参考

F
license - not found
-
quality - not tested
D
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

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

  • F
    license
    -
    quality
    D
    maintenance
    A basic MCP server implementation using Node.js and TypeScript that bridges AI models with external tools and data sources via JSON-RPC.
    2
  • F
    license
    -
    quality
    B
    maintenance
    A demo MCP server that connects an AI to external systems by exposing Python functions as tools and data as resources, using the stdio transport.

View all related MCP servers

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.

View all MCP Connectors

Latest Blog Posts

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