返回市场
易MCP

易MCP

作者:jujumilk323 星标更新:2025-03-08

项目介绍

ezmcp

专为SSE设计的易于使用的MCP服务器框架。

概述

ezmcp是一个轻量级框架,它使使用类似FastAPI的语法创建与MCP兼容的服务器变得容易。它提供了一个基于装饰器的简单API,用于定义可以由MCP客户端调用的工具。

特性

  • 类似FastAPI的装饰器API,用于定义MCP工具
  • 自动参数验证和类型转换
  • 根据函数签名自动生成工具模式
  • 内置支持SSE传输
  • 类似FastAPI的中间件支持
  • 与现有Starlette应用程序轻松集成
  • 交互式文档页面,用于探索和测试工具

docs_image

安装

pip install ezmcp

快速开始

from ezmcp import ezmcp, TextContent

# 创建一个ezmcp应用
app = ezmcp("my-app")

# 定义一个工具
@app.tool(description="将消息回传给用户")
async def echo(message: str):
    """将消息回传给用户。"""
    return [TextContent(type="text", text=f"回声: {message}")]

# 运行应用
if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000)

一旦服务器运行起来,你可以:

  • 访问交互式文档页面 http://localhost:8000/docs
  • 连接到SSE端点 http://localhost:8000/sse

中间件

ezmcp支持类似于FastAPI的中间件,允许你在整个应用程序中添加行为。

from starlette.requests import Request

from ezmcp import TextContent, ezmcp

app = ezmcp("my-app")

@app.middleware
async def process_time_middleware(request: Request, call_next):
    """添加一个带有处理时间的头部。"""
    import time
    start_time = time.perf_counter()
    response = await call_next(request)
    process_time = time.perf_counter() - start_time
    response.headers["X-Process-Time"] = str(process_time)
    return response

@app.tool(description="将消息回传给用户")
async def echo(message: str):
    """将消息回传给用户。"""
    return [TextContent(type="text", text=f"回声: {message}")]

有关更多关于中间件的信息,请参阅middleware文档

文档

有关更详细的文档,请参阅ezmcp/README.md文件。

许可证

MIT

命令

  • 测试安装 pdm install -G test
  • 开发安装 pdm install -G dev