Skip to main content
Glama

MCP Server Sample

MCP 服务器示例

此代码库包含一个用于教学目的的模型上下文协议 (MCP) 服务器实现。此代码演示了如何构建一个可与各种 LLM 客户端集成的功能性 MCP 服务器。

MCP 图

MCP 服务器示例

此代码库包含一个用于教学目的的模型上下文协议 (MCP) 服务器实现。此代码演示了如何构建一个可与各种 LLM 客户端集成的功能性 MCP 服务器。

参考:

什么是 MCP?

MCP(模型上下文协议)是一种开放协议,它规范了应用程序向 LLM 提供上下文的方式。MCP 就像 AI 应用程序的 USB-C 端口一样,它提供了一种标准化的方式,将 AI 模型连接到不同的数据源和工具。

主要优点

  • 越来越多的预建集成可供您的 LLM 直接插入
  • 灵活地在 LLM 提供商和供应商之间切换
  • 保护基础架构内数据的最佳实践

架构概述

MCP 遵循客户端-服务器架构,其中主机应用程序可以连接到多个服务器:

  • MCP 主机:像 Claude Desktop、IDE 或 AI 工具这样的程序,需要通过 MCP 访问数据
  • MCP 客户端:与服务器保持 1:1 连接的协议客户端
  • MCP 服务器:通过标准化模型上下文协议公开特定功能的轻量级程序
  • 数据源:MCP 服务器可以访问的本地(文件、数据库)和远程服务(API)

核心 MCP 概念

MCP 服务器可以提供三种主要类型的功能:

  • 资源:客户端可以读取的类似文件的数据(例如 API 响应或文件内容)
  • 工具:可由 LLM 调用的函数(经用户批准)
  • 提示:预先编写的模板,帮助用户完成特定任务

系统要求

  • Python 3.10 或更高版本
  • MCP SDK 1.2.0 或更高版本
  • uv包管理器

安装

将 MCP 添加到您的 Python 项目我们建议使用 uv 来管理您的 Python 项目。

如果您尚未创建 uv 管理项目,请创建一个:

uv init mcp-server-sample cd mcp-server-sample

然后将 MCP 添加到您的项目依赖项中:

uv add "mcp[cli]

或者,对于使用 pip 作为依赖项的项目:

pip install "mcp[cli]"

运行独立的 MCP 开发工具 要使用 uv 运行 mcp 命令:

uv run mcp

快速入门

让我们创建一个简单的 MCP 服务器,它公开一个计算器工具和一些数据:

# server.py from mcp.server.fastmcp import FastMCP # Create an MCP server mcp = FastMCP("Demo") # Add an addition tool @mcp.tool() def add(a: int, b: int) -> int: """Add two numbers""" return a + b # Add a dynamic greeting resource @mcp.resource("greeting://{name}") def get_greeting(name: str) -> str: """Get a personalized greeting""" return f"Hello, {name}!"

您可以在 Claude Desktop 中安装此服务器并通过运行以下命令立即与其交互:

mcp install server.py

或者,您可以使用 MCP 检查器进行测试:

mcp dev server.py

Antonio Scapellato用❤️制作

资源:
执照

本项目遵循 MIT 许可证。详情请参阅LICENSE文件。

-
security - not tested
A
license - permissive license
-
quality - not tested

模型上下文协议服务器的教育实现,演示了如何构建与各种 LLM 客户端集成的功能性 MCP 服务器。

  1. MCP 服务器示例
    1. 什么是 MCP?
    2. 架构概述
    3. 核心 MCP 概念
    4. 系统要求

Related MCP Servers

  • A
    security
    A
    license
    A
    quality
    A beginner-friendly Model Context Protocol (MCP) server that helps users understand MCP concepts, provides interactive examples, and lists available MCP servers. This server is designed to be a helpful companion for developers working with MCP. Also comes with a huge list of servers you can install.
    Last updated -
    3
    9
    36
    JavaScript
    Apache 2.0
  • A
    security
    A
    license
    A
    quality
    An educational implementation of a Model Context Protocol server that demonstrates how to build a functional MCP server for integrating with various LLM clients like Claude Desktop.
    Last updated -
    1
    88
    Python
    MIT License
    • Apple
    • Linux
  • -
    security
    A
    license
    -
    quality
    A Model Context Protocol (MCP) server that enables Claude or other LLMs to fetch content from URLs, supporting HTML, JSON, text, and images with configurable request parameters.
    Last updated -
    Python
    MIT License
  • -
    security
    -
    license
    -
    quality
    A specialized server that helps users create new Model Context Protocol (MCP) servers by providing tools and templates for scaffolding projects with various capabilities.
    Last updated -
    1
    TypeScript

View all related MCP servers

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/antonioscapellato/mcp-server-sample'

If you have feedback or need assistance with the MCP directory API, please join our Discord server