Skip to main content
Glama

MCP 服务器示例

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

MCP 图

MCP 服务器示例

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

参考:

什么是 MCP?

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

主要优点

  • 越来越多的预建集成可供您的 LLM 直接插入

  • 灵活地在 LLM 提供商和供应商之间切换

  • 保护基础架构内数据的最佳实践

Related MCP server: MCP Server Example

架构概述

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文件。

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections. Dates show when Glama detected each change.

  1. 1 tool updatev0.1.0
    • First observedadd

TDQS

B3.2/5.0
Disambiguation5/5

With only one tool, there is no possibility of confusion or overlap between tools. The tool 'add' has a clear and distinct purpose of adding two numbers, so disambiguation is perfect.

Naming Consistency5/5

With a single tool, naming consistency is inherently perfect as there are no other tools to compare against. The tool name 'add' follows a simple verb pattern, which is appropriate for its function.

Tool Count2/5

A single tool is too few for most server purposes, as it limits functionality and scope. While it might be appropriate for a trivial demo, it feels thin and incomplete for any meaningful domain coverage.

Completeness1/5

The server is severely incomplete, as it only offers a basic arithmetic operation. There are obvious gaps, such as missing other mathematical operations or any broader domain coverage, which will cause agent failures in most scenarios.

Maintenance

ActivityInactive
ResponsivenessNo issues

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Connectors

Related MCP Servers

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

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